SceneNode.h 9.9 KB

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