MeshCompiler.cpp 5.1 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. #include "MeshCompiler.h"
  24. #include "DynamicString.h"
  25. #include "TempAllocator.h"
  26. #include "Filesystem.h"
  27. #include "JSONParser.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. MeshCompiler::MeshCompiler()
  33. : m_vertices(default_allocator()), m_indices(default_allocator())
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. MeshCompiler::~MeshCompiler()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. size_t MeshCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  42. {
  43. File* file = fs.open(resource_path, FOM_READ);
  44. char* buf = (char*)default_allocator().allocate(file->size());
  45. file->read(buf, file->size());
  46. JSONParser json(buf);
  47. JSONElement root = json.root();
  48. // Read data arrays
  49. JSONElement position = root.key_or_nil("position");
  50. JSONElement normal = root.key_or_nil("normal");
  51. JSONElement texcoord = root.key_or_nil("texcoord");
  52. m_has_normal = false;
  53. m_has_texcoord = false;
  54. if (position.is_nil())
  55. {
  56. Log::e("Bad mesh: array 'position' not found.");
  57. return 0;
  58. }
  59. List<float> position_array(default_allocator());
  60. position.array_value(position_array);
  61. List<float> normal_array(default_allocator());
  62. if (!normal.is_nil())
  63. {
  64. m_has_normal = true;
  65. normal.array_value(normal_array);
  66. }
  67. List<float> texcoord_array(default_allocator());
  68. if (!texcoord.is_nil())
  69. {
  70. m_has_texcoord = true;
  71. texcoord.array_value(texcoord_array);
  72. }
  73. // Read index arrays
  74. JSONElement index = root.key_or_nil("index");
  75. if (index.is_nil())
  76. {
  77. Log::e("Bad mesh: array 'index' not found.");
  78. return 0;
  79. }
  80. List<uint16_t> position_index(default_allocator());
  81. List<uint16_t> normal_index(default_allocator());
  82. List<uint16_t> texcoord_index(default_allocator());
  83. index[0].array_value(position_index);
  84. if (m_has_normal)
  85. {
  86. index[1].array_value(normal_index);
  87. }
  88. if (m_has_texcoord)
  89. {
  90. index[2].array_value(texcoord_index);
  91. }
  92. // Generate vb/ib
  93. uint32_t idx = 0;
  94. for (uint32_t i = 0; i < position_index.size(); i++)
  95. {
  96. MeshVertex v;
  97. uint16_t p_idx = position_index[i] * 3;
  98. v.position = Vector3(position_array[p_idx], position_array[p_idx + 1], position_array[p_idx + 2]);
  99. if (m_has_normal)
  100. {
  101. uint16_t n_idx = normal_index[i] * 3;
  102. v.normal = Vector3(normal_array[n_idx], normal_array[n_idx + 1], normal_array[n_idx + 2]);
  103. }
  104. if (m_has_texcoord)
  105. {
  106. uint16_t t_idx = texcoord_index[i] * 2;
  107. v.texcoord = Vector2(texcoord_array[t_idx], texcoord_array[t_idx + 1]);
  108. }
  109. uint32_t f_idx = 0;
  110. bool found = false;
  111. for (; f_idx < m_vertices.size(); f_idx++)
  112. {
  113. if (m_vertices[f_idx] == v)
  114. {
  115. found = true;
  116. break;
  117. }
  118. }
  119. if (found)
  120. {
  121. m_indices.push_back(f_idx);
  122. }
  123. else
  124. {
  125. m_vertices.push_back(v);
  126. m_indices.push_back(idx);
  127. idx++;
  128. }
  129. }
  130. m_mesh_header.version = MESH_VERSION;
  131. m_mesh_header.num_meshes = 1;
  132. m_mesh_header.num_joints = 0;
  133. //m_mesh_header.padding[0] = 0xCECECECE;
  134. default_allocator().deallocate(buf);
  135. fs.close(file);
  136. return sizeof(MeshHeader) + sizeof(MeshData) +
  137. m_vertices.size() * sizeof(MeshVertex) +
  138. m_indices.size() * sizeof(uint16_t);
  139. }
  140. //-----------------------------------------------------------------------------
  141. void MeshCompiler::write_impl(File* out_file)
  142. {
  143. MeshData data;
  144. data.vertices.num_vertices = m_vertices.size();
  145. data.vertices.format = VERTEX_P3_N3_T2;
  146. data.vertices.offset = sizeof(MeshHeader) + sizeof(MeshData);
  147. data.indices.num_indices = m_indices.size();
  148. data.indices.offset = sizeof(MeshHeader) + sizeof(MeshData) + m_vertices.size() * sizeof(MeshVertex);
  149. // Write header
  150. out_file->write((char*)&m_mesh_header, sizeof(MeshHeader));
  151. // Write mesh metadata
  152. out_file->write((char*)&data, sizeof(MeshData));
  153. // Write vertices
  154. out_file->write((char*) m_vertices.begin(), m_vertices.size() * sizeof(MeshVertex));
  155. // Write indices
  156. out_file->write((char*) m_indices.begin(), m_indices.size() * sizeof(uint16_t));
  157. // Cleanup
  158. m_vertices.clear();
  159. m_indices.clear();
  160. }
  161. } // namespace crown