CmSceneObject.cpp 9.3 KB

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