Spinning Topp Logo BlackTopp Studios
inc
lightproxy.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 _graphicslightproxy_cpp
41 #define _graphicslightproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the World proxy wrapping light functionality.
45 
46 #include "Graphics/lightproxy.h"
47 #include "Graphics/scenemanager.h"
48 
49 #include "exception.h"
50 #include "serialization.h"
51 #include "stringtool.h"
52 
53 #include <Ogre.h>
54 
55 namespace
56 {
57  /// @internal
58  /// @brief Converts an Ogre LightType enum value to it's corresponding Mezzanine type.
59  /// @param Type The Ogre type to be converted.
60  /// @return Returns the Mezzanine LightType corresponding to the provided Ogre type.
61  Mezzanine::Graphics::LightType ConvertLightType(const Ogre::Light::LightTypes Type)
62  {
63  switch(Type)
64  {
65  case Ogre::Light::LT_DIRECTIONAL: return Mezzanine::Graphics::LT_Directional; break;
66  case Ogre::Light::LT_POINT: return Mezzanine::Graphics::LT_Point; break;
67  case Ogre::Light::LT_SPOTLIGHT: return Mezzanine::Graphics::LT_Spotlight; break;
68  }
70  }
71 
72  /// @internal
73  /// @brief Converts a Mezzanine LightType enum value to it's corresponding Ogre type.
74  /// @param Type The Mezzanine type to be converted.
75  /// @return Returns the Ogre LightType corresponding to the provided Mezzanine type.
76  Ogre::Light::LightTypes ConvertLightType(const Mezzanine::Graphics::LightType Type)
77  {
78  switch(Type)
79  {
80  case Mezzanine::Graphics::LT_Directional: return Ogre::Light::LT_DIRECTIONAL; break;
81  case Mezzanine::Graphics::LT_Point: return Ogre::Light::LT_POINT; break;
82  case Mezzanine::Graphics::LT_Spotlight: return Ogre::Light::LT_SPOTLIGHT; break;
83  }
84  return Ogre::Light::LT_POINT;
85  }
86 }
87 
88 namespace Mezzanine
89 {
90  namespace Graphics
91  {
93  RenderableProxy(Creator),
94  GraphicsLight(NULL)
95  { this->CreateLight(); }
96 
98  RenderableProxy(Creator),
99  GraphicsLight(NULL)
100  { this->CreateLight(); this->SetType(Type); }
101 
102  LightProxy::LightProxy(const XML::Node& SelfRoot, SceneManager* Creator) :
103  RenderableProxy(Creator),
104  GraphicsLight(NULL)
105  {
106  this->CreateLight();
107  this->ProtoDeSerialize(SelfRoot);
108  }
109 
111  { this->DestroyLight(); }
112 
114  {
115  this->GraphicsLight = this->Manager->_GetGraphicsWorldPointer()->createLight();
116  this->GraphicsNode->attachObject( this->GraphicsLight );
117  this->GraphicsLight->setUserAny( Ogre::Any( static_cast<RenderableProxy*>( this ) ) );
118  this->GraphicsLight->setVisibilityFlags(0);
119  this->GraphicsLight->setQueryFlags(0);
120  }
121 
123  {
124  if( this->GraphicsLight ) {
125  this->GraphicsNode->detachObject( this->GraphicsLight );
126  this->Manager->_GetGraphicsWorldPointer()->destroyLight( this->GraphicsLight );
127  }
128  }
129 
130  ///////////////////////////////////////////////////////////////////////////////
131  // Utility
132 
134  { return Mezzanine::PT_Graphics_LightProxy; }
135 
137  { this->SetOrientation( Vector3::Unit_Z().GetRotationToAxis( Dir ) ); }
138 
140  { return ( this->GetOrientation() * Vector3::Unit_Z() ); }
141 
142  ///////////////////////////////////////////////////////////////////////////////
143  // Light Properties
144 
146  { this->GraphicsLight->setDiffuseColour( Diffuse.GetOgreColourValue() ); }
147 
149  { return ColourValue( this->GraphicsLight->getDiffuseColour() ); }
150 
152  { this->GraphicsLight->setSpecularColour( Specular.GetOgreColourValue() ); }
153 
155  { return ColourValue( this->GraphicsLight->getSpecularColour() ); }
156 
158  { this->GraphicsLight->setType( ConvertLightType(Type) ); }
159 
161  { return ConvertLightType( this->GraphicsLight->getType() ); }
162 
163  void LightProxy::SetAttenuation(const Real Range, const Real Constant, const Real Linear, const Real Quadratic)
164  { this->GraphicsLight->setAttenuation(Range,Constant,Linear,Quadratic); }
165 
167  { return this->GraphicsLight->getAttenuationRange(); }
168 
170  { return this->GraphicsLight->getAttenuationConstant(); }
171 
173  { return this->GraphicsLight->getAttenuationLinear(); }
174 
176  { return this->GraphicsLight->getAttenuationQuadric(); }
177 
178  void LightProxy::SetPowerScale(const Real Scale)
179  { this->GraphicsLight->setPowerScale(Scale); }
180 
182  { return this->GraphicsLight->getPowerScale(); }
183 
184  void LightProxy::SetSpotlightRange(const Real InnerAngle, const Real OuterAngle, const Real Falloff)
185  { this->GraphicsLight->setSpotlightRange(Ogre::Radian(InnerAngle),Ogre::Radian(OuterAngle),Falloff); }
186 
188  { this->GraphicsLight->setSpotlightInnerAngle(Ogre::Radian(Angle)); }
189 
191  { return this->GraphicsLight->getSpotlightInnerAngle().valueRadians(); }
192 
194  { this->GraphicsLight->setSpotlightOuterAngle(Ogre::Radian(Angle)); }
195 
197  { return this->GraphicsLight->getSpotlightOuterAngle().valueRadians(); }
198 
200  { this->GraphicsLight->setSpotlightFalloff(Falloff); }
201 
203  { return this->GraphicsLight->getSpotlightFalloff(); }
204 
206  { this->GraphicsLight->setSpotlightNearClipDistance(NearClip); }
207 
209  { return this->GraphicsLight->getSpotlightNearClipDistance(); }
210 
211  ///////////////////////////////////////////////////////////////////////////////
212  // Serialization
213 
215  {
217 
218  XML::Node PropertiesNode = SelfRoot.AppendChild( LightProxy::GetSerializableName() + "Properties" );
219 
220  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
221  PropertiesNode.AppendAttribute("LightType").SetValue( this->GetType() ) &&
222  PropertiesNode.AppendAttribute("AttenRange").SetValue( this->GetAttenuationRange() ) &&
223  PropertiesNode.AppendAttribute("AttenConstant").SetValue( this->GetAttenuationConstant() ) &&
224  PropertiesNode.AppendAttribute("AttenLinear").SetValue( this->GetAttenuationLinear() ) &&
225  PropertiesNode.AppendAttribute("AttenQuadratic").SetValue( this->GetAttenuationQuadratic() ) &&
226  PropertiesNode.AppendAttribute("PowerScale").SetValue( this->GetPowerScale() ) &&
227  PropertiesNode.AppendAttribute("SpotlightInnerAngle").SetValue( this->GetSpotlightInnerAngle() ) &&
228  PropertiesNode.AppendAttribute("SpotlightOuterAngle").SetValue( this->GetSpotlightOuterAngle() ) &&
229  PropertiesNode.AppendAttribute("SpotlightFalloff").SetValue( this->GetSpotlightFalloff() ) &&
230  PropertiesNode.AppendAttribute("SpotlightNearClipDistance").SetValue( this->GetSpotlightNearClipDistance() ) )
231  {
232  XML::Node DiffuseColourNode = PropertiesNode.AppendChild("DiffuseColour");
233  this->GetDiffuseColour().ProtoSerialize( DiffuseColourNode );
234  XML::Node SpecularColourNode = PropertiesNode.AppendChild("SpecularColour");
235  this->GetSpecularColour().ProtoSerialize( SpecularColourNode );
236 
237  return;
238  }else{
239  SerializeError("Create XML Attribute Values",LightProxy::GetSerializableName() + "Properties",true);
240  }
241  }
242 
244  {
246 
247  XML::Attribute CurrAttrib;
248  XML::Node PropertiesNode = SelfRoot.GetChild( LightProxy::GetSerializableName() + "Properties" );
249 
250  if( !PropertiesNode.Empty() ) {
251  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
252  Real AttenRange = 100000.0, AttenConstant = 1.0, AttenLinear = 0.0, AttenQuadratic = 0.0;
253 
254  CurrAttrib = PropertiesNode.GetAttribute("LightType");
255  if( !CurrAttrib.Empty() )
256  this->SetType( static_cast<Graphics::LightType>( CurrAttrib.AsWhole() ) );
257 
258  CurrAttrib = PropertiesNode.GetAttribute("AttenRange");
259  if( !CurrAttrib.Empty() )
260  AttenRange = CurrAttrib.AsReal();
261 
262  CurrAttrib = PropertiesNode.GetAttribute("AttenConstant");
263  if( !CurrAttrib.Empty() )
264  AttenConstant = CurrAttrib.AsReal();
265 
266  CurrAttrib = PropertiesNode.GetAttribute("AttenLinear");
267  if( !CurrAttrib.Empty() )
268  AttenLinear = CurrAttrib.AsReal();
269 
270  CurrAttrib = PropertiesNode.GetAttribute("AttenQuadratic");
271  if( !CurrAttrib.Empty() )
272  AttenQuadratic = CurrAttrib.AsReal();
273 
274  CurrAttrib = PropertiesNode.GetAttribute("PowerScale");
275  if( !CurrAttrib.Empty() )
276  this->SetPowerScale( CurrAttrib.AsReal() );
277 
278  CurrAttrib = PropertiesNode.GetAttribute("SpotlightInnerAngle");
279  if( !CurrAttrib.Empty() )
280  this->SetSpotlightInnerAngle( CurrAttrib.AsReal() );
281 
282  CurrAttrib = PropertiesNode.GetAttribute("SpotlightOuterAngle");
283  if( !CurrAttrib.Empty() )
284  this->SetSpotlightOuterAngle( CurrAttrib.AsReal() );
285 
286  CurrAttrib = PropertiesNode.GetAttribute("SpotlightFalloff");
287  if( !CurrAttrib.Empty() )
288  this->SetSpotlightFalloff( CurrAttrib.AsReal() );
289 
290  CurrAttrib = PropertiesNode.GetAttribute("SpotlightNearClipDistance");
291  if( !CurrAttrib.Empty() )
292  this->SetSpotlightNearClipDistance( CurrAttrib.AsReal() );
293 
294  this->SetAttenuation(AttenRange,AttenConstant,AttenLinear,AttenQuadratic);
295 
296  XML::Node DiffuseColourNode = PropertiesNode.GetChild("DiffuseColour").GetFirstChild();
297  if( !DiffuseColourNode.Empty() ) {
298  ColourValue Diffuse(DiffuseColourNode);
299  this->SetDiffuseColour(Diffuse);
300  }
301 
302  XML::Node SpecularColourNode = PropertiesNode.GetChild("SpecularColour").GetFirstChild();
303  if( !SpecularColourNode.Empty() ) {
304  ColourValue Specular(SpecularColourNode);
305  this->SetSpecularColour(Specular);
306  }
307  }else{
308  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (LightProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
309  }
310  }else{
311  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,LightProxy::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
312  }
313  }
314 
316  { return LightProxy::GetSerializableName(); }
317 
319  { return "LightProxy"; }
320 
321  ///////////////////////////////////////////////////////////////////////////////
322  // Internal Methods
323 
324  Ogre::Light* LightProxy::_GetGraphicsObject() const
325  { return this->GraphicsLight; }
326 
327  Ogre::MovableObject* LightProxy::_GetBaseGraphicsObject() const
328  { return this->GraphicsLight; }
329  }//Graphics
330 }//Mezzanine
331 
332 #endif
virtual Real GetAttenuationQuadratic() const
Gets the quadric factor of the attenuation.
Definition: lightproxy.cpp:175
virtual Real GetSpotlightOuterAngle() const
Gets the Outer angle of the cone of light emitted by this spotlight.
Definition: lightproxy.cpp:196
LightType
This is used by LightProxies to describe how light is emitted from the proxy source.
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
virtual Graphics::LightType GetType() const
Gets the type of light that this light is.
Definition: lightproxy.cpp:160
virtual void SetSpotlightOuterAngle(const Real Angle)
Sets the Outer angle of the cone of light emitted by a spotlight.
Definition: lightproxy.cpp:193
This class contains utilities and functions to allow the manipulation of the Graphical scene...
Definition: scenemanager.h:85
virtual void SetOrientation(const Quaternion &Ori)
Sets the orientation of this object in parent space.
virtual Real GetSpotlightInnerAngle() const
Gets the Inner angle of the cone of light emitted by this spotlight.
Definition: lightproxy.cpp:190
virtual Real GetAttenuationLinear() const
Gets the linear factor of the attentuation.
Definition: lightproxy.cpp:172
Thrown when the requested identity could not be found.
Definition: exception.h:94
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
SceneManager * Manager
This is a pointer to the scene manager that created and owns this proxy.
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
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual void SetPowerScale(const Real Scale)
Sets the power scale of this light, which indicates it's relative power.
Definition: lightproxy.cpp:178
virtual Real GetAttenuationConstant() const
Gets the constant factor of the attenuation.
Definition: lightproxy.cpp:169
virtual Vector3 GetDirection() const
Gets the direction the light from this source is being emitted.
Definition: lightproxy.cpp:139
This is the base proxy class for world proxies wrapping functionality of the graphics subsystem...
virtual void SetSpotlightRange(const Real InnerAngle, const Real OuterAngle, const Real Falloff=1.0)
Defines the cone of light emitted by a spotlight.
Definition: lightproxy.cpp:184
This file contains the declaration for the World proxy wrapping light functionality.
This is a simple class for holding 4 reals representing the colour any give object or lightsource can...
Definition: colourvalue.h:64
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
virtual void SetSpecularColour(const ColourValue &Specular)
Sets the colour for the Specular light from this source.
Definition: lightproxy.cpp:151
virtual ColourValue GetDiffuseColour() const
Gets the current colour of Diffuse light being emitted by this proxy.
Definition: lightproxy.cpp:148
virtual void SetSpotlightFalloff(const Real Falloff)
Sets the rate of falloff of the cone of light emitted by a spotlight.
Definition: lightproxy.cpp:199
virtual void SetAttenuation(const Real Range, const Real Constant, const Real Linear, const Real Quadratic)
Sets the factors for the attenuation formula applied to this light.
Definition: lightproxy.cpp:163
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.
virtual Real GetSpotlightFalloff() const
Gets the rate of falloff of the cone of light emitted by this spotlight.
Definition: lightproxy.cpp:202
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Whole AsWhole(Whole def=0) const
Attempts to convert the value of the attribute to a Whole and returns the results.
virtual Quaternion GetOrientation() const
Gets this objects current orientation.
virtual ~LightProxy()
Class destructor.
Definition: lightproxy.cpp:110
virtual void SetType(const Graphics::LightType Type)
Sets the type of light this light is.
Definition: lightproxy.cpp:157
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
bool Empty() const
Is this storing anything at all?
From a point in space, like a Torch, campfire, muzzle flash, Mutant Fireflies, bonfires, light bulbs, non-hooded lantern, the DnD D20 Light spell, explosions, and scotch tape separating from the roll in a unlit vacuum. There may be other uses, be creative.
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
Definition: lightproxy.cpp:318
virtual void SetDirection(const Vector3 &Dir)
Sets the direction the light will be emitted from this source.
Definition: lightproxy.cpp:136
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
virtual Mezzanine::ProxyType GetProxyType() const
Accessor for the type of proxy.
Definition: lightproxy.cpp:133
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldProxy.
Definition: lightproxy.cpp:315
virtual void SetSpotlightInnerAngle(const Real Angle)
Sets the Inner angle of the cone of light emitted by a spotlight.
Definition: lightproxy.cpp:187
Ogre::Light * GraphicsLight
A pointer to the internal Light this proxy is based on.
Definition: lightproxy.h:68
From one direction, like sunlight.
static Vector3 Unit_Z()
Gets a vector representing the Z unit of a Vector3.
Definition: vector3.cpp:137
Ogre::SceneManager * _GetGraphicsWorldPointer() const
Gets the internal Ogre Scene Manager pointer.
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
From a point emanating in a cone, like a flashlight, hooded lantern, really bright computer screens...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
LightProxy(const UInt32 ID, SceneManager *Creator)
Blank constructor.
Definition: lightproxy.cpp:92
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
virtual Real GetPowerScale() const
Gets the power scale of this light, which indicates it's relative power.
Definition: lightproxy.cpp:181
virtual Ogre::Light * _GetGraphicsObject() const
Accessor for the internal light.
Definition: lightproxy.cpp:324
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: lightproxy.cpp:243
virtual Ogre::MovableObject * _GetBaseGraphicsObject() const
Accessor for the internal graphics object.
Definition: lightproxy.cpp:327
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: lightproxy.cpp:214
virtual void SetDiffuseColour(const ColourValue &Diffuse)
Sets the colour for the Diffuse light from this source.
Definition: lightproxy.cpp:145
virtual Real GetSpotlightNearClipDistance() const
Gets the near clip plane distance to be used by spotlights that use light clipping.
Definition: lightproxy.cpp:208
Ogre::ColourValue GetOgreColourValue() const
Creates and returns an Ogre ColourValue class with values equal to this one.
Definition: colourvalue.cpp:86
virtual Real GetAttenuationRange() const
Gets the absolute range of attenuation in world units.
Definition: lightproxy.cpp:166
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
virtual void CreateLight()
Creates an internal light to be used by the calling instance.
Definition: lightproxy.cpp:113
virtual ColourValue GetSpecularColour() const
Gets the current colour of Specular light being emitted by this proxy.
Definition: lightproxy.cpp:154
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
virtual void SetSpotlightNearClipDistance(const Real NearClip)
Sets the near clip plane distance to be used by spotlights that use light clipping.
Definition: lightproxy.cpp:205
virtual void DestroyLight()
Destroys the internal light in use by this proxy.
Definition: lightproxy.cpp:122
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.