Spinning Topp Logo BlackTopp Studios
inc
entityproxy.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 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 _graphicsentityproxy_cpp
41 #define _graphicsentityproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the World proxy wrapping basic entity(mesh) functionality.
45 
46 #include "Graphics/entityproxy.h"
47 #include "Graphics/scenemanager.h"
48 #include "Graphics/meshmanager.h"
49 #include "Graphics/mesh.h"
50 
51 #include "enumerations.h"
52 #include "exception.h"
53 #include "serialization.h"
54 #include "stringtool.h"
55 
56 #include <Ogre.h>
57 
58 namespace Mezzanine
59 {
60  namespace Graphics
61  {
63  RenderableProxy(ID,Creator),
64  GraphicsEntity(NULL),
65  ProxyMesh(NULL),
66  RenderDist(0),
67  LightMask(0xFFFFFFFF),
68  SceneVisible(true),
69  CanCastShadows(true)
70  { this->CreateEntity(NULL); }
71 
72  EntityProxy::EntityProxy(const UInt32 ID, Mesh* TheMesh, SceneManager* Creator) :
73  RenderableProxy(ID,Creator),
74  GraphicsEntity(NULL),
75  ProxyMesh(NULL),
76  RenderDist(0),
77  LightMask(0xFFFFFFFF),
78  SceneVisible(true),
79  CanCastShadows(true)
80  { this->CreateEntity(TheMesh); }
81 
82  EntityProxy::EntityProxy(const UInt32 ID, const String& MeshName, const String& GroupName, SceneManager* Creator) :
83  RenderableProxy(ID,Creator),
84  GraphicsEntity(NULL),
85  ProxyMesh(NULL),
86  RenderDist(0),
87  LightMask(0xFFFFFFFF),
88  SceneVisible(true),
89  CanCastShadows(true)
90  { this->CreateEntity(MeshName,GroupName); }
91 
92  EntityProxy::EntityProxy(const XML::Node& SelfRoot, SceneManager* Creator) :
93  RenderableProxy(Creator),
94  GraphicsEntity(NULL),
95  ProxyMesh(NULL),
96  RenderDist(0),
97  LightMask(0xFFFFFFFF),
98  SceneVisible(true),
99  CanCastShadows(true)
100  { this->ProtoDeSerialize(SelfRoot); }
101 
103  { this->DestroyEntity(); }
104 
106  {
107  this->WorldProxy::ProtoSerializeImpl(SelfRoot);
108  this->ProtoSerializeMesh(SelfRoot);
109  }
110 
112  {
113  this->ProtoDeSerializeMesh(SelfRoot);
114  this->WorldProxy::ProtoDeSerializeImpl(SelfRoot);
115  }
116 
117  void EntityProxy::CreateEntity(Mesh* ObjectMesh)
118  {
119  if( ObjectMesh != NULL ) {
120  this->GraphicsEntity = this->Manager->_GetGraphicsWorldPointer()->createEntity( ObjectMesh->_GetInternalMesh() );
121  this->GraphicsNode->attachObject( this->GraphicsEntity );
122  this->GraphicsEntity->setUserAny( Ogre::Any( static_cast<RenderableProxy*>( this ) ) );
123  this->GraphicsEntity->setVisibilityFlags(0);
124  this->GraphicsEntity->setQueryFlags(0);
125  }
126 
127  this->ProxyMesh = ObjectMesh;
128  }
129 
130  void EntityProxy::CreateEntity(const String& MeshName, const String& GroupName)
131  {
132  Mesh* TheMesh = MeshManager::GetSingletonPtr()->LoadMesh(MeshName,GroupName);
133  this->CreateEntity(TheMesh);
134  }
135 
137  {
138  if( this->GraphicsEntity ) {
139  this->GraphicsNode->detachObject( this->GraphicsEntity );
140  this->Manager->_GetGraphicsWorldPointer()->destroyEntity( this->GraphicsEntity );
141  }
142  }
143 
144  ///////////////////////////////////////////////////////////////////////////////
145  // Utility
146 
148  {
149  return Mezzanine::PT_Graphics_EntityProxy;
150  }
151 
153  {
154  if( this->GraphicsEntity && !this->InWorld ) {
155  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
156  this->GraphicsEntity->setQueryFlags( this->QueryMask );
157  this->InWorld = true;
158  }
159  }
160 
162  {
163  if( this->GraphicsEntity && this->InWorld ) {
164  this->GraphicsEntity->setVisibilityFlags(0);
165  this->GraphicsEntity->setQueryFlags(0);
166  this->InWorld = false;
167  }
168  }
169 
170  ///////////////////////////////////////////////////////////////////////////////
171  // Mesh Management
172 
173  void EntityProxy::SetMesh(const String& MeshName, const String& Group)
174  {
175  if( this->GraphicsEntity ) {
176  this->DestroyEntity();
177  }
178  this->CreateEntity(MeshName,Group);
179 
180  this->GraphicsEntity->setVisible( this->SceneVisible );
181  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
182  this->GraphicsEntity->setLightMask( this->LightMask );
183  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
184  this->GraphicsEntity->setQueryFlags( this->QueryMask );
185  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
186  }
187 
188  void EntityProxy::SetMesh(Mesh* ObjectMesh)
189  {
190  if( this->GraphicsEntity ) {
191  this->DestroyEntity();
192  }
193  this->CreateEntity(ObjectMesh);
194 
195  this->GraphicsEntity->setVisible( this->SceneVisible );
196  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
197  this->GraphicsEntity->setLightMask( this->LightMask );
198  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
199  this->GraphicsEntity->setQueryFlags( this->QueryMask );
200  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
201  }
202 
204  {
205  return this->ProxyMesh;
206  }
207 
208  ///////////////////////////////////////////////////////////////////////////////
209  // RenderableProxy Properties
210 
211  void EntityProxy::SetVisible(const Boole Visible)
212  {
213  this->SceneVisible = Visible;
214  if( this->GraphicsEntity ) {
215  this->GraphicsEntity->setVisible( this->SceneVisible );
216  }
217  }
218 
220  {
221  return this->SceneVisible;
222  }
223 
224  void EntityProxy::SetCastShadows(const Boole CastShadows)
225  {
226  this->CanCastShadows = CastShadows;
227  if( this->GraphicsEntity ) {
228  this->GraphicsEntity->setCastShadows( this->CanCastShadows );
229  }
230  }
231 
233  {
234  return this->CanCastShadows;
235  }
236 
238  {
239  return ( this->GraphicsEntity ? this->GraphicsEntity->getReceivesShadows() : false );
240  }
241 
243  {
244  this->LightMask = Mask;
245  if( this->GraphicsEntity ) {
246  this->GraphicsEntity->setLightMask( this->LightMask );
247  }
248  }
249 
251  {
252  return this->LightMask;
253  }
254 
256  {
257  this->VisibilityMask = Mask;
258  if( this->GraphicsEntity && this->InWorld ) {
259  this->GraphicsEntity->setVisibilityFlags( this->VisibilityMask );
260  }
261  }
262 
264  {
265  return this->VisibilityMask;
266  }
267 
269  {
270  this->QueryMask = Mask;
271  if( this->GraphicsEntity && this->InWorld ) {
272  this->GraphicsEntity->setQueryFlags( this->QueryMask );
273  }
274  }
275 
277  {
278  return this->QueryMask;
279  }
280 
282  {
283  this->RenderDist = Distance;
284  if( this->GraphicsEntity ) {
285  this->GraphicsEntity->setRenderingDistance( this->RenderDist );
286  }
287  }
288 
290  {
291  return this->RenderDist;
292  }
293 
294  ///////////////////////////////////////////////////////////////////////////////
295  // Entity Properties
296 
297  ///////////////////////////////////////////////////////////////////////////////
298  // Serialization
299 
301  {
303  }
304 
306  {
307  XML::Node MeshNode = SelfRoot.AppendChild( EntityProxy::GetSerializableName() + "Mesh" );
308 
309  if( MeshNode.AppendAttribute("Version").SetValue("1") &&
310  MeshNode.AppendAttribute("ProxyMeshName").SetValue( this->ProxyMesh ? this->ProxyMesh->GetName() : "" ) &&
311  MeshNode.AppendAttribute("ProxyMeshGroup").SetValue( this->ProxyMesh ? this->ProxyMesh->GetGroup() : "" ) )
312  {
313  return;
314  }else{
315  SerializeError("Create XML Attribute Values",EntityProxy::GetSerializableName() + "Mesh",true);
316  }
317  }
318 
320  {
322  }
323 
325  {
326  XML::Attribute CurrAttrib;
327  XML::Node MeshNode = SelfRoot.GetChild( EntityProxy::GetSerializableName() + "Mesh" );
328 
329  if( !MeshNode.Empty() ) {
330  if(MeshNode.GetAttribute("Version").AsInt() == 1) {
331  String MeshName, MeshGroup = Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME;
332 
333  CurrAttrib = MeshNode.GetAttribute("ProxyMeshName");
334  if( !CurrAttrib.Empty() )
335  MeshName = CurrAttrib.AsString();
336 
337  CurrAttrib = MeshNode.GetAttribute("ProxyMeshGroup");
338  if( !CurrAttrib.Empty() )
339  MeshGroup = CurrAttrib.AsString();
340 
341  if( !MeshName.empty() && !MeshGroup.empty() ) {
342  Mesh* NewMesh = MeshManager::GetSingletonPtr()->LoadMesh( MeshName, MeshGroup );
343  this->SetMesh( NewMesh );
344  }else{
345  this->SetMesh( NULL );
346  }
347  }
348  }
349  }
350 
353 
355  { return "EntityProxy"; }
356 
357  ///////////////////////////////////////////////////////////////////////////////
358  // Internal Methods
359 
360  Ogre::Entity* EntityProxy::_GetGraphicsObject() const
361  { return this->GraphicsEntity; }
362 
363  Ogre::MovableObject* EntityProxy::_GetBaseGraphicsObject() const
364  { return this->GraphicsEntity; }
365  }//Graphics
366 }//Mezzanine
367 
368 #endif
virtual void SetLightMask(const UInt32 Mask)
Sets which types of lights will affect this proxy.
virtual Boole GetCastShadows() const
Gets whether or not this proxy can cast a shadow.
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual void ProtoSerializeImpl(XML::Node &SelfRoot) const
Implementation method for serializing additional sets of data.
Boole CanCastShadows
Stores whether or not this object can cast shadows on other objects.
Definition: entityproxy.h:86
This class contains utilities and functions to allow the manipulation of the Graphical scene...
Definition: scenemanager.h:85
virtual Ogre::Entity * _GetGraphicsObject() const
Accessor for the internal entity.
virtual Mezzanine::ProxyType GetProxyType() const
Accessor for the type of proxy.
virtual void ProtoSerializeImpl(XML::Node &SelfRoot) const
Implementation method for serializing additional sets of data.
Definition: worldproxy.cpp:66
This class is used to check and modify the properties of a graphics mesh.
Definition: mesh.h:63
SceneManager * Manager
This is a pointer to the scene manager that created and owns this proxy.
virtual void SetVisible(const Boole Visible)
Sets whether or not this proxy is visible.
Ogre::MeshPtr _GetInternalMesh() const
Gets the internal Mesh pointer.
Definition: mesh.cpp:151
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: worldproxy.cpp:114
virtual void AddToWorld()
Performs all the necessary task to ensure this object is connected to it's respective world and ready...
Ogre::Entity * GraphicsEntity
A pointer to the internal Entity this proxy is based on.
Definition: entityproxy.h:70
virtual Ogre::MovableObject * _GetBaseGraphicsObject() const
Accessor for the internal graphics object.
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual Real GetRenderDistance() const
Gets the distance at which the proxy will stop rendering.
virtual UInt32 GetLightMask() const
Gets which types of lights will affect this proxy.
Boole SceneVisible
Stores whether or not this object should be rendered, regardless of other criteria.
Definition: entityproxy.h:83
virtual void RemoveFromWorld()
Unhooks this proxy from it's respective world.
This is the base proxy class for world proxies wrapping functionality of the graphics subsystem...
virtual UInt32 GetVisibilityMask() const
Gets the bitmask that will be used to determine if this object should be visible when rendering...
Any global enumerations shared between multiple classes is to be declared here.
UInt32 LightMask
A bitmask which will be compared against to determine which lights apply to this object.
Definition: entityproxy.h:80
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
UInt32 QueryMask
This is a bitmask identifying this objects type when being queried. Used for advanced query configura...
Ogre::SceneNode * GraphicsNode
A pointer to the internal object storing the proxy transform.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
Mesh * ProxyMesh
A pointer to the mesh being used by this proxy.
Definition: entityproxy.h:73
virtual Boole GetReceiveShadows() const
Gets whether or not this proxy can be rendered with a shadow casted on it.
virtual Mesh * GetMesh() const
Gets the mesh currently being used by this proxy.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
ConstString & GetName() const
Gets the Name of this Mesh.
Definition: mesh.cpp:142
static MeshManager * GetSingletonPtr()
Fetches a pointer to the singleton.
Definition: singleton.h:97
virtual void DestroyEntity()
Destroys the internal entity in use by this proxy.
virtual ~EntityProxy()
Class destructor.
virtual Boole GetVisible() const
Gets whether or not this proxy is visible.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
ProxyType
Used by all World proxies to describe what their derived types are.
Definition: enumerations.h:91
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
This file contains the declaration for the World proxy wrapping basic entity(mesh) functionality...
bool Empty() const
Is this storing anything at all?
virtual UInt32 GetQueryMask() const
Gets the bitmask that will be used to determine if this object should be counted in scene queries...
Real RenderDist
Stores the maximum distance this object can be from the camera before it is clipped/culled.
Definition: entityproxy.h:77
virtual void ProtoDeSerializeImpl(const XML::Node &SelfRoot)
Implementation method for deseriailizing additional sets of data.
UInt32 VisibilityMask
This is a bitmask identifying this objects type when being rendered. Used for advanced visibility con...
Ogre::SceneManager * _GetGraphicsWorldPointer() const
Gets the internal Ogre Scene Manager pointer.
EntityProxy(const UInt32 ID, SceneManager *Creator)
Blank constructor.
Definition: entityproxy.cpp:62
virtual void SetRenderDistance(const Real Distance)
Sets the distance at which the proxy will stop rendering.
Boole InWorld
This stores whether the proxy is currently in the graphics world or not.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void CreateEntity(Mesh *ObjectMesh)
Creates an internal entity based on the provided mesh.
virtual void SetMesh(const String &MeshName, const String &Group)
Sets the mesh to be used by this proxy.
virtual void SetVisibilityMask(const UInt32 Mask)
Sets the bitmask that will be used to determine if this object should be visible when rendering...
virtual void SetQueryMask(const UInt32 Mask)
Sets the bitmesk that will be used to determine if this object should be counted in scene queries...
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldProxy.
virtual void ProtoSerializeMesh(XML::Node &SelfRoot) const
Convert the mesh of this class to an XML::Node ready for serialization.
ConstString & GetGroup() const
Gets the resource group this Mesh belongs to.
Definition: mesh.cpp:145
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
virtual void ProtoDeSerializeMesh(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the mesh of this object with it.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
virtual void SetCastShadows(const Boole CastShadows)
Sets whether or not this proxy can cast a shadow.
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual void ProtoDeSerializeImpl(const XML::Node &SelfRoot)
Implementation method for deseriailizing additional sets of data.
Definition: worldproxy.cpp:69