mesh_resource.cpp 4.7 KB

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