Update camera projection matrix handling

This commit is contained in:
Georg Hagen
2024-07-20 19:30:28 +02:00
parent 22d6ea9d2e
commit bcad0116d4
3 changed files with 18 additions and 16 deletions

View File

@@ -97,9 +97,7 @@ namespace OpenVulkano::Scene
void UpdateViewProjectionMatrix()
{
//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
m_viewProjection = m_projection * Math::Matrix4f(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) * m_view;
m_viewProjection = m_projection * m_view;
}
void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) override
@@ -115,6 +113,14 @@ namespace OpenVulkano::Scene
SetMatrix(Math::Utils::inverse(view));
}
void SetProjectionMatrix(const Math::Matrix4f& projection)
{
//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
m_projection = projection * Math::Matrix4f(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
UpdateViewProjectionMatrix();
}
[[nodiscard]] const Math::Matrix4f& GetViewProjectionMatrix() const
{
return m_viewProjection;
@@ -251,8 +257,7 @@ namespace OpenVulkano::Scene
void UpdateProjectionMatrix() override
{
m_projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
UpdateViewProjectionMatrix();
SetProjectionMatrix(Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane));
}
[[nodiscard]] bool IsPerspective() const override { return true; }
@@ -265,11 +270,9 @@ namespace OpenVulkano::Scene
{
const float scale = 0.5f * m_contentScaleFactor * m_zoom;
const float widthHalf = m_width * scale, heightHalf = m_height * scale;
m_projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
UpdateViewProjectionMatrix();
SetProjectionMatrix(Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane));
}
[[nodiscard]] bool IsOrtho() const override { return true; }
};
}

View File

@@ -22,21 +22,21 @@ namespace OpenVulkano::Scene
}
void MorphableCamera::UpdateProjectionMatrix()
{
PerspectiveCamera::UpdateProjectionMatrix();
if (m_morphState == 0) PerspectiveCamera::UpdateProjectionMatrix();
if (m_morphState != 0)
{
const float scale = 0.5f * m_contentScaleFactor * m_zoom;
const float widthHalf = m_width * scale, heightHalf = m_height * scale;
m_orthoMatrix = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
Math::Matrix4f orthoMatrix = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
if (m_morphState == 1)
{
m_projection = m_orthoMatrix;
SetProjectionMatrix(orthoMatrix);
}
else
{
m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState);
Math::Matrix4f projection = Math::Utils::perspectiveRH_ZO(m_fov, m_aspect, m_nearPlane, m_farPlane);
SetProjectionMatrix(BlendMatrices(projection, orthoMatrix, m_morphState));
}
UpdateViewProjectionMatrix();
}
}
}
}

View File

@@ -13,7 +13,6 @@ namespace OpenVulkano::Scene
class MorphableCamera : public PerspectiveCamera
{
float m_morphState;
Math::Matrix4f m_orthoMatrix;
public:
MorphableCamera(float fovDegrees, float nearPlane = 0.1f, float farPlane = 1000.0f, float width = 16, float height = 9)
@@ -33,4 +32,4 @@ namespace OpenVulkano::Scene
[[nodiscard]] bool IsPerspective() const override { return m_morphState == 0; }
[[nodiscard]] bool IsOrtho() const override { return m_morphState == 1; }
};
}
}