فهرست منبع

Add nodes array to Unit resources and fix actor compilation

Daniele Bartolini 12 سال پیش
والد
کامیت
39f84c1ebe
3فایلهای تغییر یافته به همراه152 افزوده شده و 5 حذف شده
  1. 98 1
      engine/compilers/unit/UnitCompiler.cpp
  2. 21 3
      engine/compilers/unit/UnitCompiler.h
  3. 33 1
      engine/resource/UnitResource.h

+ 98 - 1
engine/compilers/unit/UnitCompiler.cpp

@@ -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

+ 21 - 3
engine/compilers/unit/UnitCompiler.h

@@ -30,10 +30,24 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Resource.h"
 #include "UnitResource.h"
 #include "List.h"
+#include "Types.h"
+#include "Matrix4x4.h"
 
 namespace crown
 {
 
+struct ParentIndex
+{
+	StringId32 parent_name;
+	int32_t parent_index;
+	int32_t inner_index;
+
+	bool operator()(const ParentIndex& a, const ParentIndex& b)
+	{
+		return a.parent_index < b.parent_index;
+	}
+};
+
 class CE_EXPORT UnitCompiler : public Compiler
 {
 public:
@@ -45,9 +59,13 @@ public:
 
 private:
 
-	List<UnitRenderable> m_renderable;
-	List<UnitCamera> m_camera;
-	List<UnitActor> m_actor;
+	List<UnitRenderable>	m_renderable;
+	List<UnitCamera>		m_camera;
+	List<UnitActor>			m_actor;
+
+	List<StringId32>		m_node_names;
+	List<ParentIndex>		m_node_parents;
+	List<Matrix4x4>			m_node_poses;
 };
 
 } // namespace crown

+ 33 - 1
engine/resource/UnitResource.h

@@ -32,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Allocator.h"
 #include "File.h"
 #include "PhysicsTypes.h"
+#include "Matrix4x4.h"
 
 namespace crown
 {
@@ -45,6 +46,10 @@ struct UnitHeader
 	uint32_t cameras_offset;
 	uint32_t num_actors;
 	uint32_t actors_offset;
+	uint32_t num_scene_graph_nodes;
+	uint32_t scene_graph_names_offset;
+	uint32_t scene_graph_poses_offset;
+	uint32_t scene_graph_parents_offset;
 };
 
 struct UnitRenderable
@@ -140,7 +145,7 @@ struct UnitResource
 	}
 
 	//-----------------------------------------------------------------------------
-	const UnitActor& get_actor(uint32_t i) const
+	UnitActor get_actor(uint32_t i) const
 	{
 		CE_ASSERT(i < num_actors(), "Index out of bounds");
 
@@ -149,6 +154,33 @@ struct UnitResource
 		return begin[i];
 	}
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_scene_graph_nodes() const
+	{
+		return ((UnitHeader*) this)->num_scene_graph_nodes;
+	}
+
+	//-----------------------------------------------------------------------------
+	const StringId32* scene_graph_names() const
+	{
+		UnitHeader* h = (UnitHeader*) this;
+		return (StringId32*) ((char*) this) + h->scene_graph_names_offset;
+	}
+
+	//-----------------------------------------------------------------------------
+	const Matrix4x4* scene_graph_poses() const
+	{
+		UnitHeader* h = (UnitHeader*) this;
+		return (Matrix4x4*) ((char*) this) + h->scene_graph_poses_offset;
+	}
+
+	//-----------------------------------------------------------------------------
+	const int32_t* scene_graph_parents() const
+	{
+		UnitHeader* h = (UnitHeader*) this;
+		return (int32_t*) ((char*) this) + h->scene_graph_parents_offset;
+	}
+
 private:
 
 	// Disable construction