UnitResource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 "List.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 List<GraphNode>& nodes)
  62. {
  63. if (node.parent == NO_PARENT) return 0;
  64. else
  65. {
  66. for (uint32_t i = 0; i < nodes.size(); 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 List<GraphNodeDepth>& node_depths)
  78. {
  79. for (uint32_t i = 0; i < node_depths.size(); i++)
  80. {
  81. if (node_depths[i].name == name)
  82. {
  83. return i;
  84. }
  85. }
  86. CE_FATAL("Node not found");
  87. }
  88. //-----------------------------------------------------------------------------
  89. int32_t find_node_parent_index(uint32_t node, const List<GraphNode>& nodes, const List<GraphNodeDepth>& node_depths)
  90. {
  91. StringId32 parent_name = nodes[node_depths[node].index].parent;
  92. if (parent_name == NO_PARENT) return -1;
  93. for (uint32_t i = 0; i < node_depths.size(); i++)
  94. {
  95. if (parent_name == node_depths[i].name)
  96. {
  97. return i;
  98. }
  99. }
  100. CE_FATAL("Node not found");
  101. }
  102. //-----------------------------------------------------------------------------
  103. void parse_node(JSONElement e, List<GraphNode>& nodes, List<GraphNodeDepth>& node_depths)
  104. {
  105. JSONElement name = e.key("name");
  106. JSONElement parent = e.key("parent");
  107. JSONElement pos = e.key("position");
  108. JSONElement rot = e.key("rotation");
  109. GraphNode gn;
  110. gn.name = hash::murmur2_32(name.string_value(), name.size(), 0);
  111. gn.parent = parent.is_nil() ? NO_PARENT : hash::murmur2_32(parent.string_value(), parent.size(), 0);
  112. gn.position = Vector3(pos[0].float_value(), pos[1].float_value(), pos[2].float_value());
  113. gn.rotation = Quaternion(Vector3(rot[0].float_value(), rot[1].float_value(), rot[2].float_value()), rot[3].float_value());
  114. GraphNodeDepth gnd;
  115. gnd.name = gn.name;
  116. gnd.index = nodes.size();
  117. gnd.depth = 0;
  118. nodes.push_back(gn);
  119. node_depths.push_back(gnd);
  120. }
  121. //-----------------------------------------------------------------------------
  122. void parse_camera(JSONElement e, List<UnitCamera>& cameras, const List<GraphNodeDepth>& node_depths)
  123. {
  124. JSONElement name = e.key("name");
  125. JSONElement node = e.key("node");
  126. StringId32 node_name = hash::murmur2_32(node.string_value(), node.size(), 0);
  127. UnitCamera cn;
  128. cn.name = hash::murmur2_32(name.string_value(), name.size(), 0);
  129. cn.node = find_node_index(node_name, node_depths);
  130. cameras.push_back(cn);
  131. }
  132. //-----------------------------------------------------------------------------
  133. void parse_renderable(JSONElement e, List<UnitRenderable>& renderables, const List<GraphNodeDepth>& node_depths)
  134. {
  135. JSONElement name = e.key("name");
  136. JSONElement node = e.key("node");
  137. JSONElement type = e.key("type");
  138. JSONElement res = e.key("resource");
  139. JSONElement vis = e.key("visible");
  140. StringId32 node_name = hash::murmur2_32(node.string_value(), node.size(), 0);
  141. UnitRenderable rn;
  142. rn.name = hash::murmur2_32(name.string_value(), name.size(), 0);
  143. rn.node = find_node_index(node_name, node_depths);
  144. rn.visible = vis.bool_value();
  145. const char* res_type = type.string_value();
  146. DynamicString res_name;
  147. if (string::strcmp(res_type, "mesh") == 0)
  148. {
  149. rn.type = UnitRenderable::MESH;
  150. res_name += res.string_value();
  151. res_name += ".mesh";
  152. }
  153. else if (string::strcmp(res_type, "sprite") == 0)
  154. {
  155. rn.type = UnitRenderable::SPRITE;
  156. res_name += res.string_value();
  157. res_name += ".sprite";
  158. }
  159. else
  160. {
  161. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type);
  162. }
  163. rn.resource.id = hash::murmur2_64(res_name.c_str(), string::strlen(res_name.c_str()), 0);
  164. renderables.push_back(rn);
  165. }
  166. //-----------------------------------------------------------------------------
  167. void parse_actor(JSONElement e, List<UnitActor>& actors, const List<GraphNodeDepth>& node_depths)
  168. {
  169. JSONElement name = e.key("name");
  170. JSONElement node = e.key("node");
  171. JSONElement type = e.key("type");
  172. JSONElement shape = e.key("shape");
  173. JSONElement active = e.key("active");
  174. StringId32 node_name = hash::murmur2_32(node.string_value(), node.size(), 0);
  175. UnitActor an;
  176. an.name = hash::murmur2_32(name.string_value(), name.size(), 0);
  177. an.node = find_node_index(node_name, node_depths);
  178. an.type = string::strcmp(type.string_value(), "STATIC") == 0 ? UnitActor::STATIC : UnitActor::DYNAMIC;
  179. an.shape = string::strcmp(shape.string_value(), "SPHERE") == 0 ? UnitActor::SPHERE :
  180. string::strcmp(shape.string_value(), "BOX") == 0 ? UnitActor::BOX : UnitActor::PLANE;
  181. an.active = active.bool_value();
  182. actors.push_back(an);
  183. }
  184. //-----------------------------------------------------------------------------
  185. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  186. {
  187. File* file = fs.open(resource_path, FOM_READ);
  188. char file_buf[4096];
  189. file->read(file_buf, file->size());
  190. fs.close(file);
  191. JSONParser json(file_buf);
  192. JSONElement root = json.root();
  193. ResourceId m_physics_resource;
  194. ResourceId m_material_resource;
  195. List<GraphNode> m_nodes(default_allocator());
  196. List<GraphNodeDepth> m_node_depths(default_allocator());
  197. List<UnitCamera> m_cameras(default_allocator());
  198. List<UnitRenderable> m_renderables(default_allocator());
  199. List<UnitActor> m_actors(default_allocator());
  200. // Check for nodes
  201. if (root.has_key("nodes"))
  202. {
  203. JSONElement nodes = root.key("nodes");
  204. const uint32_t num_nodes = nodes.size();
  205. for (uint32_t i = 0; i < num_nodes; i++)
  206. {
  207. parse_node(nodes[i], m_nodes, m_node_depths);
  208. }
  209. }
  210. for (uint32_t i = 0; i < m_nodes.size(); i++)
  211. {
  212. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  213. }
  214. std::sort(m_node_depths.begin(), m_node_depths.end(), GraphNodeDepth());
  215. // Check for renderable
  216. if (root.has_key("renderables"))
  217. {
  218. JSONElement renderables = root.key("renderables");
  219. uint32_t renderables_size = renderables.size();
  220. for (uint32_t i = 0; i < renderables_size; i++)
  221. {
  222. parse_renderable(renderables[i], m_renderables, m_node_depths);
  223. }
  224. }
  225. // Check for cameras
  226. if (root.has_key("cameras"))
  227. {
  228. JSONElement cameras = root.key("cameras");
  229. uint32_t num_cameras = cameras.size();
  230. for (uint32_t i = 0; i < num_cameras; i++)
  231. {
  232. parse_camera(cameras[i], m_cameras, m_node_depths);
  233. }
  234. }
  235. // check for actors
  236. if (root.has_key("actors"))
  237. {
  238. JSONElement actors = root.key("actors");
  239. uint32_t num_actors = actors.size();
  240. for (uint32_t i = 0; i < num_actors; i++)
  241. {
  242. parse_actor(actors[i], m_actors, m_node_depths);
  243. }
  244. }
  245. // Check if the unit has a .physics resource
  246. DynamicString unit_name(resource_path);
  247. unit_name.strip_trailing("unit");
  248. DynamicString physics_name = unit_name;
  249. physics_name += "physics";
  250. if (fs.is_file(physics_name.c_str()))
  251. {
  252. m_physics_resource.id = hash::murmur2_64(physics_name.c_str(), string::strlen(physics_name.c_str()), 0);
  253. }
  254. else
  255. {
  256. m_physics_resource.id = 0;
  257. }
  258. // Check if the unit has a .material resource
  259. DynamicString material_name = unit_name;
  260. material_name += "material";
  261. if (fs.is_file(material_name.c_str()))
  262. {
  263. m_material_resource.id = hash::murmur2_64(material_name.c_str(), string::strlen(material_name.c_str()), 0);
  264. }
  265. else
  266. {
  267. m_material_resource.id = 0;
  268. }
  269. UnitHeader h;
  270. h.physics_resource = m_physics_resource;
  271. h.material_resource = m_material_resource;
  272. h.num_renderables = m_renderables.size();
  273. h.num_cameras = m_cameras.size();
  274. h.num_actors = m_actors.size();
  275. h.num_scene_graph_nodes = m_nodes.size();
  276. uint32_t offt = sizeof(UnitHeader);
  277. h.renderables_offset = offt; offt += sizeof(UnitRenderable) * h.num_renderables;
  278. h.cameras_offset = offt; offt += sizeof(UnitCamera) * h.num_cameras;
  279. h.actors_offset = offt; offt += sizeof(UnitActor) * h.num_actors;
  280. h.scene_graph_names_offset = offt; offt += sizeof(StringId32) * h.num_scene_graph_nodes;
  281. h.scene_graph_poses_offset = offt; offt += sizeof(Matrix4x4) * h.num_scene_graph_nodes;
  282. h.scene_graph_parents_offset = offt; offt += sizeof(int32_t) * h.num_scene_graph_nodes;
  283. // Write header
  284. out_file->write((char*) &h, sizeof(UnitHeader));
  285. // Write renderables
  286. if (m_renderables.size())
  287. out_file->write((char*) m_renderables.begin(), sizeof(UnitRenderable) * h.num_renderables);
  288. // Write cameras
  289. if (m_cameras.size())
  290. out_file->write((char*) m_cameras.begin(), sizeof(UnitCamera) * h.num_cameras);
  291. // Write actors
  292. if (m_actors.size())
  293. out_file->write((char*) m_actors.begin(), sizeof(UnitActor) * h.num_actors);
  294. // Write node names
  295. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  296. {
  297. StringId32 name = m_node_depths[i].name;
  298. out_file->write((char*) &name, sizeof(StringId32));
  299. }
  300. // Write node poses
  301. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  302. {
  303. uint32_t node_index = m_node_depths[i].index;
  304. GraphNode& node = m_nodes[node_index];
  305. Matrix4x4 pose(node.rotation, node.position);
  306. out_file->write((char*) pose.to_float_ptr(), sizeof(float) * 16);
  307. }
  308. // Write parent hierarchy
  309. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  310. {
  311. int32_t parent = find_node_parent_index(i, m_nodes, m_node_depths);
  312. out_file->write((char*) &parent, sizeof(int32_t));
  313. }
  314. }
  315. } // namespace unit_resource
  316. } // namespace crown