UnitCompiler.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "Allocator.h"
  24. #include "File.h"
  25. #include "Filesystem.h"
  26. #include "Hash.h"
  27. #include "JSONParser.h"
  28. #include "UnitCompiler.h"
  29. #include "TempAllocator.h"
  30. #include "Log.h"
  31. #include "PhysicsTypes.h"
  32. #include "Quaternion.h"
  33. #include "Vector3.h"
  34. #include "Matrix4x4.h"
  35. namespace crown
  36. {
  37. //-----------------------------------------------------------------------------
  38. UnitCompiler::UnitCompiler()
  39. : m_renderable(default_allocator())
  40. , m_camera(default_allocator())
  41. , m_actor(default_allocator())
  42. , m_node_names(default_allocator())
  43. , m_node_parents(default_allocator())
  44. , m_node_poses(default_allocator())
  45. {
  46. }
  47. //-----------------------------------------------------------------------------
  48. size_t UnitCompiler::compile_impl(Filesystem& fs, const char* resource_path)
  49. {
  50. File* file = fs.open(resource_path, FOM_READ);
  51. char file_buf[4096];
  52. file->read(file_buf, file->size());
  53. fs.close(file);
  54. JSONParser json(file_buf);
  55. JSONElement root = json.root();
  56. // Check for renderable
  57. if (root.has_key("renderable"))
  58. {
  59. JSONElement renderable_array = root.key("renderable");
  60. uint32_t renderable_array_size = renderable_array.size();
  61. for (uint32_t i = 0; i < renderable_array_size; i++)
  62. {
  63. const char* type = renderable_array[i].key("type").string_value();
  64. UnitRenderable ur;
  65. DynamicString renderable;
  66. if (string::strcmp(type, "mesh") == 0)
  67. {
  68. ur.type = UnitRenderable::MESH;
  69. renderable += renderable_array[i].key("resource").string_value();
  70. renderable += ".mesh";
  71. }
  72. else if (string::strcmp(type, "sprite") == 0)
  73. {
  74. ur.type = UnitRenderable::SPRITE;
  75. renderable += renderable_array[i].key("resource").string_value();
  76. renderable += ".sprite";
  77. }
  78. else
  79. {
  80. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", type);
  81. }
  82. DynamicString renderable_name;
  83. renderable_name = renderable_array[i].key("name").string_value();
  84. ur.resource.id = hash::murmur2_64(renderable.c_str(), string::strlen(renderable.c_str()), 0);
  85. ur.name = hash::murmur2_32(renderable_name.c_str(), string::strlen(renderable_name.c_str()), 0);
  86. ur.visible = renderable_array[i].key("visible").bool_value();
  87. m_renderable.push_back(ur);
  88. }
  89. }
  90. // Check for cameras
  91. if (root.has_key("camera"))
  92. {
  93. JSONElement camera = root.key("camera");
  94. uint32_t num_cameras = camera.size();
  95. for (uint32_t i = 0; i < num_cameras; i++)
  96. {
  97. JSONElement camera_name = camera[i].key("name");
  98. UnitCamera uc;
  99. uc.name = hash::murmur2_32(camera_name.string_value(), camera_name.size(), 0);
  100. m_camera.push_back(uc);
  101. }
  102. }
  103. // check for actors
  104. if (root.has_key("actor"))
  105. {
  106. JSONElement actor = root.key("actor");
  107. uint32_t num_actors = actor.size();
  108. for (uint32_t i = 0; i < num_actors; i++)
  109. {
  110. JSONElement actor_name = actor[i].key("name");
  111. JSONElement actor_type = actor[i].key("type");
  112. JSONElement actor_shape = actor[i].key("shape");
  113. JSONElement actor_active = actor[i].key("active");
  114. UnitActor ua;
  115. ua.name = hash::murmur2_32(actor_name.string_value(), actor_name.size(), 0);
  116. ua.type = string::strcmp(actor_type.string_value(), "STATIC") == 0 ? UnitActor::STATIC : UnitActor::DYNAMIC;
  117. ua.shape = string::strcmp(actor_shape.string_value(), "SPHERE") == 0 ? UnitActor::SPHERE :
  118. string::strcmp(actor_shape.string_value(), "BOX") == 0 ? UnitActor::BOX : UnitActor::PLANE;
  119. ua.active = actor_active.bool_value();
  120. m_actor.push_back(ua);
  121. }
  122. }
  123. // Check for nodes
  124. if (root.has_key("nodes"))
  125. {
  126. JSONElement nodes = root.key("nodes");
  127. const uint32_t num_nodes = nodes.size();
  128. for (uint32_t i = 0; i < num_nodes; i++)
  129. {
  130. JSONElement node = nodes[i];
  131. JSONElement node_name = node.key("name");
  132. JSONElement node_parent = node.key("parent");
  133. JSONElement node_pos = node.key("position");
  134. JSONElement node_rot = node.key("rotation");
  135. // Read name and parent
  136. m_node_names.push_back(hash::murmur2_32(node_name.string_value(), node_name.size(), 0));
  137. ParentIndex pidx;
  138. pidx.parent_index = -1;
  139. pidx.inner_index = i;
  140. if (node_parent.is_nil())
  141. {
  142. pidx.parent_name = 0xFFFFFFFF;
  143. }
  144. else
  145. {
  146. pidx.parent_name = hash::murmur2_32(node_parent.string_value(), node_parent.size(), 0);
  147. }
  148. m_node_parents.push_back(pidx);
  149. // Read pose
  150. const Vector3 pos = Vector3(node_pos[0].float_value(), node_pos[1].float_value(), node_pos[2].float_value());
  151. const Quaternion rot = Quaternion(Vector3(node_rot[0].float_value(), node_rot[1].float_value(), node_rot[2].float_value()), node_rot[3].float_value());
  152. m_node_poses.push_back(Matrix4x4(rot, pos));
  153. }
  154. }
  155. // Convert parent names into parent indices
  156. for (uint32_t i = 0; i < m_node_parents.size(); i++)
  157. {
  158. for (uint32_t j = 0; j < m_node_names.size(); j++)
  159. {
  160. if (m_node_names[j] == m_node_parents[i].parent_name)
  161. {
  162. m_node_parents[i].parent_index = j;
  163. break;
  164. }
  165. }
  166. }
  167. // Sort by link depth
  168. std::sort(m_node_parents.begin(), m_node_parents.end(), ParentIndex());
  169. return 1;
  170. }
  171. //-----------------------------------------------------------------------------
  172. void UnitCompiler::write_impl(File* out_file)
  173. {
  174. UnitHeader header;
  175. header.num_renderables = m_renderable.size();
  176. header.num_cameras = m_camera.size();
  177. header.num_actors = m_actor.size();
  178. header.num_scene_graph_nodes = m_node_names.size();
  179. header.renderables_offset = sizeof(UnitHeader);
  180. header.cameras_offset = sizeof(UnitHeader) + sizeof(UnitRenderable) * header.num_renderables;
  181. header.actors_offset = sizeof(UnitHeader) + sizeof(UnitCamera) * header.num_cameras;
  182. header.scene_graph_names_offset = sizeof(UnitHeader) + sizeof(UnitActor) * header.num_actors;
  183. header.scene_graph_poses_offset = sizeof(UnitHeader) + sizeof(StringId32) * header.num_scene_graph_nodes;
  184. header.scene_graph_parents_offset = sizeof(UnitHeader) + sizeof(Matrix4x4) * header.num_scene_graph_nodes;
  185. out_file->write((char*) &header, sizeof(UnitHeader));
  186. if (m_renderable.size() > 0)
  187. {
  188. out_file->write((char*) m_renderable.begin(), sizeof(UnitRenderable) * header.num_renderables);
  189. }
  190. if (m_camera.size() > 0)
  191. {
  192. out_file->write((char*) m_camera.begin(), sizeof(UnitCamera) * header.num_cameras);
  193. }
  194. if (m_actor.size() > 0)
  195. {
  196. out_file->write((char*) m_actor.begin(), sizeof(UnitActor) * header.num_actors);
  197. }
  198. // Write node names
  199. for (uint32_t i = 0; i < m_node_names.size(); i++)
  200. {
  201. out_file->write((char*) &m_node_names[m_node_parents[i].inner_index], sizeof(StringId32));
  202. }
  203. // Write node poses
  204. for (uint32_t i = 0; i < m_node_names.size(); i++)
  205. {
  206. out_file->write((char*) &m_node_poses[m_node_parents[i].inner_index], sizeof(Matrix4x4));
  207. }
  208. // Write node parents
  209. for (uint32_t i = 0; i < m_node_names.size(); i++)
  210. {
  211. out_file->write((char*) &m_node_parents[i].parent_index, sizeof(int32_t));
  212. }
  213. // Cleanup
  214. m_renderable.clear();
  215. m_camera.clear();
  216. m_actor.clear();
  217. m_node_names.clear();
  218. m_node_parents.clear();
  219. m_node_poses.clear();
  220. }
  221. } // namespace crown