2
0

mesh_resource.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "dynamic_string.h"
  24. #include "filesystem.h"
  25. #include "json_parser.h"
  26. #include "log.h"
  27. #include "mesh_resource.h"
  28. #include "temp_allocator.h"
  29. #include "vector3.h"
  30. #include "resource_manager.h"
  31. namespace crown
  32. {
  33. namespace mesh_resource
  34. {
  35. struct MeshVertex
  36. {
  37. Vector3 position;
  38. Vector3 normal;
  39. Vector2 texcoord;
  40. bool operator==(const MeshVertex& other)
  41. {
  42. return position == other.position &&
  43. normal == other.normal &&
  44. texcoord == other.texcoord;
  45. }
  46. };
  47. void compile(const char* path, CompileOptions& opts)
  48. {
  49. static const uint32_t VERSION = 1;
  50. Buffer buf = opts.read(path);
  51. JSONParser json(array::begin(buf));
  52. JSONElement root = json.root();
  53. // Read data arrays
  54. JSONElement position = root.key_or_nil("position");
  55. CE_ASSERT(!position.is_nil(), "Bad mesh: array 'position' not found.");
  56. JSONElement normal = root.key_or_nil("normal");
  57. JSONElement texcoord = root.key_or_nil("texcoord");
  58. Array<float> positions(default_allocator());
  59. Array<float> normals(default_allocator());
  60. Array<float> texcoords(default_allocator());
  61. position.to_array(positions);
  62. bool has_normal = false;
  63. bool has_texcoord = false;
  64. if (!normal.is_nil())
  65. {
  66. has_normal = true;
  67. normal.to_array(normals);
  68. }
  69. if (!texcoord.is_nil())
  70. {
  71. has_texcoord = true;
  72. texcoord.to_array(texcoords);
  73. }
  74. // Read index arrays
  75. JSONElement index = root.key("index");
  76. Array<uint16_t> position_index(default_allocator());
  77. Array<uint16_t> normal_index(default_allocator());
  78. Array<uint16_t> texcoord_index(default_allocator());
  79. int ii = 0;
  80. index[ii].to_array(position_index);
  81. ++ii;
  82. if (has_normal)
  83. {
  84. index[ii].to_array(normal_index);
  85. ++ii;
  86. }
  87. if (has_texcoord)
  88. {
  89. index[ii].to_array(texcoord_index);
  90. ++ii;
  91. }
  92. Array<MeshVertex> vertices(default_allocator());
  93. Array<uint16_t> indices(default_allocator());
  94. // Generate vb/ib
  95. uint32_t idx = 0;
  96. for (uint32_t i = 0; i < array::size(position_index); i++)
  97. {
  98. MeshVertex v;
  99. uint16_t p_idx = position_index[i] * 3;
  100. v.position = Vector3(positions[p_idx], positions[p_idx + 1], positions[p_idx + 2]);
  101. if (has_normal)
  102. {
  103. uint16_t n_idx = normal_index[i] * 3;
  104. v.normal = Vector3(normals[n_idx], normals[n_idx + 1], normals[n_idx + 2]);
  105. }
  106. if (has_texcoord)
  107. {
  108. uint16_t t_idx = texcoord_index[i] * 2;
  109. v.texcoord = Vector2(texcoords[t_idx], texcoords[t_idx + 1]);
  110. }
  111. uint32_t f_idx = 0;
  112. bool found = false;
  113. for (; f_idx < array::size(vertices); f_idx++)
  114. {
  115. if (vertices[f_idx] == v)
  116. {
  117. found = true;
  118. break;
  119. }
  120. }
  121. if (found)
  122. {
  123. array::push_back(indices, (uint16_t) f_idx);
  124. }
  125. else
  126. {
  127. array::push_back(vertices, v);
  128. array::push_back(indices, (uint16_t) idx);
  129. idx++;
  130. }
  131. }
  132. bgfx::VertexDecl decl;
  133. decl.begin();
  134. decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  135. if (has_normal)
  136. decl.add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float, false);
  137. if (has_texcoord)
  138. decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  139. decl.end();
  140. // Write
  141. opts.write(VERSION);
  142. opts.write(decl);
  143. opts.write(array::size(vertices));
  144. for (uint32_t i = 0; i < array::size(vertices); ++i)
  145. {
  146. opts.write(vertices[i].position);
  147. if (has_normal)
  148. opts.write(vertices[i].normal);
  149. if (has_texcoord)
  150. opts.write(vertices[i].texcoord);
  151. }
  152. opts.write(array::size(indices));
  153. for (uint32_t i = 0; i < array::size(indices); ++i)
  154. {
  155. opts.write(indices[i]);
  156. }
  157. }
  158. void* load(File& file, Allocator& a)
  159. {
  160. BinaryReader br(file);
  161. uint32_t version;
  162. br.read(version);
  163. bgfx::VertexDecl decl;
  164. br.read(decl);
  165. uint32_t num_verts;
  166. br.read(num_verts);
  167. const bgfx::Memory* vbmem = bgfx::alloc(num_verts * decl.getStride());
  168. br.read(vbmem->data, num_verts * decl.getStride());
  169. uint32_t num_inds;
  170. br.read(num_inds);
  171. const bgfx::Memory* ibmem = bgfx::alloc(num_inds * sizeof(uint16_t));
  172. br.read(ibmem->data, num_inds * sizeof(uint16_t));
  173. MeshResource* mr = (MeshResource*)a.allocate(sizeof(MeshResource));
  174. mr->decl = decl;
  175. mr->vbmem = vbmem;
  176. mr->ibmem = ibmem;
  177. return mr;
  178. }
  179. void online(StringId64 id, ResourceManager& rm)
  180. {
  181. MeshResource* mr = (MeshResource*)rm.get(MESH_TYPE, id);
  182. mr->vb = bgfx::createVertexBuffer(mr->vbmem, mr->decl);
  183. mr->ib = bgfx::createIndexBuffer(mr->ibmem);
  184. }
  185. void offline(StringId64 id, ResourceManager& rm)
  186. {
  187. MeshResource* mr = (MeshResource*)rm.get(MESH_TYPE, id);
  188. bgfx::destroyVertexBuffer(mr->vb);
  189. bgfx::destroyIndexBuffer(mr->ib);
  190. }
  191. void unload(Allocator& a, void* res)
  192. {
  193. a.deallocate(res);
  194. }
  195. } // namespace mesh_resource
  196. } // namespace crown