CmSceneObject.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 CamelotEngine
  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>(CM_NEW(SceneObject, PoolAlloc) SceneObject(name),
  33. &MemAllocDeleter<GameObject, PoolAlloc>::deleter);
  34. sceneObject->mThisHandle = sceneObject;
  35. return sceneObject;
  36. }
  37. void SceneObject::destroy()
  38. {
  39. // Parent is our owner, so when his reference to us is removed, delete might be called.
  40. // So make sure this is the last thing we do.
  41. if(mParent != nullptr)
  42. {
  43. if(!mParent.isDestroyed())
  44. mParent->removeChild(mThisHandle);
  45. }
  46. destroyInternal();
  47. }
  48. void SceneObject::destroyInternal()
  49. {
  50. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  51. (*iter)->destroyInternal();
  52. mChildren.clear();
  53. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  54. {
  55. (*iter).destroy();
  56. }
  57. mComponents.clear();
  58. mThisHandle.destroy();
  59. }
  60. /************************************************************************/
  61. /* Transform */
  62. /************************************************************************/
  63. void SceneObject::setPosition(const Vector3& position)
  64. {
  65. mPosition = position;
  66. markTfrmDirty();
  67. }
  68. void SceneObject::setRotation(const Quaternion& rotation)
  69. {
  70. mRotation = rotation;
  71. markTfrmDirty();
  72. }
  73. void SceneObject::setScale(const Vector3& scale)
  74. {
  75. mScale = scale;
  76. markTfrmDirty();
  77. }
  78. const Vector3& SceneObject::getWorldPosition() const
  79. {
  80. if(!mIsCachedWorldTfrmUpToDate)
  81. updateWorldTfrm();
  82. return mWorldPosition;
  83. }
  84. const Quaternion& SceneObject::getWorldRotation() const
  85. {
  86. if(!mIsCachedWorldTfrmUpToDate)
  87. updateWorldTfrm();
  88. return mWorldRotation;
  89. }
  90. const Vector3& SceneObject::getWorldScale() const
  91. {
  92. if(!mIsCachedWorldTfrmUpToDate)
  93. updateWorldTfrm();
  94. return mWorldScale;
  95. }
  96. void SceneObject::lookAt(const Vector3& location, const Vector3& up)
  97. {
  98. Vector3 forward = location - mPosition;
  99. forward.normalize();
  100. // TODO - I'm ignoring "up" direction
  101. setForward(forward);
  102. Quaternion upRot = getUp().getRotationTo(up);
  103. setRotation(getRotation() * upRot);
  104. }
  105. const Matrix4& SceneObject::getWorldTfrm() const
  106. {
  107. if(!mIsCachedWorldTfrmUpToDate)
  108. updateWorldTfrm();
  109. return mCachedWorldTfrm;
  110. }
  111. const Matrix4& SceneObject::getLocalTfrm() const
  112. {
  113. if(!mIsCachedLocalTfrmUpToDate)
  114. updateLocalTfrm();
  115. return mCachedLocalTfrm;
  116. }
  117. void SceneObject::move(const Vector3& vec)
  118. {
  119. setPosition(mPosition + vec);
  120. }
  121. void SceneObject::moveRelative(const Vector3& vec)
  122. {
  123. // Transform the axes of the relative vector by camera's local axes
  124. Vector3 trans = mRotation * vec;
  125. setPosition(mPosition + trans);
  126. }
  127. void SceneObject::rotate(const Vector3& axis, const Radian& angle)
  128. {
  129. Quaternion q;
  130. q.FromAngleAxis(angle,axis);
  131. rotate(q);
  132. }
  133. void SceneObject::rotate(const Quaternion& q)
  134. {
  135. // Note the order of the mult, i.e. q comes after
  136. // Normalize the quat to avoid cumulative problems with precision
  137. Quaternion qnorm = q;
  138. qnorm.normalize();
  139. setRotation(qnorm * mRotation);
  140. }
  141. void SceneObject::roll(const Radian& angle)
  142. {
  143. // Rotate around local Z axis
  144. Vector3 zAxis = mRotation * Vector3::UNIT_Z;
  145. rotate(zAxis, angle);
  146. }
  147. void SceneObject::yaw(const Radian& angle)
  148. {
  149. Vector3 yAxis = mRotation * Vector3::UNIT_Y;
  150. rotate(yAxis, angle);
  151. }
  152. void SceneObject::pitch(const Radian& angle)
  153. {
  154. // Rotate around local X axis
  155. Vector3 xAxis = mRotation * Vector3::UNIT_X;
  156. rotate(xAxis, angle);
  157. }
  158. void SceneObject::setForward(const Vector3& forwardDir)
  159. {
  160. if (forwardDir == Vector3::ZERO)
  161. return;
  162. Vector3 nrmForwardDir = forwardDir.normalizedCopy();
  163. Vector3 currentForwardDir = getForward();
  164. const Quaternion& currentRotation = getWorldRotation();
  165. Quaternion targetRotation;
  166. if ((nrmForwardDir+currentForwardDir).squaredLength() < 0.00005f)
  167. {
  168. // Oops, a 180 degree turn (infinite possible rotation axes)
  169. // Default to yaw i.e. use current UP
  170. targetRotation = Quaternion(-currentRotation.y, -currentRotation.z, currentRotation.w, currentRotation.x);
  171. }
  172. else
  173. {
  174. // Derive shortest arc to new direction
  175. Quaternion rotQuat = currentForwardDir.getRotationTo(nrmForwardDir);
  176. targetRotation = rotQuat * currentRotation;
  177. }
  178. setRotation(targetRotation);
  179. }
  180. void SceneObject::markTfrmDirty() const
  181. {
  182. mIsCachedLocalTfrmUpToDate = false;
  183. if(mIsCachedWorldTfrmUpToDate) // If it's already marked as dirty, no point is recursing the hierarchy again
  184. {
  185. mIsCachedWorldTfrmUpToDate = false;
  186. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  187. {
  188. (*iter)->markTfrmDirty();
  189. }
  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 * (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.makeTransform(mPosition, mScale, mRotation);
  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. (*iter).destroy();
  297. mComponents.erase(iter);
  298. gSceneManager().notifyComponentRemoved((*iter));
  299. }
  300. else
  301. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  302. }
  303. RTTITypeBase* SceneObject::getRTTIStatic()
  304. {
  305. return SceneObjectRTTI::instance();
  306. }
  307. RTTITypeBase* SceneObject::getRTTI() const
  308. {
  309. return SceneObject::getRTTIStatic();
  310. }
  311. }