unit_resource.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "allocator.h"
  6. #include "file.h"
  7. #include "filesystem.h"
  8. #include "string_utils.h"
  9. #include "json_parser.h"
  10. #include "vector.h"
  11. #include "log.h"
  12. #include "matrix4x4.h"
  13. #include "physics_types.h"
  14. #include "quaternion.h"
  15. #include "compile_options.h"
  16. #include "temp_allocator.h"
  17. #include "types.h"
  18. #include "vector3.h"
  19. #include "camera.h"
  20. #include "unit_resource.h"
  21. namespace crown
  22. {
  23. namespace unit_resource
  24. {
  25. struct Projection
  26. {
  27. const char* name;
  28. ProjectionType::Enum type;
  29. };
  30. static const Projection s_projection[] =
  31. {
  32. { "perspective", ProjectionType::PERSPECTIVE },
  33. { "orthographic", ProjectionType::ORTHOGRAPHIC }
  34. };
  35. static ProjectionType::Enum projection_name_to_enum(const char* name)
  36. {
  37. for (uint32_t i = 0; i < ProjectionType::COUNT; i++)
  38. {
  39. if (strcmp(name, s_projection[i].name) == 0)
  40. return s_projection[i].type;
  41. }
  42. CE_FATAL("Bad projection type");
  43. return (ProjectionType::Enum)0;
  44. }
  45. const StringId32 NO_PARENT(0xFFFFFFFF);
  46. struct GraphNode
  47. {
  48. StringId32 name;
  49. StringId32 parent;
  50. Vector3 position;
  51. Quaternion rotation;
  52. };
  53. struct GraphNodeDepth
  54. {
  55. StringId32 name;
  56. uint32_t index;
  57. uint32_t depth;
  58. bool operator()(const GraphNodeDepth& a, const GraphNodeDepth& b)
  59. {
  60. return a.depth < b.depth;
  61. }
  62. };
  63. uint32_t compute_link_depth(const GraphNode& node, const Array<GraphNode>& nodes)
  64. {
  65. if (node.parent == NO_PARENT) return 0;
  66. else
  67. {
  68. for (uint32_t i = 0; i < array::size(nodes); i++)
  69. {
  70. if (nodes[i].name == node.parent)
  71. {
  72. return 1 + compute_link_depth(nodes[i], nodes);
  73. }
  74. }
  75. }
  76. CE_FATAL("Node not found");
  77. return 0;
  78. }
  79. uint32_t find_node_index(StringId32 name, const Array<GraphNodeDepth>& node_depths)
  80. {
  81. for (uint32_t i = 0; i < array::size(node_depths); i++)
  82. {
  83. if (node_depths[i].name == name)
  84. {
  85. return i;
  86. }
  87. }
  88. CE_FATAL("Node not found");
  89. return 0;
  90. }
  91. int32_t find_node_parent_index(uint32_t node, const Array<GraphNode>& nodes, const Array<GraphNodeDepth>& node_depths)
  92. {
  93. StringId32 parent_name = nodes[node_depths[node].index].parent;
  94. if (parent_name == NO_PARENT) return -1;
  95. for (uint32_t i = 0; i < array::size(node_depths); i++)
  96. {
  97. if (parent_name == node_depths[i].name)
  98. {
  99. return i;
  100. }
  101. }
  102. CE_FATAL("Node not found");
  103. return 0;
  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 < vector::size(keys); k++)
  110. {
  111. const char* node_name = keys[k].c_str();
  112. JSONElement node = e.key(node_name);
  113. GraphNode gn;
  114. gn.name = StringId32(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 = parent_name.to_string_id();
  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. void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphNodeDepth>& node_depths)
  135. {
  136. Vector<DynamicString> keys(default_allocator());
  137. e.to_keys(keys);
  138. for (uint32_t k = 0; k < vector::size(keys); k++)
  139. {
  140. const char* camera_name = keys[k].c_str();
  141. JSONElement camera = e.key(camera_name);
  142. JSONElement node = camera.key("node");
  143. JSONElement type = camera.key("type");
  144. DynamicString node_name;
  145. node.to_string(node_name);
  146. DynamicString camera_type;
  147. type.to_string(camera_type);
  148. StringId32 node_name_hash = node_name.to_string_id();
  149. UnitCamera cn;
  150. cn.name = StringId32(camera_name);
  151. cn.node = find_node_index(node_name_hash, node_depths);
  152. cn.type = projection_name_to_enum(camera_type.c_str());
  153. cn.fov = camera.key_or_nil("fov").to_float(16.0f / 9.0f);
  154. cn.near = camera.key_or_nil("near_clip_distance").to_float(0.01f);
  155. cn.far = camera.key_or_nil("far_clip_distance").to_float(1000.0f);
  156. array::push_back(cameras, cn);
  157. }
  158. }
  159. void parse_renderables(JSONElement e, Array<UnitRenderable>& renderables, const Array<GraphNodeDepth>& node_depths)
  160. {
  161. Vector<DynamicString> keys(default_allocator());
  162. e.to_keys(keys);
  163. for (uint32_t k = 0; k < vector::size(keys); k++)
  164. {
  165. const char* renderable_name = keys[k].c_str();
  166. JSONElement renderable = e.key(renderable_name);
  167. DynamicString node_name; renderable.key("node").to_string(node_name);
  168. StringId32 node_name_hash = node_name.to_string_id();
  169. UnitRenderable rn;
  170. rn.name = StringId32(renderable_name);
  171. rn.node = find_node_index(node_name_hash, node_depths);
  172. rn.visible = renderable.key("visible").to_bool();
  173. DynamicString res_type;
  174. renderable.key("type").to_string(res_type);
  175. if (res_type == "mesh")
  176. {
  177. rn.type = UnitRenderable::MESH;
  178. rn.resource = renderable.key("resource").to_resource_id("mesh").name;
  179. }
  180. else if (res_type == "sprite")
  181. {
  182. rn.type = UnitRenderable::SPRITE;
  183. rn.resource = renderable.key("resource").to_resource_id("sprite").name;
  184. }
  185. else
  186. {
  187. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
  188. }
  189. array::push_back(renderables, rn);
  190. }
  191. }
  192. void parse_materials(JSONElement e, Array<UnitMaterial>& materials)
  193. {
  194. for (uint32_t i = 0; i < e.size(); i++)
  195. {
  196. ResourceId mat_id = e[i].to_resource_id("material");
  197. UnitMaterial um;
  198. um.id = mat_id.name;
  199. array::push_back(materials, um);
  200. }
  201. }
  202. void compile(const char* path, CompileOptions& opts)
  203. {
  204. static const uint32_t VERSION = 1;
  205. Buffer buf = opts.read(path);
  206. JSONParser json(array::begin(buf));
  207. JSONElement root = json.root();
  208. ResourceId m_physics_resource;
  209. Array<GraphNode> m_nodes(default_allocator());
  210. Array<GraphNodeDepth> m_node_depths(default_allocator());
  211. Array<UnitCamera> m_cameras(default_allocator());
  212. Array<UnitRenderable> m_renderables(default_allocator());
  213. Array<UnitMaterial> m_materials(default_allocator());
  214. // Check for nodes
  215. if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
  216. for (uint32_t i = 0; i < array::size(m_nodes); i++)
  217. {
  218. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  219. }
  220. std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
  221. if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
  222. if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
  223. if (root.has_key("materials")) parse_materials(root.key("materials"), m_materials);
  224. // Check if the unit has a .physics resource
  225. DynamicString unit_name(path);
  226. unit_name.strip_trailing(".unit");
  227. DynamicString physics_name = unit_name;
  228. physics_name += ".physics";
  229. if (opts._fs.exists(physics_name.c_str()))
  230. {
  231. m_physics_resource = ResourceId("physics", unit_name.c_str());
  232. }
  233. else
  234. {
  235. m_physics_resource = ResourceId();
  236. }
  237. ResourceId sprite_anim;
  238. if (root.has_key("sprite_animation"))
  239. sprite_anim = root.key("sprite_animation").to_resource_id("sprite_animation");
  240. UnitResource ur;
  241. ur.version = VERSION;
  242. ur.name = StringId64(unit_name.c_str());
  243. ur.physics_resource = m_physics_resource.name;
  244. ur.sprite_animation = sprite_anim.name;
  245. ur.num_renderables = array::size(m_renderables);
  246. ur.num_materials = array::size(m_materials);
  247. ur.num_cameras = array::size(m_cameras);
  248. ur.num_scene_graph_nodes = array::size(m_nodes);
  249. uint32_t offt = sizeof(UnitResource);
  250. ur.renderables_offset = offt; offt += sizeof(UnitRenderable) * ur.num_renderables;
  251. ur.materials_offset = offt; offt += sizeof(UnitMaterial) * ur.num_materials;
  252. ur.cameras_offset = offt; offt += sizeof(UnitCamera) * ur.num_cameras;
  253. ur.scene_graph_nodes_offset = offt;
  254. opts.write(ur.version);
  255. opts.write(ur._pad);
  256. opts.write(ur.name);
  257. opts.write(ur.physics_resource);
  258. opts.write(ur.sprite_animation);
  259. opts.write(ur.num_renderables);
  260. opts.write(ur.renderables_offset);
  261. opts.write(ur.num_materials);
  262. opts.write(ur.materials_offset);
  263. opts.write(ur.num_cameras);
  264. opts.write(ur.cameras_offset);
  265. opts.write(ur.num_scene_graph_nodes);
  266. opts.write(ur.scene_graph_nodes_offset);
  267. // Renderables
  268. for (uint32_t i = 0; i < array::size(m_renderables); i++)
  269. {
  270. opts.write(m_renderables[i].type);
  271. opts.write(m_renderables[i]._pad);
  272. opts.write(m_renderables[i].resource);
  273. opts.write(m_renderables[i].name);
  274. opts.write(m_renderables[i].node);
  275. opts.write(m_renderables[i].visible);
  276. opts.write(m_renderables[i]._pad1[0]);
  277. opts.write(m_renderables[i]._pad1[1]);
  278. opts.write(m_renderables[i]._pad1[2]);
  279. opts.write(m_renderables[i]._pad2[0]);
  280. opts.write(m_renderables[i]._pad2[1]);
  281. opts.write(m_renderables[i]._pad2[2]);
  282. opts.write(m_renderables[i]._pad2[3]);
  283. }
  284. // Materials
  285. for (uint32_t i = 0; i < array::size(m_materials); i++)
  286. {
  287. opts.write(m_materials[i]);
  288. }
  289. // Cameras
  290. for (uint32_t i = 0; i < array::size(m_cameras); i++)
  291. {
  292. opts.write(m_cameras[i].name);
  293. opts.write(m_cameras[i].node);
  294. opts.write(m_cameras[i].type);
  295. opts.write(m_cameras[i].fov);
  296. opts.write(m_cameras[i].near);
  297. opts.write(m_cameras[i].far);
  298. }
  299. // Write node poses
  300. for (uint32_t i = 0; i < ur.num_scene_graph_nodes; i++)
  301. {
  302. uint32_t node_index = m_node_depths[i].index;
  303. GraphNode& node = m_nodes[node_index];
  304. UnitNode un;
  305. un.name = node.name;
  306. un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
  307. un.pose = Matrix4x4(node.rotation, node.position);
  308. opts.write(un.name);
  309. opts.write(un.pose);
  310. opts.write(un.parent);
  311. }
  312. }
  313. void* load(File& file, Allocator& a)
  314. {
  315. const size_t file_size = file.size();
  316. void* res = a.allocate(file_size);
  317. file.read(res, file_size);
  318. return res;
  319. }
  320. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  321. {
  322. }
  323. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  324. {
  325. }
  326. void unload(Allocator& allocator, void* resource)
  327. {
  328. allocator.deallocate(resource);
  329. }
  330. StringId64 sprite_animation(const UnitResource* ur)
  331. {
  332. return ur->sprite_animation;
  333. }
  334. StringId64 physics_resource(const UnitResource* ur)
  335. {
  336. return ur->physics_resource;
  337. }
  338. uint32_t num_renderables(const UnitResource* ur)
  339. {
  340. return ur->num_renderables;
  341. }
  342. const UnitRenderable* get_renderable(const UnitResource* ur, uint32_t i)
  343. {
  344. CE_ASSERT(i < num_renderables(ur), "Index out of bounds");
  345. UnitRenderable* begin = (UnitRenderable*) ((char*)ur + ur->renderables_offset);
  346. return &begin[i];
  347. }
  348. uint32_t num_materials(const UnitResource* ur)
  349. {
  350. return ur->num_materials;
  351. }
  352. const UnitMaterial* get_material(const UnitResource* ur, uint32_t i)
  353. {
  354. CE_ASSERT(i < num_materials(ur), "Index out of bounds");
  355. UnitMaterial* begin = (UnitMaterial*) ((char*)ur + ur->materials_offset);
  356. return &begin[i];
  357. }
  358. uint32_t num_cameras(const UnitResource* ur)
  359. {
  360. return ur->num_cameras;
  361. }
  362. const UnitCamera* get_camera(const UnitResource* ur, uint32_t i)
  363. {
  364. CE_ASSERT(i < num_cameras(ur), "Index out of bounds");
  365. UnitCamera* begin = (UnitCamera*) ((char*)ur + ur->cameras_offset);
  366. return &begin[i];
  367. }
  368. uint32_t num_scene_graph_nodes(const UnitResource* ur)
  369. {
  370. return ur->num_scene_graph_nodes;
  371. }
  372. const UnitNode* scene_graph_nodes(const UnitResource* ur)
  373. {
  374. return (UnitNode*) ((char*)ur + ur->scene_graph_nodes_offset);
  375. }
  376. } // namespace unit_resource
  377. } // namespace crown