Refactor Camera class

This commit is contained in:
2021-08-27 11:19:11 +02:00
parent 3d92bef231
commit ee4ad9a28d

View File

@@ -14,15 +14,13 @@ namespace openVulkanoCpp::Scene
class Camera : public Node class Camera : public Node
{ {
protected: 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}; Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {}
Math::Vector4f camPosition{};
float nearPlane, farPlane, width, height;
Camera() : nearPlane(0), farPlane(0), width(0), height(0) {}
Camera(float width, float height, float nearPlane, float farPlane) 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: public:
void Init(float width, float height, float nearPlane, float farPlane) void Init(float width, float height, float nearPlane, float farPlane)
{ {
this->width = width; m_width = width;
this->height = height; m_height = height;
this->nearPlane = nearPlane; m_nearPlane = nearPlane;
this->farPlane = farPlane; m_farPlane = farPlane;
Node::Init(); Node::Init();
UpdateProjectionMatrix(); UpdateProjectionMatrix();
} }
virtual void SetSize(const float width, const float height) virtual void SetSize(const float width, const float height)
{ {
if (this->width == width && this->height == height) return; if (m_width == width && m_height == height) return;
this->width = width; m_width = width;
this->height = height; m_height = height;
UpdateProjectionMatrix(); UpdateProjectionMatrix();
} }
void SetNearPlane(float nearPlane) void SetNearPlane(float nearPlane)
{ {
this->nearPlane = nearPlane; m_nearPlane = nearPlane;
} }
void SetFarPlane(float farPlane) void SetFarPlane(float farPlane)
{ {
this->farPlane = farPlane; m_farPlane = farPlane;
} }
[[nodiscard]] float NearPlane() const [[nodiscard]] float NearPlane() const
{ {
return nearPlane; return m_nearPlane;
} }
[[nodiscard]] float FarPlane() const [[nodiscard]] float FarPlane() const
{ {
return farPlane; return m_farPlane;
} }
virtual void UpdateProjectionMatrix() = 0; 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 //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 // 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 void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) override
{ {
Node::UpdateWorldMatrix(parentWorldMat); Node::UpdateWorldMatrix(parentWorldMat);
view = Math::Utils::inverse(GetWorldMatrix()); m_view = Math::Utils::inverse(GetWorldMatrix());
camPosition = GetWorldMatrix()[3];
UpdateViewProjectionMatrix(); UpdateViewProjectionMatrix();
} }
void SetViewMatrix(const Math::Matrix4f& view)
{
SetMatrix(Math::Utils::inverse(view));
}
[[nodiscard]] const Math::Matrix4f& GetViewProjectionMatrix() const [[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 class PerspectiveCamera : public Camera
{ {
protected: protected:
float fov = 0, aspect = 0; float m_fov = 0, m_aspect = 0;
public: public:
PerspectiveCamera() = default; PerspectiveCamera() = default;
@@ -114,31 +131,31 @@ namespace openVulkanoCpp::Scene
void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane) void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane)
{ {
this->fov = Math::Utils::radians(fovDegrees); m_fov = Math::Utils::radians(fovDegrees);
aspect = width / height; m_aspect = width / height;
Camera::Init(width, height, nearPlane, farPlane); Camera::Init(width, height, nearPlane, farPlane);
} }
void SetSize(const float width, const float height) override void SetSize(const float width, const float height) override
{ {
aspect = width / height; m_aspect = width / height;
Camera::SetSize(width, height); Camera::SetSize(width, height);
} }
void SetAspect(const float aspect) void SetAspect(const float aspect)
{ {
this->aspect = aspect; m_aspect = aspect;
Camera::SetSize(aspect, 1); Camera::SetSize(aspect, 1);
} }
void SetFovX(const float fov) 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) 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) void SetFov(const float fov)
@@ -148,32 +165,32 @@ namespace openVulkanoCpp::Scene
void SetFovRad(const float fov) void SetFovRad(const float fov)
{ {
this->fov = fov; m_fov = fov;
} }
[[nodiscard]] float GetFov() const [[nodiscard]] float GetFov() const
{ {
return Math::Utils::degrees(fov); return Math::Utils::degrees(m_fov);
} }
[[nodiscard]] float GetFovX() const [[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 [[nodiscard]] float GetFovRad() const
{ {
return fov; return m_fov;
} }
[[nodiscard]] float GetFovXRad() const [[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 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(); UpdateViewProjectionMatrix();
} }
}; };
@@ -183,8 +200,8 @@ namespace openVulkanoCpp::Scene
public: public:
void UpdateProjectionMatrix() final void UpdateProjectionMatrix() final
{ {
const float widthHalf = width * 0.5f, heightHalf = height * 0.5f; const float widthHalf = m_width * 0.5f, heightHalf = m_height * 0.5f;
projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, nearPlane, farPlane); m_projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
UpdateViewProjectionMatrix(); UpdateViewProjectionMatrix();
} }
}; };