Procházet zdrojové kódy

Read some camera settings from .unit files

Daniele Bartolini před 11 roky
rodič
revize
179e0a84fe

+ 23 - 2
engine/resource/UnitResource.cpp

@@ -37,14 +37,24 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Resource.h"
 #include "TempAllocator.h"
 #include "Types.h"
-#include "UnitResource.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
@@ -166,15 +176,26 @@ void parse_cameras(JSONElement e, Array<UnitCamera>& cameras, const Array<GraphN
 	{
 		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;
-		camera.key("node").to_string(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);
 	}

+ 6 - 0
engine/resource/UnitResource.h

@@ -33,6 +33,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "File.h"
 #include "PhysicsTypes.h"
 #include "Matrix4x4.h"
+#include "Camera.h"
 
 namespace crown
 {
@@ -63,6 +64,11 @@ struct UnitCamera
 {
 	uint32_t name;
 	int32_t node;
+
+	ProjectionType::Enum type;
+	float fov;
+	float near;
+	float far;
 };
 
 struct UnitNode

+ 5 - 2
engine/world/Camera.cpp

@@ -38,11 +38,14 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Camera::Camera(SceneGraph& sg, int32_t node)
+Camera::Camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
 	: m_scene_graph(sg)
 	, m_node(node)
-	, m_projection_type(ProjectionType::PERSPECTIVE)
+	, m_projection_type(type)
+	, m_near(near)
+	, m_far(far)
 {
+	update_projection_matrix();
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 1
engine/world/Camera.h

@@ -51,7 +51,7 @@ class SceneGraph;
 /// @ingroup World
 struct Camera
 {
-							Camera(SceneGraph& sg, int32_t node);
+							Camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far);
 
 	Vector3					local_position() const;
 	Quaternion				local_rotation() const;

+ 3 - 3
engine/world/Unit.cpp

@@ -149,9 +149,9 @@ void Unit::create_camera_objects()
 {
 	for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
 	{
-		const UnitCamera camera = m_resource->get_camera(i);
-		const CameraId cam = m_world.create_camera(m_scene_graph, camera.node);
-		add_camera(camera.name, cam);
+		const UnitCamera cam = m_resource->get_camera(i);
+		const CameraId id = m_world.create_camera(m_scene_graph, cam.node, cam.type, cam.near, cam.far);
+		add_camera(cam.name, id);
 	}
 }
 

+ 2 - 2
engine/world/World.cpp

@@ -177,10 +177,10 @@ void World::render(Camera* camera)
 }
 
 //-----------------------------------------------------------------------------
-CameraId World::create_camera(SceneGraph& sg, int32_t node)
+CameraId World::create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
 {
 	// Allocate memory for camera
-	Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node);
+	Camera* camera = CE_NEW(m_camera_pool, Camera)(sg, node, type, near, far);
 
 	// Create Id for the camera
 	const CameraId camera_id = m_cameras.create(camera);

+ 1 - 1
engine/world/World.h

@@ -95,7 +95,7 @@ public:
 	/// Renders the world form the point of view of the given @a camera.
 	void render(Camera* camera);
 
-	CameraId create_camera(SceneGraph& sg, int32_t node);
+	CameraId create_camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far);
 	void destroy_camera(CameraId id);
 
 	/// Plays the sound with the given @æ name at the given @a position, with the given