MeshLoader.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef ANKI_RESOURCE_MESH_LOADER_H
  2. #define ANKI_RESOURCE_MESH_LOADER_H
  3. #include "anki/math/Math.h"
  4. #include "anki/util/Vector.h"
  5. #include "anki/util/Array.h"
  6. #include <string>
  7. namespace anki {
  8. /// Mesh data. This class loads the mesh file and the Mesh class loads it to
  9. /// the GPU
  10. ///
  11. /// Binary file format:
  12. ///
  13. /// @code
  14. /// // Header
  15. /// <magic:ANKIMESH>
  16. /// <string:meshName>
  17. ///
  18. /// // Verts
  19. /// U32: verts number
  20. /// F32: vert 0 x, F32 vert 0 y, F32: vert 0 z
  21. /// ...
  22. ///
  23. /// // Faces
  24. /// U32: faces number
  25. /// U32: tri 0 vert ID 0, U32: tri 0 vert ID 1, U32: tri 0 vert ID 2
  26. /// ...
  27. ///
  28. /// // Tex coords
  29. /// U32: tex coords number
  30. /// F32: tex coord for vert 0 x, F32: tex coord for vert 0 y
  31. /// ...
  32. ///
  33. /// // Bone weights
  34. /// U32: bone weights number (equal to verts number)
  35. /// U32: bones number for vert 0, U32: bone id for vert 0 and weight 0,
  36. /// F32: weight for vert 0 and weight 0, ...
  37. /// ...
  38. /// @endcode
  39. class MeshLoader
  40. {
  41. public:
  42. /// Vertex weight for skeletal animation
  43. struct VertexWeight
  44. {
  45. /// Dont change this or prepare to change the skinning code in
  46. /// shader
  47. static const U32 MAX_BONES_PER_VERT = 4;
  48. /// @todo change the vals to U32 when change drivers
  49. F32 bonesNum;
  50. Array<F32, MAX_BONES_PER_VERT> boneIds;
  51. Array<F32, MAX_BONES_PER_VERT> weights;
  52. };
  53. /// Triangle
  54. struct Triangle
  55. {
  56. /// An array with the vertex indexes in the mesh class
  57. Array<U32, 3> vertIds;
  58. Vec3 normal;
  59. };
  60. MeshLoader(const char* filename)
  61. {
  62. load(filename);
  63. }
  64. ~MeshLoader()
  65. {}
  66. /// @name Accessors
  67. /// @{
  68. U getLodsCount() const
  69. {
  70. return 1;
  71. }
  72. const Vector<Vec3>& getPositions() const
  73. {
  74. return vertCoords;
  75. }
  76. const Vector<Vec3>& getNormals() const
  77. {
  78. return vertNormals;
  79. }
  80. const Vector<Vec4>& getTangents() const
  81. {
  82. return vertTangents;
  83. }
  84. const Vector<Vec2>& getTexureCoordinates(const U32 channel) const
  85. {
  86. return texCoords;
  87. }
  88. U getTextureChannelsCount() const
  89. {
  90. return 1;
  91. }
  92. const Vector<VertexWeight>& getWeights() const
  93. {
  94. return vertWeights;
  95. }
  96. const Vector<ushort>& getIndices(const U lod) const
  97. {
  98. return vertIndeces;
  99. }
  100. /// @}
  101. private:
  102. /// @name Data
  103. /// @{
  104. Vector<Vec3> vertCoords; ///< Loaded from file
  105. Vector<Vec3> vertNormals; ///< Generated
  106. Vector<Vec4> vertTangents; ///< Generated
  107. /// Optional. One for every vert so we can use vertex arrays & VBOs
  108. Vector<Vec2> texCoords;
  109. Vector<VertexWeight> vertWeights; ///< Optional
  110. Vector<Triangle> tris; ///< Required
  111. /// Generated. Used for vertex arrays & VBOs
  112. Vector<ushort> vertIndeces;
  113. /// @}
  114. /// Load the mesh data from a binary file
  115. /// @exception Exception
  116. void load(const char* filename);
  117. void createFaceNormals();
  118. void createVertNormals();
  119. void createAllNormals()
  120. {
  121. createFaceNormals();
  122. createVertNormals();
  123. }
  124. void createVertTangents();
  125. void createVertIndeces();
  126. /// This method does some sanity checks and creates normals,
  127. /// tangents, VBOs etc
  128. /// @exception Exception
  129. void doPostLoad();
  130. };
  131. } // end namespace anki
  132. #endif