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 CamelotFramework
  10. {
  11. UINT32 SceneObject::NextFreeId = 0;
  12. SceneObject::SceneObject(const String& name, UINT32 id)
  13. :mName(name), mId(id), mPosition(Vector3::ZERO), mRotation(Quaternion::IDENTITY), mScale(Vector3::ONE),
  14. mWorldPosition(Vector3::ZERO), mWorldRotation(Quaternion::IDENTITY), mWorldScale(Vector3::ONE),
  15. mCachedLocalTfrm(Matrix4::IDENTITY), mIsCachedLocalTfrmUpToDate(false),
  16. mCachedWorldTfrm(Matrix4::IDENTITY), mIsCachedWorldTfrmUpToDate(false),
  17. mCustomWorldTfrm(Matrix4::IDENTITY), mIsCustomTfrmModeActive(false)
  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().registerNewGO(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, NextFreeId++),
  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. // TODO - I'm ignoring "up" direction
  108. setForward(forward);
  109. Quaternion upRot = Quaternion::getRotationFromTo(getUp(), up);
  110. setRotation(getRotation() * upRot);
  111. }
  112. const Matrix4& SceneObject::getWorldTfrm() const
  113. {
  114. if(!mIsCachedWorldTfrmUpToDate)
  115. updateWorldTfrm();
  116. return mCachedWorldTfrm;
  117. }
  118. const Matrix4& SceneObject::getLocalTfrm() const
  119. {
  120. if(!mIsCachedLocalTfrmUpToDate)
  121. updateLocalTfrm();
  122. return mCachedLocalTfrm;
  123. }
  124. void SceneObject::move(const Vector3& vec)
  125. {
  126. setPosition(mPosition + vec);
  127. }
  128. void SceneObject::moveRelative(const Vector3& vec)
  129. {
  130. // Transform the axes of the relative vector by camera's local axes
  131. Vector3 trans = mRotation.rotate(vec);
  132. setPosition(mPosition + trans);
  133. }
  134. void SceneObject::rotate(const Vector3& axis, const Radian& angle)
  135. {
  136. Quaternion q;
  137. q.fromAxisAngle(axis, angle);
  138. rotate(q);
  139. }
  140. void SceneObject::rotate(const Quaternion& q)
  141. {
  142. // Note the order of the mult, i.e. q comes after
  143. // Normalize the quat to avoid cumulative problems with precision
  144. Quaternion qnorm = q;
  145. qnorm.normalize();
  146. setRotation(qnorm * mRotation);
  147. }
  148. void SceneObject::roll(const Radian& angle)
  149. {
  150. // Rotate around local Z axis
  151. Vector3 zAxis = mRotation.rotate(Vector3::UNIT_Z);
  152. rotate(zAxis, angle);
  153. }
  154. void SceneObject::yaw(const Radian& angle)
  155. {
  156. Vector3 yAxis = mRotation.rotate(Vector3::UNIT_Y);
  157. rotate(yAxis, angle);
  158. }
  159. void SceneObject::pitch(const Radian& angle)
  160. {
  161. // Rotate around local X axis
  162. Vector3 xAxis = mRotation.rotate(Vector3::UNIT_X);
  163. rotate(xAxis, angle);
  164. }
  165. void SceneObject::setForward(const Vector3& forwardDir)
  166. {
  167. if (forwardDir == Vector3::ZERO)
  168. return;
  169. Vector3 nrmForwardDir = Vector3::normalize(forwardDir);
  170. Vector3 currentForwardDir = getForward();
  171. const Quaternion& currentRotation = getWorldRotation();
  172. Quaternion targetRotation;
  173. if ((nrmForwardDir+currentForwardDir).squaredLength() < 0.00005f)
  174. {
  175. // Oops, a 180 degree turn (infinite possible rotation axes)
  176. // Default to yaw i.e. use current UP
  177. targetRotation = Quaternion(-currentRotation.y, -currentRotation.z, currentRotation.w, currentRotation.x);
  178. }
  179. else
  180. {
  181. // Derive shortest arc to new direction
  182. Quaternion rotQuat = Quaternion::getRotationFromTo(currentForwardDir, nrmForwardDir);
  183. targetRotation = rotQuat * currentRotation;
  184. }
  185. setRotation(targetRotation);
  186. }
  187. void SceneObject::markTfrmDirty() const
  188. {
  189. mIsCachedLocalTfrmUpToDate = false;
  190. mIsCachedWorldTfrmUpToDate = false;
  191. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  192. {
  193. (*iter)->markTfrmDirty();
  194. }
  195. }
  196. void SceneObject::updateWorldTfrm() const
  197. {
  198. if(mParent != nullptr)
  199. {
  200. mCachedWorldTfrm = getLocalTfrm() * mParent->getWorldTfrm();
  201. // Update orientation
  202. const Quaternion& parentOrientation = mParent->getWorldRotation();
  203. mWorldRotation = parentOrientation * mRotation;
  204. // Update scale
  205. const Vector3& parentScale = mParent->getWorldScale();
  206. // Scale own position by parent scale, NB just combine
  207. // as equivalent axes, no shearing
  208. mWorldScale = parentScale * mScale;
  209. // Change position vector based on parent's orientation & scale
  210. mWorldPosition = parentOrientation.rotate(parentScale * mPosition);
  211. // Add altered position vector to parents
  212. mWorldPosition += mParent->getWorldPosition();
  213. }
  214. else
  215. {
  216. mCachedWorldTfrm = getLocalTfrm();
  217. mWorldRotation = mRotation;
  218. mWorldPosition = mPosition;
  219. mWorldScale = mScale;
  220. }
  221. mIsCachedWorldTfrmUpToDate = true;
  222. }
  223. void SceneObject::updateLocalTfrm() const
  224. {
  225. mCachedLocalTfrm.setTRS(mPosition, mRotation, mScale);
  226. mIsCachedLocalTfrmUpToDate = true;
  227. }
  228. /************************************************************************/
  229. /* Hierarchy */
  230. /************************************************************************/
  231. void SceneObject::setParent(const HSceneObject& parent)
  232. {
  233. if(parent.isDestroyed())
  234. {
  235. CM_EXCEPT(InternalErrorException,
  236. "Trying to assign a SceneObject parent that is destroyed.");
  237. }
  238. if(mParent == nullptr || mParent != parent)
  239. {
  240. if(mParent != nullptr)
  241. mParent->removeChild(mThisHandle);
  242. if(parent != nullptr)
  243. parent->addChild(mThisHandle);
  244. mParent = parent;
  245. markTfrmDirty();
  246. }
  247. }
  248. HSceneObject SceneObject::getChild(unsigned int idx) const
  249. {
  250. if(idx < 0 || idx >= mChildren.size())
  251. {
  252. CM_EXCEPT(InternalErrorException,
  253. "Child index out of range.");
  254. }
  255. return mChildren[idx];
  256. }
  257. int SceneObject::indexOfChild(const HSceneObject& child) const
  258. {
  259. for(int i = 0; i < (int)mChildren.size(); i++)
  260. {
  261. if(mChildren[i] == child)
  262. return i;
  263. }
  264. return -1;
  265. }
  266. void SceneObject::addChild(const HSceneObject& object)
  267. {
  268. mChildren.push_back(object);
  269. }
  270. void SceneObject::removeChild(const HSceneObject& object)
  271. {
  272. auto result = find(mChildren.begin(), mChildren.end(), object);
  273. if(result != mChildren.end())
  274. mChildren.erase(result);
  275. else
  276. {
  277. CM_EXCEPT(InternalErrorException,
  278. "Trying to remove a child but it's not a child of the transform.");
  279. }
  280. }
  281. HSceneObject SceneObject::clone()
  282. {
  283. UINT32 bufferSize = 0;
  284. MemorySerializer serializer;
  285. UINT8* buffer = serializer.encode(this, bufferSize, &cm_alloc);
  286. std::shared_ptr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
  287. cm_free(buffer);
  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).destroy();
  312. mComponents.erase(iter);
  313. }
  314. else
  315. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  316. }
  317. void SceneObject::destroyComponent(Component* component)
  318. {
  319. auto iterFind = std::find_if(mComponents.begin(), mComponents.end(),
  320. [component](const HComponent& x)
  321. {
  322. if(x.isDestroyed())
  323. return false;
  324. return x.getHandleData()->mPtr->object.get() == component; }
  325. );
  326. if(iterFind != mComponents.end())
  327. {
  328. destroyComponent(*iterFind);
  329. }
  330. }
  331. void SceneObject::addComponentInternal(const std::shared_ptr<Component> component)
  332. {
  333. GameObjectHandle<Component> newComponent = GameObjectHandle<Component>(component);
  334. newComponent->mParent = mThisHandle;
  335. mComponents.push_back(newComponent);
  336. gSceneManager().notifyComponentAdded(newComponent);
  337. }
  338. RTTITypeBase* SceneObject::getRTTIStatic()
  339. {
  340. return SceneObjectRTTI::instance();
  341. }
  342. RTTITypeBase* SceneObject::getRTTI() const
  343. {
  344. return SceneObject::getRTTIStatic();
  345. }
  346. }