CmSceneObject.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. mIsCachedWorldTfrmUpToDate = false;
  185. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  186. {
  187. (*iter)->markTfrmDirty();
  188. }
  189. }
  190. void SceneObject::updateWorldTfrm() const
  191. {
  192. if(mParent != nullptr)
  193. {
  194. mCachedWorldTfrm = getLocalTfrm() * mParent->getWorldTfrm();
  195. // Update orientation
  196. const Quaternion& parentOrientation = mParent->getWorldRotation();
  197. mWorldRotation = parentOrientation * mRotation;
  198. // Update scale
  199. const Vector3& parentScale = mParent->getWorldScale();
  200. // Scale own position by parent scale, NB just combine
  201. // as equivalent axes, no shearing
  202. mWorldScale = parentScale * mScale;
  203. // Change position vector based on parent's orientation & scale
  204. mWorldPosition = parentOrientation * (parentScale * mPosition);
  205. // Add altered position vector to parents
  206. mWorldPosition += mParent->getWorldPosition();
  207. }
  208. else
  209. {
  210. mCachedWorldTfrm = getLocalTfrm();
  211. mWorldRotation = mRotation;
  212. mWorldPosition = mPosition;
  213. mWorldScale = mScale;
  214. }
  215. mIsCachedWorldTfrmUpToDate = true;
  216. }
  217. void SceneObject::updateLocalTfrm() const
  218. {
  219. mCachedLocalTfrm.makeTransform(mPosition, mScale, mRotation);
  220. mIsCachedLocalTfrmUpToDate = true;
  221. }
  222. /************************************************************************/
  223. /* Hierarchy */
  224. /************************************************************************/
  225. void SceneObject::setParent(const HSceneObject& parent)
  226. {
  227. if(parent.isDestroyed())
  228. {
  229. CM_EXCEPT(InternalErrorException,
  230. "Trying to assign a SceneObject parent that is destroyed.");
  231. }
  232. if(mParent == nullptr || mParent != parent)
  233. {
  234. if(mParent != nullptr)
  235. mParent->removeChild(mThisHandle);
  236. if(parent != nullptr)
  237. parent->addChild(mThisHandle);
  238. mParent = parent;
  239. markTfrmDirty();
  240. }
  241. }
  242. HSceneObject SceneObject::getChild(unsigned int idx) const
  243. {
  244. if(idx < 0 || idx >= mChildren.size())
  245. {
  246. CM_EXCEPT(InternalErrorException,
  247. "Child index out of range.");
  248. }
  249. return mChildren[idx];
  250. }
  251. int SceneObject::indexOfChild(const HSceneObject& child) const
  252. {
  253. for(int i = 0; i < (int)mChildren.size(); i++)
  254. {
  255. if(mChildren[i] == child)
  256. return i;
  257. }
  258. return -1;
  259. }
  260. void SceneObject::addChild(const HSceneObject& object)
  261. {
  262. mChildren.push_back(object);
  263. }
  264. void SceneObject::removeChild(const HSceneObject& object)
  265. {
  266. auto result = find(mChildren.begin(), mChildren.end(), object);
  267. if(result != mChildren.end())
  268. mChildren.erase(result);
  269. else
  270. {
  271. CM_EXCEPT(InternalErrorException,
  272. "Trying to remove a child but it's not a child of the transform.");
  273. }
  274. }
  275. HComponent SceneObject::getComponent(UINT32 typeId) const
  276. {
  277. for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  278. {
  279. if((*iter)->getRTTI()->getRTTIId() == typeId)
  280. return *iter;
  281. }
  282. return HComponent();
  283. }
  284. void SceneObject::destroyComponent(const HComponent& component)
  285. {
  286. if(component == nullptr)
  287. {
  288. LOGDBG("Trying to remove a null component");
  289. return;
  290. }
  291. auto iter = std::find(mComponents.begin(), mComponents.end(), component);
  292. if(iter != mComponents.end())
  293. {
  294. (*iter).destroy();
  295. mComponents.erase(iter);
  296. gSceneManager().notifyComponentRemoved((*iter));
  297. }
  298. else
  299. LOGDBG("Trying to remove a component that doesn't exist on this SceneObject.");
  300. }
  301. RTTITypeBase* SceneObject::getRTTIStatic()
  302. {
  303. return SceneObjectRTTI::instance();
  304. }
  305. RTTITypeBase* SceneObject::getRTTI() const
  306. {
  307. return SceneObject::getRTTIStatic();
  308. }
  309. }