|
|
@@ -33,6 +33,9 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "TempAllocator.h"
|
|
|
#include "Log.h"
|
|
|
#include "PhysicsTypes.h"
|
|
|
+#include "Quaternion.h"
|
|
|
+#include "Vector3.h"
|
|
|
+#include "Matrix4x4.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
@@ -42,6 +45,9 @@ UnitCompiler::UnitCompiler()
|
|
|
: m_renderable(default_allocator())
|
|
|
, m_camera(default_allocator())
|
|
|
, m_actor(default_allocator())
|
|
|
+ , m_node_names(default_allocator())
|
|
|
+ , m_node_parents(default_allocator())
|
|
|
+ , m_node_poses(default_allocator())
|
|
|
{
|
|
|
}
|
|
|
|
|
|
@@ -139,10 +145,68 @@ size_t UnitCompiler::compile_impl(Filesystem& fs, const char* resource_path)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // Check for nodes
|
|
|
+ if (root.has_key("nodes"))
|
|
|
+ {
|
|
|
+ JSONElement nodes = root.key("nodes");
|
|
|
+ const uint32_t num_nodes = nodes.size();
|
|
|
+
|
|
|
+ for (uint32_t i = 0; i < num_nodes; i++)
|
|
|
+ {
|
|
|
+ JSONElement node = nodes[i];
|
|
|
+ JSONElement node_name = node.key("name");
|
|
|
+ JSONElement node_parent = node.key("parent");
|
|
|
+ JSONElement node_pos = node.key("position");
|
|
|
+ JSONElement node_rot = node.key("rotation");
|
|
|
+
|
|
|
+ // Read name and parent
|
|
|
+ m_node_names.push_back(hash::murmur2_32(node_name.string_value(), node_name.size(), 0));
|
|
|
+
|
|
|
+ ParentIndex pidx;
|
|
|
+ pidx.parent_index = -1;
|
|
|
+ pidx.inner_index = i;
|
|
|
+
|
|
|
+ if (node_parent.is_nil())
|
|
|
+ {
|
|
|
+ pidx.parent_name = 0xFFFFFFFF;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pidx.parent_name = hash::murmur2_32(node_parent.string_value(), node_parent.size(), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ m_node_parents.push_back(pidx);
|
|
|
+
|
|
|
+ // Read pose
|
|
|
+ const Vector3 pos = Vector3(node_pos[0].float_value(), node_pos[1].float_value(), node_pos[2].float_value());
|
|
|
+ const Quaternion rot = Quaternion(Vector3(node_rot[0].float_value(), node_rot[1].float_value(), node_rot[2].float_value()), node_rot[3].float_value());
|
|
|
+ m_node_poses.push_back(Matrix4x4(rot, pos));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Convert parent names into parent indices
|
|
|
+ for (uint32_t i = 0; i < m_node_parents.size(); i++)
|
|
|
+ {
|
|
|
+ for (uint32_t j = 0; j < m_node_names.size(); j++)
|
|
|
+ {
|
|
|
+ if (m_node_names[j] == m_node_parents[i].parent_name)
|
|
|
+ {
|
|
|
+ m_node_parents[i].parent_index = j;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Sort by link depth
|
|
|
+ std::sort(m_node_parents.begin(), m_node_parents.end(), ParentIndex());
|
|
|
+
|
|
|
return sizeof(UnitHeader) +
|
|
|
m_renderable.size() * sizeof(UnitRenderable) +
|
|
|
m_camera.size() * sizeof(UnitCamera) +
|
|
|
- m_actor.size() * sizeof(UnitActor);
|
|
|
+ m_actor.size() * sizeof(UnitActor) +
|
|
|
+ m_node_names.size() * sizeof(StringId32) +
|
|
|
+ m_node_names.size() * sizeof(Matrix4x4) +
|
|
|
+ m_node_names.size() * sizeof(int32_t);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -151,9 +215,15 @@ void UnitCompiler::write_impl(File* out_file)
|
|
|
UnitHeader header;
|
|
|
header.num_renderables = m_renderable.size();
|
|
|
header.num_cameras = m_camera.size();
|
|
|
+ header.num_actors = m_actor.size();
|
|
|
+ header.num_scene_graph_nodes = m_node_names.size();
|
|
|
|
|
|
header.renderables_offset = sizeof(UnitHeader);
|
|
|
header.cameras_offset = sizeof(UnitHeader) + sizeof(UnitRenderable) * header.num_renderables;
|
|
|
+ header.actors_offset = sizeof(UnitHeader) + sizeof(UnitCamera) * header.num_cameras;
|
|
|
+ header.scene_graph_names_offset = sizeof(UnitHeader) + sizeof(UnitActor) * header.num_actors;
|
|
|
+ header.scene_graph_poses_offset = sizeof(UnitHeader) + sizeof(StringId32) * header.num_scene_graph_nodes;
|
|
|
+ header.scene_graph_parents_offset = sizeof(UnitHeader) + sizeof(Matrix4x4) * header.num_scene_graph_nodes;
|
|
|
|
|
|
out_file->write((char*) &header, sizeof(UnitHeader));
|
|
|
|
|
|
@@ -167,9 +237,36 @@ void UnitCompiler::write_impl(File* out_file)
|
|
|
out_file->write((char*) m_camera.begin(), sizeof(UnitCamera) * header.num_cameras);
|
|
|
}
|
|
|
|
|
|
+ if (m_actor.size() > 0)
|
|
|
+ {
|
|
|
+ out_file->write((char*) m_actor.begin(), sizeof(UnitActor) * header.num_actors);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write node names
|
|
|
+ for (uint32_t i = 0; i < m_node_names.size(); i++)
|
|
|
+ {
|
|
|
+ out_file->write((char*) &m_node_names[m_node_parents[i].inner_index], sizeof(StringId32));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write node poses
|
|
|
+ for (uint32_t i = 0; i < m_node_names.size(); i++)
|
|
|
+ {
|
|
|
+ out_file->write((char*) &m_node_poses[m_node_parents[i].inner_index], sizeof(Matrix4x4));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write node parents
|
|
|
+ for (uint32_t i = 0; i < m_node_names.size(); i++)
|
|
|
+ {
|
|
|
+ out_file->write((char*) &m_node_parents[i].parent_index, sizeof(int32_t));
|
|
|
+ }
|
|
|
+
|
|
|
// Cleanup
|
|
|
m_renderable.clear();
|
|
|
m_camera.clear();
|
|
|
+ m_actor.clear();
|
|
|
+ m_node_names.clear();
|
|
|
+ m_node_parents.clear();
|
|
|
+ m_node_poses.clear();
|
|
|
}
|
|
|
|
|
|
} // namespace crown
|