unit_resource.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 "string_utils.h"
  27. #include "json_parser.h"
  28. #include "vector.h"
  29. #include "log.h"
  30. #include "matrix4x4.h"
  31. #include "physics_types.h"
  32. #include "quaternion.h"
  33. #include "resource.h"
  34. #include "temp_allocator.h"
  35. #include "types.h"
  36. #include "vector3.h"
  37. #include "camera.h"
  38. #include "unit_resource.h"
  39. namespace crown
  40. {
  41. namespace unit_resource
  42. {
  43. static ProjectionType::Enum projection_name_to_enum(const char* name)
  44. {
  45. if (string::strcmp(name, "perspective") == 0) return ProjectionType::PERSPECTIVE;
  46. else if (string::strcmp(name, "orthographic") == 0) return ProjectionType::ORTHOGRAPHIC;
  47. CE_FATAL("Unknown projection type");
  48. return (ProjectionType::Enum) 0;
  49. }
  50. const StringId32 NO_PARENT = 0xFFFFFFFF;
  51. struct GraphNode
  52. {
  53. StringId32 name;
  54. StringId32 parent;
  55. Vector3 position;
  56. Quaternion rotation;
  57. };
  58. struct GraphNodeDepth
  59. {
  60. StringId32 name;
  61. uint32_t index;
  62. uint32_t depth;
  63. bool operator()(const GraphNodeDepth& a, const GraphNodeDepth& b)
  64. {
  65. return a.depth < b.depth;
  66. }
  67. };
  68. //-----------------------------------------------------------------------------
  69. uint32_t compute_link_depth(const GraphNode& node, const Array<GraphNode>& nodes)
  70. {
  71. if (node.parent == NO_PARENT) return 0;
  72. else
  73. {
  74. for (uint32_t i = 0; i < array::size(nodes); i++)
  75. {
  76. if (nodes[i].name == node.parent)
  77. {
  78. return 1 + compute_link_depth(nodes[i], nodes);
  79. }
  80. }
  81. }
  82. CE_FATAL("Node not found");
  83. return 0;
  84. }
  85. //-----------------------------------------------------------------------------
  86. uint32_t find_node_index(StringId32 name, const Array<GraphNodeDepth>& node_depths)
  87. {
  88. for (uint32_t i = 0; i < array::size(node_depths); i++)
  89. {
  90. if (node_depths[i].name == name)
  91. {
  92. return i;
  93. }
  94. }
  95. CE_FATAL("Node not found");
  96. return 0;
  97. }
  98. //-----------------------------------------------------------------------------
  99. int32_t find_node_parent_index(uint32_t node, const Array<GraphNode>& nodes, const Array<GraphNodeDepth>& node_depths)
  100. {
  101. StringId32 parent_name = nodes[node_depths[node].index].parent;
  102. if (parent_name == NO_PARENT) return -1;
  103. for (uint32_t i = 0; i < array::size(node_depths); i++)
  104. {
  105. if (parent_name == node_depths[i].name)
  106. {
  107. return i;
  108. }
  109. }
  110. CE_FATAL("Node not found");
  111. return 0;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void parse_nodes(JSONElement e, Array<GraphNode>& nodes, Array<GraphNodeDepth>& node_depths)
  115. {
  116. Vector<DynamicString> keys(default_allocator());
  117. e.to_keys(keys);
  118. for (uint32_t k = 0; k < vector::size(keys); k++)
  119. {
  120. const char* node_name = keys[k].c_str();
  121. JSONElement node = e.key(node_name);
  122. GraphNode gn;
  123. gn.name = string::murmur2_32(node_name, string::strlen(node_name));
  124. gn.parent = NO_PARENT;
  125. if (!node.key("parent").is_nil())
  126. {
  127. DynamicString parent_name;
  128. node.key("parent").to_string(parent_name);
  129. gn.parent = string::murmur2_32(parent_name.c_str(), parent_name.length(), 0);
  130. }
  131. JSONElement pos = node.key("position");
  132. JSONElement rot = node.key("rotation");
  133. gn.position = Vector3(pos[0].to_float(), pos[1].to_float(), pos[2].to_float());
  134. gn.rotation = Quaternion(Vector3(rot[0].to_float(), rot[1].to_float(), rot[2].to_float()), rot[3].to_float());
  135. GraphNodeDepth gnd;
  136. gnd.name = gn.name;
  137. gnd.index = array::size(nodes);
  138. gnd.depth = 0;
  139. array::push_back(nodes, gn);
  140. array::push_back(node_depths, gnd);
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphNodeDepth>& node_depths)
  145. {
  146. Vector<DynamicString> keys(default_allocator());
  147. e.to_keys(keys);
  148. for (uint32_t k = 0; k < vector::size(keys); k++)
  149. {
  150. const char* camera_name = keys[k].c_str();
  151. JSONElement camera = e.key(camera_name);
  152. JSONElement node = camera.key("node");
  153. JSONElement type = camera.key("type");
  154. JSONElement fov = camera.key_or_nil("fov");
  155. JSONElement near = camera.key_or_nil("near_clip_distance");
  156. JSONElement far = camera.key_or_nil("far_clip_distance");
  157. DynamicString node_name;
  158. node.to_string(node_name);
  159. DynamicString camera_type;
  160. type.to_string(camera_type);
  161. StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length());
  162. UnitCamera cn;
  163. cn.name = string::murmur2_32(camera_name, string::strlen(camera_name));
  164. cn.node = find_node_index(node_name_hash, node_depths);
  165. cn.type = projection_name_to_enum(camera_type.c_str());
  166. cn.fov = fov.is_nil() ? 16.0f / 9.0f : fov.to_float();
  167. cn.near = near.is_nil() ? 0.01f : near.to_float();
  168. cn.far = far.is_nil() ? 1000 : far.to_float();
  169. array::push_back(cameras, cn);
  170. }
  171. }
  172. //-----------------------------------------------------------------------------
  173. void parse_renderables(JSONElement e, Array<UnitRenderable>& renderables, const Array<GraphNodeDepth>& node_depths)
  174. {
  175. Vector<DynamicString> keys(default_allocator());
  176. e.to_keys(keys);
  177. for (uint32_t k = 0; k < vector::size(keys); k++)
  178. {
  179. const char* renderable_name = keys[k].c_str();
  180. JSONElement renderable = e.key(renderable_name);
  181. DynamicString node_name; renderable.key("node").to_string(node_name);
  182. StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length(), 0);
  183. UnitRenderable rn;
  184. rn.name = string::murmur2_32(renderable_name, string::strlen(renderable_name), 0);
  185. rn.node = find_node_index(node_name_hash, node_depths);
  186. rn.visible = renderable.key("visible").to_bool();
  187. DynamicString res_type;
  188. renderable.key("type").to_string(res_type);
  189. if (res_type == "mesh")
  190. {
  191. rn.type = UnitRenderable::MESH;
  192. rn.resource = renderable.key("resource").to_resource_id("mesh");
  193. }
  194. else if (res_type == "sprite")
  195. {
  196. rn.type = UnitRenderable::SPRITE;
  197. rn.resource = renderable.key("resource").to_resource_id("sprite");
  198. }
  199. else
  200. {
  201. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
  202. }
  203. array::push_back(renderables, rn);
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. void parse_keys(JSONElement e, Array<Key>& generic_keys, Array<char>& values)
  208. {
  209. Vector<DynamicString> keys(default_allocator());
  210. e.to_keys(keys);
  211. for (uint32_t k = 0; k < vector::size(keys); k++)
  212. {
  213. const char* key = keys[k].c_str();
  214. JSONElement value = e.key(key);
  215. Key out_key;
  216. out_key.name = string::murmur2_32(key, string::strlen(key));
  217. out_key.offset = array::size(values);
  218. if (value.is_bool()) out_key.type = ValueType::BOOL;
  219. else if (value.is_number()) out_key.type = ValueType::FLOAT;
  220. else if (value.is_string()) out_key.type = ValueType::STRING;
  221. else if (value.is_array() && value.size() == 3) out_key.type = ValueType::VECTOR3;
  222. else CE_FATAL("Value type not supported");
  223. array::push_back(generic_keys, out_key);
  224. switch (out_key.type)
  225. {
  226. case ValueType::BOOL:
  227. {
  228. uint32_t val = value.to_bool();
  229. array::push(values, (char*) &val, sizeof(uint32_t));
  230. break;
  231. }
  232. case ValueType::FLOAT:
  233. {
  234. float val = value.to_float();
  235. array::push(values, (char*) &val, sizeof(float));
  236. break;
  237. }
  238. case ValueType::STRING:
  239. {
  240. DynamicString val;
  241. value.to_string(val);
  242. StringId32 val_hash = string::murmur2_32(val.c_str(), val.length());
  243. array::push(values, (char*) &val_hash, sizeof(StringId32));
  244. break;
  245. }
  246. case ValueType::VECTOR3:
  247. {
  248. float val[3];
  249. val[0] = value[0].to_float();
  250. val[1] = value[1].to_float();
  251. val[2] = value[2].to_float();
  252. array::push(values, (char*) val, sizeof(float) * 3);
  253. break;
  254. }
  255. default:
  256. {
  257. CE_FATAL("Oops, you should not be here");
  258. return;
  259. }
  260. }
  261. }
  262. }
  263. void parse_materials(JSONElement e, Array<UnitMaterial>& materials)
  264. {
  265. for (uint32_t i = 0; i < e.size(); i++)
  266. {
  267. ResourceId mat_id = e[i].to_resource_id("material");
  268. UnitMaterial um;
  269. um.id = mat_id.name;
  270. array::push_back(materials, um);
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  275. {
  276. File* file = fs.open(resource_path, FOM_READ);
  277. JSONParser json(*file);
  278. fs.close(file);
  279. JSONElement root = json.root();
  280. ResourceId m_physics_resource;
  281. Array<GraphNode> m_nodes(default_allocator());
  282. Array<GraphNodeDepth> m_node_depths(default_allocator());
  283. Array<UnitCamera> m_cameras(default_allocator());
  284. Array<UnitRenderable> m_renderables(default_allocator());
  285. Array<Key> m_keys(default_allocator());
  286. Array<char> m_values(default_allocator());
  287. Array<UnitMaterial> m_materials(default_allocator());
  288. // Check for nodes
  289. if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
  290. for (uint32_t i = 0; i < array::size(m_nodes); i++)
  291. {
  292. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  293. }
  294. std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
  295. if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
  296. if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
  297. if (root.has_key("keys")) parse_keys(root.key("keys"), m_keys, m_values);
  298. if (root.has_key("materials")) parse_materials(root.key("materials"), m_materials);
  299. // Check if the unit has a .physics resource
  300. DynamicString unit_name(resource_path);
  301. unit_name.strip_trailing(".unit");
  302. DynamicString physics_name = unit_name;
  303. physics_name += ".physics";
  304. if (fs.exists(physics_name.c_str()))
  305. {
  306. m_physics_resource = ResourceId("physics", unit_name.c_str());
  307. }
  308. else
  309. {
  310. m_physics_resource = ResourceId();
  311. }
  312. UnitHeader h;
  313. h.physics_resource = m_physics_resource;
  314. h.num_renderables = array::size(m_renderables);
  315. h.num_materials = array::size(m_materials);
  316. h.num_cameras = array::size(m_cameras);
  317. h.num_scene_graph_nodes = array::size(m_nodes);
  318. h.num_keys = array::size(m_keys);
  319. h.values_size = array::size(m_values);
  320. uint32_t offt = sizeof(UnitHeader);
  321. h.renderables_offset = offt; offt += sizeof(UnitRenderable) * h.num_renderables;
  322. h.materials_offset = offt; offt += sizeof(UnitMaterial) * h.num_materials;
  323. h.cameras_offset = offt; offt += sizeof(UnitCamera) * h.num_cameras;
  324. h.scene_graph_nodes_offset = offt; offt += sizeof(UnitNode) * h.num_scene_graph_nodes;
  325. h.keys_offset = offt; offt += sizeof(Key) * h.num_keys;
  326. h.values_offset = offt;
  327. // Write header
  328. out_file->write((char*) &h, sizeof(UnitHeader));
  329. // Write renderables
  330. if (array::size(m_renderables))
  331. out_file->write((char*) array::begin(m_renderables), sizeof(UnitRenderable) * h.num_renderables);
  332. // Write materials
  333. if (array::size(m_materials))
  334. out_file->write((char*) array::begin(m_materials), sizeof(UnitMaterial) * h.num_materials);
  335. // Write cameras
  336. if (array::size(m_cameras))
  337. out_file->write((char*) array::begin(m_cameras), sizeof(UnitCamera) * h.num_cameras);
  338. // Write node poses
  339. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  340. {
  341. uint32_t node_index = m_node_depths[i].index;
  342. GraphNode& node = m_nodes[node_index];
  343. UnitNode un;
  344. un.name = node.name;
  345. un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
  346. un.pose = Matrix4x4(node.rotation, node.position);
  347. out_file->write((char*) &un, sizeof(UnitNode));
  348. }
  349. // Write key/values
  350. if (array::size(m_keys))
  351. {
  352. out_file->write((char*) array::begin(m_keys), sizeof(Key) * h.num_keys);
  353. out_file->write((char*) array::begin(m_values), array::size(m_values));
  354. }
  355. }
  356. } // namespace unit_resource
  357. } // namespace crown