SceneNode.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Util/Hierarchy.h>
  8. #include <AnKi/Util/BitMask.h>
  9. #include <AnKi/Util/BitSet.h>
  10. #include <AnKi/Util/List.h>
  11. #include <AnKi/Util/Enum.h>
  12. namespace anki {
  13. /// @addtogroup scene
  14. /// @{
  15. /// Interface class backbone of scene
  16. class SceneNode : public SceneHierarchy<SceneNode>, public IntrusiveListEnabled<SceneNode>
  17. {
  18. friend class SceneComponent;
  19. public:
  20. using Base = SceneHierarchy<SceneNode>;
  21. /// The one and only constructor.
  22. /// @param scene The owner scene.
  23. /// @param name The unique name of the node. If it's empty the the node is not searchable.
  24. SceneNode(CString name);
  25. /// Unregister node
  26. virtual ~SceneNode();
  27. /// A dummy init for those scene nodes that don't need it.
  28. Error init()
  29. {
  30. return Error::kNone;
  31. }
  32. /// Return the name. It may be empty for nodes that we don't want to track.
  33. CString getName() const
  34. {
  35. return (!m_name.isEmpty()) ? m_name.toCString() : CString();
  36. }
  37. U64 getUuid() const
  38. {
  39. return m_uuid;
  40. }
  41. Bool getMarkedForDeletion() const
  42. {
  43. return m_markedForDeletion;
  44. }
  45. void setMarkedForDeletion();
  46. Timestamp getComponentMaxTimestamp() const
  47. {
  48. return m_maxComponentTimestamp;
  49. }
  50. void setComponentMaxTimestamp(Timestamp maxComponentTimestamp)
  51. {
  52. ANKI_ASSERT(maxComponentTimestamp > 0);
  53. m_maxComponentTimestamp = maxComponentTimestamp;
  54. }
  55. void addChild(SceneNode* obj)
  56. {
  57. Base::addChild(obj);
  58. }
  59. /// This is called by the scenegraph every frame after all component updates. By default it does nothing.
  60. /// @param prevUpdateTime Timestamp of the previous update
  61. /// @param crntTime Timestamp of this update
  62. virtual Error frameUpdate([[maybe_unused]] Second prevUpdateTime, [[maybe_unused]] Second crntTime)
  63. {
  64. return Error::kNone;
  65. }
  66. /// Iterate all components.
  67. template<typename TFunct>
  68. void iterateComponents(TFunct func) const
  69. {
  70. for(U32 i = 0; i < m_components.getSize(); ++i)
  71. {
  72. func(*m_components[i]);
  73. }
  74. }
  75. /// Iterate all components.
  76. template<typename TFunct>
  77. void iterateComponents(TFunct func)
  78. {
  79. for(U32 i = 0; i < m_components.getSize(); ++i)
  80. {
  81. func(*m_components[i]);
  82. }
  83. }
  84. /// Iterate all components of a specific type
  85. template<typename TComponent, typename TFunct>
  86. void iterateComponentsOfType(TFunct func) const
  87. {
  88. if(!!(m_componentTypeMask & (1 << SceneComponentTypeMask(TComponent::kClassType))))
  89. {
  90. for(U32 i = 0; i < m_components.getSize(); ++i)
  91. {
  92. if(m_components[i]->getType() == TComponent::kClassType)
  93. {
  94. func(static_cast<const TComponent&>(*m_components[i]));
  95. }
  96. }
  97. }
  98. }
  99. /// Iterate all components of a specific type
  100. template<typename TComponent, typename TFunct>
  101. void iterateComponentsOfType(TFunct func)
  102. {
  103. if(!!(m_componentTypeMask & (1 << SceneComponentTypeMask(TComponent::kClassType))))
  104. {
  105. for(U32 i = 0; i < m_components.getSize(); ++i)
  106. {
  107. if(m_components[i]->getType() == TComponent::kClassType)
  108. {
  109. func(static_cast<TComponent&>(*m_components[i]));
  110. }
  111. }
  112. }
  113. }
  114. /// Try geting a pointer to the first component of the requested type
  115. template<typename TComponent>
  116. const TComponent* tryGetFirstComponentOfType() const
  117. {
  118. if(!!(m_componentTypeMask & (1 << SceneComponentTypeMask(TComponent::kClassType))))
  119. {
  120. for(U32 i = 0; i < m_components.getSize(); ++i)
  121. {
  122. if(m_components[i]->getType() == TComponent::kClassType)
  123. {
  124. return static_cast<const TComponent*>(m_components[i]);
  125. }
  126. }
  127. }
  128. return nullptr;
  129. }
  130. /// Try geting a pointer to the first component of the requested type
  131. template<typename TComponent>
  132. TComponent* tryGetFirstComponentOfType()
  133. {
  134. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetFirstComponentOfType<TComponent>();
  135. return const_cast<TComponent*>(c);
  136. }
  137. /// Get a pointer to the first component of the requested type
  138. template<typename TComponent>
  139. const TComponent& getFirstComponentOfType() const
  140. {
  141. const TComponent* out = tryGetFirstComponentOfType<TComponent>();
  142. ANKI_ASSERT(out != nullptr);
  143. return *out;
  144. }
  145. /// Get a pointer to the first component of the requested type
  146. template<typename TComponent>
  147. TComponent& getFirstComponentOfType()
  148. {
  149. const TComponent& c = static_cast<const SceneNode*>(this)->getFirstComponentOfType<TComponent>();
  150. return const_cast<TComponent&>(c);
  151. }
  152. /// Try geting a pointer to the nth component of the requested type.
  153. template<typename TComponent>
  154. const TComponent* tryGetNthComponentOfType(U32 nth) const
  155. {
  156. if(!!(m_componentTypeMask & (1 << SceneComponentTypeMask(TComponent::kClassType))))
  157. {
  158. I32 inth = I32(nth);
  159. for(U32 i = 0; i < m_components.getSize(); ++i)
  160. {
  161. if(m_components[i]->getType() == TComponent::kClassType && inth-- == 0)
  162. {
  163. return static_cast<const TComponent*>(m_components[i]);
  164. }
  165. }
  166. }
  167. return nullptr;
  168. }
  169. /// Try geting a pointer to the nth component of the requested type.
  170. template<typename TComponent>
  171. TComponent* tryGetNthComponentOfType(U32 nth)
  172. {
  173. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetNthComponentOfType<TComponent>(nth);
  174. return const_cast<TComponent*>(c);
  175. }
  176. template<typename TComponent>
  177. const TComponent& getNthComponentOfType(U32 nth) const
  178. {
  179. const TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  180. ANKI_ASSERT(out);
  181. return *out;
  182. }
  183. template<typename TComponent>
  184. TComponent& getNthComponentOfType(U32 nth)
  185. {
  186. TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  187. ANKI_ASSERT(out);
  188. return *out;
  189. }
  190. /// Get the nth component.
  191. template<typename TComponent>
  192. TComponent& getComponentAt(U32 idx)
  193. {
  194. ANKI_ASSERT(m_components[idx]->getType() == TComponent::kClassType);
  195. SceneComponent* c = m_components[idx];
  196. return *static_cast<TComponent*>(c);
  197. }
  198. /// Get the nth component.
  199. template<typename TComponent>
  200. const TComponent& getComponentAt(U32 idx) const
  201. {
  202. ANKI_ASSERT(m_components[idx]->getType() == TComponent::kClassType);
  203. const SceneComponent* c = m_components[idx];
  204. return *static_cast<const TComponent*>(c);
  205. }
  206. U32 getComponentCount() const
  207. {
  208. return m_components.getSize();
  209. }
  210. template<typename TComponent>
  211. U32 countComponentsOfType() const
  212. {
  213. U32 count = 0;
  214. iterateComponentsOfType<TComponent>([&]([[maybe_unused]] const TComponent& c) {
  215. ++count;
  216. });
  217. return count;
  218. }
  219. /// Ignore parent nodes's transform.
  220. void setIgnoreParentTransform(Bool ignore)
  221. {
  222. m_ignoreParentNodeTransform = ignore;
  223. }
  224. const Transform& getLocalTransform() const
  225. {
  226. return m_ltrf;
  227. }
  228. void setLocalTransform(const Transform& x)
  229. {
  230. m_ltrf = x;
  231. m_localTransformDirty = true;
  232. }
  233. void setLocalOrigin(const Vec4& x)
  234. {
  235. m_ltrf.setOrigin(x);
  236. m_localTransformDirty = true;
  237. }
  238. const Vec4& getLocalOrigin() const
  239. {
  240. return m_ltrf.getOrigin();
  241. }
  242. void setLocalRotation(const Mat3x4& x)
  243. {
  244. m_ltrf.setRotation(x);
  245. m_localTransformDirty = true;
  246. }
  247. const Mat3x4& getLocalRotation() const
  248. {
  249. return m_ltrf.getRotation();
  250. }
  251. void setLocalScale(F32 x)
  252. {
  253. m_ltrf.setScale(x);
  254. m_localTransformDirty = true;
  255. }
  256. F32 getLocalScale() const
  257. {
  258. return m_ltrf.getScale();
  259. }
  260. const Transform& getWorldTransform() const
  261. {
  262. return m_wtrf;
  263. }
  264. const Transform& getPreviousWorldTransform() const
  265. {
  266. return m_prevWTrf;
  267. }
  268. /// @name Mess with the local transform
  269. /// @{
  270. void rotateLocalX(F32 angleRad)
  271. {
  272. m_ltrf.getRotation().rotateXAxis(angleRad);
  273. m_localTransformDirty = true;
  274. }
  275. void rotateLocalY(F32 angleRad)
  276. {
  277. m_ltrf.getRotation().rotateYAxis(angleRad);
  278. m_localTransformDirty = true;
  279. }
  280. void rotateLocalZ(F32 angleRad)
  281. {
  282. m_ltrf.getRotation().rotateZAxis(angleRad);
  283. m_localTransformDirty = true;
  284. }
  285. void moveLocalX(F32 distance)
  286. {
  287. Vec3 x_axis = m_ltrf.getRotation().getColumn(0);
  288. m_ltrf.getOrigin() += Vec4(x_axis, 0.0) * distance;
  289. m_localTransformDirty = true;
  290. }
  291. void moveLocalY(F32 distance)
  292. {
  293. Vec3 y_axis = m_ltrf.getRotation().getColumn(1);
  294. m_ltrf.getOrigin() += Vec4(y_axis, 0.0) * distance;
  295. m_localTransformDirty = true;
  296. }
  297. void moveLocalZ(F32 distance)
  298. {
  299. Vec3 z_axis = m_ltrf.getRotation().getColumn(2);
  300. m_ltrf.getOrigin() += Vec4(z_axis, 0.0) * distance;
  301. m_localTransformDirty = true;
  302. }
  303. void scale(F32 s)
  304. {
  305. m_ltrf.getScale() *= s;
  306. m_localTransformDirty = true;
  307. }
  308. void lookAtPoint(const Vec4& point)
  309. {
  310. m_ltrf.lookAt(point, Vec4(0.0f, 1.0f, 0.0f, 0.0f));
  311. m_localTransformDirty = true;
  312. }
  313. /// @}
  314. Bool movedThisFrame() const
  315. {
  316. return m_transformUpdatedThisFrame;
  317. }
  318. ANKI_INTERNAL Bool updateTransform();
  319. /// Create and append a component to the components container. The SceneNode has the ownership.
  320. template<typename TComponent>
  321. TComponent* newComponent();
  322. private:
  323. U32 m_uuid;
  324. SceneString m_name; ///< A unique name.
  325. GrDynamicArray<SceneComponent*> m_components;
  326. SceneComponentTypeMask m_componentTypeMask = SceneComponentTypeMask::kNone;
  327. Timestamp m_maxComponentTimestamp = 0;
  328. /// The transformation in local space.
  329. Transform m_ltrf = Transform::getIdentity();
  330. /// The transformation in world space (local combined with parent's transformation).
  331. Transform m_wtrf = Transform::getIdentity();
  332. /// Keep the previous transformation for checking if it moved.
  333. Transform m_prevWTrf = Transform::getIdentity();
  334. // Flags
  335. Bool m_markedForDeletion : 1 = false;
  336. Bool m_localTransformDirty : 1 = true;
  337. Bool m_ignoreParentNodeTransform : 1 = false;
  338. Bool m_transformUpdatedThisFrame : 1 = true;
  339. void newComponentInternal(SceneComponent* newc);
  340. };
  341. /// @}
  342. } // end namespace anki