CmSceneObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include "CmSceneObject.h"
  2. #include "CmComponent.h"
  3. #include "CmSceneManager.h"
  4. #include "CmException.h"
  5. #include "CmDebug.h"
  6. #include "CmSceneObjectRTTI.h"
  7. #include "CmMemorySerializer.h"
  8. #include "CmGameObjectManager.h"
  9. namespace BansheeEngine
  10. {
  11. SceneObject::SceneObject(const String& name)
  12. :GameObject(), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY), mScale(Vector3::ONE),
  13. mWorldPosition(Vector3::ZERO), mWorldRotation(Quaternion::IDENTITY), mWorldScale(Vector3::ONE),
  14. mCachedLocalTfrm(Matrix4::IDENTITY), mIsCachedLocalTfrmUpToDate(false),
  15. mCachedWorldTfrm(Matrix4::IDENTITY), mIsCachedWorldTfrmUpToDate(false)
  16. {
  17. setName(name);
  18. }
  19. SceneObject::~SceneObject()
  20. {
  21. if(!mThisHandle.isDestroyed())
  22. {
  23. LOGWRN("Object is being deleted without being destroyed first?");
  24. destroyInternal();
  25. }
  26. }
  27. HSceneObject SceneObject::create(const String& name)
  28. {
  29. HSceneObject newObject = createInternal(name);
  30. gSceneManager().registerNewSO(newObject);
  31. return newObject;
  32. }
  33. HSceneObject SceneObject::createInternal(const String& name)
  34. {
  35. std::shared_ptr<SceneObject> sceneObjectPtr = std::shared_ptr<SceneObject>(new (cm_alloc<SceneObject, PoolAlloc>()) SceneObject(name),
  36. &cm_delete<PoolAlloc, SceneObject>, StdAlloc<PoolAlloc>());
  37. HSceneObject sceneObject = GameObjectManager::instance().registerObject(sceneObjectPtr);
  38. sceneObject->mThisHandle = sceneObject;
  39. return sceneObject;
  40. }
  41. void SceneObject::destroy()
  42. {
  43. // Parent is our owner, so when his reference to us is removed, delete might be called.
  44. // So make sure this is the last thing we do.
  45. if(mParent != nullptr)
  46. {
  47. if(!mParent.isDestroyed())
  48. mParent->removeChild(mThisHandle);
  49. }
  50. destroyInternal();
  51. }
  52. void SceneObject::destroyInternal()
  53. {
  54. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  55. (*iter)->destroyInternal();
  56. mChildren.clear();
  57. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  58. {
  59. gSceneManager().notifyComponentRemoved((*iter));
  60. GameObjectManager::instance().unregisterObject(*iter);
  61. (*iter).destroy();
  62. }
  63. mComponents.clear();
  64. GameObjectManager::instance().unregisterObject(mThisHandle);
  65. mThisHandle.destroy();
  66. }
  67. /************************************************************************/
  68. /* Transform */
  69. /************************************************************************/
  70. void SceneObject::setPosition(const Vector3& position)
  71. {
  72. mPosition = position;
  73. markTfrmDirty();
  74. }
  75. void SceneObject::setRotation(const Quaternion& rotation)
  76. {
  77. mRotation = rotation;
  78. markTfrmDirty();
  79. }
  80. void SceneObject::setScale(const Vector3& scale)
  81. {
  82. mScale = scale;
  83. markTfrmDirty();
  84. }
  85. const Vector3& SceneObject::getWorldPosition() const
  86. {
  87. if(!mIsCachedWorldTfrmUpToDate)
  88. updateWorldTfrm();
  89. return mWorldPosition;
  90. }
  91. const Quaternion& SceneObject::getWorldRotation() const
  92. {
  93. if(!mIsCachedWorldTfrmUpToDate)
  94. updateWorldTfrm();
  95. return mWorldRotation;
  96. }
  97. const Vector3& SceneObject::getWorldScale() const
  98. {
  99. if(!mIsCachedWorldTfrmUpToDate)
  100. updateWorldTfrm();
  101. return mWorldScale;
  102. }
  103. void SceneObject::lookAt(const Vector3& location, const Vector3& up)
  104. {
  105. Vector3 forward = location - mPosition;
  106. forward.normalize();
  107. setForward(forward);
  108. Quaternion upRot = Quaternion::getRotationFromTo(getUp(), up);
  109. setRotation(getRotation() * upRot);
  110. }
  111. const Matrix4& SceneObject::getWorldTfrm() const
  112. {
  113. if(!mIsCachedWorldTfrmUpToDate)
  114. updateWorldTfrm();
  115. return mCachedWorldTfrm;
  116. }
  117. const Matrix4& SceneObject::getLocalTfrm() const
  118. {
  119. if(!mIsCachedLocalTfrmUpToDate)
  120. updateLocalTfrm();
  121. return mCachedLocalTfrm;
  122. }
  123. void SceneObject::move(const Vector3& vec)
  124. {
  125. setPosition(mPosition + vec);
  126. }
  127. void SceneObject::moveRelative(const Vector3& vec)
  128. {
  129. // Transform the axes of the relative vector by camera's local axes
  130. Vector3 trans = mRotation.rotate(vec);
  131. setPosition(mPosition + trans);
  132. }
  133. void SceneObject::rotate(const Vector3& axis, const Radian& angle)
  134. {
  135. Quaternion q;
  136. q.fromAxisAngle(axis, angle);
  137. rotate(q);
  138. }
  139. void SceneObject::rotate(const Quaternion& q)
  140. {
  141. // Note the order of the mult, i.e. q comes after
  142. // Normalize the quat to avoid cumulative problems with precision
  143. Quaternion qnorm = q;
  144. qnorm.normalize();
  145. setRotation(qnorm * mRotation);
  146. }
  147. void SceneObject::roll(const Radian& angle)
  148. {
  149. // Rotate around local Z axis
  150. Vector3 zAxis = mRotation.rotate(Vector3::UNIT_Z);
  151. rotate(zAxis, angle);
  152. }
  153. void SceneObject::yaw(const Radian& angle)
  154. {
  155. Vector3 yAxis = mRotation.rotate(Vector3::UNIT_Y);
  156. rotate(yAxis, angle);
  157. }
  158. void SceneObject::pitch(const Radian& angle)
  159. {
  160. // Rotate around local X axis
  161. Vector3 xAxis = mRotation.rotate(Vector3::UNIT_X);
  162. rotate(xAxis, angle);
  163. }
  164. void SceneObject::setForward(const Vector3& forwardDir)
  165. {
  166. if (forwardDir == Vector3::ZERO)
  167. return;
  168. Vector3 nrmForwardDir = Vector3::normalize(forwardDir);
  169. Vector3 currentForwardDir = getForward();
  170. const Quaternion& currentRotation = getWorldRotation();
  171. Quaternion targetRotation;
  172. if ((nrmForwardDir+currentForwardDir).squaredLength() < 0.00005f)
  173. {
  174. // Oops, a 180 degree turn (infinite possible rotation axes)
  175. // Default to yaw i.e. use current UP
  176. targetRotation = Quaternion(-currentRotation.y, -currentRotation.z, currentRotation.w, currentRotation.x);
  177. }
  178. else
  179. {
  180. // Derive shortest arc to new direction
  181. Quaternion rotQuat = Quaternion::getRotationFromTo(currentForwardDir, nrmForwardDir);
  182. targetRotation = rotQuat * currentRotation;
  183. }
  184. setRotation(targetRotation);
  185. }
  186. void SceneObject::markTfrmDirty() const
  187. {
  188. mIsCachedLocalTfrmUpToDate = false;
  189. mIsCachedWorldTfrmUpToDate = false;
  190. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  191. {
  192. (*iter)->markTfrmDirty();
  193. }
  194. }
  195. void SceneObject::updateWorldTfrm() const
  196. {
  197. if(mParent != nullptr)
  198. {
  199. mCachedWorldTfrm = getLocalTfrm() * mParent->getWorldTfrm();
  200. // Update orientation
  201. const Quaternion& parentOrientation = mParent->getWorldRotation();
  202. mWorldRotation = parentOrientation * mRotation;
  203. // Update scale
  204. const Vector3& parentScale = mParent->getWorldScale();
  205. // Scale own position by parent scale, NB just combine
  206. // as equivalent axes, no shearing
  207. mWorldScale = parentScale * mScale;
  208. // Change position vector based on parent's orientation & scale
  209. mWorldPosition = parentOrientation.rotate(parentScale * mPosition);
  210. // Add altered position vector to parents
  211. mWorldPosition += mParent->getWorldPosition();
  212. }
  213. else
  214. {
  215. mCachedWorldTfrm = getLocalTfrm();
  216. mWorldRotation = mRotation;
  217. mWorldPosition = mPosition;
  218. mWorldScale = mScale;
  219. }
  220. mIsCachedWorldTfrmUpToDate = true;
  221. }
  222. void SceneObject::updateLocalTfrm() const
  223. {
  224. mCachedLocalTfrm.setTRS(mPosition, mRotation, mScale);
  225. mIsCachedLocalTfrmUpToDate = true;
  226. }
  227. /************************************************************************/
  228. /* Hierarchy */
  229. /************************************************************************/
  230. void SceneObject::setParent(const HSceneObject& parent)
  231. {
  232. if(parent.isDestroyed())
  233. {
  234. CM_EXCEPT(InternalErrorException,
  235. "Trying to assign a SceneObject parent that is destroyed.");
  236. }
  237. if(mParent == nullptr || mParent != parent)
  238. {
  239. if(mParent != nullptr)
  240. mParent->removeChild(mThisHandle);
  241. if(parent != nullptr)
  242. parent->addChild(mThisHandle);
  243. mParent = parent;
  244. markTfrmDirty();
  245. }
  246. }
  247. HSceneObject SceneObject::getChild(UINT32 idx) const
  248. {
  249. if(idx < 0 || idx >= mChildren.size())
  250. {
  251. CM_EXCEPT(InternalErrorException, "Child index out of range.");
  252. }
  253. return mChildren[idx];
  254. }
  255. int SceneObject::indexOfChild(const HSceneObject& child) const
  256. {
  257. for(int i = 0; i < (int)mChildren.size(); i++)
  258. {
  259. if(mChildren[i] == child)
  260. return i;
  261. }
  262. return -1;
  263. }
  264. void SceneObject::addChild(const HSceneObject& object)
  265. {
  266. mChildren.push_back(object);
  267. }
  268. void SceneObject::removeChild(const HSceneObject& object)
  269. {
  270. auto result = find(mChildren.begin(), mChildren.end(), object);
  271. if(result != mChildren.end())
  272. mChildren.erase(result);
  273. else
  274. {
  275. CM_EXCEPT(InternalErrorException,
  276. "Trying to remove a child but it's not a child of the transform.");
  277. }
  278. }
  279. HSceneObject SceneObject::clone()
  280. {
  281. UINT32 bufferSize = 0;
  282. MemorySerializer serializer;
  283. UINT8* buffer = serializer.encode(this, bufferSize, &cm_alloc);
  284. GameObjectManager::instance().startDeserialization();
  285. std::shared_ptr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
  286. cm_free(buffer);
  287. GameObjectManager::instance().endDeserialization();
  288. return cloneObj->mThisHandle;
  289. }
  290. HComponent SceneObject::getComponent(UINT32 typeId) const
  291. {
  292. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  293. {
  294. if((*iter)->getRTTI()->getRTTIId() == typeId)
  295. return *iter;
  296. }
  297. return HComponent();
  298. }
  299. void SceneObject::destroyComponent(const HComponent& component)
  300. {
  301. if(component == nullptr)
  302. {
  303. LOGDBG("Trying to remove a null component");
  304. return;
  305. }
  306. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  307. if(iter != mComponents.end())
  308. {
  309. gSceneManager().notifyComponentRemoved((*iter));
  310. GameObjectManager::instance().unregisterObject(component);
  311. (*iter)->onDestroyed();
  312. (*iter).destroy();
  313. mComponents.erase(iter);
  314. }
  315. else
  316. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  317. }
  318. void SceneObject::destroyComponent(Component* component)
  319. {
  320. auto iterFind = std::find_if(mComponents.begin(), mComponents.end(),
  321. [component](const HComponent& x)
  322. {
  323. if(x.isDestroyed())
  324. return false;
  325. return x._getHandleData()->mPtr->object.get() == component; }
  326. );
  327. if(iterFind != mComponents.end())
  328. {
  329. destroyComponent(*iterFind);
  330. }
  331. }
  332. void SceneObject::addComponentInternal(const std::shared_ptr<Component> component)
  333. {
  334. GameObjectHandle<Component> newComponent = GameObjectHandle<Component>(component);
  335. newComponent->mParent = mThisHandle;
  336. mComponents.push_back(newComponent);
  337. gSceneManager().notifyComponentAdded(newComponent);
  338. }
  339. RTTITypeBase* SceneObject::getRTTIStatic()
  340. {
  341. return SceneObjectRTTI::instance();
  342. }
  343. RTTITypeBase* SceneObject::getRTTI() const
  344. {
  345. return SceneObject::getRTTIStatic();
  346. }
  347. }