UnitResource.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "ContainerTypes.h"
  29. #include "Log.h"
  30. #include "Matrix4x4.h"
  31. #include "PhysicsTypes.h"
  32. #include "Quaternion.h"
  33. #include "Resource.h"
  34. #include "TempAllocator.h"
  35. #include "Types.h"
  36. #include "UnitResource.h"
  37. #include "Vector3.h"
  38. namespace crown
  39. {
  40. namespace unit_resource
  41. {
  42. const StringId32 NO_PARENT = 0xFFFFFFFF;
  43. struct GraphNode
  44. {
  45. StringId32 name;
  46. StringId32 parent;
  47. Vector3 position;
  48. Quaternion rotation;
  49. };
  50. struct GraphNodeDepth
  51. {
  52. StringId32 name;
  53. uint32_t index;
  54. uint32_t depth;
  55. bool operator()(const GraphNodeDepth& a, const GraphNodeDepth& b)
  56. {
  57. return a.depth < b.depth;
  58. }
  59. };
  60. //-----------------------------------------------------------------------------
  61. uint32_t compute_link_depth(const GraphNode& node, const Array<GraphNode>& nodes)
  62. {
  63. if (node.parent == NO_PARENT) return 0;
  64. else
  65. {
  66. for (uint32_t i = 0; i < array::size(nodes); i++)
  67. {
  68. if (nodes[i].name == node.parent)
  69. {
  70. return 1 + compute_link_depth(nodes[i], nodes);
  71. }
  72. }
  73. }
  74. CE_FATAL("Node not found");
  75. }
  76. //-----------------------------------------------------------------------------
  77. uint32_t find_node_index(StringId32 name, const Array<GraphNodeDepth>& node_depths)
  78. {
  79. for (uint32_t i = 0; i < array::size(node_depths); i++)
  80. {
  81. if (node_depths[i].name == name)
  82. {
  83. return i;
  84. }
  85. }
  86. CE_FATAL("Node not found");
  87. return 0;
  88. }
  89. //-----------------------------------------------------------------------------
  90. int32_t find_node_parent_index(uint32_t node, const Array<GraphNode>& nodes, const Array<GraphNodeDepth>& node_depths)
  91. {
  92. StringId32 parent_name = nodes[node_depths[node].index].parent;
  93. if (parent_name == NO_PARENT) return -1;
  94. for (uint32_t i = 0; i < array::size(node_depths); i++)
  95. {
  96. if (parent_name == node_depths[i].name)
  97. {
  98. return i;
  99. }
  100. }
  101. CE_FATAL("Node not found");
  102. return 0;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void parse_nodes(JSONElement e, Array<GraphNode>& nodes, Array<GraphNodeDepth>& node_depths)
  106. {
  107. Vector<DynamicString> keys(default_allocator());
  108. e.to_keys(keys);
  109. for (uint32_t k = 0; k < keys.size(); k++)
  110. {
  111. const char* node_name = keys[k].c_str();
  112. JSONElement node = e.key(node_name);
  113. GraphNode gn;
  114. gn.name = hash::murmur2_32(node_name, string::strlen(node_name));
  115. gn.parent = NO_PARENT;
  116. if (!node.key("parent").is_nil())
  117. {
  118. DynamicString parent_name;
  119. node.key("parent").to_string(parent_name);
  120. gn.parent = hash::murmur2_32(parent_name.c_str(), parent_name.length(), 0);
  121. }
  122. JSONElement pos = node.key("position");
  123. JSONElement rot = node.key("rotation");
  124. gn.position = Vector3(pos[0].to_float(), pos[1].to_float(), pos[2].to_float());
  125. gn.rotation = Quaternion(Vector3(rot[0].to_float(), rot[1].to_float(), rot[2].to_float()), rot[3].to_float());
  126. GraphNodeDepth gnd;
  127. gnd.name = gn.name;
  128. gnd.index = array::size(nodes);
  129. gnd.depth = 0;
  130. array::push_back(nodes, gn);
  131. array::push_back(node_depths, gnd);
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphNodeDepth>& node_depths)
  136. {
  137. Vector<DynamicString> keys(default_allocator());
  138. e.to_keys(keys);
  139. for (uint32_t k = 0; k < keys.size(); k++)
  140. {
  141. const char* camera_name = keys[k].c_str();
  142. JSONElement camera = e.key(camera_name);
  143. DynamicString node_name;
  144. camera.key("node").to_string(node_name);
  145. StringId32 node_name_hash = hash::murmur2_32(node_name.c_str(), node_name.length());
  146. UnitCamera cn;
  147. cn.name = hash::murmur2_32(camera_name, string::strlen(camera_name));
  148. cn.node = find_node_index(node_name_hash, node_depths);
  149. array::push_back(cameras, cn);
  150. }
  151. }
  152. //-----------------------------------------------------------------------------
  153. void parse_renderables(JSONElement e, Array<UnitRenderable>& renderables, const Array<GraphNodeDepth>& node_depths)
  154. {
  155. Vector<DynamicString> keys(default_allocator());
  156. e.to_keys(keys);
  157. for (uint32_t k = 0; k < keys.size(); k++)
  158. {
  159. const char* renderable_name = keys[k].c_str();
  160. JSONElement renderable = e.key(renderable_name);
  161. DynamicString node_name; renderable.key("node").to_string(node_name);
  162. StringId32 node_name_hash = hash::murmur2_32(node_name.c_str(), node_name.length(), 0);
  163. UnitRenderable rn;
  164. rn.name = hash::murmur2_32(renderable_name, string::strlen(renderable_name), 0);
  165. rn.node = find_node_index(node_name_hash, node_depths);
  166. rn.visible = renderable.key("visible").to_bool();
  167. DynamicString res_type; renderable.key("type").to_string(res_type);
  168. DynamicString resource_name; renderable.key("resource").to_string(resource_name);
  169. DynamicString res_name;
  170. if (res_type == "mesh")
  171. {
  172. rn.type = UnitRenderable::MESH;
  173. res_name += resource_name;
  174. res_name += ".mesh";
  175. }
  176. else if (res_type == "sprite")
  177. {
  178. rn.type = UnitRenderable::SPRITE;
  179. res_name += resource_name;
  180. res_name += ".sprite";
  181. }
  182. else
  183. {
  184. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
  185. }
  186. rn.resource.id = hash::murmur2_64(res_name.c_str(), res_name.length(), 0);
  187. array::push_back(renderables, rn);
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  192. {
  193. File* file = fs.open(resource_path, FOM_READ);
  194. char file_buf[4096];
  195. file->read(file_buf, file->size());
  196. fs.close(file);
  197. JSONParser json(file_buf);
  198. JSONElement root = json.root();
  199. ResourceId m_physics_resource;
  200. ResourceId m_material_resource;
  201. Array<GraphNode> m_nodes(default_allocator());
  202. Array<GraphNodeDepth> m_node_depths(default_allocator());
  203. Array<UnitCamera> m_cameras(default_allocator());
  204. Array<UnitRenderable> m_renderables(default_allocator());
  205. // Check for nodes
  206. if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
  207. for (uint32_t i = 0; i < array::size(m_nodes); i++)
  208. {
  209. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  210. }
  211. std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
  212. if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
  213. if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
  214. // Check if the unit has a .physics resource
  215. DynamicString unit_name(resource_path);
  216. unit_name.strip_trailing("unit");
  217. DynamicString physics_name = unit_name;
  218. physics_name += "physics";
  219. if (fs.is_file(physics_name.c_str()))
  220. {
  221. m_physics_resource.id = hash::murmur2_64(physics_name.c_str(), string::strlen(physics_name.c_str()), 0);
  222. }
  223. else
  224. {
  225. m_physics_resource.id = 0;
  226. }
  227. // Check if the unit has a .material resource
  228. DynamicString material_name = unit_name;
  229. material_name += "material";
  230. if (fs.is_file(material_name.c_str()))
  231. {
  232. m_material_resource.id = hash::murmur2_64(material_name.c_str(), string::strlen(material_name.c_str()), 0);
  233. }
  234. else
  235. {
  236. m_material_resource.id = 0;
  237. }
  238. UnitHeader h;
  239. h.physics_resource = m_physics_resource;
  240. h.material_resource = m_material_resource;
  241. h.num_renderables = array::size(m_renderables);
  242. h.num_cameras = array::size(m_cameras);
  243. h.num_scene_graph_nodes = array::size(m_nodes);
  244. uint32_t offt = sizeof(UnitHeader);
  245. h.renderables_offset = offt; offt += sizeof(UnitRenderable) * h.num_renderables;
  246. h.cameras_offset = offt; offt += sizeof(UnitCamera) * h.num_cameras;
  247. h.scene_graph_nodes_offset = offt; offt += sizeof(UnitNode) * h.num_scene_graph_nodes;
  248. // Write header
  249. out_file->write((char*) &h, sizeof(UnitHeader));
  250. // Write renderables
  251. if (array::size(m_renderables))
  252. out_file->write((char*) array::begin(m_renderables), sizeof(UnitRenderable) * h.num_renderables);
  253. // Write cameras
  254. if (array::size(m_cameras))
  255. out_file->write((char*) array::begin(m_cameras), sizeof(UnitCamera) * h.num_cameras);
  256. // Write node poses
  257. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  258. {
  259. uint32_t node_index = m_node_depths[i].index;
  260. GraphNode& node = m_nodes[node_index];
  261. UnitNode un;
  262. un.name = node.name;
  263. un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
  264. un.pose = Matrix4x4(node.rotation, node.position);
  265. out_file->write((char*) &un, sizeof(UnitNode));
  266. }
  267. }
  268. } // namespace unit_resource
  269. } // namespace crown