114 lines
2.6 KiB
C++
114 lines
2.6 KiB
C++
// src/CameraComponent.cpp
|
|
|
|
#include "CameraComponent.h"
|
|
|
|
|
|
|
|
// Constructor with default values
|
|
CameraComponent::CameraComponent()
|
|
: zoom(10.0f),
|
|
fov(45.0f),
|
|
orthographic(false),
|
|
nearPlane(0.1f),
|
|
farPlane(1000.0f)
|
|
{
|
|
}
|
|
|
|
// Override to provide the component's name
|
|
const std::string& CameraComponent::GetName() const
|
|
{
|
|
static std::string name = "Camera";
|
|
return name;
|
|
}
|
|
|
|
// Static method to retrieve the component's name
|
|
std::string CameraComponent::GetStaticName()
|
|
{
|
|
return "Camera";
|
|
}
|
|
|
|
// Computes the view matrix based on position and rotation
|
|
glm::mat4 CameraComponent::GetViewMatrix(const glm::vec3& position, const glm::vec3& rotation) const
|
|
{
|
|
// Start with identity matrix
|
|
glm::mat4 view = glm::mat4(1.0f);
|
|
|
|
// Apply rotations: Roll (Z), Pitch (X), then Yaw (Y)
|
|
view = glm::rotate(view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); // Roll
|
|
view = glm::rotate(view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); // Pitch
|
|
view = glm::rotate(view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); // Yaw
|
|
|
|
// Apply translation: Move the world opposite to the camera's position
|
|
view = glm::translate(view, -position);
|
|
|
|
return view;
|
|
}
|
|
|
|
// Computes the projection matrix based on the current projection type and aspect ratio
|
|
glm::mat4 CameraComponent::GetProjectionMatrix(float aspectRatio) const
|
|
{
|
|
|
|
if (orthographic)
|
|
{
|
|
float halfWidth = zoom;
|
|
float halfHeight = zoom / aspectRatio;
|
|
return glm::ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, nearPlane, farPlane);
|
|
}
|
|
else
|
|
{
|
|
return glm::perspective(glm::radians(fov), aspectRatio, nearPlane, farPlane);
|
|
}
|
|
}
|
|
|
|
// Serialization Method
|
|
YAML::Node CameraComponent::Serialize()
|
|
{
|
|
YAML::Node node;
|
|
|
|
// Zoom
|
|
node["Zoom"] = zoom;
|
|
|
|
// Field of View
|
|
node["FOV"] = fov;
|
|
|
|
// Orthographic Projection Toggle
|
|
node["Orthographic"] = orthographic;
|
|
|
|
// Near Plane
|
|
node["NearPlane"] = nearPlane;
|
|
|
|
// Far Plane
|
|
node["FarPlane"] = farPlane;
|
|
|
|
return node;
|
|
}
|
|
|
|
// Deserialization Method
|
|
void CameraComponent::Deserialize(const YAML::Node& node)
|
|
{
|
|
if (node["Zoom"])
|
|
{
|
|
zoom = node["Zoom"].as<float>();
|
|
}
|
|
|
|
if (node["FOV"])
|
|
{
|
|
fov = node["FOV"].as<float>();
|
|
}
|
|
|
|
if (node["Orthographic"])
|
|
{
|
|
orthographic = node["Orthographic"].as<bool>();
|
|
}
|
|
|
|
if (node["NearPlane"])
|
|
{
|
|
nearPlane = node["NearPlane"].as<float>();
|
|
}
|
|
|
|
if (node["FarPlane"])
|
|
{
|
|
farPlane = node["FarPlane"].as<float>();
|
|
}
|
|
}
|