DAECompiler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. #include "Compiler.h"
  28. #include "MeshFormat.h"
  29. #include "tinyxml2.h"
  30. using tinyxml2::XMLElement;
  31. using std::vector;
  32. namespace crown
  33. {
  34. //
  35. // STRUCT
  36. // {
  37. // FIELD : SIZE COMMENT
  38. // }
  39. //
  40. // MeshHeader [1]
  41. // {
  42. // version : uint32_t Version identifier
  43. // mesh_count : uint32_t Number of meshes in the file
  44. // joint_count : uint32_t Number of joints in the file
  45. // padding : uint32_t * 16 Reserved
  46. // }
  47. // MeshChunk [1, 2, ..., n]
  48. // {
  49. // vertex_count : uint32_t Number of vertices in the mesh
  50. // vertices : float * vertex_count Vertex data
  51. //
  52. // tri_count : uint32_t Number of triangles in the mesh
  53. // tris : uint16_t * tri_count Triangle data as indices into 'vertices'
  54. // }
  55. //
  56. // Bump the version whenever a change in the format is made.
  57. const uint32_t MESH_VERSION = 1;
  58. struct MeshHeader
  59. {
  60. uint32_t version;
  61. uint32_t mesh_count;
  62. uint32_t joint_count;
  63. uint32_t padding[16];
  64. };
  65. //-----------------------------------------------------------------------------
  66. struct DAEFloatArray
  67. {
  68. std::string id; // UUID of the array
  69. std::vector<float> array;
  70. };
  71. //-----------------------------------------------------------------------------
  72. struct DAEParam
  73. {
  74. std::string name;
  75. std::string type;
  76. };
  77. //-----------------------------------------------------------------------------
  78. struct DAEAccessor
  79. {
  80. std::string source;
  81. uint32_t count;
  82. uint32_t stride;
  83. std::vector<DAEParam> params;
  84. };
  85. //-----------------------------------------------------------------------------
  86. struct DAETechniqueCommon
  87. {
  88. DAEAccessor accessor;
  89. };
  90. //-----------------------------------------------------------------------------
  91. struct DAESource
  92. {
  93. std::string id; // UUID of the source
  94. DAEFloatArray float_array; // Array of floats
  95. DAETechniqueCommon technique_common;
  96. };
  97. //-----------------------------------------------------------------------------
  98. struct DAEInput
  99. {
  100. std::string semantic;
  101. std::string source;
  102. uint32_t offset;
  103. };
  104. //-----------------------------------------------------------------------------
  105. struct DAEVertices
  106. {
  107. std::string id;
  108. std::vector<DAEInput> inputs;
  109. };
  110. //-----------------------------------------------------------------------------
  111. struct DAEPolylist
  112. {
  113. uint32_t count;
  114. std::vector<DAEInput> inputs;
  115. std::vector<uint32_t> vcount;
  116. std::vector<uint32_t> p;
  117. };
  118. //-----------------------------------------------------------------------------
  119. struct DAEMesh
  120. {
  121. std::vector<DAESource> sources;
  122. DAEVertices vertices;
  123. DAEPolylist polylist;
  124. };
  125. //-----------------------------------------------------------------------------
  126. struct DAEGeometry
  127. {
  128. std::string id; // UUID of the geometry
  129. std::string name; // Name of the geometry
  130. DAEMesh mesh; // The mesh
  131. };
  132. //-----------------------------------------------------------------------------
  133. struct DAEModel
  134. {
  135. std::vector<DAEGeometry> geometries;
  136. };
  137. class DAECompiler : public Compiler
  138. {
  139. public:
  140. DAECompiler();
  141. ~DAECompiler();
  142. size_t compile_impl(const char* resource_path);
  143. void write_impl(std::fstream& out_file);
  144. private:
  145. // The following functions return false if parsing fails, true otherwise
  146. static bool parse_collada(const char* path, DAEModel& m);
  147. static bool parse_geometry(XMLElement* geometry, DAEGeometry& g);
  148. static bool parse_mesh(XMLElement* mesh, DAEMesh& m);
  149. static bool parse_source(XMLElement* source, DAESource& s);
  150. static bool parse_float_array(XMLElement* array, DAEFloatArray& a);
  151. static bool parse_technique_common(XMLElement* technique, DAETechniqueCommon& t);
  152. static bool parse_accessor(XMLElement* accessor, DAEAccessor& a);
  153. static bool parse_param(XMLElement* param, DAEParam& p);
  154. static bool parse_vertices(XMLElement* vertices, DAEVertices& v);
  155. static bool parse_input(XMLElement* input, DAEInput& i);
  156. static bool parse_polylist(XMLElement* polylist, DAEPolylist& p);
  157. bool find_vertices(const DAEMesh& mesh, DAESource& source_out);
  158. bool find_normals(const DAEMesh& mesh, DAESource& source_out);
  159. bool extract_vertex_indices(const DAEMesh& mesh, vector<uint16_t>& indices_out);
  160. bool extract_vertex_normals(const DAEMesh& mesh, vector<uint32_t>& indices_out);
  161. private:
  162. MeshHeader m_mesh_header;
  163. vector<float> m_vertex_vertices;
  164. vector<uint16_t> m_vertex_indices;
  165. };
  166. } // namespace crown