MeshResource.cpp 5.0 KB

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