Model.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  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/Obb.h>
  9. #include <anki/resource/RenderingKey.h>
  10. #include <anki/resource/Mesh.h>
  11. #include <anki/resource/Material.h>
  12. #include <anki/resource/Skeleton.h>
  13. #include <anki/resource/Animation.h>
  14. namespace anki
  15. {
  16. // Forward
  17. class PhysicsCollisionShape;
  18. /// @addtogroup resource
  19. /// @{
  20. /// Model patch interface class. Its very important class and it binds the
  21. /// material with the mesh
  22. class ModelPatch
  23. {
  24. public:
  25. ModelPatch(Model* model);
  26. ~ModelPatch();
  27. const Material& getMaterial() const
  28. {
  29. return *m_mtl;
  30. }
  31. const Mesh& getMesh(const RenderingKey& key) const
  32. {
  33. return *m_meshes[key.m_lod];
  34. }
  35. const Model& getModel() const
  36. {
  37. ANKI_ASSERT(m_model);
  38. return *m_model;
  39. }
  40. const Obb& getBoundingShape() const
  41. {
  42. return m_meshes[0]->getBoundingShape();
  43. }
  44. const Obb& getBoundingShapeSub(U32 subMeshId) const
  45. {
  46. return m_meshes[0]->getBoundingShapeSub(subMeshId);
  47. }
  48. U32 getSubMeshesCount() const
  49. {
  50. return m_meshes[0]->getSubMeshesCount();
  51. }
  52. ANKI_USE_RESULT Error create(SArray<CString> meshFNames,
  53. const CString& mtlFName,
  54. ResourceManager* resources);
  55. /// Get information for multiDraw rendering.
  56. /// Given an array of submeshes that are visible return the correct indices
  57. /// offsets and counts.
  58. void getRenderingDataSub(const RenderingKey& key,
  59. SArray<U8> subMeshIndicesArray,
  60. ResourceGroupPtr& resourceGroup,
  61. PipelinePtr& ppline,
  62. Array<U32, ANKI_GL_MAX_SUB_DRAWCALLS>& indicesCountArray,
  63. Array<PtrSize, ANKI_GL_MAX_SUB_DRAWCALLS>& indicesOffsetArray,
  64. U32& drawcallCount) const;
  65. private:
  66. Model* m_model ANKI_DBG_NULLIFY_PTR;
  67. Array<MeshResourcePtr, MAX_LODS> m_meshes; ///< One for each LOD
  68. U8 m_meshCount = 0;
  69. MaterialResourcePtr m_mtl;
  70. mutable Array4d<PipelinePtr,
  71. U(Pass::COUNT),
  72. MAX_LODS,
  73. 2,
  74. MAX_INSTANCE_GROUPS>
  75. m_pplines;
  76. mutable SpinLock m_lock; ///< Protect m_pplines
  77. Array<ResourceGroupPtr, MAX_LODS> m_grResources;
  78. /// Return the maximum number of LODs
  79. U getLodCount() const;
  80. PipelinePtr getPipeline(const RenderingKey& key) const;
  81. void computePipelineInitInfo(
  82. const RenderingKey& key, PipelineInitInfo& pinit) const;
  83. };
  84. /// Model is an entity that acts as a container for other resources. Models are
  85. /// all the non static objects in a map.
  86. ///
  87. /// XML file format:
  88. /// @code
  89. /// <model>
  90. /// <modelPatches>
  91. /// <modelPatch>
  92. /// <mesh>path/to/mesh.mesh</mesh>
  93. /// [<mesh1>path/to/mesh_lod_1.mesh</mesh1>]
  94. /// [<mesh2>path/to/mesh_lod_2.mesh</mesh2>]
  95. /// <material>path/to/material.mtl</material>
  96. /// </modelPatch>
  97. /// ...
  98. /// <modelPatch>...</modelPatch>
  99. /// </modelPatches>
  100. /// [<skeleton>path/to/skeleton.skel</skeleton>]
  101. /// [<skeletonAnimations>
  102. /// <animation>path/to/animation.anim</animation>
  103. /// ...
  104. /// </skeletonAnimations>]
  105. /// </model>
  106. /// @endcode
  107. ///
  108. /// Requirements:
  109. /// - If the materials need texture coords then mesh should have them
  110. /// - The skeleton and skelAnims are optional
  111. /// - Its an error to have skelAnims without skeleton
  112. class Model : public ResourceObject
  113. {
  114. public:
  115. Model(ResourceManager* manager);
  116. ~Model();
  117. const DArray<ModelPatch*>& getModelPatches() const
  118. {
  119. return m_modelPatches;
  120. }
  121. const Obb& getVisibilityShape() const
  122. {
  123. return m_visibilityShape;
  124. }
  125. ANKI_USE_RESULT Error load(const ResourceFilename& filename);
  126. private:
  127. DArray<ModelPatch*> m_modelPatches;
  128. Obb m_visibilityShape;
  129. SkeletonResourcePtr m_skeleton;
  130. DArray<AnimationResourcePtr> m_animations;
  131. };
  132. /// @}
  133. } // end namespace anki