SceneGraph.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/scene/Common.h>
  7. #include <anki/scene/SceneNode.h>
  8. #include <anki/scene/Visibility.h>
  9. #include <anki/core/Timestamp.h>
  10. #include <anki/Math.h>
  11. #include <anki/util/Singleton.h>
  12. #include <anki/util/HighRezTimer.h>
  13. #include <anki/core/App.h>
  14. #include <anki/event/EventManager.h>
  15. namespace anki
  16. {
  17. // Forward
  18. class MainRenderer;
  19. class ResourceManager;
  20. class Camera;
  21. class Input;
  22. class SectorGroup;
  23. class ConfigSet;
  24. /// @addtogroup scene
  25. /// @{
  26. /// The scene graph that all the scene entities
  27. class SceneGraph
  28. {
  29. friend class SceneNode;
  30. public:
  31. SceneGraph();
  32. ~SceneGraph();
  33. ANKI_USE_RESULT Error init(AllocAlignedCallback allocCb,
  34. void* allocCbData,
  35. ThreadPool* threadpool,
  36. ResourceManager* resources,
  37. Input* input,
  38. const Timestamp* globalTimestamp,
  39. const ConfigSet& config);
  40. Timestamp getGlobalTimestamp() const
  41. {
  42. return m_timestamp;
  43. }
  44. /// @note Return a copy
  45. SceneAllocator<U8> getAllocator() const
  46. {
  47. return m_alloc;
  48. }
  49. /// @note Return a copy
  50. SceneFrameAllocator<U8> getFrameAllocator() const
  51. {
  52. return m_frameAlloc;
  53. }
  54. Vec4 getAmbientColor() const
  55. {
  56. return Vec4(m_ambientCol, 1.0);
  57. }
  58. void setAmbientColor(const Vec4& x)
  59. {
  60. m_ambientCol = x.xyz();
  61. m_ambiendColorUpdateTimestamp = getGlobalTimestamp();
  62. }
  63. U32 getAmbientColorUpdateTimestamp() const
  64. {
  65. return m_ambiendColorUpdateTimestamp;
  66. }
  67. Camera& getActiveCamera()
  68. {
  69. ANKI_ASSERT(m_mainCam != nullptr);
  70. return *m_mainCam;
  71. }
  72. const Camera& getActiveCamera() const
  73. {
  74. return *m_mainCam;
  75. }
  76. void setActiveCamera(Camera* cam)
  77. {
  78. m_mainCam = cam;
  79. m_activeCameraChangeTimestamp = getGlobalTimestamp();
  80. }
  81. U32 getActiveCameraChangeTimestamp() const
  82. {
  83. return m_activeCameraChangeTimestamp;
  84. }
  85. U32 getSceneNodesCount() const
  86. {
  87. return m_nodesCount;
  88. }
  89. EventManager& getEventManager()
  90. {
  91. return m_events;
  92. }
  93. const EventManager& getEventManager() const
  94. {
  95. return m_events;
  96. }
  97. ThreadPool& _getThreadPool()
  98. {
  99. return *m_threadpool;
  100. }
  101. ANKI_USE_RESULT Error update(
  102. F32 prevUpdateTime, F32 crntTime, MainRenderer& renderer);
  103. SceneNode& findSceneNode(const CString& name);
  104. SceneNode* tryFindSceneNode(const CString& name);
  105. /// Iterate the scene nodes using a lambda
  106. template<typename Func>
  107. ANKI_USE_RESULT Error iterateSceneNodes(Func func)
  108. {
  109. for(SceneNode& psn : m_nodes)
  110. {
  111. Error err = func(psn);
  112. if(err)
  113. {
  114. return err;
  115. }
  116. }
  117. return ErrorCode::NONE;
  118. }
  119. /// Iterate a range of scene nodes using a lambda
  120. template<typename Func>
  121. ANKI_USE_RESULT Error iterateSceneNodes(
  122. PtrSize begin, PtrSize end, Func func);
  123. /// Create a new SceneNode
  124. template<typename Node, typename... Args>
  125. ANKI_USE_RESULT Error newSceneNode(
  126. const CString& name, Node*& node, Args&&... args);
  127. /// Delete a scene node. It actualy marks it for deletion
  128. void deleteSceneNode(SceneNode* node)
  129. {
  130. node->setMarkedForDeletion();
  131. }
  132. void increaseObjectsMarkedForDeletion()
  133. {
  134. m_objectsMarkedForDeletionCount.fetchAdd(1);
  135. }
  136. anki_internal:
  137. ResourceManager& _getResourceManager()
  138. {
  139. return *m_resources;
  140. }
  141. GrManager& getGrManager()
  142. {
  143. return *m_gr;
  144. }
  145. PhysicsWorld& _getPhysicsWorld()
  146. {
  147. return *m_physics;
  148. }
  149. const Input& getInput() const
  150. {
  151. ANKI_ASSERT(m_input);
  152. return *m_input;
  153. }
  154. SectorGroup& getSectorGroup()
  155. {
  156. ANKI_ASSERT(m_sectors);
  157. return *m_sectors;
  158. }
  159. F32 getMaxReflectionProxyDistance() const
  160. {
  161. ANKI_ASSERT(m_maxReflectionProxyDistance > 0.0);
  162. return m_maxReflectionProxyDistance;
  163. }
  164. private:
  165. const Timestamp* m_globalTimestamp = nullptr;
  166. Timestamp m_timestamp = 0; ///< Cached timestamp
  167. // Sub-systems
  168. ThreadPool* m_threadpool = nullptr;
  169. ResourceManager* m_resources = nullptr;
  170. GrManager* m_gr = nullptr;
  171. PhysicsWorld* m_physics = nullptr;
  172. Input* m_input = nullptr;
  173. SceneAllocator<U8> m_alloc;
  174. SceneFrameAllocator<U8> m_frameAlloc;
  175. IntrusiveList<SceneNode> m_nodes;
  176. U32 m_nodesCount = 0;
  177. // SceneDictionary<SceneNode*> m_dict;
  178. Vec3 m_ambientCol = Vec3(1.0); ///< The global ambient color
  179. Timestamp m_ambiendColorUpdateTimestamp = getGlobalTimestamp();
  180. Camera* m_mainCam = nullptr;
  181. Timestamp m_activeCameraChangeTimestamp = getGlobalTimestamp();
  182. EventManager m_events;
  183. SectorGroup* m_sectors;
  184. Atomic<U32> m_objectsMarkedForDeletionCount;
  185. F32 m_maxReflectionProxyDistance = 0.0;
  186. /// Put a node in the appropriate containers
  187. ANKI_USE_RESULT Error registerNode(SceneNode* node);
  188. void unregisterNode(SceneNode* node);
  189. /// Delete the nodes that are marked for deletion
  190. void deleteNodesMarkedForDeletion();
  191. };
  192. //==============================================================================
  193. template<typename Node, typename... Args>
  194. inline Error SceneGraph::newSceneNode(
  195. const CString& name, Node*& node, Args&&... args)
  196. {
  197. ANKI_ASSERT(!name.isEmpty());
  198. Error err = ErrorCode::NONE;
  199. SceneAllocator<Node> al = m_alloc;
  200. node = al.template newInstance<Node>(this);
  201. if(node)
  202. {
  203. err = node->create(name, std::forward<Args>(args)...);
  204. }
  205. else
  206. {
  207. err = ErrorCode::OUT_OF_MEMORY;
  208. }
  209. if(!err)
  210. {
  211. err = registerNode(node);
  212. }
  213. if(err)
  214. {
  215. ANKI_LOGE("Failed to create scene node: %s",
  216. (name.isEmpty()) ? "unnamed" : &name[0]);
  217. if(node)
  218. {
  219. al.deleteInstance(node);
  220. node = nullptr;
  221. }
  222. }
  223. return err;
  224. }
  225. //==============================================================================
  226. template<typename Func>
  227. Error SceneGraph::iterateSceneNodes(PtrSize begin, PtrSize end, Func func)
  228. {
  229. ANKI_ASSERT(begin < m_nodesCount && end <= m_nodesCount);
  230. auto it = m_nodes.getBegin() + begin;
  231. PtrSize count = end - begin;
  232. Error err = ErrorCode::NONE;
  233. while(count-- != 0 && !err)
  234. {
  235. ANKI_ASSERT(it != m_nodes.getEnd());
  236. err = func(*it);
  237. ++it;
  238. }
  239. return ErrorCode::NONE;
  240. }
  241. /// @}
  242. } // end namespace anki