Przeglądaj źródła

Do not use pointer to parent unit, store cached local and world poses instead

Daniele Bartolini 12 lat temu
rodzic
commit
2f95fa45ac
8 zmienionych plików z 40 dodań i 34 usunięć
  1. 25 20
      engine/Camera.cpp
  2. 3 4
      engine/Camera.h
  3. 3 3
      engine/Mesh.h
  4. 2 2
      engine/RenderWorld.cpp
  5. 3 1
      engine/RenderWorld.h
  6. 1 1
      engine/Unit.cpp
  7. 2 2
      engine/World.cpp
  8. 1 1
      engine/World.h

+ 25 - 20
engine/Camera.cpp

@@ -35,67 +35,72 @@ namespace crown
 {
 {
 
 
 //-----------------------------------------------------------------------
 //-----------------------------------------------------------------------
-void Camera::create(Unit& parent, int32_t node)
+void Camera::create(int32_t node, const Vec3& pos, const Quat& rot)
 {
 {
-	m_parent = &parent;
 	m_node = node;
 	m_node = node;
 	m_projection_type = ProjectionType::PERSPECTIVE;
 	m_projection_type = ProjectionType::PERSPECTIVE;
 
 
+	set_local_position(pos);
+	set_local_rotation(rot);
 	update_projection_matrix();
 	update_projection_matrix();
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Vec3 Camera::local_position() const
 Vec3 Camera::local_position() const
 {
 {
-	return m_parent->local_position(m_node);
+	return m_local_pose.translation();
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Quat Camera::local_rotation() const
 Quat Camera::local_rotation() const
 {
 {
-	return m_parent->local_rotation(m_node);
+	return Quat(Vec3(1, 0, 0), 0.0f);
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Mat4 Camera::local_pose() const
 Mat4 Camera::local_pose() const
 {
 {
-	return m_parent->local_pose(m_node);
+	return m_local_pose;
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Vec3 Camera::world_position() const
 Vec3 Camera::world_position() const
 {
 {
-	return m_parent->world_position(m_node);
+	return m_world_pose.translation();
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Quat Camera::world_rotation() const
 Quat Camera::world_rotation() const
 {
 {
-	return m_parent->world_rotation(m_node);
+	return Quat(Vec3(1, 0, 0), 0.0f);
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 Mat4 Camera::world_pose() const
 Mat4 Camera::world_pose() const
 {
 {
-	return m_parent->world_pose(m_node);
+	return m_world_pose;
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 void Camera::set_local_position(const Vec3& pos)
 void Camera::set_local_position(const Vec3& pos)
 {
 {
-	m_parent->set_local_position(pos, m_node);
+	m_local_pose.set_translation(pos);
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 void Camera::set_local_rotation(const Quat& rot)
 void Camera::set_local_rotation(const Quat& rot)
 {
 {
-	m_parent->set_local_rotation(rot, m_node);
+	Mat4& local_pose = m_local_pose;
+
+	Vec3 local_translation = local_pose.translation();
+	local_pose = rot.to_mat4();
+	local_pose.set_translation(local_translation);
 }
 }
 
 
-//-----------------------------------------------------------------------
+//-----------------------------------------------------------------------------
 void Camera::set_local_pose(const Mat4& pose)
 void Camera::set_local_pose(const Mat4& pose)
 {
 {
-	m_parent->set_local_pose(pose, m_node);
+	m_local_pose = pose;
 }
 }
 
 
 //-----------------------------------------------------------------------
 //-----------------------------------------------------------------------

+ 3 - 4
engine/Camera.h

@@ -29,8 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "Types.h"
 #include "Frustum.h"
 #include "Frustum.h"
 #include "Mat4.h"
 #include "Mat4.h"
-#include "Mat3.h"
-#include "Vec3.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -50,7 +48,7 @@ class Unit;
 /// Represents the point of view into the game world.
 /// Represents the point of view into the game world.
 struct Camera
 struct Camera
 {
 {
-	void					create(Unit& parent, int32_t node);
+	void					create(int32_t node, const Vec3& pos, const Quat& rot);
 
 
 	Vec3					local_position() const;
 	Vec3					local_position() const;
 	Quat					local_rotation() const;
 	Quat					local_rotation() const;
@@ -85,8 +83,9 @@ public:
 
 
 public:
 public:
 
 
-	Unit*					m_parent;
 	int32_t					m_node;
 	int32_t					m_node;
+	Mat4					m_local_pose;
+	Mat4					m_world_pose;
 
 
 	ProjectionType::Enum	m_projection_type;
 	ProjectionType::Enum	m_projection_type;
 	Mat4					m_projection;
 	Mat4					m_projection;

+ 3 - 3
engine/Mesh.h

@@ -27,18 +27,18 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 #pragma once
 
 
 #include "RendererTypes.h"
 #include "RendererTypes.h"
-#include "Vec3.h"
-#include "Quat.h"
 #include "Mat4.h"
 #include "Mat4.h"
 
 
 namespace crown
 namespace crown
 {
 {
 
 
 struct MeshResource;
 struct MeshResource;
+class Vec3;
+class Quat;
 
 
 struct Mesh
 struct Mesh
 {
 {
-	void			create(const MeshResource* mr, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat::IDENTITY);
+	void			create(const MeshResource* mr, const Vec3& pos, const Quat& rot);
 
 
 	Vec3			local_position() const;
 	Vec3			local_position() const;
 	Quat			local_rotation() const;
 	Quat			local_rotation() const;

+ 2 - 2
engine/RenderWorld.cpp

@@ -120,12 +120,12 @@ RenderWorld::~RenderWorld()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-MeshId RenderWorld::create_mesh(const char* name)
+MeshId RenderWorld::create_mesh(const char* name, const Vec3& pos, const Quat& rot)
 {
 {
 	MeshResource* mr = (MeshResource*) device()->resource_manager()->lookup("mesh", name);
 	MeshResource* mr = (MeshResource*) device()->resource_manager()->lookup("mesh", name);
 
 
 	MeshId mesh = allocate_mesh();
 	MeshId mesh = allocate_mesh();
-	m_mesh[mesh.index].create(mr);
+	m_mesh[mesh.index].create(mr, pos, rot);
 
 
 	return mesh;
 	return mesh;
 }
 }

+ 3 - 1
engine/RenderWorld.h

@@ -29,6 +29,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "IdTable.h"
 #include "IdTable.h"
 #include "Mesh.h"
 #include "Mesh.h"
 #include "List.h"
 #include "List.h"
+#include "Vec3.h"
+#include "Quat.h"
 
 
 #define MAX_MESHES 100
 #define MAX_MESHES 100
 
 
@@ -45,7 +47,7 @@ public:
 	RenderWorld();
 	RenderWorld();
 	~RenderWorld();
 	~RenderWorld();
 
 
-	MeshId create_mesh(const char* mesh);
+	MeshId create_mesh(const char* mesh, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat::IDENTITY);
 	void destroy_mesh(MeshId id);
 	void destroy_mesh(MeshId id);
 
 
 	void update(Camera& camera, float dt);
 	void update(Camera& camera, float dt);

+ 1 - 1
engine/Unit.cpp

@@ -49,7 +49,7 @@ void Unit::create(World& creator, const Vec3& pos, const Quat& rot)
 	m_root_node = m_scene_graph.create_node(-1, pos, rot);
 	m_root_node = m_scene_graph.create_node(-1, pos, rot);
 
 
 	int32_t camera_node = m_scene_graph.create_node(m_root_node, Vec3::ZERO, Quat::IDENTITY);
 	int32_t camera_node = m_scene_graph.create_node(m_root_node, Vec3::ZERO, Quat::IDENTITY);
-	CameraId camera = m_creator->create_camera(*this, camera_node);
+	CameraId camera = m_creator->create_camera(camera_node);
 	m_camera = m_creator->lookup_camera(camera);
 	m_camera = m_creator->lookup_camera(camera);
 }
 }
 
 

+ 2 - 2
engine/World.cpp

@@ -124,11 +124,11 @@ RenderWorld& World::render_world()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-CameraId World::create_camera(Unit& parent, int32_t node)
+CameraId World::create_camera(int32_t node, const Vec3& pos, const Quat& rot)
 {
 {
 	CameraId camera = m_camera_table.create();
 	CameraId camera = m_camera_table.create();
 
 
-	m_camera[camera.index].create(parent, node);
+	m_camera[camera.index].create(node, pos, rot);
 	return camera;
 	return camera;
 }
 }
 
 

+ 1 - 1
engine/World.h

@@ -74,7 +74,7 @@ public:
 	RenderWorld&			render_world();
 	RenderWorld&			render_world();
 	void					update(Camera& camera, float dt);
 	void					update(Camera& camera, float dt);
 
 
-	CameraId				create_camera(Unit& parent, int32_t node);
+	CameraId				create_camera(int32_t node, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat::IDENTITY);
 	void					destroy_camera(CameraId camera);
 	void					destroy_camera(CameraId camera);
 
 
 	Mesh*					mesh();
 	Mesh*					mesh();