Scene.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "Base.h"
  2. #include "AudioListener.h"
  3. #include "Scene.h"
  4. #include "SceneLoader.h"
  5. #include "MeshSkin.h"
  6. #include "Joint.h"
  7. #include "Terrain.h"
  8. #include "Bundle.h"
  9. namespace gameplay
  10. {
  11. // Global list of active scenes
  12. static std::vector<Scene*> __sceneList;
  13. static inline char lowercase(char c)
  14. {
  15. if (c >= 'A' && c <='Z')
  16. {
  17. c |= 0x20;
  18. }
  19. return c;
  20. }
  21. // Returns true if 'str' ends with 'suffix'; false otherwise.
  22. static bool endsWith(const char* str, const char* suffix, bool ignoreCase)
  23. {
  24. if (str == NULL || suffix == NULL)
  25. return false;
  26. size_t length = strlen(str);
  27. size_t suffixLength = strlen(suffix);
  28. if (suffixLength > length)
  29. {
  30. return false;
  31. }
  32. size_t offset = length - suffixLength;
  33. const char* p = str + offset;
  34. while (*p != '\0' && *suffix != '\0')
  35. {
  36. if (ignoreCase)
  37. {
  38. if (lowercase(*p) != lowercase(*suffix))
  39. {
  40. return false;
  41. }
  42. }
  43. else if (*p != *suffix)
  44. {
  45. return false;
  46. }
  47. ++p;
  48. ++suffix;
  49. }
  50. return true;
  51. }
  52. Scene::Scene()
  53. : _id(""), _activeCamera(NULL), _firstNode(NULL), _lastNode(NULL), _nodeCount(0), _bindAudioListenerToCamera(true),
  54. _nextItr(NULL), _nextReset(true)
  55. {
  56. __sceneList.push_back(this);
  57. }
  58. Scene::~Scene()
  59. {
  60. // Unbind our active camera from the audio listener
  61. if (_activeCamera)
  62. {
  63. AudioListener* audioListener = AudioListener::getInstance();
  64. if (audioListener && (audioListener->getCamera() == _activeCamera))
  65. {
  66. audioListener->setCamera(NULL);
  67. }
  68. SAFE_RELEASE(_activeCamera);
  69. }
  70. // Remove all nodes from the scene
  71. removeAllNodes();
  72. // Remove the scene from global list
  73. std::vector<Scene*>::iterator itr = std::find(__sceneList.begin(), __sceneList.end(), this);
  74. if (itr != __sceneList.end())
  75. __sceneList.erase(itr);
  76. }
  77. Scene* Scene::create(const char* id)
  78. {
  79. Scene* scene = new Scene();
  80. scene->setId(id);
  81. return scene;
  82. }
  83. Scene* Scene::load(const char* filePath)
  84. {
  85. if (endsWith(filePath, ".gpb", true))
  86. {
  87. Scene* scene = NULL;
  88. Bundle* bundle = Bundle::create(filePath);
  89. if (bundle)
  90. {
  91. scene = bundle->loadScene();
  92. SAFE_RELEASE(bundle);
  93. }
  94. return scene;
  95. }
  96. return SceneLoader::load(filePath);
  97. }
  98. Scene* Scene::getScene(const char* id)
  99. {
  100. if (id == NULL)
  101. return __sceneList.size() ? __sceneList[0] : NULL;
  102. for (size_t i = 0, count = __sceneList.size(); i < count; ++i)
  103. {
  104. if (__sceneList[i]->_id == id)
  105. return __sceneList[i];
  106. }
  107. return NULL;
  108. }
  109. const char* Scene::getId() const
  110. {
  111. return _id.c_str();
  112. }
  113. void Scene::setId(const char* id)
  114. {
  115. _id = id ? id : "";
  116. }
  117. Node* Scene::findNode(const char* id, bool recursive, bool exactMatch) const
  118. {
  119. GP_ASSERT(id);
  120. // Search immediate children first.
  121. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  122. {
  123. // Does this child's ID match?
  124. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  125. {
  126. return child;
  127. }
  128. }
  129. // Recurse.
  130. if (recursive)
  131. {
  132. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  133. {
  134. Node* match = child->findNode(id, true, exactMatch);
  135. if (match)
  136. {
  137. return match;
  138. }
  139. }
  140. }
  141. return NULL;
  142. }
  143. unsigned int Scene::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
  144. {
  145. GP_ASSERT(id);
  146. unsigned int count = 0;
  147. // Search immediate children first.
  148. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  149. {
  150. // Does this child's ID match?
  151. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  152. {
  153. nodes.push_back(child);
  154. ++count;
  155. }
  156. }
  157. // Recurse.
  158. if (recursive)
  159. {
  160. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  161. {
  162. count += child->findNodes(id, nodes, true, exactMatch);
  163. }
  164. }
  165. return count;
  166. }
  167. void Scene::visitNode(Node* node, const char* visitMethod)
  168. {
  169. ScriptController* sc = Game::getInstance()->getScriptController();
  170. // Invoke the visit method for this node.
  171. if (!sc->executeFunction<bool>(visitMethod, "<Node>", node))
  172. return;
  173. // If this node has a model with a mesh skin, visit the joint hierarchy within it
  174. // since we don't add joint hierarcies directly to the scene. If joints are never
  175. // visited, it's possible that nodes embedded within the joint hierarchy that contain
  176. // models will never get visited (and therefore never get drawn).
  177. if (node->_model && node->_model->_skin && node->_model->_skin->_rootNode)
  178. {
  179. visitNode(node->_model->_skin->_rootNode, visitMethod);
  180. }
  181. // Recurse for all children.
  182. for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
  183. {
  184. visitNode(child, visitMethod);
  185. }
  186. }
  187. Node* Scene::addNode(const char* id)
  188. {
  189. Node* node = Node::create(id);
  190. GP_ASSERT(node);
  191. addNode(node);
  192. // Call release to decrement the ref count to 1 before returning.
  193. node->release();
  194. return node;
  195. }
  196. void Scene::addNode(Node* node)
  197. {
  198. GP_ASSERT(node);
  199. if (node->_scene == this)
  200. {
  201. // The node is already a member of this scene.
  202. return;
  203. }
  204. node->addRef();
  205. // If the node is part of another scene, remove it.
  206. if (node->_scene && node->_scene != this)
  207. {
  208. node->_scene->removeNode(node);
  209. }
  210. // If the node is part of another node hierarchy, remove it.
  211. if (node->getParent())
  212. {
  213. node->getParent()->removeChild(node);
  214. }
  215. // Link the new node into the end of our list.
  216. if (_lastNode)
  217. {
  218. _lastNode->_nextSibling = node;
  219. node->_prevSibling = _lastNode;
  220. _lastNode = node;
  221. }
  222. else
  223. {
  224. _firstNode = _lastNode = node;
  225. }
  226. node->_scene = this;
  227. ++_nodeCount;
  228. // If we don't have an active camera set, then check for one and set it.
  229. if (_activeCamera == NULL)
  230. {
  231. Camera* camera = node->getCamera();
  232. if (camera)
  233. {
  234. setActiveCamera(camera);
  235. }
  236. }
  237. }
  238. void Scene::removeNode(Node* node)
  239. {
  240. GP_ASSERT(node);
  241. if (node->_scene != this)
  242. return;
  243. if (node == _firstNode)
  244. {
  245. _firstNode = node->_nextSibling;
  246. }
  247. if (node == _lastNode)
  248. {
  249. _lastNode = node->_prevSibling;
  250. }
  251. node->remove();
  252. node->_scene = NULL;
  253. SAFE_RELEASE(node);
  254. --_nodeCount;
  255. }
  256. void Scene::removeAllNodes()
  257. {
  258. while (_lastNode)
  259. {
  260. removeNode(_lastNode);
  261. }
  262. }
  263. unsigned int Scene::getNodeCount() const
  264. {
  265. return _nodeCount;
  266. }
  267. Node* Scene::getFirstNode() const
  268. {
  269. return _firstNode;
  270. }
  271. Camera* Scene::getActiveCamera()
  272. {
  273. return _activeCamera;
  274. }
  275. void Scene::setActiveCamera(Camera* camera)
  276. {
  277. // Make sure we don't release the camera if the same camera is set twice.
  278. if (_activeCamera != camera)
  279. {
  280. AudioListener* audioListener = AudioListener::getInstance();
  281. if (_activeCamera)
  282. {
  283. // Unbind the active camera from the audio listener
  284. if (audioListener && (audioListener->getCamera() == _activeCamera))
  285. {
  286. audioListener->setCamera(NULL);
  287. }
  288. SAFE_RELEASE(_activeCamera);
  289. }
  290. _activeCamera = camera;
  291. if (_activeCamera)
  292. {
  293. _activeCamera->addRef();
  294. if (audioListener && _bindAudioListenerToCamera)
  295. {
  296. audioListener->setCamera(_activeCamera);
  297. }
  298. }
  299. }
  300. }
  301. void Scene::bindAudioListenerToCamera(bool bind)
  302. {
  303. if (_bindAudioListenerToCamera != bind)
  304. {
  305. _bindAudioListenerToCamera = bind;
  306. if (AudioListener::getInstance())
  307. {
  308. AudioListener::getInstance()->setCamera(bind ? _activeCamera : NULL);
  309. }
  310. }
  311. }
  312. const Vector3& Scene::getAmbientColor() const
  313. {
  314. return _ambientColor;
  315. }
  316. void Scene::setAmbientColor(float red, float green, float blue)
  317. {
  318. _ambientColor.set(red, green, blue);
  319. }
  320. void Scene::reset()
  321. {
  322. _nextItr = NULL;
  323. _nextReset = true;
  324. }
  325. Node* Scene::getNext()
  326. {
  327. if (_nextReset)
  328. {
  329. _nextItr = findNextVisibleSibling(getFirstNode());
  330. _nextReset = false;
  331. }
  332. else if (_nextItr)
  333. {
  334. Node* node = findNextVisibleSibling(_nextItr->getFirstChild());
  335. if (node == NULL)
  336. {
  337. node = findNextVisibleSibling(_nextItr->getNextSibling());
  338. if (node == NULL)
  339. {
  340. // Find first parent with a sibling
  341. node = _nextItr->getParent();
  342. while (node && (!findNextVisibleSibling(node->getNextSibling())))
  343. {
  344. node = node->getParent();
  345. }
  346. if (node)
  347. {
  348. node = findNextVisibleSibling(node->getNextSibling());
  349. }
  350. }
  351. }
  352. _nextItr = node;
  353. }
  354. return _nextItr;
  355. }
  356. Node* Scene::findNextVisibleSibling(Node* node)
  357. {
  358. while (node != NULL && !isNodeVisible(node))
  359. {
  360. node = node->getNextSibling();
  361. }
  362. return node;
  363. }
  364. bool Scene::isNodeVisible(Node* node)
  365. {
  366. if (!node->isEnabled())
  367. return false;
  368. if (node->getForm() || node->getParticleEmitter() || node->getTerrain() || node->getLight() || node->getCamera())
  369. {
  370. return true;
  371. }
  372. else
  373. {
  374. return node->getBoundingSphere().intersects(_activeCamera->getFrustum());
  375. }
  376. }
  377. }