Node.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Node.h
  3. */
  4. #ifndef NODE_H_
  5. #define NODE_H_
  6. #include "Transform.h"
  7. #include "Camera.h"
  8. #include "Light.h"
  9. #include "Model.h"
  10. #include "AudioSource.h"
  11. #include "ParticleEmitter.h"
  12. #include "PhysicsRigidBody.h"
  13. #include "BoundingBox.h"
  14. namespace gameplay
  15. {
  16. class Package;
  17. class Scene;
  18. /**
  19. * Defines a basic hierachial structure of transformation spaces.
  20. */
  21. class Node : public Transform
  22. {
  23. friend class Scene;
  24. friend class Package;
  25. public:
  26. /**
  27. * Defines the types of nodes.
  28. */
  29. enum Type
  30. {
  31. NODE = 1,
  32. JOINT = 2
  33. };
  34. /**
  35. * Defines types of bounding volumes for nodes.
  36. */
  37. enum BoundsType
  38. {
  39. NONE,
  40. BOX,
  41. SPHERE
  42. };
  43. /**
  44. * Creates a new node with the specified ID.
  45. *
  46. * @param id The ID for the new node.
  47. */
  48. static Node* create(const char* id = NULL);
  49. /**
  50. * Gets the identifier for the node.
  51. *
  52. * @return The node identifier.
  53. */
  54. const char* getId() const;
  55. /**
  56. * Sets the identifier for the node.
  57. *
  58. * @param id The identifier to set for the node.
  59. */
  60. void setId(const char* id);
  61. /**
  62. * Returns the type of the node.
  63. */
  64. virtual Node::Type getType() const;
  65. /**
  66. * Adds a child node.
  67. *
  68. * @param child The child to add.
  69. */
  70. virtual void addChild(Node* child);
  71. /**
  72. * Removes a child node.
  73. *
  74. * @param child The child to remove.
  75. */
  76. virtual void removeChild(Node* child);
  77. /**
  78. * Removes all child nodes.
  79. */
  80. virtual void removeAllChildren();
  81. /**
  82. * Returns the first child for this node.
  83. *
  84. * @return The first child.
  85. */
  86. Node* getFirstChild() const;
  87. /**
  88. * Returns the first sibling of this node.
  89. *
  90. * @return The first sibling.
  91. */
  92. Node* getNextSibling() const;
  93. /**
  94. * Returns the previous sibling to this node.
  95. *
  96. * @return The previous sibling.
  97. */
  98. Node* getPreviousSibling() const;
  99. /**
  100. * Returns the parent of this node.
  101. *
  102. * @return The parent.
  103. */
  104. Node* getParent() const;
  105. /**
  106. * Returns the number of direct children of this item.
  107. *
  108. * @return The number of children.
  109. */
  110. unsigned int getChildCount() const;
  111. /**
  112. * Returns the first child node that matches the given ID.
  113. *
  114. * This method checks the specified ID against its own ID, as well as its
  115. * immediate children nodes. If recursive is true, it also traverses the
  116. * Node's hierarchy.
  117. *
  118. * @param id The ID of the child to find.
  119. * @param recursive true to search recursively all the node's children, false for only direct children.
  120. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  121. * or false if nodes that start with the given ID are returned.
  122. *
  123. * @return The Node found or NULL if not found.
  124. */
  125. Node* findNode(const char* id, bool recursive = true, bool exactMatch = true);
  126. /**
  127. * Returns all child nodes that match the given ID.
  128. *
  129. * @param id The ID of the node to find.
  130. * @param nodes A vector of nodes to be populated with matches.
  131. * @param recursive true if a recursive search should be performed, false otherwise.
  132. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  133. * or false if nodes that start with the given ID are returned.
  134. *
  135. * @return The number of matches found.
  136. */
  137. unsigned int findNodes(const char* id, std::vector<Node*>& nodes, bool recursive = true, bool exactMatch = true);
  138. /**
  139. * Gets the scene.
  140. *
  141. * @return The scene.
  142. */
  143. Scene* getScene() const;
  144. /**
  145. * Gets the top level node in this node's parent hierarchy.
  146. */
  147. Node* getRootNode() const;
  148. /**
  149. * Gets the world matrix corresponding to this node.
  150. *
  151. * @return The world matrix of this node.
  152. */
  153. virtual const Matrix& getWorldMatrix() const;
  154. /**
  155. * Gets the world view matrix corresponding to this node.
  156. *
  157. * @return The world view matrix of this node.
  158. */
  159. const Matrix& getWorldViewMatrix() const;
  160. /**
  161. * Gets the inverse transpose world view matrix corresponding to this node.
  162. *
  163. * This matrix is typically used to transform normal vectors.
  164. *
  165. * @return The inverse world view matrix of this node.
  166. */
  167. const Matrix& getInverseTransposeWorldViewMatrix() const;
  168. /**
  169. * Gets the view matrix corresponding to this node based
  170. * on the scene's active camera.
  171. *
  172. * @return The view matrix of this node.
  173. */
  174. const Matrix& getViewMatrix() const;
  175. /**
  176. * Gets the inverse view matrix corresponding to this node based
  177. * on the scene's active camera.
  178. *
  179. * @return The inverse view matrix of this node.
  180. */
  181. const Matrix& getInverseViewMatrix() const;
  182. /**
  183. * Gets the projection matrix corresponding to this node based
  184. * on the scene's active camera.
  185. *
  186. * @return The projection matrix of this node.
  187. */
  188. const Matrix& getProjectionMatrix() const;
  189. /**
  190. * Gets the view * projection matrix corresponding to this node based
  191. * on the scene's active camera.
  192. *
  193. * @return The view * projection matrix of this node.
  194. */
  195. const Matrix& getViewProjectionMatrix() const;
  196. /**
  197. * Gets the inverse view * projection matrix corresponding to this node based
  198. * on the scene's active camera.
  199. *
  200. * @return The inverse view * projection matrix of this node.
  201. */
  202. const Matrix& getInverseViewProjectionMatrix() const;
  203. /**
  204. * Gets the world * view * projection matrix corresponding to this node based
  205. * on the scene's active camera.
  206. *
  207. * @return The world * view * projection matrix of this node.
  208. */
  209. const Matrix& getWorldViewProjectionMatrix() const;
  210. /**
  211. * Gets the translation vector (or position) of this Node in world space.
  212. *
  213. * @return The world translation vector.
  214. */
  215. Vector3 getTranslationWorld() const;
  216. /**
  217. * Gets the translation vector (or position) of this Node in view space.
  218. *
  219. * @return The view space translation vector.
  220. */
  221. Vector3 getTranslationView() const;
  222. /**
  223. * Returns the forward vector of the Node in world space.
  224. */
  225. Vector3 getForwardVectorWorld() const;
  226. /**
  227. * Returns the forward vector of the Node in view space.
  228. */
  229. Vector3 getForwardVectorView() const;
  230. /**
  231. * Returns the translation vector of the currently active camera for this node's scene.
  232. *
  233. * @return The translation vector of the scene's active camera.
  234. */
  235. Vector3 getActiveCameraTranslationWorld() const;
  236. /**
  237. * Returns the pointer to this node's camera.
  238. *
  239. * @return The pointer to this node's camera or NULL.
  240. */
  241. Camera* getCamera() const;
  242. /**
  243. * Assigns a camera to this node.
  244. *
  245. * This will increase the reference count of the new camera and decrease
  246. * the reference count of the old camera.
  247. *
  248. * @param camera A pointer to a camera. May be NULL.
  249. */
  250. void setCamera(Camera* camera);
  251. /**
  252. * Returns the pointer to this node's light.
  253. *
  254. * @return The pointer to this node's light or NULL.
  255. */
  256. Light* getLight() const;
  257. /**
  258. * Assigns a light to this node.
  259. *
  260. * This will increase the reference count of the new light and decrease
  261. * the reference count of the old light.
  262. *
  263. * @param light The new light. May be NULL.
  264. */
  265. void setLight(Light* light);
  266. /**
  267. * Returns the pointer to this node's model.
  268. *
  269. * @return The pointer to this node's model or NULL.
  270. */
  271. Model* getModel() const;
  272. /**
  273. * Assigns a model to this node.
  274. *
  275. * This will increase the reference count of the new model and decrease
  276. * the reference count of the old model.
  277. *
  278. * @param model The new model. May be NULL.
  279. */
  280. void setModel(Model* model);
  281. /**
  282. * Returns the pointer to this node's audio source.
  283. *
  284. * @return The pointer to this node's audio source or NULL.
  285. */
  286. AudioSource* getAudioSource() const;
  287. /**
  288. * Assigns an audio source to this node.
  289. *
  290. * This will increase the reference count of the new audio source and decrease
  291. * the reference count of the old audio source.
  292. *
  293. * @param audio The new audio source. May be NULL.
  294. */
  295. void setAudioSource(AudioSource* audio);
  296. /**
  297. * Returns the pointer to this node's particle emitter.
  298. *
  299. * @return The pointer to this node's particle emitter or NULL.
  300. */
  301. ParticleEmitter* getParticleEmitter() const;
  302. /**
  303. * Assigns a particle emitter to this node.
  304. *
  305. * This will increase the reference count of the new particle emitter and decrease
  306. * the reference count of the old particle emitter.
  307. *
  308. * @param emitter The new particle emitter. May be NULL.
  309. */
  310. void setParticleEmitter(ParticleEmitter* emitter);
  311. /**
  312. * Returns the pointer to this node's physics rigid body or NULL.
  313. *
  314. * @return The pointer to this node's physics rigid body or NULL.
  315. */
  316. PhysicsRigidBody* getPhysicsRigidBody() const;
  317. /**
  318. * Sets (or disables) the physics rigid body for this node.
  319. *
  320. * Note: This is only allowed for nodes that have a model attached to them.
  321. *
  322. * @param type The type of rigid body to set; to disable the physics rigid
  323. * body, pass PhysicsRigidBody#SHAPE_NONE.
  324. * @param mass The mass of the rigid body, in kilograms.
  325. * @param friction The friction of the rigid body (between 0.0 and 1.0, where 0.0 is
  326. * minimal friction and 1.0 is maximal friction).
  327. * @param restitution The restitution of the rigid body (this controls the bounciness of
  328. * the rigid body; between 0.0 and 1.0, where 0.0 is minimal bounciness and 1.0 is maximal bounciness).
  329. * @param linearDamping The percentage of linear velocity lost per second (between 0.0 and 1.0).
  330. * @param angularDamping The percentage of angular velocity lost per second (between 0.0 and 1.0).
  331. */
  332. void setPhysicsRigidBody(PhysicsRigidBody::Type type, float mass = 0.0f, float friction = 0.5f,
  333. float restitution = 0.0f, float linearDamping = 0.0f, float angularDamping = 0.0f);
  334. /**
  335. * Returns the bounding box for the Node, in world space.
  336. *
  337. * The returned box is only meaningful for nodes who have a
  338. * bounds type of BOUNDS_TYPE_BOX, which can be specified
  339. * via the setBoundsType method. Additionally, bounding volumes
  340. * are only meaningful for nodes that contain data which itself
  341. * contains a bounding volume, such as Model/Mesh.
  342. *
  343. * @return The world-space bounding box for the node.
  344. */
  345. const BoundingBox& getBoundingBox() const;
  346. /**
  347. * Returns the bounding sphere for the Node, in world space.
  348. *
  349. * The returned sphere is only meaningful for nodes who have a
  350. * bounds type of BOUNDS_TYPE_SPHERE, which can be specified
  351. * via the setBoundsType method. Additionally, bounding volumes
  352. * are only meaningful for nodes that contain data which itself
  353. * contains a bounding volume, such as Model/Mesh.
  354. *
  355. * @return The world-space bounding sphere for the node.
  356. */
  357. const BoundingSphere& getBoundingSphere() const;
  358. /**
  359. * Returns the current bounding volume type of the node.
  360. */
  361. Node::BoundsType getBoundsType() const;
  362. /**
  363. * Sets the bounding volume type of the node.
  364. */
  365. void setBoundsType(Node::BoundsType type);
  366. protected:
  367. /**
  368. * Constructor.
  369. */
  370. Node(const char* id);
  371. /**
  372. * Copy constructor.
  373. */
  374. Node(const Node& copy);
  375. /**
  376. * Destructor.
  377. */
  378. virtual ~Node();
  379. /**
  380. * Removes this node from its parent.
  381. */
  382. void remove();
  383. void transformChanged();
  384. void hierarchyChanged();
  385. Scene* _scene;
  386. std::string _id;
  387. Node* _firstChild;
  388. Node* _nextSibling;
  389. Node* _prevSibling;
  390. Node* _parent;
  391. unsigned int _childCount;
  392. Camera* _camera;
  393. Light* _light;
  394. Model* _model;
  395. AudioSource* _audioSource;
  396. ParticleEmitter* _particleEmitter;
  397. PhysicsRigidBody* _physicsRigidBody;
  398. mutable Matrix _world;
  399. mutable int _dirtyBits;
  400. bool _notifyHierarchyChanged;
  401. mutable union
  402. {
  403. BoundingBox* box;
  404. BoundingSphere* sphere;
  405. } _bounds;
  406. BoundsType _boundsType;
  407. };
  408. }
  409. #endif