CmSceneObject.cpp 11 KB

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