MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lightproxy.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 _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(); this->SetType(Type); }
96 
98  RenderableProxy(Creator),
99  GraphicsLight(NULL)
100  { this->CreateLight(); }
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(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (LightProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
309  }
310  }else{
311  MEZZ_EXCEPTION(Exception::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