Преглед изворни кода

Add node id to Mesh, clean and adapt code in RenderWorld

Daniele Bartolini пре 12 година
родитељ
комит
caea86872b
4 измењених фајлова са 28 додато и 21 уклоњено
  1. 3 1
      engine/Mesh.cpp
  2. 1 1
      engine/Mesh.h
  3. 20 14
      engine/RenderWorld.cpp
  4. 4 5
      engine/RenderWorld.h

+ 3 - 1
engine/Mesh.cpp

@@ -34,13 +34,15 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-void Mesh::create(const MeshResource* mr, const Vector3& pos, const Quaternion& rot)
+void Mesh::create(const MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot)
 {
 	m_vbuffer = mr->m_vbuffer;
 	m_ibuffer = mr->m_ibuffer;
 
 	set_local_position(pos);
 	set_local_rotation(rot);
+
+	m_node = node;
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 1
engine/Mesh.h

@@ -38,7 +38,7 @@ class Quaternion;
 
 struct Mesh
 {
-	void			create(const MeshResource* mr, const Vector3& pos, const Quaternion& rot);
+	void			create(const MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot);
 
 	Vector3			local_position() const;
 	Quaternion		local_rotation() const;

+ 20 - 14
engine/RenderWorld.cpp

@@ -100,8 +100,6 @@ RenderWorld::RenderWorld()
 
 	default_program = r->create_gpu_program(default_vs, default_fs);
 	texture_program = r->create_gpu_program(default_vs, texture_fs);
-
-	create_mesh("monkey");
 }
 
 //-----------------------------------------------------------------------------
@@ -120,12 +118,11 @@ RenderWorld::~RenderWorld()
 }
 
 //-----------------------------------------------------------------------------
-MeshId RenderWorld::create_mesh(const char* name, const Vector3& pos, const Quaternion& rot)
+MeshId RenderWorld::create_mesh(const char* name, int32_t node, const Vector3& pos, const Quaternion& rot)
 {
 	MeshResource* mr = (MeshResource*) device()->resource_manager()->lookup("mesh", name);
 
-	MeshId mesh = allocate_mesh();
-	m_mesh[mesh.index].create(mr, pos, rot);
+	MeshId mesh = allocate_mesh(mr, node, pos, rot);
 
 	return mesh;
 }
@@ -135,12 +132,20 @@ void RenderWorld::destroy_mesh(MeshId /*id*/)
 {
 }
 
+//-----------------------------------------------------------------------------
+Mesh* RenderWorld::lookup_mesh(MeshId mesh)
+{
+	CE_ASSERT(m_mesh_table.has(mesh), "Mesh does not exits");
+
+	return &m_mesh[m_sparse_to_packed[mesh.index]];
+}
+
 //-----------------------------------------------------------------------------
 void RenderWorld::update(Camera& camera, float /*dt*/)
 {
 	Renderer* r = device()->renderer();
 
-	Matrix4x4 camera_view = camera.local_pose();
+	Matrix4x4 camera_view = camera.world_pose();
 	camera_view.invert();
 
 	r->set_layer_view(0, camera_view);
@@ -169,16 +174,17 @@ void RenderWorld::update(Camera& camera, float /*dt*/)
 }
 
 //-----------------------------------------------------------------------------
-MeshId RenderWorld::allocate_mesh()
+MeshId RenderWorld::allocate_mesh(MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot)
 {
-	MeshId mesh = m_mesh_table.create();
-	
-	if (m_mesh.size() <= mesh.index)
-	{
-		m_mesh.resize(1);
-	}
+	MeshId mesh_id = m_mesh_table.create();
 
-	return mesh;
+	Mesh mesh;
+	mesh.create(mr, node, pos, rot);
+
+	uint32_t index = m_mesh.push_back(mesh);
+	m_sparse_to_packed[mesh_id.index] = index;
+
+	return mesh_id;
 }
 
 //-----------------------------------------------------------------------------

+ 4 - 5
engine/RenderWorld.h

@@ -47,21 +47,20 @@ public:
 	RenderWorld();
 	~RenderWorld();
 
-	MeshId create_mesh(const char* mesh, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
+	MeshId create_mesh(const char* mesh, int32_t node = -1, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
 	void destroy_mesh(MeshId id);
 
+	Mesh* lookup_mesh(MeshId mesh);
+
 	void update(Camera& camera, float dt);
 
-	MeshId allocate_mesh();
+	MeshId allocate_mesh(MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot);
 	void deallocate_mesh(MeshId id);
 
-	Mesh* mesh() { return &m_mesh[0]; }
-
 private:
 
 	IdTable<MAX_MESHES>		m_mesh_table;
 	uint32_t				m_sparse_to_packed[MAX_MESHES];
-
 	List<Mesh>				m_mesh;
 };