SceneNode.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (C) 2009-present, 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. U32 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. void setParent(SceneNode* obj)
  60. {
  61. Base::setParent(obj);
  62. }
  63. /// This is called by the scenegraph every frame after all component updates. By default it does nothing.
  64. /// @param prevUpdateTime Timestamp of the previous update
  65. /// @param crntTime Timestamp of this update
  66. virtual Error frameUpdate([[maybe_unused]] Second prevUpdateTime, [[maybe_unused]] Second crntTime)
  67. {
  68. return Error::kNone;
  69. }
  70. /// Iterate all components.
  71. template<typename TFunct>
  72. void iterateComponents(TFunct func) const
  73. {
  74. for(U32 i = 0; i < m_components.getSize(); ++i)
  75. {
  76. func(*m_components[i]);
  77. }
  78. }
  79. /// Iterate all components.
  80. template<typename TFunct>
  81. void iterateComponents(TFunct func)
  82. {
  83. for(U32 i = 0; i < m_components.getSize(); ++i)
  84. {
  85. func(*m_components[i]);
  86. }
  87. }
  88. /// Iterate all components of a specific type
  89. template<typename TComponent, typename TFunct>
  90. void iterateComponentsOfType(TFunct func) const
  91. {
  92. if(hasComponent<TComponent>())
  93. {
  94. for(U32 i = 0; i < m_components.getSize(); ++i)
  95. {
  96. if(m_components[i]->getType() == TComponent::kClassType)
  97. {
  98. func(static_cast<const TComponent&>(*m_components[i]));
  99. }
  100. }
  101. }
  102. }
  103. /// Iterate all components of a specific type
  104. template<typename TComponent, typename TFunct>
  105. void iterateComponentsOfType(TFunct func)
  106. {
  107. if(hasComponent<TComponent>())
  108. {
  109. for(U32 i = 0; i < m_components.getSize(); ++i)
  110. {
  111. if(m_components[i]->getType() == TComponent::kClassType)
  112. {
  113. func(static_cast<TComponent&>(*m_components[i]));
  114. }
  115. }
  116. }
  117. }
  118. /// Try geting a pointer to the first component of the requested type
  119. template<typename TComponent>
  120. const TComponent* tryGetFirstComponentOfType() const
  121. {
  122. if(hasComponent<TComponent>())
  123. {
  124. for(U32 i = 0; i < m_components.getSize(); ++i)
  125. {
  126. if(m_components[i]->getType() == TComponent::kClassType)
  127. {
  128. return static_cast<const TComponent*>(m_components[i]);
  129. }
  130. }
  131. }
  132. return nullptr;
  133. }
  134. /// Try geting a pointer to the first component of the requested type
  135. template<typename TComponent>
  136. TComponent* tryGetFirstComponentOfType()
  137. {
  138. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetFirstComponentOfType<TComponent>();
  139. return const_cast<TComponent*>(c);
  140. }
  141. /// Get a pointer to the first component of the requested type
  142. template<typename TComponent>
  143. const TComponent& getFirstComponentOfType() const
  144. {
  145. const TComponent* out = tryGetFirstComponentOfType<TComponent>();
  146. ANKI_ASSERT(out != nullptr);
  147. return *out;
  148. }
  149. /// Get a pointer to the first component of the requested type
  150. template<typename TComponent>
  151. TComponent& getFirstComponentOfType()
  152. {
  153. const TComponent& c = static_cast<const SceneNode*>(this)->getFirstComponentOfType<TComponent>();
  154. return const_cast<TComponent&>(c);
  155. }
  156. /// Try geting a pointer to the nth component of the requested type.
  157. template<typename TComponent>
  158. const TComponent* tryGetNthComponentOfType(U32 nth) const
  159. {
  160. if(hasComponent<TComponent>())
  161. {
  162. I32 inth = I32(nth);
  163. for(U32 i = 0; i < m_components.getSize(); ++i)
  164. {
  165. if(m_components[i]->getType() == TComponent::kClassType && inth-- == 0)
  166. {
  167. return static_cast<const TComponent*>(m_components[i]);
  168. }
  169. }
  170. }
  171. return nullptr;
  172. }
  173. /// Try geting a pointer to the nth component of the requested type.
  174. template<typename TComponent>
  175. TComponent* tryGetNthComponentOfType(U32 nth)
  176. {
  177. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetNthComponentOfType<TComponent>(nth);
  178. return const_cast<TComponent*>(c);
  179. }
  180. template<typename TComponent>
  181. const TComponent& getNthComponentOfType(U32 nth) const
  182. {
  183. const TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  184. ANKI_ASSERT(out);
  185. return *out;
  186. }
  187. template<typename TComponent>
  188. TComponent& getNthComponentOfType(U32 nth)
  189. {
  190. TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  191. ANKI_ASSERT(out);
  192. return *out;
  193. }
  194. /// Get the nth component.
  195. template<typename TComponent>
  196. TComponent& getComponentAt(U32 idx)
  197. {
  198. ANKI_ASSERT(m_components[idx]->getType() == TComponent::kClassType);
  199. SceneComponent* c = m_components[idx];
  200. return *static_cast<TComponent*>(c);
  201. }
  202. /// Get the nth component.
  203. template<typename TComponent>
  204. const TComponent& getComponentAt(U32 idx) const
  205. {
  206. ANKI_ASSERT(m_components[idx]->getType() == TComponent::kClassType);
  207. const SceneComponent* c = m_components[idx];
  208. return *static_cast<const TComponent*>(c);
  209. }
  210. U32 getComponentCount() const
  211. {
  212. return m_components.getSize();
  213. }
  214. template<typename TComponent>
  215. U32 countComponentsOfType() const
  216. {
  217. U32 count = 0;
  218. iterateComponentsOfType<TComponent>([&]([[maybe_unused]] const TComponent& c) {
  219. ++count;
  220. });
  221. return count;
  222. }
  223. /// Ignore parent nodes's transform.
  224. void setIgnoreParentTransform(Bool ignore)
  225. {
  226. m_ignoreParentNodeTransform = ignore;
  227. }
  228. const Transform& getLocalTransform() const
  229. {
  230. return m_ltrf;
  231. }
  232. void setLocalTransform(const Transform& x)
  233. {
  234. m_ltrf = x;
  235. m_localTransformDirty = true;
  236. }
  237. void setLocalOrigin(const Vec4& x)
  238. {
  239. m_ltrf.setOrigin(x);
  240. m_localTransformDirty = true;
  241. }
  242. const Vec4& getLocalOrigin() const
  243. {
  244. return m_ltrf.getOrigin();
  245. }
  246. void setLocalRotation(const Mat3x4& x)
  247. {
  248. m_ltrf.setRotation(x);
  249. m_localTransformDirty = true;
  250. }
  251. const Mat3x4& getLocalRotation() const
  252. {
  253. return m_ltrf.getRotation();
  254. }
  255. void setLocalScale(const Vec4& x)
  256. {
  257. m_ltrf.setScale(x);
  258. m_localTransformDirty = true;
  259. }
  260. const Vec4& getLocalScale() const
  261. {
  262. return m_ltrf.getScale();
  263. }
  264. const Transform& getWorldTransform() const
  265. {
  266. return m_wtrf;
  267. }
  268. const Transform& getPreviousWorldTransform() const
  269. {
  270. return m_prevWTrf;
  271. }
  272. /// @name Mess with the local transform
  273. /// @{
  274. void rotateLocalX(F32 angleRad)
  275. {
  276. Mat3x4 r = m_ltrf.getRotation();
  277. r.rotateXAxis(angleRad);
  278. m_ltrf.setRotation(r);
  279. m_localTransformDirty = true;
  280. }
  281. void rotateLocalY(F32 angleRad)
  282. {
  283. Mat3x4 r = m_ltrf.getRotation();
  284. r.rotateYAxis(angleRad);
  285. m_ltrf.setRotation(r);
  286. m_localTransformDirty = true;
  287. }
  288. void rotateLocalZ(F32 angleRad)
  289. {
  290. Mat3x4 r = m_ltrf.getRotation();
  291. r.rotateZAxis(angleRad);
  292. m_ltrf.setRotation(r);
  293. m_localTransformDirty = true;
  294. }
  295. void moveLocalX(F32 distance)
  296. {
  297. Vec3 x_axis = m_ltrf.getRotation().getColumn(0);
  298. m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(x_axis, 0.0f) * distance);
  299. m_localTransformDirty = true;
  300. }
  301. void moveLocalY(F32 distance)
  302. {
  303. Vec3 y_axis = m_ltrf.getRotation().getColumn(1);
  304. m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(y_axis, 0.0) * distance);
  305. m_localTransformDirty = true;
  306. }
  307. void moveLocalZ(F32 distance)
  308. {
  309. Vec3 z_axis = m_ltrf.getRotation().getColumn(2);
  310. m_ltrf.setOrigin(m_ltrf.getOrigin() + Vec4(z_axis, 0.0) * distance);
  311. m_localTransformDirty = true;
  312. }
  313. void scale(F32 s)
  314. {
  315. m_ltrf.setScale(m_ltrf.getScale() * s);
  316. m_localTransformDirty = true;
  317. }
  318. void lookAtPoint(const Vec4& point)
  319. {
  320. m_ltrf = m_ltrf.lookAt(point, Vec4::yAxis());
  321. m_localTransformDirty = true;
  322. }
  323. /// @}
  324. Bool movedThisFrame() const
  325. {
  326. return m_transformUpdatedThisFrame;
  327. }
  328. ANKI_INTERNAL Bool updateTransform();
  329. /// Create and append a component to the components container. The SceneNode has the ownership.
  330. template<typename TComponent>
  331. TComponent* newComponent();
  332. template<typename TComponent>
  333. Bool hasComponent() const
  334. {
  335. return !!((1u << SceneComponentTypeMask(TComponent::kClassType)) & m_componentTypeMask);
  336. }
  337. private:
  338. SceneString m_name; ///< A unique name.
  339. U32 m_uuid;
  340. SceneComponentTypeMask m_componentTypeMask = SceneComponentTypeMask::kNone;
  341. GrDynamicArray<SceneComponent*> m_components;
  342. Timestamp m_maxComponentTimestamp = 0;
  343. /// The transformation in local space.
  344. Transform m_ltrf = Transform::getIdentity();
  345. /// The transformation in world space (local combined with parent's transformation).
  346. Transform m_wtrf = Transform::getIdentity();
  347. /// Keep the previous transformation for checking if it moved.
  348. Transform m_prevWTrf = Transform::getIdentity();
  349. // Flags
  350. Bool m_markedForDeletion : 1 = false;
  351. Bool m_localTransformDirty : 1 = true;
  352. Bool m_ignoreParentNodeTransform : 1 = false;
  353. Bool m_transformUpdatedThisFrame : 1 = true;
  354. void newComponentInternal(SceneComponent* newc);
  355. };
  356. /// @}
  357. } // end namespace anki