MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
renderableproxy.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _graphicsrenderableproxy_cpp
41 #define _graphicsrenderableproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the base class from which graphics proxies inherit.
45 
47 #include "Graphics/scenemanager.h"
48 
49 #include "enumerations.h"
50 #include "exception.h"
51 #include "serialization.h"
52 #include "stringtool.h"
53 
54 #include <Ogre.h>
55 
56 namespace Mezzanine
57 {
58  namespace Graphics
59  {
61  GraphicsNode(NULL),
62  Manager(Creator),
63  VisibilityMask(Ogre::MovableObject::getDefaultVisibilityFlags()),
64  QueryMask(Ogre::MovableObject::getDefaultQueryFlags()),
65  InWorld(false)
66  {
67  this->GraphicsNode = this->Manager->_GetGraphicsWorldPointer()->getRootSceneNode()->createChildSceneNode();
68  }
69 
71  {
72  this->GraphicsNode->getParentSceneNode()->removeChild(this->GraphicsNode);
73  this->Manager->_GetGraphicsWorldPointer()->destroySceneNode(this->GraphicsNode);
74  }
75 
76  ///////////////////////////////////////////////////////////////////////////////
77  // Utility
78 
80  { return AxisAlignedBox( this->_GetBaseGraphicsObject()->getBoundingBox() ); }
81 
83  {
84  if( !this->InWorld ) {
85  this->_GetBaseGraphicsObject()->setVisibilityFlags( this->VisibilityMask );
86  this->_GetBaseGraphicsObject()->setQueryFlags( this->QueryMask );
87  this->InWorld = true;
88  }
89  }
90 
92  {
93  if( this->InWorld ) {
94  this->_GetBaseGraphicsObject()->setVisibilityFlags(0);
95  this->_GetBaseGraphicsObject()->setQueryFlags(0);
96  this->InWorld = false;
97  }
98  }
99 
101  { return this->InWorld; }
102 
104  { return this->Manager; }
105 
106  ///////////////////////////////////////////////////////////////////////////////
107  // RenderableProxy Properties
108 
109  void RenderableProxy::SetVisible(const Boolean Visible)
110  { this->_GetBaseGraphicsObject()->setVisible(Visible); }
111 
113  { return this->_GetBaseGraphicsObject()->getVisible(); }
114 
115  void RenderableProxy::SetCastShadows(const Boolean CastShadows)
116  { this->_GetBaseGraphicsObject()->setCastShadows(CastShadows); }
117 
119  { return this->_GetBaseGraphicsObject()->getCastShadows(); }
120 
122  { return this->_GetBaseGraphicsObject()->getReceivesShadows(); }
123 
125  { this->_GetBaseGraphicsObject()->setLightMask(Mask); }
126 
128  { return this->_GetBaseGraphicsObject()->getLightMask(); }
129 
131  {
132  this->VisibilityMask = Mask;
133  if( this->InWorld ) {
134  this->_GetBaseGraphicsObject()->setVisibilityFlags( this->VisibilityMask );
135  }
136  }
137 
139  { return this->VisibilityMask; }
140 
142  {
143  this->QueryMask = Mask;
144  if( this->InWorld ) {
145  this->_GetBaseGraphicsObject()->setQueryFlags( this->QueryMask );
146  }
147  }
148 
150  { return this->QueryMask; }
151 
153  { this->_GetBaseGraphicsObject()->setRenderingDistance(Distance); }
154 
156  { return this->_GetBaseGraphicsObject()->getRenderingDistance(); }
157 
158  ///////////////////////////////////////////////////////////////////////////////
159  // Transform Methods
160 
162  { this->GraphicsNode->setPosition( Loc.GetOgreVector3() ); }
163 
164  void RenderableProxy::SetLocation(const Real X, const Real Y, const Real Z)
165  { this->GraphicsNode->setPosition(X,Y,Z); }
166 
168  { return Vector3( this->GraphicsNode->getPosition() ); }
169 
171  { this->GraphicsNode->setOrientation( Ori.GetOgreQuaternion() ); }
172 
173  void RenderableProxy::SetOrientation(const Real X, const Real Y, const Real Z, const Real W)
174  { this->GraphicsNode->setOrientation(W,X,Y,Z); }
175 
177  { return Quaternion( this->GraphicsNode->getOrientation() ); }
178 
180  { this->GraphicsNode->setScale( Sc.GetOgreVector3() ); }
181 
182  void RenderableProxy::SetScale(const Real X, const Real Y, const Real Z)
183  { this->GraphicsNode->setScale(X,Y,Z); }
184 
186  { return Vector3( this->GraphicsNode->getScale() ); }
187 
189  { this->SetLocation( this->GetLocation() + Trans ); }
190 
191  void RenderableProxy::Translate(const Real X, const Real Y, const Real Z)
192  { this->SetLocation( this->GetLocation() + Vector3(X,Y,Z) ); }
193 
194  void RenderableProxy::Yaw(const Real Angle)
195  { this->SetOrientation( this->GetOrientation() * Quaternion(Angle,Vector3::Unit_Y()) ); }
196 
197  void RenderableProxy::Pitch(const Real Angle)
198  { this->SetOrientation( this->GetOrientation() * Quaternion(Angle,Vector3::Unit_X()) ); }
199 
200  void RenderableProxy::Roll(const Real Angle)
201  { this->SetOrientation( this->GetOrientation() * Quaternion(Angle,Vector3::Unit_Z()) ); }
202 
203  void RenderableProxy::Rotate(const Vector3& Axis, const Real Angle)
204  { this->SetOrientation( this->GetOrientation() * Quaternion(Angle,Axis) ); }
205 
206  void RenderableProxy::Rotate(const Quaternion& Rotation)
207  { this->SetOrientation( this->GetOrientation() * Rotation ); }
208 
209  void RenderableProxy::Scale(const Vector3& Scale)
210  { this->SetScale( this->GetScale() * Scale ); }
211 
212  void RenderableProxy::Scale(const Real X, const Real Y, const Real Z)
213  { this->SetScale( this->GetScale() * Vector3(X,Y,Z) ); }
214 
215  ///////////////////////////////////////////////////////////////////////////////
216  // Serialization
217 
219  {
220  XML::Node SelfRoot = ParentNode.AppendChild(this->GetDerivedSerializableName());
221  if( !SelfRoot.AppendAttribute("InWorld").SetValue( this->IsInWorld() ? "true" : "false" ) ) {
222  SerializeError("Create XML Attribute Values",WorldProxy::GetSerializableName(),true);
223  }
224 
225  this->ProtoSerializeProperties(SelfRoot);
226  }
227 
229  {
230  this->WorldProxy::ProtoSerializeProperties(SelfRoot);
231 
232  XML::Node PropertiesNode = SelfRoot.AppendChild( RenderableProxy::GetSerializableName() + "Properties" );
233 
234  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
235  PropertiesNode.AppendAttribute("Visible").SetValue( this->GetVisible() ? "true" : "false" ) &&
236  PropertiesNode.AppendAttribute("CastShadows").SetValue( this->GetCastShadows() ? "true" : "false" ) &&
237  PropertiesNode.AppendAttribute("LightMask").SetValue( this->GetLightMask() ) &&
238  PropertiesNode.AppendAttribute("VisibilityMask").SetValue( this->GetVisibilityMask() ) &&
239  PropertiesNode.AppendAttribute("QueryMask").SetValue( this->GetQueryMask() ) &&
240  PropertiesNode.AppendAttribute("RenderDistance").SetValue( this->GetRenderDistance() ) )
241  {
242  return;
243  }else{
244  SerializeError("Create XML Attribute Values",RenderableProxy::GetSerializableName() + "Properties",true);
245  }
246  }
247 
249  {
250  Boolean WasInWorld = false;
251  XML::Attribute InWorldAttrib = SelfRoot.GetAttribute("InWorld");
252  if( !InWorldAttrib.Empty() ) {
253  WasInWorld = StringTools::ConvertToBool( InWorldAttrib.AsString() );
254  }
255 
256  this->ProtoDeSerializeProperties(SelfRoot);
257 
258  if( WasInWorld ) {
259  this->AddToWorld();
260  }
261  }
262 
264  {
266 
267  XML::Attribute CurrAttrib;
268  XML::Node PropertiesNode = SelfRoot.GetChild( RenderableProxy::GetSerializableName() + "Properties" );
269 
270  if( !PropertiesNode.Empty() ) {
271  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
272  CurrAttrib = PropertiesNode.GetAttribute("Visible");
273  if( !CurrAttrib.Empty() )
274  this->SetVisible( StringTools::ConvertToBool( CurrAttrib.AsString() ) );
275 
276  CurrAttrib = PropertiesNode.GetAttribute("CastShadows");
277  if( !CurrAttrib.Empty() )
278  this->SetCastShadows( StringTools::ConvertToBool( CurrAttrib.AsString() ) );
279 
280  CurrAttrib = PropertiesNode.GetAttribute("LightMask");
281  if( !CurrAttrib.Empty() )
282  this->SetLightMask( CurrAttrib.AsWhole() );
283 
284  CurrAttrib = PropertiesNode.GetAttribute("VisibilityMask");
285  if( !CurrAttrib.Empty() )
286  this->SetVisibilityMask( CurrAttrib.AsWhole() );
287 
288  CurrAttrib = PropertiesNode.GetAttribute("QueryMask");
289  if( !CurrAttrib.Empty() )
290  this->SetQueryMask( CurrAttrib.AsWhole() );
291 
292  CurrAttrib = PropertiesNode.GetAttribute("RenderDistance");
293  if( !CurrAttrib.Empty() )
294  this->SetRenderDistance( CurrAttrib.AsReal() );
295  }else{
296  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (RenderableProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
297  }
298  }else{
299  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,RenderableProxy::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
300  }
301  }
302 
305 
307  { return "RenderableProxy"; }
308 
309  ///////////////////////////////////////////////////////////////////////////////
310  // Internal Methods
311 
312  Ogre::SceneNode* RenderableProxy::_GetGraphicsNode() const
313  { return this->GraphicsNode; }
314  }//Graphics
315 }//Mezzanine
316 
317 #endif