Node.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "Form.h"
  8. #include "AudioSource.h"
  9. #include "ParticleEmitter.h"
  10. #include "PhysicsRigidBody.h"
  11. #include "PhysicsCollisionObject.h"
  12. #include "PhysicsCollisionShape.h"
  13. #include "BoundingBox.h"
  14. namespace gameplay
  15. {
  16. class Package;
  17. class Scene;
  18. class Form;
  19. /**
  20. * Defines a basic hierachial structure of transformation spaces.
  21. */
  22. class Node : public Transform, public Ref
  23. {
  24. friend class Scene;
  25. friend class Package;
  26. friend class MeshSkin;
  27. public:
  28. /**
  29. * Defines the types of nodes.
  30. */
  31. enum Type
  32. {
  33. NODE = 1,
  34. JOINT = 2
  35. };
  36. /**
  37. * Creates a new node with the specified ID.
  38. *
  39. * @param id The ID for the new node.
  40. */
  41. static Node* create(const char* id = NULL);
  42. /**
  43. * Gets the identifier for the node.
  44. *
  45. * @return The node identifier.
  46. */
  47. const char* getId() const;
  48. /**
  49. * Sets the identifier for the node.
  50. *
  51. * @param id The identifier to set for the node.
  52. */
  53. void setId(const char* id);
  54. /**
  55. * Returns the type of the node.
  56. */
  57. virtual Node::Type getType() const;
  58. /**
  59. * Adds a child node.
  60. *
  61. * @param child The child to add.
  62. */
  63. virtual void addChild(Node* child);
  64. /**
  65. * Removes a child node.
  66. *
  67. * @param child The child to remove.
  68. */
  69. virtual void removeChild(Node* child);
  70. /**
  71. * Removes all child nodes.
  72. */
  73. virtual void removeAllChildren();
  74. /**
  75. * Returns the first child for this node.
  76. *
  77. * @return The first child.
  78. */
  79. Node* getFirstChild() const;
  80. /**
  81. * Returns the first sibling of this node.
  82. *
  83. * @return The first sibling.
  84. */
  85. Node* getNextSibling() const;
  86. /**
  87. * Returns the previous sibling to this node.
  88. *
  89. * @return The previous sibling.
  90. */
  91. Node* getPreviousSibling() const;
  92. /**
  93. * Returns the parent of this node.
  94. *
  95. * @return The parent.
  96. */
  97. Node* getParent() const;
  98. /**
  99. * Returns whether this node is visible (true by default).
  100. *
  101. * @return Whether the node is visible.
  102. */
  103. bool isVisible() const;
  104. /**
  105. * Sets whether this node is visible.
  106. *
  107. * @return Whether this node is visible.
  108. */
  109. void setVisible(bool visible);
  110. /**
  111. * Returns whether this node is transparent (false by default).
  112. *
  113. * All nodes are opaque by default, unless otherwise set as
  114. * transparent using the setTransparent method. These methods
  115. * can be used to flag nodes as transparent and then query the
  116. * property during game execution, for example to render all
  117. * opaque objects first, followed by transparent objects with
  118. * alpha blending enabled.
  119. *
  120. * @return Whether the node is transparent.
  121. */
  122. bool isTransparent() const;
  123. /**
  124. * Sets whether this node is transparent.
  125. *
  126. * @param transparent Whether the node is transparent.
  127. */
  128. void setTransparent(bool transparent);
  129. /**
  130. * Returns the user pointer for this node.
  131. *
  132. * @return The user pointer for this node.
  133. * @see setUserPointer(void*)
  134. */
  135. void* getUserPointer() const;
  136. /**
  137. * Sets the user pointer for this node.
  138. *
  139. * The user pointer is initially NULL and can be set to anything.
  140. * This is normally used to store game-specific data, such as
  141. * game state for a particular node. For example, attributes
  142. * for a game character, such as hit points, stamina, etc can
  143. * be defined in a game structure and stored in this field.
  144. *
  145. * When a node is deleted, the (optional) cleanup callback
  146. * function passed to this function is called to allow the
  147. * user to free any memory associated with the user pointer.
  148. *
  149. * @param pointer User pointer.
  150. * @param cleanupCallback Optional callback that is called when the
  151. * Node is being destroyed (or when the user pointer changes),
  152. * to allow the user to cleanup any memory associated with the
  153. * user pointer.
  154. */
  155. void setUserPointer(void* pointer, void (*cleanupCallback)(void*) = NULL);
  156. /**
  157. * Returns the number of direct children of this item.
  158. *
  159. * @return The number of children.
  160. */
  161. unsigned int getChildCount() const;
  162. /**
  163. * Returns the first child node that matches the given ID.
  164. *
  165. * This method checks the specified ID against its immediate child nodes
  166. * but does not check the ID against itself.
  167. * If recursive is true, it also traverses the Node's hierarchy with a breadth first search.
  168. *
  169. * @param id The ID of the child to find.
  170. * @param recursive True to search recursively all the node's children, false for only direct children.
  171. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  172. * or false if nodes that start with the given ID are returned.
  173. *
  174. * @return The Node found or NULL if not found.
  175. */
  176. Node* findNode(const char* id, bool recursive = true, bool exactMatch = true) const;
  177. /**
  178. * Returns all child nodes that match the given ID.
  179. *
  180. * @param id The ID of the node to find.
  181. * @param nodes A vector of nodes to be populated with matches.
  182. * @param recursive true if a recursive search should be performed, false otherwise.
  183. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  184. * or false if nodes that start with the given ID are returned.
  185. *
  186. * @return The number of matches found.
  187. */
  188. unsigned int findNodes(const char* id, std::vector<Node*>& nodes, bool recursive = true, bool exactMatch = true) const;
  189. /**
  190. * Gets the scene.
  191. *
  192. * @return The scene.
  193. */
  194. Scene* getScene() const;
  195. /**
  196. * Gets the top level node in this node's parent hierarchy.
  197. */
  198. Node* getRootNode() const;
  199. /**
  200. * Gets the world matrix corresponding to this node.
  201. *
  202. * @return The world matrix of this node.
  203. */
  204. virtual const Matrix& getWorldMatrix() const;
  205. /**
  206. * Gets the world view matrix corresponding to this node.
  207. *
  208. * @return The world view matrix of this node.
  209. */
  210. const Matrix& getWorldViewMatrix() const;
  211. /**
  212. * Gets the inverse transpose world matrix corresponding to this node.
  213. *
  214. * This matrix is typically used to transform normal vectors into world space.
  215. *
  216. * @return The inverse world matrix of this node.
  217. */
  218. const Matrix& getInverseTransposeWorldMatrix() const;
  219. /**
  220. * Gets the inverse transpose world view matrix corresponding to this node.
  221. *
  222. * This matrix is typically used to transform normal vectors into view space.
  223. *
  224. * @return The inverse world view matrix of this node.
  225. */
  226. const Matrix& getInverseTransposeWorldViewMatrix() const;
  227. /**
  228. * Gets the view matrix corresponding to this node based
  229. * on the scene's active camera.
  230. *
  231. * @return The view matrix of this node.
  232. */
  233. const Matrix& getViewMatrix() const;
  234. /**
  235. * Gets the inverse view matrix corresponding to this node based
  236. * on the scene's active camera.
  237. *
  238. * @return The inverse view matrix of this node.
  239. */
  240. const Matrix& getInverseViewMatrix() const;
  241. /**
  242. * Gets the projection matrix corresponding to this node based
  243. * on the scene's active camera.
  244. *
  245. * @return The projection matrix of this node.
  246. */
  247. const Matrix& getProjectionMatrix() const;
  248. /**
  249. * Gets the view * projection matrix corresponding to this node based
  250. * on the scene's active camera.
  251. *
  252. * @return The view * projection matrix of this node.
  253. */
  254. const Matrix& getViewProjectionMatrix() const;
  255. /**
  256. * Gets the inverse view * projection matrix corresponding to this node based
  257. * on the scene's active camera.
  258. *
  259. * @return The inverse view * projection matrix of this node.
  260. */
  261. const Matrix& getInverseViewProjectionMatrix() const;
  262. /**
  263. * Gets the world * view * projection matrix corresponding to this node based
  264. * on the scene's active camera.
  265. *
  266. * @return The world * view * projection matrix of this node.
  267. */
  268. const Matrix& getWorldViewProjectionMatrix() const;
  269. /**
  270. * Gets the translation vector (or position) of this Node in world space.
  271. *
  272. * @return The world translation vector.
  273. */
  274. Vector3 getTranslationWorld() const;
  275. /**
  276. * Gets the translation vector (or position) of this Node in view space.
  277. *
  278. * @return The view space translation vector.
  279. */
  280. Vector3 getTranslationView() const;
  281. /**
  282. * Returns the forward vector of the Node in world space.
  283. */
  284. Vector3 getForwardVectorWorld() const;
  285. /**
  286. * Returns the forward vector of the Node in view space.
  287. */
  288. Vector3 getForwardVectorView() const;
  289. /**
  290. * Returns the translation vector of the currently active camera for this node's scene.
  291. *
  292. * @return The translation vector of the scene's active camera.
  293. */
  294. Vector3 getActiveCameraTranslationWorld() const;
  295. /**
  296. * Returns the view-space translation vector of the currently active camera for this node's scene.
  297. *
  298. * @return The translation vector of the scene's active camera, in view-space.
  299. */
  300. Vector3 getActiveCameraTranslationView() const;
  301. /**
  302. * Gets the first animation in the node hierarchy with the specified ID.
  303. *
  304. * @param id The ID of the animation to get. Returns the first animation if ID is NULL.
  305. * @return The first animation with the specified ID.
  306. */
  307. Animation* getAnimation(const char* id = NULL) const;
  308. /**
  309. * Returns the pointer to this node's camera.
  310. *
  311. * @return The pointer to this node's camera or NULL.
  312. */
  313. Camera* getCamera() const;
  314. /**
  315. * Assigns a camera to this node.
  316. *
  317. * This will increase the reference count of the new camera and decrease
  318. * the reference count of the old camera.
  319. *
  320. * @param camera A pointer to a camera. May be NULL.
  321. */
  322. void setCamera(Camera* camera);
  323. /**
  324. * Returns the pointer to this node's light.
  325. *
  326. * @return The pointer to this node's light or NULL.
  327. */
  328. Light* getLight() const;
  329. /**
  330. * Assigns a light to this node.
  331. *
  332. * This will increase the reference count of the new light and decrease
  333. * the reference count of the old light.
  334. *
  335. * @param light The new light. May be NULL.
  336. */
  337. void setLight(Light* light);
  338. /**
  339. * Returns the pointer to this node's model.
  340. *
  341. * @return The pointer to this node's model or NULL.
  342. */
  343. Model* getModel() const;
  344. /**
  345. * Assigns a model to this node.
  346. *
  347. * This will increase the reference count of the new model and decrease
  348. * the reference count of the old model.
  349. *
  350. * @param model The new model. May be NULL.
  351. */
  352. void setModel(Model* model);
  353. Form* getForm() const;
  354. void setForm(Form* form);
  355. /**
  356. * Returns the pointer to this node's audio source.
  357. *
  358. * @return The pointer to this node's audio source or NULL.
  359. */
  360. AudioSource* getAudioSource() const;
  361. /**
  362. * Assigns an audio source to this node.
  363. *
  364. * This will increase the reference count of the new audio source and decrease
  365. * the reference count of the old audio source.
  366. *
  367. * @param audio The new audio source. May be NULL.
  368. */
  369. void setAudioSource(AudioSource* audio);
  370. /**
  371. * Returns the pointer to this node's particle emitter.
  372. *
  373. * @return The pointer to this node's particle emitter or NULL.
  374. */
  375. ParticleEmitter* getParticleEmitter() const;
  376. /**
  377. * Assigns a particle emitter to this node.
  378. *
  379. * This will increase the reference count of the new particle emitter and decrease
  380. * the reference count of the old particle emitter.
  381. *
  382. * @param emitter The new particle emitter. May be NULL.
  383. */
  384. void setParticleEmitter(ParticleEmitter* emitter);
  385. /**
  386. * Returns the pointer to this node's physics collision object.
  387. *
  388. * The type of the returned collision object can be queried using
  389. * the PhysicsCollisionObject::getType() method.
  390. *
  391. * @return The pointer to this node's physics collision object.
  392. */
  393. PhysicsCollisionObject* getCollisionObject() const;
  394. /**
  395. * Sets (or disables) the physics collision object for this node.
  396. *
  397. * The supported collision object types include rigid bodies, ghost objects and
  398. * characters.
  399. *
  400. * Rigid bodies are used to represent most physical objects in a game. The important
  401. * feature of rigid bodies is that they can be simulated by the physics system as other
  402. * rigid bodies or collision objects collide with them. To support this physics simulation,
  403. * rigid bodies require additional parameters, such as mass, friction and restitution to
  404. * define their physical features. These parameters can be passed into the
  405. * 'rigidBodyParameters' parameter.
  406. *
  407. * Ghost objects are a simple type of collision object that are not simulated. By default
  408. * they pass through other objects in the scene without affecting them. Ghost objects do
  409. * receive collision events however, which makes them useful for representing non-simulated
  410. * entities in a game that still require collision events, such as volumetric triggers,
  411. * power-ups, etc.
  412. *
  413. * Characters are an extention of ghost objects which provide a number of additional features
  414. * for animating and moving characters within a game. Characters are represented as ghost
  415. * objects instead of rigid bodies to allow more direct control over character movement,
  416. * since attempting to model a physics character with a simulated rigid body usually results
  417. * in unresponse and unpredictable character movement. Unlike normal ghost objects,
  418. * characters to react to other characters and rigid bodies in the world. Characters react
  419. * to gravity and collide (and respond) with rigid bodies to allow them to walk on the ground,
  420. * slide along walls and walk up/down slopes and stairs.
  421. *
  422. * @param type The type of the collision object to set; to disable the physics
  423. * collision object, pass PhysicsCollisionObject::NONE.
  424. * @param shape Definition of a physics collision shape to be used for this collision object.
  425. * Use the static shape methods on the PhysicsCollisionShape class to specificy a shape
  426. * definition, such as PhysicsCollisionShape::box().
  427. * @param rigidBodyParameters If type is PhysicsCollisionObject::RIGID_BODY, this
  428. * must point to a valid rigid body parameters object containing information
  429. * about the rigid body; otherwise, this parmater may be NULL.
  430. */
  431. PhysicsCollisionObject* setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters = NULL);
  432. /**
  433. * Sets the physics collision object for this node using the definition in the given file.
  434. *
  435. * @param filePath The path to the file that set the collision object definition.
  436. */
  437. PhysicsCollisionObject* setCollisionObject(const char* filePath);
  438. /**
  439. * Sets the physics collision object for this node from the given properties object.
  440. *
  441. * @param properties The properties object defining the collision ojbect.
  442. */
  443. PhysicsCollisionObject* setCollisionObject(Properties* properties);
  444. /**
  445. * Returns the bounding sphere for the Node, in world space.
  446. *
  447. * The bounding sphere for a node represents the area, in world
  448. * space, that the node contains. This includes the space occupied
  449. * by any child nodes as well as the space occupied by any data
  450. * inside the node (such as models).
  451. *
  452. * Bounding spheres for nodes are rough approximations of the data
  453. * contained within a node and they are intended for visibility
  454. * testing or first-pass intersection testing only. They are not
  455. * appropriate for accurate collision detection since they most often
  456. * do not tightly contain a node's content.
  457. *
  458. * A node that does not occupy any space will return a bounding sphere
  459. * with a center point equal to the node translation and a radius of zero.
  460. *
  461. * @return The world-space bounding sphere for the node.
  462. */
  463. const BoundingSphere& getBoundingSphere() const;
  464. /**
  465. * Clones the node and all of its child nodes.
  466. *
  467. * @return A new node.
  468. */
  469. Node* clone() const;
  470. protected:
  471. /**
  472. * Constructor.
  473. */
  474. Node(const char* id);
  475. /**
  476. * Destructor.
  477. */
  478. virtual ~Node();
  479. /**
  480. * Clones a single node and its data but not its children.
  481. *
  482. * @param context The clone context.
  483. *
  484. * @return Pointer to the newly created node.
  485. */
  486. virtual Node* cloneSingleNode(NodeCloneContext &context) const;
  487. /**
  488. * Recursively clones this node and its children.
  489. *
  490. * @param context The clone context.
  491. *
  492. * @return The newly created node.
  493. */
  494. Node* cloneRecursive(NodeCloneContext &context) const;
  495. /**
  496. * Copies the data from this node into the given node.
  497. *
  498. * @param node The node to copy the data to.
  499. * @param context The clone context.
  500. */
  501. void cloneInto(Node* node, NodeCloneContext &context) const;
  502. /**
  503. * Removes this node from its parent.
  504. */
  505. void remove();
  506. /**
  507. * Called when this Node's transform changes.
  508. */
  509. void transformChanged();
  510. /**
  511. * Called when this Node's hierarchy changes.
  512. */
  513. void hierarchyChanged();
  514. /**
  515. * Marks the bounding volume of the node as dirty.
  516. */
  517. void setBoundsDirty();
  518. private:
  519. /**
  520. * Hidden copy constructor.
  521. */
  522. Node(const Node& copy);
  523. /**
  524. * Hidden copy assignment operator.
  525. */
  526. Node& operator=(const Node&);
  527. protected:
  528. struct UserData
  529. {
  530. UserData() : pointer(NULL), cleanupCallback(NULL) {}
  531. void* pointer;
  532. void (*cleanupCallback)(void*);
  533. };
  534. Scene* _scene;
  535. std::string _id;
  536. Node* _firstChild;
  537. Node* _nextSibling;
  538. Node* _prevSibling;
  539. Node* _parent;
  540. unsigned int _childCount;
  541. unsigned int _nodeFlags;
  542. Camera* _camera;
  543. Light* _light;
  544. Model* _model;
  545. Form* _form;
  546. AudioSource* _audioSource;
  547. ParticleEmitter* _particleEmitter;
  548. PhysicsCollisionObject* _collisionObject;
  549. mutable Matrix _world;
  550. mutable int _dirtyBits;
  551. bool _notifyHierarchyChanged;
  552. mutable BoundingSphere _bounds;
  553. UserData* _userData;
  554. };
  555. /**
  556. * NodeCloneContext represents the context data that is kept when cloning a node.
  557. *
  558. * The NodeCloneContext is used to make sure objects don't get cloned twice.
  559. */
  560. class NodeCloneContext
  561. {
  562. public:
  563. /**
  564. * Constructor.
  565. */
  566. NodeCloneContext();
  567. /**
  568. * Destructor.
  569. */
  570. ~NodeCloneContext();
  571. /**
  572. * Finds the cloned animation of the given animation or NULL if this animation was not registered with this context.
  573. *
  574. * @param animation The animation to search for the cloned copy of.
  575. *
  576. * @return The cloned animation or NULL if not found.
  577. */
  578. Animation* findClonedAnimation(const Animation* animation);
  579. /**
  580. * Registers the cloned animation with this context so that it doesn't get cloned twice.
  581. *
  582. * @param original The pointer to the original animation.
  583. * @param clone The pointer to the cloned animation.
  584. */
  585. void registerClonedAnimation(const Animation* original, Animation* clone);
  586. /**
  587. * Finds the cloned node of the given node or NULL if this node was not registered with this context.
  588. *
  589. * @param node The node to search for the cloned copy of.
  590. *
  591. * @return The cloned node or NULL if not found.
  592. */
  593. Node* findClonedNode(const Node* node);
  594. /**
  595. * Registers the cloned node with this context so that it doens't get cloned twice.
  596. *
  597. * @param original The pointer to the original node.
  598. * @param clone The pointer to the cloned node.
  599. */
  600. void registerClonedNode(const Node* original, Node* clone);
  601. private:
  602. /**
  603. * Hidden copy constructor.
  604. */
  605. NodeCloneContext(const NodeCloneContext&);
  606. /**
  607. * Hidden copy assignment operator.
  608. */
  609. NodeCloneContext& operator=(const NodeCloneContext&);
  610. private:
  611. typedef std::map<const Animation*, Animation*> AnimationMap;
  612. typedef std::map<const Node*, Node*> NodeMap;
  613. AnimationMap _clonedAnimations;
  614. NodeMap _clonedNodes;
  615. };
  616. }
  617. #endif