MeshLoader.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef ANKI_RESOURCE_MESH_LOADER_H
  2. #define ANKI_RESOURCE_MESH_LOADER_H
  3. #include "anki/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. /// If two vertices have the same position and normals under the angle
  43. /// specified by this constant then combine those normals
  44. static constexpr F32 NORMALS_ANGLE_MERGE = getPi<F32>() / 6;
  45. /// Vertex weight for skeletal animation
  46. struct VertexWeight
  47. {
  48. /// Dont change this or prepare to change the skinning code in
  49. /// shader
  50. static const U32 MAX_BONES_PER_VERT = 4;
  51. U16 bonesNum;
  52. Array<U16, MAX_BONES_PER_VERT> boneIds;
  53. Array<F16, MAX_BONES_PER_VERT> weights;
  54. };
  55. /// Triangle
  56. struct Triangle
  57. {
  58. /// An array with the vertex indexes in the mesh class
  59. Array<U32, 3> vertIds;
  60. Vec3 normal;
  61. };
  62. MeshLoader()
  63. {}
  64. MeshLoader(const char* filename)
  65. {
  66. load(filename);
  67. }
  68. ~MeshLoader()
  69. {}
  70. /// @name Accessors
  71. /// @{
  72. const Vector<Vec3>& getPositions() const
  73. {
  74. return positions;
  75. }
  76. const Vector<HVec3>& getNormals() const
  77. {
  78. return normalsF16;
  79. }
  80. const Vector<HVec4>& getTangents() const
  81. {
  82. return tangentsF16;
  83. }
  84. const Vector<HVec2>& getTextureCoordinates(const U32 channel) const
  85. {
  86. return texCoordsF16;
  87. }
  88. U getTextureChannelsCount() const
  89. {
  90. return 1;
  91. }
  92. const Vector<VertexWeight>& getWeights() const
  93. {
  94. return weights;
  95. }
  96. const Vector<U16>& getIndices() const
  97. {
  98. return vertIndices;
  99. }
  100. /// @}
  101. /// Append data from another mesh loader. BucketMesh method
  102. void append(const MeshLoader& other);
  103. /// Load the mesh data from a binary file
  104. /// @exception Exception
  105. void load(const char* filename);
  106. private:
  107. /// @name Data
  108. /// @{
  109. Vector<Vec3> positions; ///< Loaded from file
  110. Vector<Vec3> normals; ///< Generated
  111. Vector<HVec3> normalsF16;
  112. Vector<Vec4> tangents; ///< Generated
  113. Vector<HVec4> tangentsF16;
  114. /// Optional. One for every vert so we can use vertex arrays & VBOs
  115. Vector<Vec2> texCoords;
  116. Vector<HVec2> texCoordsF16;
  117. Vector<VertexWeight> weights; ///< Optional
  118. Vector<Triangle> tris; ///< Required
  119. /// Generated. Used for vertex arrays & VBOs
  120. Vector<U16> vertIndices;
  121. /// @}
  122. void createFaceNormals();
  123. void createVertNormals();
  124. void createAllNormals()
  125. {
  126. createFaceNormals();
  127. createVertNormals();
  128. }
  129. void createVertTangents();
  130. void createVertIndeces();
  131. /// This method does some sanity checks and creates normals,
  132. /// tangents, VBOs etc
  133. /// @exception Exception
  134. void doPostLoad();
  135. /// It iterates all verts and fixes the normals on seams
  136. void fixNormals();
  137. /// Compress some buffers for increased BW performance
  138. void compressBuffers();
  139. };
  140. } // end namespace anki
  141. #endif