| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include "Allocator.h"
- #include "File.h"
- #include "Filesystem.h"
- #include "StringUtils.h"
- #include "JSONParser.h"
- #include "Vector.h"
- #include "Log.h"
- #include "Matrix4x4.h"
- #include "PhysicsTypes.h"
- #include "Quaternion.h"
- #include "Resource.h"
- #include "TempAllocator.h"
- #include "Types.h"
- #include "Vector3.h"
- #include "Camera.h"
- #include "UnitResource.h"
- namespace crown
- {
- namespace unit_resource
- {
- static ProjectionType::Enum projection_name_to_enum(const char* name)
- {
- if (string::strcmp(name, "perspective") == 0) return ProjectionType::PERSPECTIVE;
- else if (string::strcmp(name, "orthographic") == 0) return ProjectionType::ORTHOGRAPHIC;
- CE_FATAL("Unknown projection type");
- return (ProjectionType::Enum) 0;
- }
- const StringId32 NO_PARENT = 0xFFFFFFFF;
- struct GraphNode
- {
- StringId32 name;
- StringId32 parent;
- Vector3 position;
- Quaternion rotation;
- };
- struct GraphNodeDepth
- {
- StringId32 name;
- uint32_t index;
- uint32_t depth;
- bool operator()(const GraphNodeDepth& a, const GraphNodeDepth& b)
- {
- return a.depth < b.depth;
- }
- };
- //-----------------------------------------------------------------------------
- uint32_t compute_link_depth(const GraphNode& node, const Array<GraphNode>& nodes)
- {
- if (node.parent == NO_PARENT) return 0;
- else
- {
- for (uint32_t i = 0; i < array::size(nodes); i++)
- {
- if (nodes[i].name == node.parent)
- {
- return 1 + compute_link_depth(nodes[i], nodes);
- }
- }
- }
- CE_FATAL("Node not found");
- return 0;
- }
- //-----------------------------------------------------------------------------
- uint32_t find_node_index(StringId32 name, const Array<GraphNodeDepth>& node_depths)
- {
- for (uint32_t i = 0; i < array::size(node_depths); i++)
- {
- if (node_depths[i].name == name)
- {
- return i;
- }
- }
- CE_FATAL("Node not found");
- return 0;
- }
- //-----------------------------------------------------------------------------
- int32_t find_node_parent_index(uint32_t node, const Array<GraphNode>& nodes, const Array<GraphNodeDepth>& node_depths)
- {
- StringId32 parent_name = nodes[node_depths[node].index].parent;
- if (parent_name == NO_PARENT) return -1;
- for (uint32_t i = 0; i < array::size(node_depths); i++)
- {
- if (parent_name == node_depths[i].name)
- {
- return i;
- }
- }
- CE_FATAL("Node not found");
- return 0;
- }
- //-----------------------------------------------------------------------------
- void parse_nodes(JSONElement e, Array<GraphNode>& nodes, Array<GraphNodeDepth>& node_depths)
- {
- Vector<DynamicString> keys(default_allocator());
- e.to_keys(keys);
- for (uint32_t k = 0; k < vector::size(keys); k++)
- {
- const char* node_name = keys[k].c_str();
- JSONElement node = e.key(node_name);
- GraphNode gn;
- gn.name = string::murmur2_32(node_name, string::strlen(node_name));
- gn.parent = NO_PARENT;
- if (!node.key("parent").is_nil())
- {
- DynamicString parent_name;
- node.key("parent").to_string(parent_name);
- gn.parent = string::murmur2_32(parent_name.c_str(), parent_name.length(), 0);
- }
- JSONElement pos = node.key("position");
- JSONElement rot = node.key("rotation");
- gn.position = Vector3(pos[0].to_float(), pos[1].to_float(), pos[2].to_float());
- gn.rotation = Quaternion(Vector3(rot[0].to_float(), rot[1].to_float(), rot[2].to_float()), rot[3].to_float());
- GraphNodeDepth gnd;
- gnd.name = gn.name;
- gnd.index = array::size(nodes);
- gnd.depth = 0;
- array::push_back(nodes, gn);
- array::push_back(node_depths, gnd);
- }
- }
- //-----------------------------------------------------------------------------
- void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphNodeDepth>& node_depths)
- {
- Vector<DynamicString> keys(default_allocator());
- e.to_keys(keys);
- for (uint32_t k = 0; k < vector::size(keys); k++)
- {
- const char* camera_name = keys[k].c_str();
- JSONElement camera = e.key(camera_name);
- JSONElement node = camera.key("node");
- JSONElement type = camera.key("type");
- JSONElement fov = camera.key_or_nil("fov");
- JSONElement near = camera.key_or_nil("near_clip_distance");
- JSONElement far = camera.key_or_nil("far_clip_distance");
- DynamicString node_name;
- node.to_string(node_name);
- DynamicString camera_type;
- type.to_string(camera_type);
- StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length());
- UnitCamera cn;
- cn.name = string::murmur2_32(camera_name, string::strlen(camera_name));
- cn.node = find_node_index(node_name_hash, node_depths);
- cn.type = projection_name_to_enum(camera_type.c_str());
- cn.fov = fov.is_nil() ? 16.0 / 9.0 : fov.to_float();
- cn.near = near.is_nil() ? 0.01 : near.to_float();
- cn.far = far.is_nil() ? 1000 : far.to_float();
- array::push_back(cameras, cn);
- }
- }
- //-----------------------------------------------------------------------------
- void parse_renderables(JSONElement e, Array<UnitRenderable>& renderables, const Array<GraphNodeDepth>& node_depths)
- {
- Vector<DynamicString> keys(default_allocator());
- e.to_keys(keys);
- for (uint32_t k = 0; k < vector::size(keys); k++)
- {
- const char* renderable_name = keys[k].c_str();
- JSONElement renderable = e.key(renderable_name);
- DynamicString node_name; renderable.key("node").to_string(node_name);
- StringId32 node_name_hash = string::murmur2_32(node_name.c_str(), node_name.length(), 0);
- UnitRenderable rn;
- rn.name = string::murmur2_32(renderable_name, string::strlen(renderable_name), 0);
- rn.node = find_node_index(node_name_hash, node_depths);
- rn.visible = renderable.key("visible").to_bool();
- DynamicString res_type; renderable.key("type").to_string(res_type);
- DynamicString resource_name; renderable.key("resource").to_string(resource_name);
- DynamicString res_name;
- if (res_type == "mesh")
- {
- rn.type = UnitRenderable::MESH;
- res_name += resource_name;
- res_name += ".mesh";
- }
- else if (res_type == "sprite")
- {
- rn.type = UnitRenderable::SPRITE;
- res_name += resource_name;
- res_name += ".sprite";
- }
- else
- {
- CE_ASSERT(false, "Oops, unknown renderable type: '%s'", res_type.c_str());
- }
- rn.resource.id = string::murmur2_64(res_name.c_str(), res_name.length(), 0);
- array::push_back(renderables, rn);
- }
- }
- //-----------------------------------------------------------------------------
- void compile(Filesystem& fs, const char* resource_path, File* out_file)
- {
- File* file = fs.open(resource_path, FOM_READ);
- char file_buf[4096];
- file->read(file_buf, file->size());
- fs.close(file);
- JSONParser json(file_buf);
- JSONElement root = json.root();
- ResourceId m_physics_resource;
- ResourceId m_material_resource;
- Array<GraphNode> m_nodes(default_allocator());
- Array<GraphNodeDepth> m_node_depths(default_allocator());
- Array<UnitCamera> m_cameras(default_allocator());
- Array<UnitRenderable> m_renderables(default_allocator());
- // Check for nodes
- if (root.has_key("nodes")) parse_nodes(root.key("nodes"), m_nodes, m_node_depths);
- for (uint32_t i = 0; i < array::size(m_nodes); i++)
- {
- m_node_depths[i].depth = compute_link_depth(m_nodes[i], m_nodes);
- }
- std::sort(array::begin(m_node_depths), array::end(m_node_depths), GraphNodeDepth());
- if (root.has_key("renderables")) parse_renderables(root.key("renderables"), m_renderables, m_node_depths);
- if (root.has_key("cameras")) parse_cameras(root.key("cameras"), m_cameras, m_node_depths);
- // Check if the unit has a .physics resource
- DynamicString unit_name(resource_path);
- unit_name.strip_trailing("unit");
- DynamicString physics_name = unit_name;
- physics_name += "physics";
- if (fs.is_file(physics_name.c_str()))
- {
- m_physics_resource.id = string::murmur2_64(physics_name.c_str(), string::strlen(physics_name.c_str()), 0);
- }
- else
- {
- m_physics_resource.id = 0;
- }
- // Check if the unit has a .material resource
- DynamicString material_name = unit_name;
- material_name += "material";
- if (fs.is_file(material_name.c_str()))
- {
- m_material_resource.id = string::murmur2_64(material_name.c_str(), string::strlen(material_name.c_str()), 0);
- }
- else
- {
- m_material_resource.id = 0;
- }
- UnitHeader h;
- h.physics_resource = m_physics_resource;
- h.material_resource = m_material_resource;
- h.num_renderables = array::size(m_renderables);
- h.num_cameras = array::size(m_cameras);
- h.num_scene_graph_nodes = array::size(m_nodes);
- uint32_t offt = sizeof(UnitHeader);
- h.renderables_offset = offt; offt += sizeof(UnitRenderable) * h.num_renderables;
- h.cameras_offset = offt; offt += sizeof(UnitCamera) * h.num_cameras;
- h.scene_graph_nodes_offset = offt; offt += sizeof(UnitNode) * h.num_scene_graph_nodes;
- // Write header
- out_file->write((char*) &h, sizeof(UnitHeader));
- // Write renderables
- if (array::size(m_renderables))
- out_file->write((char*) array::begin(m_renderables), sizeof(UnitRenderable) * h.num_renderables);
- // Write cameras
- if (array::size(m_cameras))
- out_file->write((char*) array::begin(m_cameras), sizeof(UnitCamera) * h.num_cameras);
- // Write node poses
- for (uint32_t i = 0; i < h.num_scene_graph_nodes; i++)
- {
- uint32_t node_index = m_node_depths[i].index;
- GraphNode& node = m_nodes[node_index];
- UnitNode un;
- un.name = node.name;
- un.parent = find_node_parent_index(i, m_nodes, m_node_depths);
- un.pose = Matrix4x4(node.rotation, node.position);
- out_file->write((char*) &un, sizeof(UnitNode));
- }
- }
- } // namespace unit_resource
- } // namespace crown
|