Refactor Camera class
This commit is contained in:
@@ -14,15 +14,13 @@ namespace openVulkanoCpp::Scene
|
||||
class Camera : public Node
|
||||
{
|
||||
protected:
|
||||
Math::Matrix4f m_viewProjection{1}, m_view{1}, m_projection{1};
|
||||
float m_nearPlane, m_farPlane, m_width, m_height;
|
||||
|
||||
Math::Matrix4f viewProjection{1}, view{1}, projection{1};
|
||||
Math::Vector4f camPosition{};
|
||||
float nearPlane, farPlane, width, height;
|
||||
|
||||
Camera() : nearPlane(0), farPlane(0), width(0), height(0) {}
|
||||
Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {}
|
||||
|
||||
Camera(float width, float height, float nearPlane, float farPlane)
|
||||
: nearPlane(nearPlane), farPlane(farPlane), width(width), height(height)
|
||||
: m_nearPlane(nearPlane), m_farPlane(farPlane), m_width(width), m_height(height)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -31,41 +29,41 @@ namespace openVulkanoCpp::Scene
|
||||
public:
|
||||
void Init(float width, float height, float nearPlane, float farPlane)
|
||||
{
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->nearPlane = nearPlane;
|
||||
this->farPlane = farPlane;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_nearPlane = nearPlane;
|
||||
m_farPlane = farPlane;
|
||||
Node::Init();
|
||||
UpdateProjectionMatrix();
|
||||
}
|
||||
|
||||
virtual void SetSize(const float width, const float height)
|
||||
{
|
||||
if (this->width == width && this->height == height) return;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
if (m_width == width && m_height == height) return;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
UpdateProjectionMatrix();
|
||||
}
|
||||
|
||||
void SetNearPlane(float nearPlane)
|
||||
{
|
||||
this->nearPlane = nearPlane;
|
||||
m_nearPlane = nearPlane;
|
||||
}
|
||||
|
||||
void SetFarPlane(float farPlane)
|
||||
{
|
||||
this->farPlane = farPlane;
|
||||
m_farPlane = farPlane;
|
||||
}
|
||||
|
||||
|
||||
[[nodiscard]] float NearPlane() const
|
||||
{
|
||||
return nearPlane;
|
||||
return m_nearPlane;
|
||||
}
|
||||
|
||||
[[nodiscard]] float FarPlane() const
|
||||
{
|
||||
return farPlane;
|
||||
return m_farPlane;
|
||||
}
|
||||
|
||||
virtual void UpdateProjectionMatrix() = 0;
|
||||
@@ -74,27 +72,46 @@ namespace openVulkanoCpp::Scene
|
||||
{
|
||||
//TODO this should be done based on the clipspace used by the rendering api
|
||||
// In vulkan the screen space is defined as y=0=top and y=1=bottom and thus the coordinate have to be flipped
|
||||
viewProjection = projection * Math::Matrix4f(1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1) * view;
|
||||
m_viewProjection = m_projection * Math::Matrix4f(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) * m_view;
|
||||
}
|
||||
|
||||
void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) override
|
||||
{
|
||||
Node::UpdateWorldMatrix(parentWorldMat);
|
||||
view = Math::Utils::inverse(GetWorldMatrix());
|
||||
camPosition = GetWorldMatrix()[3];
|
||||
m_view = Math::Utils::inverse(GetWorldMatrix());
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
|
||||
void SetViewMatrix(const Math::Matrix4f& view)
|
||||
{
|
||||
SetMatrix(Math::Utils::inverse(view));
|
||||
}
|
||||
|
||||
[[nodiscard]] const Math::Matrix4f& GetViewProjectionMatrix() const
|
||||
{
|
||||
return viewProjection;
|
||||
return m_viewProjection;
|
||||
}
|
||||
|
||||
[[nodiscard]] Math::Vector4f GetPosition() const
|
||||
{
|
||||
return GetWorldMatrix()[3];
|
||||
}
|
||||
|
||||
[[nodiscard]] Math::Vector3f GetRightVector() const
|
||||
{
|
||||
return Math::Utils::transpose(m_view)[0];
|
||||
}
|
||||
|
||||
[[nodiscard]] Math::Vector3f GetViewDirection() const
|
||||
{
|
||||
return Math::Utils::transpose(m_view)[2];
|
||||
}
|
||||
};
|
||||
|
||||
class PerspectiveCamera : public Camera
|
||||
{
|
||||
protected:
|
||||
float fov = 0, aspect = 0;
|
||||
float m_fov = 0, m_aspect = 0;
|
||||
|
||||
public:
|
||||
PerspectiveCamera() = default;
|
||||
@@ -114,31 +131,31 @@ namespace openVulkanoCpp::Scene
|
||||
|
||||
void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane)
|
||||
{
|
||||
this->fov = Math::Utils::radians(fovDegrees);
|
||||
aspect = width / height;
|
||||
m_fov = Math::Utils::radians(fovDegrees);
|
||||
m_aspect = width / height;
|
||||
Camera::Init(width, height, nearPlane, farPlane);
|
||||
}
|
||||
|
||||
void SetSize(const float width, const float height) override
|
||||
{
|
||||
aspect = width / height;
|
||||
m_aspect = width / height;
|
||||
Camera::SetSize(width, height);
|
||||
}
|
||||
|
||||
void SetAspect(const float aspect)
|
||||
{
|
||||
this->aspect = aspect;
|
||||
m_aspect = aspect;
|
||||
Camera::SetSize(aspect, 1);
|
||||
}
|
||||
|
||||
void SetFovX(const float fov)
|
||||
{
|
||||
SetFov(2 * atan(tan(fov * 0.5f) * aspect));
|
||||
SetFov(2.0f * atanf(tanf(fov * 0.5f) * m_aspect));
|
||||
}
|
||||
|
||||
void SetFovXRad(const float fov)
|
||||
{
|
||||
SetFovRad(2 * atan(tan(fov * 0.5f) * aspect));
|
||||
SetFovRad(2.0f * atanf(tanf(fov * 0.5f) * m_aspect));
|
||||
}
|
||||
|
||||
void SetFov(const float fov)
|
||||
@@ -148,32 +165,32 @@ namespace openVulkanoCpp::Scene
|
||||
|
||||
void SetFovRad(const float fov)
|
||||
{
|
||||
this->fov = fov;
|
||||
m_fov = fov;
|
||||
}
|
||||
|
||||
[[nodiscard]] float GetFov() const
|
||||
{
|
||||
return Math::Utils::degrees(fov);
|
||||
return Math::Utils::degrees(m_fov);
|
||||
}
|
||||
|
||||
[[nodiscard]] float GetFovX() const
|
||||
{
|
||||
return 2.0f * atanf(tanf(GetFov() * 0.5f) * aspect);
|
||||
return 2.0f * atanf(tanf(GetFov() * 0.5f) * m_aspect);
|
||||
}
|
||||
|
||||
[[nodiscard]] float GetFovRad() const
|
||||
{
|
||||
return fov;
|
||||
return m_fov;
|
||||
}
|
||||
|
||||
[[nodiscard]] float GetFovXRad() const
|
||||
{
|
||||
return 2.0f * atanf(tanf(fov * 0.5f) * aspect);
|
||||
return 2.0f * atanf(tanf(m_fov * 0.5f) * m_aspect);
|
||||
}
|
||||
|
||||
void UpdateProjectionMatrix() final
|
||||
{
|
||||
projection = Math::Utils::perspectiveRH_ZO(fov, aspect, nearPlane, farPlane);
|
||||
m_projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
};
|
||||
@@ -183,8 +200,8 @@ namespace openVulkanoCpp::Scene
|
||||
public:
|
||||
void UpdateProjectionMatrix() final
|
||||
{
|
||||
const float widthHalf = width * 0.5f, heightHalf = height * 0.5f;
|
||||
projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, nearPlane, farPlane);
|
||||
const float widthHalf = m_width * 0.5f, heightHalf = m_height * 0.5f;
|
||||
m_projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
|
||||
UpdateViewProjectionMatrix();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user