CmSceneObject.cpp 9.2 KB

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