1
0

CCScene.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCScene.h"
  24. #include "base/CCDirector.h"
  25. #include "2d/CCCamera.h"
  26. #include "base/CCEventDispatcher.h"
  27. #include "base/CCEventListenerCustom.h"
  28. #include "base/ccUTF8.h"
  29. #include "renderer/CCRenderer.h"
  30. #include "renderer/CCFrameBuffer.h"
  31. #if CC_USE_PHYSICS
  32. #include "physics/CCPhysicsWorld.h"
  33. #endif
  34. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  35. #include "physics3d/CCPhysics3DWorld.h"
  36. #include "physics3d/CCPhysics3DComponent.h"
  37. #endif
  38. #if CC_USE_NAVMESH
  39. #include "navmesh/CCNavMesh.h"
  40. #endif
  41. NS_CC_BEGIN
  42. Scene::Scene()
  43. {
  44. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  45. _physics3DWorld = nullptr;
  46. _physics3dDebugCamera = nullptr;
  47. #endif
  48. #if CC_USE_NAVMESH
  49. _navMesh = nullptr;
  50. _navMeshDebugCamera = nullptr;
  51. #endif
  52. #if CC_USE_PHYSICS
  53. _physicsWorld = nullptr;
  54. #endif
  55. _ignoreAnchorPointForPosition = true;
  56. setAnchorPoint(Vec2(0.5f, 0.5f));
  57. _cameraOrderDirty = true;
  58. //create default camera
  59. _defaultCamera = Camera::create();
  60. addChild(_defaultCamera);
  61. _event = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, std::bind(&Scene::onProjectionChanged, this, std::placeholders::_1));
  62. _event->retain();
  63. Camera::_visitingCamera = nullptr;
  64. }
  65. Scene::~Scene()
  66. {
  67. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  68. CC_SAFE_RELEASE(_physics3DWorld);
  69. CC_SAFE_RELEASE(_physics3dDebugCamera);
  70. #endif
  71. #if CC_USE_NAVMESH
  72. CC_SAFE_RELEASE(_navMesh);
  73. #endif
  74. Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
  75. CC_SAFE_RELEASE(_event);
  76. #if CC_USE_PHYSICS
  77. delete _physicsWorld;
  78. #endif
  79. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  80. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  81. if (sEngine)
  82. {
  83. sEngine->releaseAllChildrenRecursive(this);
  84. }
  85. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  86. }
  87. #if CC_USE_NAVMESH
  88. void Scene::setNavMesh(NavMesh* navMesh)
  89. {
  90. if (_navMesh != navMesh)
  91. {
  92. CC_SAFE_RETAIN(navMesh);
  93. CC_SAFE_RELEASE(_navMesh);
  94. _navMesh = navMesh;
  95. }
  96. }
  97. #endif
  98. bool Scene::init()
  99. {
  100. auto size = Director::getInstance()->getWinSize();
  101. return initWithSize(size);
  102. }
  103. bool Scene::initWithSize(const Size& size)
  104. {
  105. setContentSize(size);
  106. return true;
  107. }
  108. Scene* Scene::create()
  109. {
  110. Scene *ret = new (std::nothrow) Scene();
  111. if (ret && ret->init())
  112. {
  113. ret->autorelease();
  114. return ret;
  115. }
  116. else
  117. {
  118. CC_SAFE_DELETE(ret);
  119. return nullptr;
  120. }
  121. }
  122. Scene* Scene::createWithSize(const Size& size)
  123. {
  124. Scene *ret = new (std::nothrow) Scene();
  125. if (ret && ret->initWithSize(size))
  126. {
  127. ret->autorelease();
  128. return ret;
  129. }
  130. else
  131. {
  132. CC_SAFE_DELETE(ret);
  133. return nullptr;
  134. }
  135. }
  136. std::string Scene::getDescription() const
  137. {
  138. return StringUtils::format("<Scene | tag = %d>", _tag);
  139. }
  140. void Scene::onProjectionChanged(EventCustom* /*event*/)
  141. {
  142. if (_defaultCamera)
  143. {
  144. _defaultCamera->initDefault();
  145. }
  146. }
  147. static bool camera_cmp(const Camera* a, const Camera* b)
  148. {
  149. return a->getRenderOrder() < b->getRenderOrder();
  150. }
  151. const std::vector<Camera*>& Scene::getCameras()
  152. {
  153. if (_cameraOrderDirty)
  154. {
  155. stable_sort(_cameras.begin(), _cameras.end(), camera_cmp);
  156. _cameraOrderDirty = false;
  157. }
  158. return _cameras;
  159. }
  160. void Scene::render(Renderer* renderer, const Mat4& eyeTransform, const Mat4* eyeProjection)
  161. {
  162. render(renderer, &eyeTransform, eyeProjection, 1);
  163. }
  164. void Scene::render(Renderer* renderer, const Mat4* eyeTransforms, const Mat4* eyeProjections, unsigned int multiViewCount)
  165. {
  166. auto director = Director::getInstance();
  167. Camera* defaultCamera = nullptr;
  168. const auto& transform = getNodeToParentTransform();
  169. for (const auto& camera : getCameras())
  170. {
  171. if (!camera->isVisible())
  172. continue;
  173. Camera::_visitingCamera = camera;
  174. if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT)
  175. {
  176. defaultCamera = Camera::_visitingCamera;
  177. }
  178. // There are two ways to modify the "default camera" with the eye Transform:
  179. // a) modify the "nodeToParentTransform" matrix
  180. // b) modify the "additional transform" matrix
  181. // both alternatives are correct, if the user manually modifies the camera with a camera->setPosition()
  182. // then the "nodeToParent transform" will be lost.
  183. // And it is important that the change is "permanent", because the matrix might be used for calculate
  184. // culling and other stuff.
  185. for (unsigned int i = 0; i < multiViewCount; ++i) {
  186. if (eyeProjections)
  187. camera->setAdditionalProjection(eyeProjections[i] * camera->getProjectionMatrix().getInversed());
  188. if (eyeTransforms)
  189. camera->setAdditionalTransform(eyeTransforms[i].getInversed());
  190. director->pushProjectionMatrix(i);
  191. director->loadProjectionMatrix(Camera::_visitingCamera->getViewProjectionMatrix(), i);
  192. }
  193. camera->apply();
  194. //clear background with max depth
  195. camera->clearBackground();
  196. //visit the scene
  197. visit(renderer, transform, 0);
  198. #if CC_USE_NAVMESH
  199. if (_navMesh && _navMeshDebugCamera == camera)
  200. {
  201. _navMesh->debugDraw(renderer);
  202. }
  203. #endif
  204. renderer->render();
  205. camera->restore();
  206. for (unsigned int i = 0; i < multiViewCount; ++i)
  207. director->popProjectionMatrix(i);
  208. // we shouldn't restore the transform matrix since it could be used
  209. // from "update" or other parts of the game to calculate culling or something else.
  210. // camera->setNodeToParentTransform(eyeCopy);
  211. }
  212. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  213. if (_physics3DWorld && _physics3DWorld->isDebugDrawEnabled())
  214. {
  215. Camera *physics3dDebugCamera = _physics3dDebugCamera != nullptr ? _physics3dDebugCamera: defaultCamera;
  216. for (unsigned int i = 0; i < multiViewCount; ++i) {
  217. if (eyeProjections)
  218. physics3dDebugCamera->setAdditionalProjection(eyeProjections[i] * physics3dDebugCamera->getProjectionMatrix().getInversed());
  219. if (eyeTransforms)
  220. physics3dDebugCamera->setAdditionalTransform(eyeTransforms[i].getInversed());
  221. director->pushProjectionMatrix(i);
  222. director->loadProjectionMatrix(physics3dDebugCamera->getViewProjectionMatrix(), i);
  223. }
  224. physics3dDebugCamera->apply();
  225. physics3dDebugCamera->clearBackground();
  226. _physics3DWorld->debugDraw(renderer);
  227. renderer->render();
  228. physics3dDebugCamera->restore();
  229. for (unsigned int i = 0; i < multiViewCount; ++i)
  230. director->popProjectionMatrix(i);
  231. }
  232. #endif
  233. Camera::_visitingCamera = nullptr;
  234. // experimental::FrameBuffer::applyDefaultFBO();
  235. }
  236. void Scene::removeAllChildren()
  237. {
  238. if (_defaultCamera)
  239. _defaultCamera->retain();
  240. Node::removeAllChildren();
  241. if (_defaultCamera)
  242. {
  243. addChild(_defaultCamera);
  244. _defaultCamera->release();
  245. }
  246. }
  247. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  248. void Scene::setPhysics3DDebugCamera(Camera* camera)
  249. {
  250. CC_SAFE_RETAIN(camera);
  251. CC_SAFE_RELEASE(_physics3dDebugCamera);
  252. _physics3dDebugCamera = camera;
  253. }
  254. #endif
  255. #if CC_USE_NAVMESH
  256. void Scene::setNavMeshDebugCamera(Camera *camera)
  257. {
  258. CC_SAFE_RETAIN(camera);
  259. CC_SAFE_RELEASE(_navMeshDebugCamera);
  260. _navMeshDebugCamera = camera;
  261. }
  262. #endif
  263. #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION))
  264. Scene* Scene::createWithPhysics()
  265. {
  266. Scene *ret = new (std::nothrow) Scene();
  267. if (ret && ret->initWithPhysics())
  268. {
  269. ret->autorelease();
  270. return ret;
  271. }
  272. else
  273. {
  274. CC_SAFE_DELETE(ret);
  275. return nullptr;
  276. }
  277. }
  278. bool Scene::initWithPhysics()
  279. {
  280. #if CC_USE_PHYSICS
  281. _physicsWorld = PhysicsWorld::construct(this);
  282. #endif
  283. bool ret = false;
  284. do
  285. {
  286. Director * director;
  287. CC_BREAK_IF( ! (director = Director::getInstance()) );
  288. this->setContentSize(director->getWinSize());
  289. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  290. Physics3DWorldDes info;
  291. CC_BREAK_IF(! (_physics3DWorld = Physics3DWorld::create(&info)));
  292. _physics3DWorld->retain();
  293. #endif
  294. // success
  295. ret = true;
  296. } while (0);
  297. return ret;
  298. }
  299. #endif
  300. #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
  301. void Scene::stepPhysicsAndNavigation(float deltaTime)
  302. {
  303. #if CC_USE_PHYSICS
  304. if (_physicsWorld && _physicsWorld->isAutoStep())
  305. _physicsWorld->update(deltaTime);
  306. #endif
  307. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  308. if (_physics3DWorld)
  309. {
  310. _physics3DWorld->stepSimulate(deltaTime);
  311. }
  312. #endif
  313. #if CC_USE_NAVMESH
  314. if (_navMesh)
  315. {
  316. _navMesh->update(deltaTime);
  317. }
  318. #endif
  319. }
  320. #endif
  321. NS_CC_END