UnitResource.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "StringUtils.h"
  27. #include "JSONParser.h"
  28. #include "Vector.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 "Vector3.h"
  37. #include "Camera.h"
  38. #include "UnitResource.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.0 / 9.0 : fov.to_float();
  167. cn.near = near.is_nil() ? 0.01 : 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; renderable.key("type").to_string(res_type);
  188. DynamicString resource_name; renderable.key("resource").to_string(resource_name);
  189. DynamicString res_name;
  190. if (res_type == "mesh")
  191. {
  192. rn.type = UnitRenderable::MESH;
  193. res_name += resource_name;
  194. res_name += ".mesh";
  195. }
  196. else if (res_type == "sprite")
  197. {
  198. rn.type = UnitRenderable::SPRITE;
  199. res_name += resource_name;
  200. res_name += ".sprite";
  201. }
  202. else
  203. {
  204. CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
  205. }
  206. rn.resource.id = string::murmur2_64(res_name.c_str(), res_name.length(), 0);
  207. array::push_back(renderables, rn);
  208. }
  209. }
  210. //-----------------------------------------------------------------------------
  211. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  212. {
  213. File* file = fs.open(resource_path, FOM_READ);
  214. char file_buf[4096];
  215. file->read(file_buf, file->size());
  216. fs.close(file);
  217. JSONParser json(file_buf);
  218. JSONElement root = json.root();
  219. ResourceId m_physics_resource;
  220. ResourceId m_material_resource;
  221. Array<GraphNode> m_nodes(default_allocator());
  222. Array<GraphNodeDepth> m_node_depths(default_allocator());
  223. Array<UnitCamera> m_cameras(default_allocator());
  224. Array<UnitRenderable> m_renderables(default_allocator());
  225. // Check for nodes
  226. if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
  227. for (uint32_t i = 0; i < array::size(m_nodes); i++)
  228. {
  229. m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
  230. }
  231. std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
  232. if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
  233. if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
  234. // Check if the unit has a .physics resource
  235. DynamicString unit_name(resource_path);
  236. unit_name.strip_trailing("unit");
  237. DynamicString physics_name = unit_name;
  238. physics_name += "physics";
  239. if (fs.is_file(physics_name.c_str()))
  240. {
  241. m_physics_resource.id = string::murmur2_64(physics_name.c_str(), string::strlen(physics_name.c_str()), 0);
  242. }
  243. else
  244. {
  245. m_physics_resource.id = 0;
  246. }
  247. // Check if the unit has a .material resource
  248. DynamicString material_name = unit_name;
  249. material_name += "material";
  250. if (fs.is_file(material_name.c_str()))
  251. {
  252. m_material_resource.id = string::murmur2_64(material_name.c_str(), string::strlen(material_name.c_str()), 0);
  253. }
  254. else
  255. {
  256. m_material_resource.id = 0;
  257. }
  258. UnitHeader h;
  259. h.physics_resource = m_physics_resource;
  260. h.material_resource = m_material_resource;
  261. h.num_renderables = array::size(m_renderables);
  262. h.num_cameras = array::size(m_cameras);
  263. h.num_scene_graph_nodes = array::size(m_nodes);
  264. uint32_t offt = sizeof(UnitHeader);
  265. h.renderables_offset = offt; offt += sizeof(UnitRenderable) * h.num_renderables;
  266. h.cameras_offset = offt; offt += sizeof(UnitCamera) * h.num_cameras;
  267. h.scene_graph_nodes_offset = offt; offt += sizeof(UnitNode) * h.num_scene_graph_nodes;
  268. // Write header
  269. out_file->write((char*) &h, sizeof(UnitHeader));
  270. // Write renderables
  271. if (array::size(m_renderables))
  272. out_file->write((char*) array::begin(m_renderables), sizeof(UnitRenderable) * h.num_renderables);
  273. // Write cameras
  274. if (array::size(m_cameras))
  275. out_file->write((char*) array::begin(m_cameras), sizeof(UnitCamera) * h.num_cameras);
  276. // Write node poses
  277. for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
  278. {
  279. uint32_t node_index = m_node_depths[i].index;
  280. GraphNode& node = m_nodes[node_index];
  281. UnitNode un;
  282. un.name = node.name;
  283. un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
  284. un.pose = Matrix4x4(node.rotation, node.position);
  285. out_file->write((char*) &un, sizeof(UnitNode));
  286. }
  287. }
  288. } // namespace unit_resource
  289. } // namespace crown