MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cameraproxy.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 _graphicscameraproxy_cpp
41 #define _graphicscameraproxy_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the World proxy wrapping camera functionality.
45 
46 #include "Graphics/cameraproxy.h"
47 #include "Graphics/cameramanager.h"
48 #include "Graphics/scenemanager.h"
49 
50 #include "exception.h"
51 #include "serialization.h"
52 #include "stringtool.h"
53 
54 #include <Ogre.h>
55 
56 namespace
57 {
58  /// @internal
59  /// @brief Converts an Ogre PolygonMode enum value to it's corresponding Mezzanine value.
60  /// @param PolyMode The Ogre value to be converted.
61  /// @return Returns the Mezzanine CameraPolyMode corresponding to the provided Ogre value.
62  Mezzanine::Graphics::CameraPolyMode ConvertPolygonMode(const Ogre::PolygonMode PolyMode)
63  {
64  switch(PolyMode)
65  {
66  case Ogre::PM_POINTS: return Mezzanine::Graphics::CPM_Points; break;
67  case Ogre::PM_WIREFRAME: return Mezzanine::Graphics::CPM_WireFrame; break;
68  case Ogre::PM_SOLID: return Mezzanine::Graphics::CPM_Solid; break;
69  }
71  }
72  /// @internal
73  /// @brief Converts a Mezzanine CameraPolyMode enum value to it's corresponding Ogre value.
74  /// @param PolyMode The Mezzanine value to be converted.
75  /// @return Returns the Ogre PolygonMode corresponding to the provided Mezzanine value.
76  Ogre::PolygonMode ConvertPolygonMode(const Mezzanine::Graphics::CameraPolyMode PolyMode)
77  {
78  switch(PolyMode)
79  {
80  case Mezzanine::Graphics::CPM_Points: return Ogre::PM_POINTS; break;
81  case Mezzanine::Graphics::CPM_WireFrame: return Ogre::PM_WIREFRAME; break;
82  case Mezzanine::Graphics::CPM_Solid: return Ogre::PM_SOLID; break;
83  }
84  return Ogre::PM_SOLID;
85  }
86 
87  /// @internal
88  /// @brief Converts an Ogre ProjectionType enum value to it's corresponding Mezzanine type.
89  /// @param ProjType The Ogre type to be converted.
90  /// @return Returns the Mezzanine ProjectionType corresponding to the provided Ogre type.
91  Mezzanine::Graphics::ProjectionType ConvertProjectionType(const Ogre::ProjectionType ProjType)
92  {
93  switch(ProjType)
94  {
95  case Ogre::PT_ORTHOGRAPHIC: return Mezzanine::Graphics::PT_Orthographic; break;
96  case Ogre::PT_PERSPECTIVE: return Mezzanine::Graphics::PT_Perspective; break;
97  }
99  }
100  /// @internal
101  /// @brief Converts a Mezzanine ProjectionType enum value to it's corresponding Ogre type.
102  /// @param ProjType The Mezzanine type to be converted.
103  /// @return Returns the Ogre ProjectionType corresponding to the provided Mezzanine type.
104  Ogre::ProjectionType ConvertProjectionType(const Mezzanine::Graphics::ProjectionType ProjType)
105  {
106  switch(ProjType)
107  {
108  case Mezzanine::Graphics::PT_Orthographic: return Ogre::PT_ORTHOGRAPHIC; break;
109  case Mezzanine::Graphics::PT_Perspective: return Ogre::PT_PERSPECTIVE; break;
110  }
111  return Ogre::PT_PERSPECTIVE;
112  }
113 
114  /// @internal
115  /// @brief Converts an Ogre OrientationMode enum value to it's corresponding Mezzanine type.
116  /// @param OriMode The Ogre type to be converted.
117  /// @return Returns the Mezzanine OrientationMode corresponding to the provided Ogre type.
118  Mezzanine::Graphics::OrientationMode ConvertOrientationMode(const Ogre::OrientationMode OriMode)
119  {
120  switch(OriMode)
121  {
122  case Ogre::OR_DEGREE_0: return Mezzanine::Graphics::OM_Degree_0; break;
123  case Ogre::OR_DEGREE_90: return Mezzanine::Graphics::OM_Degree_90; break;
124  case Ogre::OR_DEGREE_180: return Mezzanine::Graphics::OM_Degree_180; break;
125  case Ogre::OR_DEGREE_270: return Mezzanine::Graphics::OM_Degree_270; break;
126  }
127  return Mezzanine::Graphics::OM_Degree_0;
128  }
129  /// @internal
130  /// @brief Converts a Mezzanine OrientationMode enum value to it's corresponding Ogre type.
131  /// @param OriMode The Mezzanine type to be converted.
132  /// @return Returns the Ogre OrientationMode corresponding to the provided Mezzanine type.
133  Ogre::OrientationMode ConvertOrientationMode(const Mezzanine::Graphics::OrientationMode OriMode)
134  {
135  switch(OriMode)
136  {
137  case Mezzanine::Graphics::OM_Degree_0: return Ogre::OR_DEGREE_0; break;
138  case Mezzanine::Graphics::OM_Degree_90: return Ogre::OR_DEGREE_90; break;
139  case Mezzanine::Graphics::OM_Degree_180: return Ogre::OR_DEGREE_180; break;
140  case Mezzanine::Graphics::OM_Degree_270: return Ogre::OR_DEGREE_270; break;
141  }
142  return Ogre::OR_DEGREE_0;
143  }
144 }
145 
146 namespace Mezzanine
147 {
148  namespace Graphics
149  {
151  RenderableProxy(Creator->GetScene()),
152  CamManager(Creator),
153  GraphicsCamera(NULL),
154  CameraVP(NULL),
155  UseFixedYaw(true)
156  { this->CreateCamera(Name); }
157 
158  CameraProxy::CameraProxy(const XML::Node& SelfRoot, CameraManager* Creator) :
159  RenderableProxy(Creator->GetScene()),
160  CamManager(Creator),
161  GraphicsCamera(NULL),
162  CameraVP(NULL),
163  UseFixedYaw(true)
164  {
165  this->CreateCamera("");
166  this->ProtoDeSerialize(SelfRoot);
167  }
168 
170  { this->DestroyCamera(); }
171 
173  {
174  this->GraphicsCamera = this->Manager->_GetGraphicsWorldPointer()->createCamera( CameraProxy::GenerateName() );
175  this->GraphicsNode->attachObject( this->GraphicsCamera );
176  this->GraphicsCamera->MovableObject::setUserAny( Ogre::Any( static_cast<RenderableProxy*>( this ) ) );
177  this->GraphicsCamera->setVisibilityFlags(0);
178  this->GraphicsCamera->setQueryFlags(0);
179 
180  this->CamName = Name;
181  }
182 
184  {
185  if( this->GraphicsCamera ) {
186  this->GraphicsNode->detachObject( this->GraphicsCamera );
187  this->Manager->_GetGraphicsWorldPointer()->destroyCamera( this->GraphicsCamera );
188  }
189  }
190 
192  {
193  static UInt32 NameCounter = 0;
194  StringStream NameStream;
195  NameStream << "Camera" << ++NameCounter;
196  return NameStream.str();
197  }
198 
199  ///////////////////////////////////////////////////////////////////////////////
200  // Utility
201 
203  { return Mezzanine::PT_Graphics_CameraProxy; }
204 
206  { return this->CamManager; }
207 
209  { return this->CameraVP; }
210 
212  { return this->UseFixedYaw; }
213 
214  void CameraProxy::LookAt(const Vector3& TargetLoc)
215  { this->GraphicsCamera->lookAt( TargetLoc.GetOgreVector3() ); }
216 
218  { this->SetOrientation( Vector3::Neg_Unit_Z().GetRotationToAxis( Dir ) ); }
219 
221  { return ( this->GetOrientation() * Vector3::Neg_Unit_Z() ); }
222 
223  Ray CameraProxy::GetCameraToViewportRay(const Real ScreenX, const Real ScreenY) const
224  { return Ray( this->GraphicsCamera->getCameraToViewportRay(ScreenX,ScreenY) ); }
225 
226  ///////////////////////////////////////////////////////////////////////////////
227  // Camera Properties
228 
230  { return this->CamName; }
231 
233  { this->GraphicsCamera->setPolygonMode( ConvertPolygonMode(PolyMode) ); }
234 
236  { return ConvertPolygonMode( this->GraphicsCamera->getPolygonMode() ); }
237 
239  { this->GraphicsCamera->setProjectionType( ConvertProjectionType(ProjType) ); }
240 
242  { return ConvertProjectionType( this->GraphicsCamera->getProjectionType() ); }
243 
244  void CameraProxy::SetOrientationMode(const Graphics::OrientationMode OriMode)
245  { this->GraphicsCamera->setOrientationMode( ConvertOrientationMode(OriMode) ); }
246 
247  Graphics::OrientationMode CameraProxy::GetOrientationMode() const
248  { return ConvertOrientationMode( this->GraphicsCamera->getOrientationMode() ); }
249 
250  void CameraProxy::SetOrthoWindow(const Real Width, const Real Height)
251  { this->GraphicsCamera->setOrthoWindow(Width,Height); }
252 
254  { this->GraphicsCamera->setOrthoWindowWidth(Width); }
255 
257  { return this->GraphicsCamera->getOrthoWindowWidth(); }
258 
260  { this->GraphicsCamera->setOrthoWindowHeight(Height); }
261 
263  { return this->GraphicsCamera->getOrthoWindowHeight(); }
264 
266  { this->GraphicsCamera->setNearClipDistance(NearDist); }
267 
269  { return this->GraphicsCamera->getNearClipDistance(); }
270 
272  { this->GraphicsCamera->setFarClipDistance(FarDist); }
273 
275  { return this->GraphicsCamera->getFarClipDistance(); }
276 
278  { this->GraphicsCamera->setFOVy( Ogre::Radian(FOV) ); }
279 
281  { return this->GraphicsCamera->getFOVy().valueRadians(); }
282 
284  { this->GraphicsCamera->setAspectRatio(Ratio); }
285 
287  { return this->GraphicsCamera->getAspectRatio(); }
288 
289  void CameraProxy::SetFixedYawAxis(const Boolean UseFixed, const Vector3& Axis)
290  {
291  this->UseFixedYaw = UseFixed;
292  this->FixedYawAxis = Axis;
293  this->GraphicsCamera->setFixedYawAxis(UseFixed,Axis.GetOgreVector3());
294  }
295 
297  { return this->FixedYawAxis; }
298 
299  ///////////////////////////////////////////////////////////////////////////////
300  // Serialization
301 
303  {
305 
306  XML::Node PropertiesNode = SelfRoot.AppendChild( CameraProxy::GetSerializableName() + "Properties" );
307 
308  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
309  PropertiesNode.AppendAttribute("Name").SetValue( this->GetName() ) &&
310  PropertiesNode.AppendAttribute("PolygonMode").SetValue( this->GetPolygonMode() ) &&
311  PropertiesNode.AppendAttribute("ProjectionType").SetValue( this->GetProjectionType() ) &&
312  PropertiesNode.AppendAttribute("OrientationMode").SetValue( this->GetOrientationMode() ) &&
313  PropertiesNode.AppendAttribute("OrthoWidth").SetValue( this->GetOrthoWindowWidth() ) &&
314  PropertiesNode.AppendAttribute("OrthoHeight").SetValue( this->GetOrthoWindowHeight() ) &&
315  PropertiesNode.AppendAttribute("NearClipDistance").SetValue( this->GetNearClipDistance() ) &&
316  PropertiesNode.AppendAttribute("FarClipDistance").SetValue( this->GetFarClipDistance() ) &&
317  PropertiesNode.AppendAttribute("FieldOfView").SetValue( this->GetFieldOfViewY() ) &&
318  PropertiesNode.AppendAttribute("AspectRatio").SetValue( this->GetAspectRatio() ) &&
319  PropertiesNode.AppendAttribute("UseFixedYaw").SetValue( this->IsFixedYawEnabled() ) )
320  {
321  XML::Node FixedYawAxisNode = PropertiesNode.AppendChild("FixedYawAxis");
322  this->GetFixedYawAxis().ProtoSerialize( FixedYawAxisNode );
323 
324  return;
325  }else{
326  SerializeError("Create XML Attribute Values",CameraProxy::GetSerializableName() + "Properties",true);
327  }
328  }
329 
331  {
333 
334  XML::Attribute CurrAttrib;
335  XML::Node PropertiesNode = SelfRoot.GetChild( CameraProxy::GetSerializableName() + "Properties" );
336 
337  if( !PropertiesNode.Empty() ) {
338  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
339  Boolean UseFixed = true;
340  Vector3 FixedYaw = Vector3::Unit_Y();
341  Real OrthoWidth = 0, OrthoHeight = 0;
342 
343  CurrAttrib = PropertiesNode.GetAttribute("Name");
344  if( !CurrAttrib.Empty() )
345  this->CamName = CurrAttrib.AsString();
346 
347  CurrAttrib = PropertiesNode.GetAttribute("PolygonMode");
348  if( !CurrAttrib.Empty() )
349  this->SetPolygonMode( static_cast<Graphics::CameraPolyMode>( CurrAttrib.AsWhole() ) );
350 
351  CurrAttrib = PropertiesNode.GetAttribute("ProjectionType");
352  if( !CurrAttrib.Empty() )
353  this->SetProjectionType( static_cast<Graphics::ProjectionType>( CurrAttrib.AsWhole() ) );
354 
355  CurrAttrib = PropertiesNode.GetAttribute("OrientationMode");
356  if( !CurrAttrib.Empty() )
357  this->SetOrientationMode( static_cast<Graphics::OrientationMode>( CurrAttrib.AsWhole() ) );
358 
359  CurrAttrib = PropertiesNode.GetAttribute("OrthoWidth");
360  if( !CurrAttrib.Empty() )
361  OrthoWidth = CurrAttrib.AsReal();
362 
363  CurrAttrib = PropertiesNode.GetAttribute("OrthoHeight");
364  if( !CurrAttrib.Empty() )
365  OrthoHeight = CurrAttrib.AsReal();
366 
367  this->SetOrthoWindow(OrthoWidth,OrthoHeight);
368 
369  CurrAttrib = PropertiesNode.GetAttribute("NearClipDistance");
370  if( !CurrAttrib.Empty() )
371  this->SetNearClipDistance( CurrAttrib.AsReal() );
372 
373  CurrAttrib = PropertiesNode.GetAttribute("FarClipDistance");
374  if( !CurrAttrib.Empty() )
375  this->SetFarClipDistance( CurrAttrib.AsReal() );
376 
377  CurrAttrib = PropertiesNode.GetAttribute("FieldOfView");
378  if( !CurrAttrib.Empty() )
379  this->SetFieldOfViewY( CurrAttrib.AsReal() );
380 
381  CurrAttrib = PropertiesNode.GetAttribute("AspectRatio");
382  if( !CurrAttrib.Empty() )
383  this->SetAspectRatio( CurrAttrib.AsReal() );
384 
385  CurrAttrib = PropertiesNode.GetAttribute("UseFixedYaw");
386  if( !CurrAttrib.Empty() )
387  UseFixed = StringTools::ConvertToBool( CurrAttrib.AsString() );
388 
389  XML::Node FixedYawAxisNode = PropertiesNode.GetChild("FixedYawAxis").GetFirstChild();
390  if( !FixedYawAxisNode.Empty() ) {
391  FixedYaw.ProtoDeSerialize(FixedYawAxisNode);
392  }
393 
394  this->SetFixedYawAxis(UseFixed,FixedYaw);
395  }else{
396  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (CameraProxy::GetSerializableName() + "Properties" ) + ": Not Version 1.");
397  }
398  }else{
399  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,CameraProxy::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
400  }
401  }
402 
405 
407  { return "CameraProxy"; }
408 
409  ///////////////////////////////////////////////////////////////////////////////
410  // Internal Methods
411 
412  Ogre::Camera* CameraProxy::_GetGraphicsObject() const
413  { return this->GraphicsCamera; }
414 
415  Ogre::MovableObject* CameraProxy::_GetBaseGraphicsObject() const
416  { return this->GraphicsCamera; }
417  }//Graphics
418 }//Mezzanine
419 
420 #endif