DAECompiler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 DAEFloatArray
  36. {
  37. std::string id; // UUID of the array
  38. std::vector<float> array;
  39. };
  40. //-----------------------------------------------------------------------------
  41. struct DAEParam
  42. {
  43. std::string name;
  44. std::string type;
  45. };
  46. //-----------------------------------------------------------------------------
  47. struct DAEAccessor
  48. {
  49. std::string source;
  50. uint32_t count;
  51. uint32_t stride;
  52. std::vector<DAEParam> params;
  53. };
  54. //-----------------------------------------------------------------------------
  55. struct DAETechniqueCommon
  56. {
  57. DAEAccessor accessor;
  58. };
  59. //-----------------------------------------------------------------------------
  60. struct DAESource
  61. {
  62. std::string id; // UUID of the source
  63. DAEFloatArray float_array; // Array of floats
  64. DAETechniqueCommon technique_common;
  65. };
  66. //-----------------------------------------------------------------------------
  67. struct DAEInput
  68. {
  69. std::string semantic;
  70. std::string source;
  71. uint32_t offset;
  72. };
  73. //-----------------------------------------------------------------------------
  74. struct DAEVertices
  75. {
  76. std::string id;
  77. std::vector<DAEInput> inputs;
  78. };
  79. //-----------------------------------------------------------------------------
  80. struct DAEPolylist
  81. {
  82. uint32_t count;
  83. std::vector<DAEInput> inputs;
  84. std::vector<uint32_t> vcount;
  85. std::vector<uint32_t> p;
  86. };
  87. //-----------------------------------------------------------------------------
  88. struct DAEMesh
  89. {
  90. std::vector<DAESource> sources;
  91. DAEVertices vertices;
  92. DAEPolylist polylist;
  93. };
  94. //-----------------------------------------------------------------------------
  95. struct DAEGeometry
  96. {
  97. std::string id; // UUID of the geometry
  98. std::string name; // Name of the geometry
  99. DAEMesh mesh; // The mesh
  100. };
  101. //-----------------------------------------------------------------------------
  102. struct DAEModel
  103. {
  104. std::vector<DAEGeometry> geometries;
  105. };
  106. class DAECompiler : public Compiler
  107. {
  108. public:
  109. DAECompiler();
  110. ~DAECompiler();
  111. size_t compile_impl(const char* resource_path);
  112. void write_impl(std::fstream& out_file);
  113. private:
  114. // The following functions return false if parsing fails, true otherwise
  115. static bool parse_collada(const char* path, DAEModel& m);
  116. static bool parse_geometry(XMLElement* geometry, DAEGeometry& g);
  117. static bool parse_mesh(XMLElement* mesh, DAEMesh& m);
  118. static bool parse_source(XMLElement* source, DAESource& s);
  119. static bool parse_float_array(XMLElement* array, DAEFloatArray& a);
  120. static bool parse_technique_common(XMLElement* technique, DAETechniqueCommon& t);
  121. static bool parse_accessor(XMLElement* accessor, DAEAccessor& a);
  122. static bool parse_param(XMLElement* param, DAEParam& p);
  123. static bool parse_vertices(XMLElement* vertices, DAEVertices& v);
  124. static bool parse_input(XMLElement* input, DAEInput& i);
  125. static bool parse_polylist(XMLElement* polylist, DAEPolylist& p);
  126. bool find_vertices(const DAEMesh& mesh, DAESource& source_out);
  127. bool find_normals(const DAEMesh& mesh, DAESource& source_out);
  128. bool extract_vertex_indices(const DAEMesh& mesh, vector<uint16_t>& indices_out);
  129. bool extract_vertex_normals(const DAEMesh& mesh, vector<uint32_t>& indices_out);
  130. private:
  131. MeshHeader m_mesh_header;
  132. vector<float> m_vertex_vertices;
  133. vector<uint16_t> m_vertex_indices;
  134. };
  135. } // namespace crown