2
0

UnitResource.cpp 11 KB

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