CmSceneObject.cpp 9.7 KB

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