ModelNode.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "anki/scene/ModelNode.h"
  2. #include "anki/resource/Model.h"
  3. #include "anki/resource/Skeleton.h"
  4. namespace anki {
  5. //==============================================================================
  6. // ModelPatchNodeInstance =
  7. //==============================================================================
  8. //==============================================================================
  9. ModelPatchNodeInstance::ModelPatchNodeInstance(
  10. const char* name, SceneGraph* scene, SceneNode* parent, // Scene
  11. U32 movableFlags, // Movable
  12. const ModelPatchBase* modelPatchResource) // Self
  13. : SceneNode(name, scene, parent),
  14. Movable(movableFlags, this),
  15. Spatial(&obb, getSceneAllocator()),
  16. modelPatch(modelPatchResource)
  17. {
  18. sceneNodeProtected.movable = this;
  19. // Dont mark it as spatial because it's sub-spatial and don't want to
  20. // be updated by the scene
  21. sceneNodeProtected.spatial = nullptr;
  22. ANKI_ASSERT(modelPatch);
  23. }
  24. //==============================================================================
  25. void ModelPatchNodeInstance::movableUpdate()
  26. {
  27. ANKI_ASSERT(modelPatch);
  28. // Update the obb of self
  29. obb = modelPatch->getBoundingShape().getTransformed(
  30. getWorldTransform());
  31. spatialMarkForUpdate();
  32. // If this instance is the last update the parent's collision shape
  33. SceneNode* parentNode = getParent();
  34. ANKI_ASSERT(parentNode);
  35. ModelPatchNode* modelPatchNode =
  36. #if ANKI_DEBUG
  37. dynamic_cast<ModelPatchNode*>(parentNode);
  38. ANKI_ASSERT(modelPatchNode);
  39. #else
  40. static_cast<ModelPatchNode*>(parentNode);
  41. #endif
  42. ANKI_ASSERT(modelPatchNode->instances.size() > 0);
  43. if(this == modelPatchNode->instances.back())
  44. {
  45. modelPatchNode->updateSpatialCs();
  46. }
  47. }
  48. //==============================================================================
  49. // ModelPatchNode =
  50. //==============================================================================
  51. //==============================================================================
  52. ModelPatchNode::ModelPatchNode(
  53. const char* name, SceneGraph* scene, SceneNode* parent,
  54. U32 movableFlags,
  55. const ModelPatchBase* modelPatch_, U instancesCount)
  56. : SceneNode(name, scene, parent),
  57. Movable(movableFlags, this),
  58. Renderable(getSceneAllocator()),
  59. Spatial(&obb, getSceneAllocator()),
  60. modelPatch(modelPatch_),
  61. instances(getSceneAllocator()),
  62. transforms(getSceneAllocator())
  63. {
  64. sceneNodeProtected.movable = this;
  65. sceneNodeProtected.renderable = this;
  66. sceneNodeProtected.spatial = this;
  67. Renderable::init(*this);
  68. // Create the instances as ModelPatchNodeInstance
  69. if(instancesCount > 1)
  70. {
  71. spatialProtected.subSpatials.resize(instancesCount, nullptr);
  72. instances.resize(instancesCount, nullptr);
  73. transforms.resize(instancesCount, Transform::getIdentity());
  74. Vec3 pos = Vec3(0.0);
  75. for(U i = 0; i < instancesCount; i++)
  76. {
  77. ModelPatchNodeInstance* instance = ANKI_NEW(
  78. ModelPatchNodeInstance, getSceneAllocator(),
  79. nullptr, scene, this, Movable::MF_NONE,
  80. modelPatch);
  81. instance->setLocalOrigin(pos);
  82. pos.x() += 2.0;
  83. spatialProtected.subSpatials[i] = instance;
  84. instances[i] = instance;
  85. }
  86. }
  87. }
  88. //==============================================================================
  89. ModelPatchNode::~ModelPatchNode()
  90. {
  91. for(ModelPatchNodeInstance* instance : instances)
  92. {
  93. ANKI_DELETE(instance, getSceneAllocator());
  94. }
  95. }
  96. //==============================================================================
  97. const Transform* ModelPatchNode::getRenderableWorldTransforms()
  98. {
  99. if(transforms.size() == 0)
  100. {
  101. // NO instancing
  102. return &getWorldTransform();
  103. }
  104. else
  105. {
  106. // Instancing
  107. ANKI_ASSERT(transforms.size() == instances.size());
  108. // Set the transforms
  109. for(U i = 0; i < instances.size(); i++)
  110. {
  111. transforms[i] = instances[i]->getWorldTransform();
  112. }
  113. return &transforms[0];
  114. }
  115. }
  116. //==============================================================================
  117. void ModelPatchNode::movableUpdate()
  118. {
  119. Movable::movableUpdate();
  120. if(instances.size() == 0)
  121. {
  122. // NO instancing
  123. obb = modelPatch->getBoundingShape().getTransformed(
  124. getWorldTransform());
  125. spatialMarkForUpdate();
  126. }
  127. else
  128. {
  129. // Instancing
  130. // Do nothing. You cannot update the obb because the instances have
  131. // not been updated their CSs yet. The update will be triggered by the
  132. // last instance.
  133. }
  134. }
  135. //==============================================================================
  136. void ModelPatchNode::updateSpatialCs()
  137. {
  138. ANKI_ASSERT(instances.size() > 0);
  139. obb = instances[0]->obb;
  140. for(U i = 1; i < instances.size(); i++)
  141. {
  142. ANKI_ASSERT(instances[i]);
  143. obb = obb.getCompoundShape(instances[i]->obb);
  144. }
  145. spatialMarkForUpdate();
  146. }
  147. //==============================================================================
  148. // ModelNode =
  149. //==============================================================================
  150. //==============================================================================
  151. ModelNode::ModelNode(
  152. const char* name, SceneGraph* scene, SceneNode* parent,
  153. U32 movableFlags,
  154. const char* modelFname, U instances)
  155. : SceneNode(name, scene, parent),
  156. Movable(movableFlags, this),
  157. patches(getSceneAllocator())
  158. {
  159. sceneNodeProtected.movable = this;
  160. model.load(modelFname);
  161. patches.reserve(model->getModelPatches().size());
  162. U i = 0;
  163. for(const ModelPatchBase* patch : model->getModelPatches())
  164. {
  165. std::string name_ = name + std::to_string(i);
  166. ModelPatchNode* mpn = ANKI_NEW(ModelPatchNode, getSceneAllocator(),
  167. name_.c_str(), scene, this,
  168. Movable::MF_IGNORE_LOCAL_TRANSFORM, patch, instances);
  169. patches.push_back(mpn);
  170. ++i;
  171. }
  172. }
  173. //==============================================================================
  174. ModelNode::~ModelNode()
  175. {
  176. for(ModelPatchNode* patch : patches)
  177. {
  178. ANKI_DELETE(patch, getSceneAllocator());
  179. }
  180. }
  181. } // end namespace anki