Scene.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #ifndef SCENE_H_
  2. #define SCENE_H_
  3. #include "Node.h"
  4. #include "MeshBatch.h"
  5. #include "ScriptController.h"
  6. #include "Light.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Represents the root container for a hierarchy of nodes.
  11. */
  12. class Scene : public Ref
  13. {
  14. public:
  15. /**
  16. * Creates a new empty scene.
  17. *
  18. * @param id ID of the new scene, or NULL to use an empty string for the ID (default).
  19. *
  20. * @return The newly created empty scene.
  21. * @script{create}
  22. */
  23. static Scene* create(const char* id = NULL);
  24. /**
  25. * Loads a scene from the given '.scene' or '.gpb' file.
  26. *
  27. * @param filePath The path to the '.scene' or '.gpb' file to load from.
  28. * @return The loaded scene or <code>NULL</code> if the scene
  29. * could not be loaded from the given file.
  30. * @script{create}
  31. */
  32. static Scene* load(const char* filePath);
  33. /**
  34. * Gets a currently active scene.
  35. *
  36. * If id is an NULL, the first active scene is returned.
  37. *
  38. * @param id ID of the scene to retrieve, or NULL to retrieve the first active scene.
  39. *
  40. * @return The scene that matches the specified ID, or NULL if no matching scene could be found.
  41. */
  42. static Scene* getScene(const char* id = NULL);
  43. /**
  44. * Gets the identifier for the scene.
  45. *
  46. * @return The scene identifier.
  47. */
  48. const char* getId() const;
  49. /**
  50. * Sets the identifier for the scene.
  51. *
  52. * @param id The identifier to set for the scene.
  53. */
  54. void setId(const char* id);
  55. /**
  56. * Returns the first node in the scene that matches the given ID.
  57. *
  58. * @param id The ID of the node to find.
  59. * @param recursive true if a recursive search should be performed, false otherwise.
  60. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  61. * or false if nodes that start with the given ID are returned.
  62. *
  63. * @return The first node found that matches the given ID.
  64. */
  65. Node* findNode(const char* id, bool recursive = true, bool exactMatch = true) const;
  66. /**
  67. * Returns all nodes in the scene that match the given ID.
  68. *
  69. * @param id The ID of the node to find.
  70. * @param nodes Vector of nodes to be populated with matches.
  71. * @param recursive true if a recursive search should be performed, false otherwise.
  72. * @param exactMatch true if only nodes who's ID exactly matches the specified ID are returned,
  73. * or false if nodes that start with the given ID are returned.
  74. *
  75. * @return The number of matches found.
  76. * @script{ignore}
  77. */
  78. unsigned int findNodes(const char* id, std::vector<Node*>& nodes, bool recursive = true, bool exactMatch = true) const;
  79. /**
  80. * Creates and adds a new node to the scene.
  81. *
  82. * @param id An optional node ID.
  83. *
  84. * @return The new node.
  85. */
  86. Node* addNode(const char* id = NULL);
  87. /**
  88. * Adds the specified node to the scene.
  89. *
  90. * @param node The node to be added to the scene.
  91. */
  92. void addNode(Node* node);
  93. /**
  94. * Removes the specified node from the scene.
  95. *
  96. * @param node The node to remove.
  97. */
  98. void removeNode(Node* node);
  99. /**
  100. * Removes all nodes from the scene.
  101. */
  102. void removeAllNodes();
  103. /**
  104. * Returns the number of nodes at the root level of the scene.
  105. *
  106. * @return The node count.
  107. */
  108. unsigned int getNodeCount() const;
  109. /**
  110. * Returns the first node in the scene.
  111. *
  112. * @return The first node in the scene.
  113. */
  114. Node* getFirstNode() const;
  115. /**
  116. * Gets the active camera for the scene.
  117. *
  118. * @return The active camera for the scene.
  119. * @see VisibleSet#getActiveCamera
  120. */
  121. Camera* getActiveCamera();
  122. /**
  123. * Sets the active camera on the scene.
  124. *
  125. * @param camera The active camera to be set on the scene.
  126. */
  127. void setActiveCamera(Camera* camera);
  128. /**
  129. * Sets the audio listener to transform along with the active camera if set to true.
  130. * If you have a 2D game that doesn't require it, then set to false. This is on by default for the scene.
  131. *
  132. * @param bind true if you want to the audio listener to follow the active camera's transform.
  133. */
  134. void bindAudioListenerToCamera(bool bind);
  135. /**
  136. * Returns the ambient color of the scene.
  137. *
  138. * The default ambient light color is black (0,0,0).
  139. *
  140. * This value can be bound to materials using the SCENE_LIGHT_AMBIENT_COLOR auto binding.
  141. *
  142. * @return The scene's ambient color.
  143. */
  144. const Vector3& getAmbientColor() const;
  145. /**
  146. * Sets the ambient color of the scene.
  147. *
  148. * @param red The red channel between 0.0 and 1.0.
  149. * @param green The green channel between 0.0 and 1.0.
  150. * @param blue The blue channel between 0.0 and 1.0.
  151. *
  152. * @see getAmbientColor()
  153. */
  154. void setAmbientColor(float red, float green, float blue);
  155. /**
  156. * Visits each node in the scene and calls the specified method pointer.
  157. *
  158. * Calling this method invokes the specified method pointer for each node
  159. * in the scene hierarchy.
  160. *
  161. * The visitMethod parameter must be a pointer to a method that has a bool
  162. * return type and accepts a single parameter of type Node*.
  163. *
  164. * A depth-first traversal of the scene continues while the visit method
  165. * returns true. Returning false will stop traversing further children for
  166. * the given node and the traversal will continue at the next sibling.
  167. *
  168. * @param instance The pointer to an instance of the object that contains visitMethod.
  169. * @param visitMethod The pointer to the class method to call for each node in the scene.
  170. */
  171. template <class T>
  172. void visit(T* instance, bool (T::*visitMethod)(Node*));
  173. /**
  174. * Visits each node in the scene and calls the specified method pointer.
  175. *
  176. * Calling this method invokes the specified method pointer for each node
  177. * in the scene hierarchy, passing the Node and the specified cookie value.
  178. *
  179. * The visitMethod parameter must be a pointer to a method that has a bool
  180. * return type and accepts two parameters: a Node pointer and a cookie of a
  181. * user-specified type.
  182. *
  183. * A depth-first traversal of the scene continues while the visit method
  184. * returns true. Returning false will stop traversing further children for
  185. * the given node and the traversal will continue at the next sibling.
  186. *
  187. * @param instance The pointer to an instance of the object that contains visitMethod.
  188. * @param visitMethod The pointer to the class method to call for each node in the scene.
  189. * @param cookie An optional user-defined parameter that will be passed to each invocation of visitMethod.
  190. */
  191. template <class T, class C>
  192. void visit(T* instance, bool (T::*visitMethod)(Node*,C), C cookie);
  193. /**
  194. * Visits each node in the scene and calls the specified Lua function.
  195. *
  196. * Calling this method invokes the specified Lua function for each node
  197. * in the scene hierarchy.
  198. *
  199. * The visitMethod parameter must be a string containing the name of a
  200. * valid Lua function that has a boolean return type and accepts a
  201. * single parameter of type Node*.
  202. *
  203. * A depth-first traversal of the scene continues while the visit method
  204. * returns true. Returning false will stop traversing further children for
  205. * the given node and the traversal will continue at the next sibling.
  206. *
  207. * @param visitMethod The name of the Lua function to call for each node in the scene.
  208. */
  209. inline void visit(const char* visitMethod);
  210. /**
  211. * Updates all the active nodes in the scene.
  212. */
  213. void update(float elapsedTime);
  214. /**
  215. * @see VisibleSet#getNext
  216. */
  217. Node* getNext();
  218. /**
  219. * @see VisibleSet#reset
  220. */
  221. void reset();
  222. private:
  223. /**
  224. * Constructor.
  225. */
  226. Scene();
  227. /**
  228. * Hidden copy constructor.
  229. */
  230. Scene(const Scene& copy);
  231. /**
  232. * Destructor.
  233. */
  234. virtual ~Scene();
  235. /**
  236. * Hidden copy assignment operator.
  237. */
  238. Scene& operator=(const Scene&);
  239. /**
  240. * Visits the given node and all of its children recursively.
  241. */
  242. template <class T>
  243. void visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*));
  244. /**
  245. * Visits the given node and all of its children recursively.
  246. */
  247. template <class T, class C>
  248. void visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*,C), C cookie);
  249. /**
  250. * Visits the given node and all of its children recursively.
  251. */
  252. void visitNode(Node* node, const char* visitMethod);
  253. Node* findNextVisibleSibling(Node* node);
  254. bool isNodeVisible(Node* node);
  255. std::string _id;
  256. Camera* _activeCamera;
  257. Node* _firstNode;
  258. Node* _lastNode;
  259. unsigned int _nodeCount;
  260. Vector3 _ambientColor;
  261. bool _bindAudioListenerToCamera;
  262. Node* _nextItr;
  263. bool _nextReset;
  264. };
  265. template <class T>
  266. void Scene::visit(T* instance, bool (T::*visitMethod)(Node*))
  267. {
  268. for (Node* node = getFirstNode(); node != NULL; node = node->getNextSibling())
  269. {
  270. visitNode(node, instance, visitMethod);
  271. }
  272. }
  273. template <class T, class C>
  274. void Scene::visit(T* instance, bool (T::*visitMethod)(Node*,C), C cookie)
  275. {
  276. for (Node* node = getFirstNode(); node != NULL; node = node->getNextSibling())
  277. {
  278. visitNode(node, instance, visitMethod, cookie);
  279. }
  280. }
  281. inline void Scene::visit(const char* visitMethod)
  282. {
  283. for (Node* node = getFirstNode(); node != NULL; node = node->getNextSibling())
  284. {
  285. visitNode(node, visitMethod);
  286. }
  287. }
  288. template <class T>
  289. void Scene::visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*))
  290. {
  291. // Invoke the visit method for this node.
  292. if (!(instance->*visitMethod)(node))
  293. return;
  294. // If this node has a model with a mesh skin, visit the joint hierarchy within it
  295. // since we don't add joint hierarchies directly to the scene. If joints are never
  296. // visited, it's possible that nodes embedded within the joint hierarchy that contain
  297. // models will never get visited (and therefore never get drawn).
  298. if (node->_model && node->_model->_skin && node->_model->_skin->_rootNode)
  299. {
  300. visitNode(node->_model->_skin->_rootNode, instance, visitMethod);
  301. }
  302. // Recurse for all children.
  303. for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
  304. {
  305. visitNode(child, instance, visitMethod);
  306. }
  307. }
  308. template <class T, class C>
  309. void Scene::visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*,C), C cookie)
  310. {
  311. // Invoke the visit method for this node.
  312. if (!(instance->*visitMethod)(node, cookie))
  313. return;
  314. // If this node has a model with a mesh skin, visit the joint hierarchy within it
  315. // since we don't add joint hierarchies directly to the scene. If joints are never
  316. // visited, it's possible that nodes embedded within the joint hierarchy that contain
  317. // models will never get visited (and therefore never get drawn).
  318. if (node->_model && node->_model->_skin && node->_model->_skin->_rootNode)
  319. {
  320. visitNode(node->_model->_skin->_rootNode, instance, visitMethod, cookie);
  321. }
  322. // Recurse for all children.
  323. for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
  324. {
  325. visitNode(child, instance, visitMethod, cookie);
  326. }
  327. }
  328. }
  329. #endif