Model.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef ANKI_RESOURCE_MODEL_H
  2. #define ANKI_RESOURCE_MODEL_H
  3. #include "anki/resource/Resource.h"
  4. #include "anki/gl/Vao.h"
  5. #include "anki/collision/Obb.h"
  6. #include "anki/resource/PassLevelKey.h"
  7. #include "anki/resource/Mesh.h"
  8. #include "anki/util/Vector.h"
  9. namespace anki {
  10. /// Model patch interface class. Its very important class and it binds the
  11. /// material with the mesh
  12. class ModelPatchBase
  13. {
  14. public:
  15. /// VAOs container
  16. typedef Vector<Vao> VaosContainer;
  17. /// Map to get the VAO given a PassLod key
  18. typedef PassLevelHashMap<Vao*> PassLevelToVaoMap;
  19. virtual ~ModelPatchBase()
  20. {}
  21. virtual const MeshBase& getMeshBase() const = 0;
  22. virtual const Material& getMaterial() const = 0;
  23. const Vao& getVao(const PassLevelKey& key) const
  24. {
  25. PassLevelToVaoMap::const_iterator it = vaosMap.find(key);
  26. ANKI_ASSERT(it != vaosMap.end());
  27. return *(it->second);
  28. }
  29. /// Allias to MeshBase::getIndicesCount()
  30. U32 getIndecesCount(const U32 lod) const
  31. {
  32. return getMeshBase().getIndicesCount(lod);
  33. }
  34. protected:
  35. VaosContainer vaos;
  36. PassLevelToVaoMap vaosMap;
  37. void create()
  38. {
  39. createVaos(getMaterial(), getMeshBase(), vaos, vaosMap);
  40. }
  41. /// Create VAOs using a material and a mesh. It writes a VaosContainer and
  42. /// a hash map
  43. static void createVaos(const Material& mtl,
  44. const MeshBase& mesh,
  45. VaosContainer& vaos,
  46. PassLevelToVaoMap& vaosMap);
  47. private:
  48. /// Called by @a createVaos multiple times to create and populate a single
  49. /// VAO
  50. static void createVao(const Material& mtl,
  51. const MeshBase& mesh,
  52. const PassLevelKey& key,
  53. Vao& vao);
  54. };
  55. /// Its a chunk of a model. Its very important class and it binds the material
  56. /// with the mesh
  57. class ModelPatch: public ModelPatchBase
  58. {
  59. public:
  60. /// Map to get the VAO given a PassLod key
  61. typedef PassLevelHashMap<Vao> PassLevelToVaoMap;
  62. ModelPatch(const char* meshFName, const char* mtlFName);
  63. ~ModelPatch();
  64. /// @name Accessors
  65. /// @{
  66. /// Implements ModelPatchBase::getMeshBase
  67. const MeshBase& getMeshBase() const
  68. {
  69. return *mesh;
  70. }
  71. /// Implements ModelPatchBase::getMaterial
  72. const Material& getMaterial() const
  73. {
  74. return *mtl;
  75. }
  76. /// @}
  77. private:
  78. MeshResourcePointer mesh; ///< The geometry
  79. MaterialResourcePointer mtl; ///< Material
  80. };
  81. /// Model is an entity that acts as a container for other resources. Models are
  82. /// all the non static objects in a map.
  83. ///
  84. /// XML file format:
  85. /// @code
  86. /// <model>
  87. /// <modelPatches>
  88. /// <modelPatch>
  89. /// <mesh>path/to/mesh.mesh</mesh>
  90. /// <material>path/to/material.mtl</material>
  91. /// </modelPatch>
  92. /// ...
  93. /// <modelPatch>...</modelPatch>
  94. /// </modelPatches>
  95. /// </model>
  96. /// @endcode
  97. ///
  98. /// Requirements:
  99. /// - If the materials need texture coords then mesh should have them
  100. /// - The skeleton and skelAnims are optional
  101. /// - Its an error to have skelAnims without skeleton
  102. class Model
  103. {
  104. public:
  105. typedef PtrVector<ModelPatch> ModelPatchesContainer;
  106. /// @name Accessors
  107. /// @{
  108. const ModelPatchesContainer& getModelPatches() const
  109. {
  110. return modelPatches;
  111. }
  112. const Obb& getVisibilityShape() const
  113. {
  114. return visibilityShape;
  115. }
  116. /// @}
  117. void load(const char* filename);
  118. private:
  119. /// The vector of ModelPatch
  120. ModelPatchesContainer modelPatches;
  121. Obb visibilityShape;
  122. };
  123. } // end namespace
  124. #endif