SceneGraph.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef ANKI_SCENE_SCENE_GRAPH_H
  2. #define ANKI_SCENE_SCENE_GRAPH_H
  3. #include "anki/scene/Common.h"
  4. #include "anki/scene/SceneNode.h"
  5. #include "anki/scene/Visibility.h"
  6. #include "anki/core/Timestamp.h"
  7. #include "anki/Math.h"
  8. #include "anki/util/Singleton.h"
  9. #include "anki/util/HighRezTimer.h"
  10. #include "anki/scene/Sector.h"
  11. #include "anki/physics/PhysWorld.h"
  12. #include "anki/event/EventManager.h"
  13. namespace anki {
  14. // Forward
  15. class Renderer;
  16. class Camera;
  17. /// @addtogroup Scene
  18. /// @{
  19. /// The scene graph that all the scene entities
  20. class SceneGraph
  21. {
  22. friend class SceneNode;
  23. public:
  24. /// @name Constructors/Destructor
  25. /// @{
  26. SceneGraph();
  27. ~SceneGraph();
  28. /// @}
  29. void load(const char* filename);
  30. /// @name Accessors
  31. /// @{
  32. /// @note Return a copy
  33. SceneAllocator<U8> getAllocator() const
  34. {
  35. return SceneAllocator<U8>(alloc);
  36. }
  37. /// @note Return a copy
  38. SceneAllocator<U8> getFrameAllocator() const
  39. {
  40. return SceneAllocator<U8>(frameAlloc);
  41. }
  42. Vec4 getAmbientColor() const
  43. {
  44. return Vec4(ambientCol, 1.0);
  45. }
  46. void setAmbientColor(const Vec4& x)
  47. {
  48. ambientCol = x.xyz();
  49. ambiendColorUpdateTimestamp = getGlobTimestamp();
  50. }
  51. U32 getAmbientColorUpdateTimestamp() const
  52. {
  53. return ambiendColorUpdateTimestamp;
  54. }
  55. Camera& getActiveCamera()
  56. {
  57. ANKI_ASSERT(mainCam != nullptr);
  58. return *mainCam;
  59. }
  60. const Camera& getActiveCamera() const
  61. {
  62. return *mainCam;
  63. }
  64. void setActiveCamera(Camera* cam)
  65. {
  66. mainCam = cam;
  67. activeCameraChangeTimestamp = getGlobTimestamp();
  68. }
  69. U32 getActiveCameraChangeTimestamp() const
  70. {
  71. return activeCameraChangeTimestamp;
  72. }
  73. U32 getSceneNodesCount() const
  74. {
  75. return nodes.size();
  76. }
  77. PhysWorld& getPhysics()
  78. {
  79. return physics;
  80. }
  81. const PhysWorld& getPhysics() const
  82. {
  83. return physics;
  84. }
  85. EventManager& getEventManager()
  86. {
  87. return events;
  88. }
  89. const EventManager& getEventManager() const
  90. {
  91. return events;
  92. }
  93. SectorGroup& getSectorGroup()
  94. {
  95. return sectorGroup;
  96. }
  97. const SectorGroup& getSectorGroup() const
  98. {
  99. return sectorGroup;
  100. }
  101. /// @}
  102. void update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer);
  103. SceneNode& findSceneNode(const char* name);
  104. SceneNode* tryFindSceneNode(const char* name);
  105. /// Iterate the scene nodes using a lambda
  106. template<typename Func>
  107. void iterateSceneNodes(Func func)
  108. {
  109. for(SceneNode* psn : nodes)
  110. {
  111. func(*psn);
  112. }
  113. }
  114. /// Iterate a range of scene nodes using a lambda
  115. template<typename Func>
  116. void iterateSceneNodes(PtrSize begin, PtrSize count, Func func)
  117. {
  118. ANKI_ASSERT(begin < nodes.size() && count <= nodes.size());
  119. for(auto it = nodes.begin() + begin; it != nodes.begin() + count; it++)
  120. {
  121. func(*(*it));
  122. }
  123. }
  124. /// Create a new SceneNode
  125. template<typename Node, typename... Args>
  126. void newSceneNode(Node*& node, const char* name, Args&&... args)
  127. {
  128. SceneAllocator<Node> al = alloc;
  129. node = al.allocate(1);
  130. al.construct(node, name, this, std::forward<Args>(args)...);
  131. registerNode(node);
  132. }
  133. /// Delete a scene node. It actualy marks it for deletion
  134. void deleteSceneNode(SceneNode* node)
  135. {
  136. node->markForDeletion();
  137. ++nodesMarkedForDeletionCount;
  138. }
  139. private:
  140. SceneAllocator<U8> alloc;
  141. SceneAllocator<U8> frameAlloc;
  142. SceneVector<SceneNode*> nodes;
  143. SceneDictionary<SceneNode*> dict;
  144. Vec3 ambientCol = Vec3(1.0); ///< The global ambient color
  145. Timestamp ambiendColorUpdateTimestamp = getGlobTimestamp();
  146. Camera* mainCam = nullptr;
  147. Timestamp activeCameraChangeTimestamp = getGlobTimestamp();
  148. PhysWorld physics;
  149. SectorGroup sectorGroup;
  150. EventManager events;
  151. U32 nodesMarkedForDeletionCount = 0;
  152. /// Put a node in the appropriate containers
  153. void registerNode(SceneNode* node);
  154. void unregisterNode(SceneNode* node);
  155. /// Delete the nodes that are marked for deletion
  156. void deleteNodesMarkedForDeletion();
  157. };
  158. typedef Singleton<SceneGraph> SceneGraphSingleton;
  159. /// @}
  160. } // end namespace anki
  161. #endif