T3DSceneComponent.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3DSceneComponent.h"
  23. #include "T3DSceneClient.h"
  24. void T3DSceneComponent::setSceneGroup(const char * sceneGroup)
  25. {
  26. AssertFatal(getOwner()==NULL, "Changing scene group name after registration will have no effect.");
  27. if (sceneGroup == NULL)
  28. sceneGroup = StringTable->insert("");
  29. _sceneGroup = StringTable->insert(sceneGroup);
  30. }
  31. void T3DSceneComponent::setParentTransformName(const char * name)
  32. {
  33. _parentTransformName = StringTable->insert(name);
  34. if (getOwner() != NULL)
  35. {
  36. Transform3D * old = _transform->getParentTransform();
  37. ValueWrapperInterface<Transform3D*> * iface = NULL;
  38. if (_parentTransformName != NULL)
  39. iface = getInterface<ValueWrapperInterface<Transform3D*> >("transform3D", _parentTransformName);
  40. _transform->setParentTransform(iface == NULL ? NULL : iface->get());
  41. if (_transform->getParentTransform() != old)
  42. setDirtyWorldBox(true);
  43. }
  44. }
  45. void T3DSceneComponent::setObjectType(U32 objectTypeMask)
  46. {
  47. _objectType = objectTypeMask;
  48. setUseOwnerObjectType(true);
  49. }
  50. void T3DSceneComponent::setDirtyObjectBox(bool val)
  51. {
  52. _SetFlag(T3DSceneComponent::DirtyObjectBox, val);
  53. if (val && !isObjectBoxLocked())
  54. _ComputeObjectBox();
  55. }
  56. void T3DSceneComponent::setDirtyWorldBox(bool val)
  57. {
  58. _SetFlag(T3DSceneComponent::DirtyWorldBox, val);
  59. if (val && !isWorldBoxLocked())
  60. _UpdateWorldBox();
  61. }
  62. void T3DSceneComponent::setObjectBoxLocked(bool val)
  63. {
  64. _SetFlag(T3DSceneComponent::LockObjectBox, val);
  65. if (!val && isDirtyObjectBox())
  66. _ComputeObjectBox();
  67. }
  68. void T3DSceneComponent::setWorldBoxLocked(bool val)
  69. {
  70. _SetFlag(T3DSceneComponent::LockWorldBox, val);
  71. if (!val && isDirtyWorldBox())
  72. _UpdateWorldBox();
  73. }
  74. void T3DSceneComponent::setPosition(const Point3F & pos)
  75. {
  76. _transform->setPosition(pos);
  77. setDirtyWorldBox(true);
  78. }
  79. void T3DSceneComponent::setRotation(const QuatF & rotation)
  80. {
  81. _transform->setRotation(rotation);
  82. setDirtyWorldBox(true);
  83. }
  84. void T3DSceneComponent::setScale(const Point3F & scale)
  85. {
  86. _transform->setScale (scale);
  87. Parent::setScale(scale);
  88. setDirtyWorldBox(true);
  89. }
  90. void T3DSceneComponent::setTransform3D(Transform3D * transform)
  91. {
  92. if (transform == NULL)
  93. // never let it be null
  94. return;
  95. if (_transform != NULL)
  96. delete _transform;
  97. _transform = transform;
  98. _transform->setDirtyListener(this);
  99. }
  100. void T3DSceneComponent::setTransform(const MatrixF & mat)
  101. {
  102. _transform->setLocalMatrix(mat);
  103. Parent::setTransform(mat);
  104. setDirtyWorldBox(true);
  105. }
  106. Box3F T3DSceneComponent::getObjectBox()
  107. {
  108. if (DirtyObjectBox && !LockObjectBox)
  109. _ComputeObjectBox();
  110. return _objectBox->get();
  111. }
  112. Box3F T3DSceneComponent::getWorldBox()
  113. {
  114. if (DirtyWorldBox && !LockWorldBox)
  115. _UpdateWorldBox();
  116. MatrixF mat = getTransform3D()->getWorldMatrix();
  117. Box3F box = getObjectBox();
  118. mat.mul(box);
  119. return box;
  120. }
  121. void T3DSceneComponent::Render()
  122. {
  123. if (_sceneClientList == NULL)
  124. return;
  125. PROFILE_SCOPE(T3DSceneComponent_Render);
  126. GFX->multWorld(_transform->getWorldMatrix());
  127. if (doRenderObjectBounds())
  128. {
  129. // TODO
  130. }
  131. if (doRenderWorldBounds())
  132. {
  133. // TODO
  134. }
  135. for (T3DSceneClient * walk = _sceneClientList; walk != NULL; walk = walk->getNextSceneClient())
  136. {
  137. IRenderable3D * render = dynamic_cast<IRenderable3D*>(walk);
  138. if (render != NULL)
  139. render->Render();
  140. }
  141. GFX->popWorldMatrix();
  142. }
  143. void T3DSceneComponent::AddSceneClient(T3DSceneClient * sceneClient)
  144. {
  145. AssertFatal(sceneClient,"Cannot add a NULL scene client");
  146. // add to the front of the list
  147. sceneClient->setNextSceneClient(_sceneClientList);
  148. _sceneClientList = sceneClient;
  149. // extend object box
  150. ISolid3D * solid = dynamic_cast<ISolid3D*>(sceneClient);
  151. if (solid != NULL)
  152. {
  153. if (isObjectBoxLocked())
  154. setDirtyObjectBox(true);
  155. else
  156. {
  157. Box3F box = solid->getObjectBox();
  158. if (solid->getTransform3D() != NULL)
  159. {
  160. MatrixF mat;
  161. solid->getTransform3D()->getObjectMatrix(mat, true);
  162. mat.mul(box);
  163. }
  164. box.extend(_objectBox->get().min);
  165. box.extend(_objectBox->get().max);
  166. _objectBox->set(box);
  167. }
  168. // policy is that we become parent transform
  169. if (solid->getTransform3D() != NULL && solid->getTransform3D()->isChildOf(_transform, true))
  170. solid->getTransform3D()->setParentTransform(_transform);
  171. }
  172. }
  173. void T3DSceneComponent::RemoveClientObject(T3DSceneClient * sceneClient)
  174. {
  175. AssertFatal(sceneClient != NULL, "Removing null scene client");
  176. if (sceneClient == NULL)
  177. return;
  178. if (_sceneClientList == sceneClient)
  179. _sceneClientList = _sceneClientList->getNextSceneClient();
  180. else
  181. {
  182. T3DSceneClient * walk = _sceneClientList;
  183. while (walk->getNextSceneClient() != sceneClient && walk != NULL)
  184. walk = walk->getNextSceneClient();
  185. if (walk != NULL)
  186. walk->setNextSceneClient(sceneClient->getNextSceneClient());
  187. }
  188. if (dynamic_cast<ISolid3D*>(sceneClient))
  189. setDirtyObjectBox(true);
  190. }
  191. bool T3DSceneComponent::onComponentRegister(SimComponent * owner)
  192. {
  193. if (!Parent::onComponentRegister(owner) || !registerObject())
  194. return false;
  195. // Note: was added to scene graph in register object
  196. setTransform3D(_transform);
  197. setParentTransformName(_parentTransformName);
  198. return true;
  199. }
  200. void T3DSceneComponent::onComponentUnRegister()
  201. {
  202. _sceneClientList = NULL;
  203. Parent::onComponentUnRegister();
  204. }
  205. void T3DSceneComponent::registerInterfaces(SimComponent * owner)
  206. {
  207. Parent::registerInterfaces(owner);
  208. // TODO: need to figure out the best way to wrap these
  209. // we don't need to track these interfaces ourselves -- just create them here
  210. ComponentInterface * sceneComponent = new ComponentInterface();
  211. ValueWrapperInterface<Transform3D*> * transform = new ValueWrapperInterface<Transform3D*>(_transform);
  212. registerCachedInterface("sceneComponent", _sceneGroup, owner, sceneComponent);
  213. registerCachedInterface("transform3D", _sceneGroup, owner, transform);
  214. registerCachedInterface("box", _sceneGroup, this, _objectBox);
  215. }
  216. void T3DSceneComponent::_ComputeObjectBox()
  217. {
  218. _objectBox->set(Box3F(10E30f, 10E30f, 10E30f, -10E30f, -10E30f, -10E30f));
  219. bool gotone = false;
  220. for (T3DSceneClient * walk = _sceneClientList; walk != NULL; walk = walk->getNextSceneClient())
  221. {
  222. ISolid3D * solid = dynamic_cast<ISolid3D*>(walk);
  223. if (solid == NULL)
  224. continue;
  225. Box3F box = solid->getObjectBox();
  226. if (solid->getTransform3D() != NULL)
  227. {
  228. MatrixF mat;
  229. solid->getTransform3D()->getObjectMatrix(mat, true);
  230. mat.mul(box);
  231. }
  232. box.extend(_objectBox->get().min);
  233. box.extend(_objectBox->get().max);
  234. _objectBox->set(box);
  235. gotone = true;
  236. }
  237. if (!gotone)
  238. _objectBox->set(Box3F());
  239. setDirtyObjectBox(false);
  240. setDirtyWorldBox(true);
  241. }
  242. void T3DSceneComponent::_UpdateWorldBox()
  243. {
  244. setDirtyWorldBox(false);
  245. // TODO:
  246. // T3DSceneGraph.Instance.UpdateObject(this);
  247. }