CmSceneObject.cpp 9.6 KB

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