Node.h 13 KB

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