2
0

ModelResource.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/ResourceObject.h>
  7. #include <AnKi/Gr.h>
  8. #include <AnKi/Collision/Aabb.h>
  9. #include <AnKi/Resource/RenderingKey.h>
  10. #include <AnKi/Resource/MeshResource.h>
  11. #include <AnKi/Resource/MaterialResource.h>
  12. namespace anki
  13. {
  14. /// @addtogroup resource
  15. /// @{
  16. /// @memberof ModelResource
  17. class ModelVertexBufferBinding
  18. {
  19. public:
  20. BufferPtr m_buffer;
  21. PtrSize m_offset;
  22. PtrSize m_stride;
  23. Bool operator==(const ModelVertexBufferBinding& b) const
  24. {
  25. return m_buffer == b.m_buffer && m_offset == b.m_offset && m_stride == b.m_stride;
  26. }
  27. Bool operator!=(const ModelVertexBufferBinding& b) const
  28. {
  29. return !(*this == b);
  30. }
  31. };
  32. /// @memberof ModelResource
  33. class ModelVertexAttribute
  34. {
  35. public:
  36. VertexAttributeId m_location;
  37. Format m_format;
  38. U32 m_bufferBinding;
  39. U32 m_relativeOffset;
  40. Bool operator==(const ModelVertexAttribute& b) const
  41. {
  42. return m_bufferBinding == b.m_bufferBinding && m_format == b.m_format && m_relativeOffset == b.m_relativeOffset
  43. && m_location == b.m_location;
  44. }
  45. Bool operator!=(const ModelVertexAttribute& b) const
  46. {
  47. return !(*this == b);
  48. }
  49. };
  50. /// @memberof ModelResource
  51. /// Part of the information required render the model.
  52. class ModelRenderingInfo
  53. {
  54. public:
  55. ShaderProgramPtr m_program;
  56. Array<ModelVertexBufferBinding, MAX_VERTEX_ATTRIBUTES> m_vertexBufferBindings;
  57. U32 m_vertexBufferBindingCount;
  58. Array<ModelVertexAttribute, MAX_VERTEX_ATTRIBUTES> m_vertexAttributes;
  59. U32 m_vertexAttributeCount;
  60. BufferPtr m_indexBuffer;
  61. PtrSize m_indexBufferOffset;
  62. IndexType m_indexType;
  63. U32 m_firstIndex;
  64. U32 m_indexCount;
  65. U32 m_boneTransformsBinding;
  66. U32 m_prevFrameBoneTransformsBinding;
  67. };
  68. /// Model patch class. Its very important class and it binds a material with a few meshes (one for each LOD).
  69. class ModelPatch
  70. {
  71. friend class ModelResource;
  72. public:
  73. const MaterialResourcePtr& getMaterial() const
  74. {
  75. return m_mtl;
  76. }
  77. const MeshResourcePtr& getMesh(U32 lod) const
  78. {
  79. return m_meshes[lod];
  80. }
  81. const Aabb& getBoundingShape() const
  82. {
  83. return m_meshes[0]->getBoundingShape();
  84. }
  85. /// Get information for rendering.
  86. void getRenderingInfo(const RenderingKey& key, ModelRenderingInfo& inf) const;
  87. RayTypeBit getSupportedRayTracingTypes() const
  88. {
  89. return m_mtl->getSupportedRayTracingTypes();
  90. }
  91. private:
  92. #if ANKI_ENABLE_ASSERTIONS
  93. ModelResource* m_model = nullptr;
  94. #endif
  95. MaterialResourcePtr m_mtl;
  96. Array<MeshResourcePtr, MAX_LOD_COUNT> m_meshes; ///< Just keep the references.
  97. // Begin cached data
  98. class VertexAttributeInfo
  99. {
  100. public:
  101. U32 m_bufferBinding : 8;
  102. U32 m_relativeOffset : 24;
  103. Format m_format = Format::NONE;
  104. };
  105. Array<VertexAttributeInfo, U(VertexAttributeId::COUNT)> m_vertexAttributeInfos;
  106. class VertexBufferInfo
  107. {
  108. public:
  109. BufferPtr m_buffer;
  110. PtrSize m_stride : 16;
  111. PtrSize m_offset : 48;
  112. };
  113. Array2d<VertexBufferInfo, MAX_LOD_COUNT, U(VertexAttributeBufferId::COUNT)> m_vertexBufferInfos;
  114. class IndexBufferInfo
  115. {
  116. public:
  117. BufferPtr m_buffer;
  118. U32 m_firstIndex = MAX_U32;
  119. U32 m_indexCount = MAX_U32;
  120. };
  121. Array<IndexBufferInfo, MAX_LOD_COUNT> m_indexBufferInfos;
  122. BitSet<U(VertexAttributeId::COUNT)> m_presentVertexAttributes = {false};
  123. IndexType m_indexType : 2;
  124. // End cached data
  125. U8 m_meshLodCount : 6;
  126. ANKI_USE_RESULT Error init(ModelResource* model, ConstWeakArray<CString> meshFNames, const CString& mtlFName,
  127. U32 subMeshIndex, Bool async, ResourceManager* resources);
  128. ANKI_USE_RESULT Bool supportsSkinning() const
  129. {
  130. return m_meshes[0]->hasBoneWeights() && m_mtl->supportsSkinning();
  131. }
  132. };
  133. /// Model is an entity that acts as a container for other resources. Models are all the non static objects in a map.
  134. ///
  135. /// XML file format:
  136. /// @code
  137. /// <model>
  138. /// <modelPatches>
  139. /// <modelPatch [subMeshIndex=int]>
  140. /// <mesh>path/to/mesh.mesh</mesh>
  141. /// [<mesh1>path/to/mesh_lod_1.ankimesh</mesh1>]
  142. /// [<mesh2>path/to/mesh_lod_2.ankimesh</mesh2>]
  143. /// <material>path/to/material.ankimtl</material>
  144. /// </modelPatch>
  145. /// ...
  146. /// <modelPatch>...</modelPatch>
  147. /// </modelPatches>
  148. /// </model>
  149. /// @endcode
  150. ///
  151. /// Notes:
  152. /// - If the materials need texture coords then mesh should have them
  153. /// - If the subMeshIndex is not present then assume the whole mesh
  154. class ModelResource : public ResourceObject
  155. {
  156. public:
  157. ModelResource(ResourceManager* manager);
  158. ~ModelResource();
  159. ConstWeakArray<ModelPatch> getModelPatches() const
  160. {
  161. return m_modelPatches;
  162. }
  163. /// The volume that includes all the geometry of all model patches.
  164. const Aabb& getBoundingVolume() const
  165. {
  166. return m_boundingVolume;
  167. }
  168. Bool supportsSkinning() const
  169. {
  170. return m_skinning;
  171. }
  172. ANKI_USE_RESULT Error load(const ResourceFilename& filename, Bool async);
  173. private:
  174. DynamicArray<ModelPatch> m_modelPatches;
  175. Aabb m_boundingVolume;
  176. Bool m_skinning = false;
  177. };
  178. /// @}
  179. } // end namespace anki