ModelNode.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef ANKI_SCENE_MODEL_NODE_H
  2. #define ANKI_SCENE_MODEL_NODE_H
  3. #include "anki/scene/SceneNode.h"
  4. #include "anki/scene/RenderComponent.h"
  5. #include "anki/scene/MoveComponent.h"
  6. #include "anki/scene/SpatialComponent.h"
  7. #include "anki/resource/Resource.h"
  8. #include "anki/resource/Model.h"
  9. #include "anki/collision/Obb.h"
  10. namespace anki {
  11. // Forward
  12. class ModelPatchNode;
  13. /// @addtogroup Scene
  14. /// @{
  15. /// A model instance
  16. class ModelPatchNodeInstance: public SceneNode, public MoveComponent,
  17. public SpatialComponent
  18. {
  19. friend class ModelPatchNode;
  20. friend class ModelNode;
  21. public:
  22. ModelPatchNodeInstance(
  23. const char* name, SceneGraph* scene, // SceneNode
  24. ModelPatchNode* modelPatchNode); // Self
  25. /// @name MoveComponent virtuals
  26. /// @{
  27. /// Overrides MoveComponent::moveUpdate(). This does:
  28. /// - Update the collision shape
  29. /// - If it's the last instance update the parent's CS.
  30. void moveUpdate();
  31. /// @}
  32. private:
  33. Obb obb; ///< In world space
  34. ModelPatchNode* modelPatchNode; ///< Keep the father here
  35. };
  36. /// A fragment of the ModelNode
  37. class ModelPatchNode: public SceneNode, public MoveComponent,
  38. public RenderComponent, public SpatialComponent
  39. {
  40. friend class ModelPatchNodeInstance;
  41. friend class ModelNode;
  42. public:
  43. /// @name Constructors/Destructor
  44. /// @{
  45. ModelPatchNode(
  46. const char* name, SceneGraph* scene, // Scene
  47. const ModelPatchBase* modelPatch, U instances); // Self
  48. ~ModelPatchNode();
  49. /// @}
  50. /// @name MoveComponent virtuals
  51. /// @{
  52. /// Overrides MoveComponent::moveUpdate(). This does:
  53. /// - Update the collision shape
  54. void moveUpdate();
  55. /// @}
  56. /// @name RenderComponent virtuals
  57. /// @{
  58. /// Implements RenderComponent::getRenderingData
  59. void getRenderingData(
  60. const PassLodKey& key,
  61. const Vao*& vao, const ShaderProgram*& prog,
  62. const U32* subMeshIndicesArray, U subMeshIndicesCount,
  63. Array<U32, ANKI_MAX_MULTIDRAW_PRIMITIVES>& indicesCountArray,
  64. Array<const void*, ANKI_MAX_MULTIDRAW_PRIMITIVES>& indicesOffsetArray,
  65. U32& drawcallCount) const
  66. {
  67. modelPatch->getRenderingDataSub(key, vao, prog,
  68. subMeshIndicesArray, subMeshIndicesCount,
  69. indicesCountArray, indicesOffsetArray, drawcallCount);
  70. }
  71. /// Implements RenderComponent::getMaterial
  72. const Material& getMaterial()
  73. {
  74. return modelPatch->getMaterial();
  75. }
  76. /// Overrides RenderComponent::getRenderComponentWorldTransforms
  77. const Transform* getRenderWorldTransforms();
  78. /// Overrides RenderComponent::getRenderComponentInstancesCount
  79. U32 getRenderInstancesCount()
  80. {
  81. // return this and the instances
  82. return (transforms.size() > 0) ? transforms.size() : 1;
  83. }
  84. /// @}
  85. private:
  86. Obb obb; ///< In world space.
  87. const ModelPatchBase* modelPatch; ///< The resource
  88. SceneVector<ModelPatchNodeInstance*> instances;
  89. SceneVector<Transform> transforms;
  90. /// This is called by the last of the instances on it's moveUpdate()
  91. void updateSpatialCs();
  92. };
  93. /// The model scene node
  94. class ModelNode: public SceneNode, public MoveComponent
  95. {
  96. public:
  97. typedef SceneVector<ModelPatchNode*> ModelPatchNodes;
  98. /// @name Constructors/Destructor
  99. /// @{
  100. ModelNode(
  101. const char* name, SceneGraph* scene, // SceneNode
  102. const char* modelFname, U instances = 1); // Self
  103. virtual ~ModelNode();
  104. /// @}
  105. /// @name Accessors
  106. /// @{
  107. const Model& getModel() const
  108. {
  109. return *model;
  110. }
  111. /// @}
  112. /// Set the local transform of one instance
  113. void setInstanceLocalTransform(U instanceIndex, const Transform& trf);
  114. private:
  115. ModelResourcePointer model; ///< The resource
  116. ModelPatchNodes patches;
  117. };
  118. /// @}
  119. } // end namespace anki
  120. #endif