Model.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_RESOURCE_MODEL_H
  6. #define ANKI_RESOURCE_MODEL_H
  7. #include "anki/resource/ResourceManager.h"
  8. #include "anki/Gl.h"
  9. #include "anki/collision/Obb.h"
  10. #include "anki/resource/RenderingKey.h"
  11. #include "anki/resource/Mesh.h"
  12. #include "anki/resource/Material.h"
  13. #include "anki/resource/Skeleton.h"
  14. #include "anki/resource/Animation.h"
  15. namespace anki {
  16. // Forward
  17. class PhysicsCollisionShape;
  18. /// Model patch interface class. Its very important class and it binds the
  19. /// material with the mesh
  20. class ModelPatchBase
  21. {
  22. public:
  23. ModelPatchBase(ResourceAllocator<U8> alloc)
  24. : m_alloc(alloc)
  25. {}
  26. virtual ~ModelPatchBase();
  27. const Material& getMaterial() const
  28. {
  29. ANKI_ASSERT(m_mtl);
  30. return *m_mtl;
  31. }
  32. const Mesh& getMesh(const RenderingKey& key) const
  33. {
  34. return *m_meshes[key.m_lod];
  35. }
  36. U32 getMeshesCount() const
  37. {
  38. return m_meshes.getSize();
  39. }
  40. const Obb& getBoundingShape() const
  41. {
  42. RenderingKey key(Pass::COLOR, 0, false);
  43. return getMesh(key).getBoundingShape();
  44. }
  45. const Obb& getBoundingShapeSub(U32 subMeshId) const
  46. {
  47. RenderingKey key(Pass::COLOR, 0, false);
  48. return getMesh(key).getBoundingShapeSub(subMeshId);
  49. }
  50. U32 getSubMeshesCount() const
  51. {
  52. RenderingKey key(Pass::COLOR, 0, false);
  53. return getMesh(key).getSubMeshesCount();
  54. }
  55. /// Get information for multiDraw rendering.
  56. /// Given an array of submeshes that are visible return the correct indices
  57. /// offsets and counts
  58. ANKI_USE_RESULT Error getRenderingDataSub(
  59. const RenderingKey& key,
  60. GlCommandBufferHandle& vertJobs,
  61. GlPipelineHandle& ppline,
  62. const U8* subMeshIndicesArray,
  63. U32 subMeshIndicesCount,
  64. Array<U32, ANKI_GL_MAX_SUB_DRAWCALLS>& indicesCountArray,
  65. Array<PtrSize, ANKI_GL_MAX_SUB_DRAWCALLS>& indicesOffsetArray,
  66. U32& drawcallCount) const;
  67. protected:
  68. ResourceAllocator<U8> m_alloc;
  69. /// Array [lod][pass]
  70. ResourceDArray<GlCommandBufferHandle> m_vertJobs;
  71. Material* m_mtl = nullptr;
  72. ResourceDArray<Mesh*> m_meshes; ///< One for each LOD
  73. /// Create vertex descriptors using a material and a mesh
  74. ANKI_USE_RESULT Error create(GlDevice* gl);
  75. private:
  76. /// Called by @a create multiple times to create and populate a single
  77. /// vertex descriptor
  78. static ANKI_USE_RESULT Error createVertexDesc(
  79. const GlShaderHandle& prog,
  80. const Mesh& mesh,
  81. GlCommandBufferHandle& vertexJobs);
  82. /// Return the maximum number of LODs
  83. U getLodsCount() const;
  84. U getVertexDescIdx(const RenderingKey& key) const;
  85. };
  86. /// Its a chunk of a model. Its very important class and it binds the material
  87. /// with the mesh
  88. template<typename MeshResourcePointerType>
  89. class ModelPatch: public ModelPatchBase
  90. {
  91. public:
  92. using Base = ModelPatchBase;
  93. /// Accepts a number of mesh filenames, one for each LOD
  94. ModelPatch(ResourceAllocator<U8> alloc)
  95. : ModelPatchBase(alloc)
  96. {}
  97. ~ModelPatch()
  98. {
  99. m_meshResources.destroy(ModelPatchBase::m_alloc);
  100. }
  101. ANKI_USE_RESULT Error create(
  102. CString meshFNames[],
  103. U32 meshesCount,
  104. const CString& mtlFName,
  105. ResourceManager* resources);
  106. private:
  107. ResourceDArray<MeshResourcePointerType> m_meshResources; ///< Geometries
  108. MaterialResourcePointer m_mtlResource; ///< Material
  109. };
  110. /// Model is an entity that acts as a container for other resources. Models are
  111. /// all the non static objects in a map.
  112. ///
  113. /// XML file format:
  114. /// @code
  115. /// <model>
  116. /// <modelPatches>
  117. /// <modelPatch>
  118. /// [<mesh>path/to/mesh.mesh</mesh>
  119. /// [<mesh1>path/to/mesh_lod_1.mesh</mesh1>]
  120. /// [<mesh2>path/to/mesh_lod_2.mesh</mesh2>]] |
  121. /// [<bucketMesh>path/to/mesh.bmesh</bucketMesh>
  122. /// [<bucketMesh1>path/to/mesh_lod_1.bmesh</bucketMesh1>]
  123. /// [<bucketMesh2>path/to/mesh_lod_2.bmesh</bucketMesh2>]]
  124. /// <material>path/to/material.mtl</material>
  125. /// </modelPatch>
  126. /// ...
  127. /// <modelPatch>...</modelPatch>
  128. /// </modelPatches>
  129. /// [<skeleton>path/to/skeleton.skel</skeleton>]
  130. /// [<skeletonAnimations>
  131. /// <animation>path/to/animation.anim</animation>
  132. /// ...
  133. /// </skeletonAnimations>]
  134. /// [<collisionShape>
  135. /// <type>sphere | box | mesh</type>
  136. /// <value>...</value>
  137. /// </collisionShape>]
  138. /// </model>
  139. /// @endcode
  140. ///
  141. /// Requirements:
  142. /// - If the materials need texture coords then mesh should have them
  143. /// - The skeleton and skelAnims are optional
  144. /// - Its an error to have skelAnims without skeleton
  145. class Model
  146. {
  147. public:
  148. Model(ResourceAllocator<U8>& alloc);
  149. ~Model();
  150. const ResourceDArray<ModelPatchBase*>& getModelPatches() const
  151. {
  152. return m_modelPatches;
  153. }
  154. const Obb& getVisibilityShape() const
  155. {
  156. return m_visibilityShape;
  157. }
  158. const PhysicsCollisionShape* getPhysicsCollisionShape() const
  159. {
  160. return m_physicsShape;
  161. }
  162. ANKI_USE_RESULT Error load(
  163. const CString& filename, ResourceInitializer& init);
  164. private:
  165. ResourceManager* m_resources = nullptr;
  166. ResourceDArray<ModelPatchBase*> m_modelPatches;
  167. Obb m_visibilityShape;
  168. SkeletonResourcePointer m_skeleton;
  169. ResourceDArray<AnimationResourcePointer> m_animations;
  170. PhysicsCollisionShape* m_physicsShape = nullptr;
  171. };
  172. } // end namespace anki
  173. #endif