Model.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /// For garbage collection
  16. typedef PtrVector<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. return *vaosMap.at(key);
  26. }
  27. uint getIndecesNumber(uint lod) const
  28. {
  29. return getMeshBase().getIndicesNumber(lod);
  30. }
  31. protected:
  32. VaosContainer vaos;
  33. PassLevelToVaoMap vaosMap;
  34. void create()
  35. {
  36. createVaos(getMaterial(), getMeshBase(), vaos, vaosMap);
  37. }
  38. /// Create VAOs using a material and a mesh. It writes a VaosContainer and
  39. /// a hash map
  40. static void createVaos(const Material& mtl,
  41. const MeshBase& mesh,
  42. VaosContainer& vaos,
  43. PassLevelToVaoMap& vaosMap);
  44. /// Called by @a createVaos multiple times to create and populate a single
  45. /// VAO
  46. static Vao* createNewVao(const Material& mtl,
  47. const MeshBase& mesh,
  48. const PassLevelKey& key);
  49. };
  50. /// Its a chunk of a model. Its very important class and it binds the material
  51. /// with the mesh
  52. class ModelPatch: public ModelPatchBase
  53. {
  54. public:
  55. /// Map to get the VAO given a PassLod key
  56. typedef PassLevelHashMap<Vao> PassLevelToVaoMap;
  57. ModelPatch(const char* meshFName, const char* mtlFName);
  58. ~ModelPatch();
  59. /// @name Accessors
  60. /// @{
  61. /// Implements ModelPatchBase::getMeshBase
  62. const MeshBase& getMeshBase() const
  63. {
  64. return *mesh;
  65. }
  66. /// Implements ModelPatchBase::getMaterial
  67. const Material& getMaterial() const
  68. {
  69. return *mtl;
  70. }
  71. /// @}
  72. private:
  73. MeshResourcePointer mesh; ///< The geometry
  74. MaterialResourcePointer mtl; ///< Material
  75. };
  76. /// Model is an entity that acts as a container for other resources. Models are
  77. /// all the non static objects in a map.
  78. ///
  79. /// XML file format:
  80. /// @code
  81. /// <model>
  82. /// <modelPatches>
  83. /// <modelPatch>
  84. /// <mesh>path/to/mesh.mesh</mesh>
  85. /// <material>path/to/material.mtl</material>
  86. /// </modelPatch>
  87. /// ...
  88. /// <modelPatch>...</modelPatch>
  89. /// </modelPatches>
  90. /// </model>
  91. /// @endcode
  92. ///
  93. /// Requirements:
  94. /// - If the materials need texture coords then mesh should have them
  95. /// - The skeleton and skelAnims are optional
  96. /// - Its an error to have skelAnims without skeleton
  97. class Model
  98. {
  99. public:
  100. typedef PtrVector<ModelPatch> ModelPatchesContainer;
  101. /// @name Accessors
  102. /// @{
  103. const ModelPatchesContainer& getModelPatches() const
  104. {
  105. return modelPatches;
  106. }
  107. const Obb& getVisibilityShape() const
  108. {
  109. return visibilityShape;
  110. }
  111. /// @}
  112. void load(const char* filename);
  113. private:
  114. /// The vector of ModelPatch
  115. ModelPatchesContainer modelPatches;
  116. Obb visibilityShape;
  117. };
  118. } // end namespace
  119. #endif