Ocular Engine
SceneObject.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_CORE_SCENE_OBJECT__H__
19 #define __H__OCULAR_CORE_SCENE_OBJECT__H__
20 
21 #include "Object.hpp"
22 
23 #include "ObjectIO/ObjectIO.hpp"
24 
25 #include "Math/Transform.hpp"
26 #include "Math/Bounds/BoundsSphere.hpp"
27 #include "Math/Bounds/BoundsAABB.hpp"
28 #include "Math/Bounds/BoundsOBB.hpp"
29 
30 #include "Graphics/Shader/Uniform/UniformPerObject.hpp"
31 
32 #include <string>
33 #include <map>
34 #include <vector>
35 
36 #include "Math/Matrix3x3.hpp"
37 
38 //------------------------------------------------------------------------------------------
39 
44 namespace Ocular
45 {
46  namespace Math
47  {
48  class Matrix4x4;
49  }
50 
55  namespace Core
56  {
57  class SceneManager;
58  class ARoutine;
59  class ARenderable;
60 
93  class SceneObject : public Object
94  {
95  friend class SceneManager;
96 
97  public:
98 
109  SceneObject(std::string const& name, SceneObject* parent = nullptr, std::string const& type = "SceneObject");
110 
117  SceneObject();
118 
119  virtual ~SceneObject();
120 
121  //------------------------------------------------------------
122  // General Misc. Methods
123  //------------------------------------------------------------
124 
132  virtual void onVariableModified(std::string const& varName) override;
133 
138 
153  void setActive(bool active);
154 
158  bool isActive() const;
159 
170  void setVisible(bool visible);
171 
175  bool isVisible() const;
176 
187  void setForcedVisible(bool forced);
188 
192  bool isForcedVisible() const;
193 
208  void setStatic(bool isStatic);
209 
213  bool isStatic() const;
214 
225  void setPersistent(bool persists);
226 
230  bool isPersistent() const;
231 
235  Graphics::UniformPerObject const& getUniformData(Math::Matrix4x4 const& viewMatrix, Math::Matrix4x4 const& projMatrix);
236 
237  //------------------------------------------------------------
238  // Movement and Rotation Methods
239  //------------------------------------------------------------
240 
248  void setPosition(float x, float y, float z);
249 
254  void setPosition(Math::Vector3f const& position);
255 
259  Math::Vector3f getPosition(bool local = true) const;
260 
265  void translate(Math::Vector3f const& translation, bool local = true);
266 
273  void rotate(float angle, Math::Vector3f const& axis);
274 
278  void rotate(Math::Quaternion const& rotation);
279 
284  void setRotation(Math::Quaternion const& rotation);
285 
289  void resetRotation();
290 
294  Math::Quaternion const& getRotation() const;
295 
299  void setScale(Math::Vector3f const& scale);
300 
304  void setScale(float xScale, float yScale, float zScale);
305 
309  Math::Vector3f getScale(bool local = true) const;
310 
314  void setTransform(Math::Transform const& transform);
315 
319  void lookAt(Math::Vector3f const& point);
320 
324  Math::Transform const& getTransform() const;
325 
329  virtual Math::Matrix4x4 getModelMatrix(bool local = true) const;
330 
331  //------------------------------------------------------------
332  // Child Object Methods
333  //------------------------------------------------------------
334 
364  void setParent(SceneObject* parent, bool maintainWorldPos = true);
365 
369  SceneObject* getParent() const;
370 
382  SceneObject* createChild(std::string const& name);
383 
414  void addChild(SceneObject* child, bool maintainWorldPos = true);
415 
420  SceneObject* findChild(std::string const& name);
421 
426  SceneObject* findChild(UUID const& uuid);
427 
448  SceneObject* removeChild(std::string const& name);
449 
470  SceneObject* removeChild(UUID const& uuid);
471 
492  SceneObject* removeChild(SceneObject const* object);
493 
497  uint32_t getNumChildren() const;
498 
502  std::vector<SceneObject*> const& getAllChildren() const;
503 
504  //------------------------------------------------------------
505  // Routine Methods
506  //------------------------------------------------------------
507 
515  ARoutine* addRoutine(std::string const& name);
516 
520  void addRoutine(ARoutine* routine);
521 
528  template<class T> T* addRoutine()
529  {
530  T* t = new T();
531  ARoutine* routine = dynamic_cast<ARoutine*>(t);
532 
533  if(routine)
534  {
535  routine->setParent(this);
536  m_Routines.push_back(routine);
537  routine->onCreation();
538  }
539  else
540  {
541  // T is not a routine
542  delete t;
543  t = nullptr;
544  }
545 
546  return t;
547  }
548 
557  bool removeRoutine(std::string const& name);
558 
569  bool removeRoutine(ARoutine* routine, bool transferring = false);
570 
574  void removeAllRoutines();
575 
582  ARoutine* getRoutine(std::string const& name);
583 
587  std::vector<ARoutine*> const& getAllRoutines() const;
588 
592  uint32_t getNumRoutines() const;
593 
594  //------------------------------------------------------------
595  // Renderable Methods
596  //------------------------------------------------------------
597 
601  ARenderable* setRenderable(std::string const& name);
602 
606  void setRenderable(ARenderable* renderable);
607 
611  template<class T>
612  T* setRenderable()
613  {
614  T* t = new T();
615  ARenderable* renderable = dynamic_cast<ARenderable*>(t);
616 
617  if(renderable)
618  {
619  removeRenderable();
620  m_Renderable = renderable;
621  m_Renderable->m_Parent = this;
622  }
623  else
624  {
625  delete t;
626  t = nullptr;
627  }
628 
629  return t;
630  }
631 
635  void removeRenderable(bool transferring = false);
636 
640  ARenderable* getRenderable() const;
641 
642  //------------------------------------------------------------
643  // Inherited Methods
644  //------------------------------------------------------------
645 
646  virtual void onLoad(BuilderNode const* node) override;
647  virtual void onSave(BuilderNode* node) const override;
648 
649  //------------------------------------------------------------
650  // Bounds Related
651  //------------------------------------------------------------
652 
653  void forceBoundsRebuild();
654 
661  Math::BoundsSphere getBoundsSphere(bool local);
662 
669  Math::BoundsAABB getBoundsAABB(bool local);
670 
677  Math::BoundsOBB getBoundsOBB(bool local);
678 
679  protected:
680 
681  void getModelMatrix(Math::Matrix4x4& matrix);
682  virtual void updateBounds(uint32_t dirtyFlags);
683 
684  //------------------------------------------------------------
685 
686  SceneObject* m_Parent;
687  uint32_t m_Layer;
688 
689  // Physical Characteristics
690 
691  Graphics::UniformPerObject m_UniformData;
692  Math::Transform m_Transform;
693 
694  Math::BoundsSphere m_BoundsSphereLocal;
695  Math::BoundsAABB m_BoundsAABBLocal;
696  Math::BoundsOBB m_BoundsOBBLocal;
697  Math::BoundsSphere m_BoundsSphereWorld;
698  Math::BoundsAABB m_BoundsAABBWorld;
699  Math::BoundsOBB m_BoundsOBBWorld;
700 
701  private:
702 
703  void removeChild(std::vector<SceneObject*>::iterator& child);
704 
705  //------------------------------------------------------------
706 
707  bool m_IsStatic;
708  bool m_IsActive;
709  bool m_IsVisible;
710  bool m_ForcedVisible;
711  bool m_Persists;
712 
713  std::vector<ARoutine*> m_Routines;
714  ARenderable* m_Renderable;
715 
716  std::vector<SceneObject*> m_Children;
717  };
718  }
722 }
727 //------------------------------------------------------------------------------------------
728 
729 #endif
uint32_t m_Layer
The render layer this object is part of.
Definition: SceneObject.hpp:687
Math::Transform & getTransform()
Definition: SceneObject.cpp:152
void rotate(float angle, Math::Vector3f const &axis)
Definition: SceneObject.cpp:306
void removeAllRoutines()
Definition: SceneObject.cpp:696
virtual void onLoad(BuilderNode const *node) override
Definition: SceneObject.cpp:804
virtual void onCreation()
Definition: ARoutine.cpp:70
SceneObject()
Definition: SceneObject.cpp:55
SceneObject * getParent() const
Definition: SceneObject.cpp:439
ARoutine * getRoutine(std::string const &name)
Definition: SceneObject.cpp:714
virtual void onSave(BuilderNode *node) const override
Definition: SceneObject.cpp:892
Math::BoundsAABB getBoundsAABB(bool local)
Definition: SceneObject.cpp:988
virtual void onVariableModified(std::string const &varName) override
Definition: SceneObject.cpp:144
void resetRotation()
Definition: SceneObject.cpp:318
Definition: SceneManager.hpp:64
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
Definition: BoundsOBB.hpp:56
std::vector< SceneObject * > const & getAllChildren() const
Definition: SceneObject.cpp:601
Definition: Quaternion.hpp:50
Definition: ARoutine.hpp:59
SceneObject * createChild(std::string const &name)
Definition: SceneObject.cpp:444
T * addRoutine()
Definition: SceneObject.hpp:528
virtual ~SceneObject()
Definition: SceneObject.cpp:72
void setRotation(Math::Quaternion const &rotation)
Definition: SceneObject.cpp:324
bool isPersistent() const
Definition: SceneObject.cpp:251
void setVisible(bool visible)
Definition: SceneObject.cpp:195
Definition: SceneObject.hpp:93
void addChild(SceneObject *child, bool maintainWorldPos=true)
Definition: SceneObject.cpp:450
Definition: ARenderable.hpp:53
void setParent(SceneObject *parent, bool maintainWorldPos=true)
Definition: SceneObject.cpp:410
Definition: UUID.hpp:45
Definition: Transform.hpp:45
std::vector< ARoutine * > const & getAllRoutines() const
Definition: SceneObject.cpp:735
void translate(Math::Vector3f const &translation, bool local=true)
Definition: SceneObject.cpp:300
uint32_t getNumChildren() const
Definition: SceneObject.cpp:596
Math::BoundsOBB getBoundsOBB(bool local)
Definition: SceneObject.cpp:1001
Math::BoundsSphere getBoundsSphere(bool local)
Definition: SceneObject.cpp:975
SceneObject * removeChild(std::string const &name)
Definition: SceneObject.cpp:521
The fixed struct for all Uniform data for Objects.
Definition: UniformPerObject.hpp:41
A 4x4 column-major float matrix.
Definition: Matrix4x4.hpp:64
Math::Vector3f getPosition(bool local=true) const
Definition: SceneObject.cpp:283
bool isStatic() const
Definition: SceneObject.cpp:241
void setForcedVisible(bool forced)
Definition: SceneObject.cpp:205
Definition: BoundsAABB.hpp:63
SceneObject * findChild(std::string const &name)
Definition: SceneObject.cpp:479
void setActive(bool active)
Definition: SceneObject.cpp:157
Definition: BoundsSphere.hpp:52
Base class of all representable Ocular constructs.
Definition: Object.hpp:44
void setPersistent(bool persists)
Definition: SceneObject.cpp:246
void setPosition(float x, float y, float z)
Definition: SceneObject.cpp:271
void setStatic(bool isStatic)
Definition: SceneObject.cpp:232
bool isForcedVisible() const
Definition: SceneObject.cpp:227
bool removeRoutine(std::string const &name)
Definition: SceneObject.cpp:635
bool isVisible() const
Definition: SceneObject.cpp:200
bool isActive() const
Definition: SceneObject.cpp:190