unit_resource.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. struct Projection
  44. {
  45. const char* name;
  46. ProjectionType::Enum type;
  47. };
  48. static const Projection s_projection[] =
  49. {
  50. { "perspective", ProjectionType::PERSPECTIVE },
  51. { "orthographic", ProjectionType::ORTHOGRAPHIC }
  52. };
  53. static ProjectionType::Enum projection_name_to_enum(const char* name)
  54. {
  55. for (uint32_t i = 0; i < ProjectionType::COUNT; i++)
  56. {
  57. if (string::strcmp(name, s_projection[i].name) == 0)
  58. return s_projection[i].type;
  59. }
  60. CE_FATAL("Bad projection type");
  61. return (ProjectionType::Enum)0;
  62. }
  63. const StringId32 NO_PARENT = 0xFFFFFFFF;
  64. struct GraphNode
  65. {
  66. StringId32 name;
  67. StringId32 parent;
  68. Vector3 position;
  69. Quaternion rotation;
  70. };
  71. struct GraphNodeDepth
  72. {
  73. StringId32 name;
  74. uint32_t index;
  75. uint32_t depth;
  76. bool operator()(const GraphNodeDepth& a, const GraphNodeDepth& b)
  77. {
  78. return a.depth < b.depth;
  79. }
  80. };
  81. uint32_t compute_link_depth(const GraphNode& node, const Array<GraphNode>& nodes)
  82. {
  83. if (node.parent == NO_PARENT) return 0;
  84. else
  85. {
  86. for (uint32_t i = 0; i < array::size(nodes); i++)
  87. {
  88. if (nodes[i].name == node.parent)
  89. {
  90. return 1 + compute_link_depth(nodes[i], nodes);
  91. }
  92. }
  93. }
  94. CE_FATAL("Node not found");
  95. return 0;
  96. }
  97. uint32_t find_node_index(StringId32 name, const Array<GraphNodeDepth>& node_depths)
  98. {
  99. for (uint32_t i = 0; i < array::size(node_depths); i++)
  100. {
  101. if (node_depths[i].name == name)
  102. {
  103. return i;
  104. }
  105. }
  106. CE_FATAL("Node not found");
  107. return 0;
  108. }
  109. int32_t find_node_parent_index(uint32_t node, const Array<GraphNode>& nodes, const Array<GraphNodeDepth>& node_depths)
  110. {
  111. StringId32 parent_name = nodes[node_depths[node].index].parent;
  112. if (parent_name == NO_PARENT) return -1;
  113. for (uint32_t i = 0; i < array::size(node_depths); i++)
  114. {
  115. if (parent_name == node_depths[i].name)
  116. {
  117. return i;
  118. }
  119. }
  120. CE_FATAL("Node not found");
  121. return 0;
  122. }
  123. void parse_nodes(JSONElement e, Array<GraphNode>& nodes, Array<GraphNodeDepth>& node_depths)
  124. {
  125. Vector<DynamicString> keys(default_allocator());
  126. e.to_keys(keys);
  127. for (uint32_t k = 0; k < vector::size(keys); k++)
  128. {
  129. const char* node_name = keys[k].c_str();
  130. JSONElement node = e.key(node_name);
  131. GraphNode gn;
  132. gn.name = string::murmur2_32(node_name, string::strlen(node_name));
  133. gn.parent = NO_PARENT;
  134. if (!node.key("parent").is_nil())
  135. {
  136. DynamicString parent_name;
  137. node.key("parent").to_string(parent_name);
  138. gn.parent = string::murmur2_32(parent_name.c_str(), parent_name.length(), 0);
  139. }
  140. JSONElement pos = node.key("position");
  141. JSONElement rot = node.key("rotation");
  142. gn.position = Vector3(pos[0].to_float(), pos[1].to_float(), pos[2].to_float());
  143. gn.rotation = Quaternion(Vector3(rot[0].to_float(), rot[1].to_float(), rot[2].to_float()), rot[3].to_float());
  144. GraphNodeDepth gnd;
  145. gnd.name = gn.name;
  146. gnd.index = array::size(nodes);
  147. gnd.depth = 0;
  148. array::push_back(nodes, gn);
  149. array::push_back(node_depths, gnd);
  150. }
  151. }
  152. void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphNodeDepth>& node_depths)
  153. {
  154. Vector<DynamicString> keys(default_allocator());
  155. e.to_keys(keys);
  156. for (uint32_t k = 0; k < vector::size(keys); k++)
  157. {
  158. const char* camera_name = keys[k].c_str();
  159. JSONElement camera = e.key(camera_name);
  160. JSONElement node = camera.key("node");
  161. JSONElement type = camera.key("type");
  162. DynamicString node_name;
  163. node.to_string(node_name);
  164. DynamicString camera_type;
  165. type.to_string(camera_type);
  166. StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length());
  167. UnitCamera cn;
  168. cn.name = string::murmur2_32(camera_name, string::strlen(camera_name));
  169. cn.node = find_node_index(node_name_hash, node_depths);
  170. cn.type = projection_name_to_enum(camera_type.c_str());
  171. cn.fov = camera.key_or_nil("fov").to_float(16.0f / 9.0f);
  172. cn.near = camera.key_or_nil("near_clip_distance").to_float(0.01f);
  173. cn.far = camera.key_or_nil("far_clip_distance").to_float(1000.0f);
  174. array::push_back(cameras, cn);
  175. }
  176. }
  177. void parse_renderables(JSONElement e, Array<UnitRenderable>& renderables, const Array<GraphNodeDepth>& node_depths)
  178. {
  179. Vector<DynamicString> keys(default_allocator());
  180. e.to_keys(keys);
  181. for (uint32_t k = 0; k < vector::size(keys); k++)
  182. {
  183. const char* renderable_name = keys[k].c_str();
  184. JSONElement renderable = e.key(renderable_name);
  185. DynamicString node_name; renderable.key("node").to_string(node_name);
  186. StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length(), 0);
  187. UnitRenderable rn;
  188. rn.name = string::murmur2_32(renderable_name, string::strlen(renderable_name), 0);
  189. rn.node = find_node_index(node_name_hash, node_depths);
  190. rn.visible = renderable.key("visible").to_bool();
  191. DynamicString res_type;
  192. renderable.key("type").to_string(res_type);
  193. if (res_type == "mesh")
  194. {
  195. rn.type = UnitRenderable::MESH;
  196. rn.resource = renderable.key("resource").to_resource_id("mesh").name;
  197. }
  198. else if (res_type == "sprite")
  199. {
  200. rn.type = UnitRenderable::SPRITE;
  201. rn.resource = renderable.key("resource").to_resource_id("sprite").name;
  202. }
  203. else
  204. {
  205. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
  206. }
  207. array::push_back(renderables, rn);
  208. }
  209. }
  210. void parse_keys(JSONElement e, Array<Key>& generic_keys, Array<char>& values)
  211. {
  212. Vector<DynamicString> keys(default_allocator());
  213. e.to_keys(keys);
  214. for (uint32_t k = 0; k < vector::size(keys); k++)
  215. {
  216. const char* key = keys[k].c_str();
  217. JSONElement value = e.key(key);
  218. Key out_key;
  219. out_key.name = string::murmur2_32(key, string::strlen(key));
  220. out_key.offset = array::size(values);
  221. if (value.is_bool()) out_key.type = ValueType::BOOL;
  222. else if (value.is_number()) out_key.type = ValueType::FLOAT;
  223. else if (value.is_string()) out_key.type = ValueType::STRING;
  224. else if (value.is_array() && value.size() == 3) out_key.type = ValueType::VECTOR3;
  225. else CE_FATAL("Value type not supported");
  226. array::push_back(generic_keys, out_key);
  227. switch (out_key.type)
  228. {
  229. case ValueType::BOOL:
  230. {
  231. uint32_t val = value.to_bool();
  232. array::push(values, (char*) &val, sizeof(uint32_t));
  233. break;
  234. }
  235. case ValueType::FLOAT:
  236. {
  237. float val = value.to_float();
  238. array::push(values, (char*) &val, sizeof(float));
  239. break;
  240. }
  241. case ValueType::STRING:
  242. {
  243. DynamicString val;
  244. value.to_string(val);
  245. StringId32 val_hash = string::murmur2_32(val.c_str(), val.length());
  246. array::push(values, (char*) &val_hash, sizeof(StringId32));
  247. break;
  248. }
  249. case ValueType::VECTOR3:
  250. {
  251. float val[3];
  252. val[0] = value[0].to_float();
  253. val[1] = value[1].to_float();
  254. val[2] = value[2].to_float();
  255. array::push(values, (char*) val, sizeof(float) * 3);
  256. break;
  257. }
  258. default:
  259. {
  260. CE_FATAL("Oops, you should not be here");
  261. return;
  262. }
  263. }
  264. }
  265. }
  266. void parse_materials(JSONElement e, Array<UnitMaterial>& materials)
  267. {
  268. for (uint32_t i = 0; i < e.size(); i++)
  269. {
  270. ResourceId mat_id = e[i].to_resource_id("material");
  271. UnitMaterial um;
  272. um.id = mat_id.name;
  273. array::push_back(materials, um);
  274. }
  275. }
  276. void compile(const char* path, CompileOptions& opts)
  277. {
  278. static const uint32_t VERSION = 1;
  279. Buffer buf = opts.read(path);
  280. JSONParser json(array::begin(buf));
  281. JSONElement root = json.root();
  282. ResourceId m_physics_resource;
  283. Array<GraphNode> m_nodes(default_allocator());
  284. Array<GraphNodeDepth> m_node_depths(default_allocator());
  285. Array<UnitCamera> m_cameras(default_allocator());
  286. Array<UnitRenderable> m_renderables(default_allocator());
  287. Array<Key> m_keys(default_allocator());
  288. Array<char> m_values(default_allocator());
  289. Array<UnitMaterial> m_materials(default_allocator());
  290. // Check for nodes
  291. if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
  292. for (uint32_t i = 0; i < array::size(m_nodes); i++)
  293. {
  294. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  295. }
  296. std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
  297. if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
  298. if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
  299. if (root.has_key("keys")) parse_keys(root.key("keys"), m_keys, m_values);
  300. if (root.has_key("materials")) parse_materials(root.key("materials"), m_materials);
  301. // Check if the unit has a .physics resource
  302. DynamicString unit_name(path);
  303. unit_name.strip_trailing(".unit");
  304. DynamicString physics_name = unit_name;
  305. physics_name += ".physics";
  306. if (opts._fs.exists(physics_name.c_str()))
  307. {
  308. m_physics_resource = ResourceId("physics", unit_name.c_str());
  309. }
  310. else
  311. {
  312. m_physics_resource = ResourceId();
  313. }
  314. ResourceId sprite_anim;
  315. sprite_anim.type = 0;
  316. sprite_anim.name = 0;
  317. if (root.has_key("sprite_animation"))
  318. sprite_anim = root.key("sprite_animation").to_resource_id("sprite_animation");
  319. UnitResource ur;
  320. ur.version = VERSION;
  321. ur.physics_resource = m_physics_resource.name;
  322. ur.sprite_animation = sprite_anim.name;
  323. ur.num_renderables = array::size(m_renderables);
  324. ur.num_materials = array::size(m_materials);
  325. ur.num_cameras = array::size(m_cameras);
  326. ur.num_scene_graph_nodes = array::size(m_nodes);
  327. ur.num_keys = array::size(m_keys);
  328. ur.values_size = array::size(m_values);
  329. uint32_t offt = sizeof(UnitResource);
  330. ur.renderables_offset = offt; offt += sizeof(UnitRenderable) * ur.num_renderables;
  331. ur.materials_offset = offt; offt += sizeof(UnitMaterial) * ur.num_materials;
  332. ur.cameras_offset = offt; offt += sizeof(UnitCamera) * ur.num_cameras;
  333. ur.scene_graph_nodes_offset = offt; offt += sizeof(UnitNode) * ur.num_scene_graph_nodes;
  334. ur.keys_offset = offt; offt += sizeof(Key) * ur.num_keys;
  335. ur.values_offset = offt;
  336. opts.write(ur.version);
  337. opts.write(ur._pad);
  338. opts.write(ur.physics_resource);
  339. opts.write(ur.sprite_animation);
  340. opts.write(ur.num_renderables);
  341. opts.write(ur.renderables_offset);
  342. opts.write(ur.num_materials);
  343. opts.write(ur.materials_offset);
  344. opts.write(ur.num_cameras);
  345. opts.write(ur.cameras_offset);
  346. opts.write(ur.num_scene_graph_nodes);
  347. opts.write(ur.scene_graph_nodes_offset);
  348. opts.write(ur.num_keys);
  349. opts.write(ur.keys_offset);
  350. opts.write(ur.values_size);
  351. opts.write(ur.values_offset);
  352. // Renderables
  353. for (uint32_t i = 0; i < array::size(m_renderables); i++)
  354. {
  355. opts.write(m_renderables[i].type);
  356. opts.write(m_renderables[i]._pad);
  357. opts.write(m_renderables[i].resource);
  358. opts.write(m_renderables[i].name);
  359. opts.write(m_renderables[i].node);
  360. opts.write(m_renderables[i].visible);
  361. opts.write(m_renderables[i]._pad1[0]);
  362. opts.write(m_renderables[i]._pad1[1]);
  363. opts.write(m_renderables[i]._pad1[2]);
  364. opts.write(m_renderables[i]._pad2[0]);
  365. opts.write(m_renderables[i]._pad2[1]);
  366. opts.write(m_renderables[i]._pad2[2]);
  367. opts.write(m_renderables[i]._pad2[3]);
  368. }
  369. // Materials
  370. for (uint32_t i = 0; i < array::size(m_materials); i++)
  371. {
  372. opts.write(m_materials[i]);
  373. }
  374. // Cameras
  375. for (uint32_t i = 0; i < array::size(m_cameras); i++)
  376. {
  377. opts.write(m_cameras[i].name);
  378. opts.write(m_cameras[i].node);
  379. opts.write(m_cameras[i].type);
  380. opts.write(m_cameras[i].fov);
  381. opts.write(m_cameras[i].near);
  382. opts.write(m_cameras[i].far);
  383. }
  384. // Write node poses
  385. for (uint32_t i = 0; i < ur.num_scene_graph_nodes; i++)
  386. {
  387. uint32_t node_index = m_node_depths[i].index;
  388. GraphNode& node = m_nodes[node_index];
  389. UnitNode un;
  390. un.name = node.name;
  391. un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
  392. un.pose = Matrix4x4(node.rotation, node.position);
  393. opts.write(un.name);
  394. opts.write(un.pose);
  395. opts.write(un.parent);
  396. }
  397. // Key/values
  398. for (uint32_t i = 0; i < array::size(m_keys); i++)
  399. {
  400. opts.write(m_keys[i].name);
  401. opts.write(m_keys[i].type);
  402. opts.write(m_keys[i].offset);
  403. }
  404. for (uint32_t i = 0; i < array::size(m_values); i++)
  405. {
  406. opts.write(m_values[i]);
  407. }
  408. }
  409. void* load(File& file, Allocator& a)
  410. {
  411. const size_t file_size = file.size();
  412. void* res = a.allocate(file_size);
  413. file.read(res, file_size);
  414. return res;
  415. }
  416. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  417. {
  418. }
  419. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  420. {
  421. }
  422. void unload(Allocator& allocator, void* resource)
  423. {
  424. allocator.deallocate(resource);
  425. }
  426. StringId64 sprite_animation(const UnitResource* ur)
  427. {
  428. return ur->sprite_animation;
  429. }
  430. StringId64 physics_resource(const UnitResource* ur)
  431. {
  432. return ur->physics_resource;
  433. }
  434. uint32_t num_renderables(const UnitResource* ur)
  435. {
  436. return ur->num_renderables;
  437. }
  438. const UnitRenderable* get_renderable(const UnitResource* ur, uint32_t i)
  439. {
  440. CE_ASSERT(i < num_renderables(ur), "Index out of bounds");
  441. UnitRenderable* begin = (UnitRenderable*) ((char*)ur + ur->renderables_offset);
  442. return &begin[i];
  443. }
  444. uint32_t num_materials(const UnitResource* ur)
  445. {
  446. return ur->num_materials;
  447. }
  448. const UnitMaterial* get_material(const UnitResource* ur, uint32_t i)
  449. {
  450. CE_ASSERT(i < num_materials(ur), "Index out of bounds");
  451. UnitMaterial* begin = (UnitMaterial*) ((char*)ur + ur->materials_offset);
  452. return &begin[i];
  453. }
  454. uint32_t num_cameras(const UnitResource* ur)
  455. {
  456. return ur->num_cameras;
  457. }
  458. const UnitCamera* get_camera(const UnitResource* ur, uint32_t i)
  459. {
  460. CE_ASSERT(i < num_cameras(ur), "Index out of bounds");
  461. UnitCamera* begin = (UnitCamera*) ((char*)ur + ur->cameras_offset);
  462. return &begin[i];
  463. }
  464. uint32_t num_scene_graph_nodes(const UnitResource* ur)
  465. {
  466. return ur->num_scene_graph_nodes;
  467. }
  468. const UnitNode* scene_graph_nodes(const UnitResource* ur)
  469. {
  470. return (UnitNode*) ((char*)ur + ur->scene_graph_nodes_offset);
  471. }
  472. uint32_t num_keys(const UnitResource* ur)
  473. {
  474. return ur->num_keys;
  475. }
  476. bool has_key(const UnitResource* ur, const char* k)
  477. {
  478. const uint32_t nk = num_keys(ur);
  479. Key* begin = (Key*) ((char*)ur + ur->keys_offset);
  480. for (uint32_t i = 0; i < nk; i++)
  481. {
  482. if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
  483. {
  484. return true;
  485. }
  486. }
  487. return false;
  488. }
  489. bool get_key(const UnitResource* ur, const char* k, Key& out_k)
  490. {
  491. const uint32_t nk = num_keys(ur);
  492. Key* begin = (Key*) ((char*)ur + ur->keys_offset);
  493. for (uint32_t i = 0; i < nk; i++)
  494. {
  495. if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
  496. {
  497. out_k = begin[i];
  498. return true;
  499. }
  500. }
  501. return false;
  502. }
  503. uint32_t values_size(const UnitResource* ur)
  504. {
  505. return ur->values_size;
  506. }
  507. const char* values(const UnitResource* ur)
  508. {
  509. return ((char*)ur + ur->values_offset);
  510. }
  511. } // namespace unit_resource
  512. } // namespace crown