Extend ortho camera

This commit is contained in:
2024-06-28 09:34:50 +02:00
parent ebd12cfd2b
commit 475c72e505
4 changed files with 23 additions and 9 deletions

View File

@@ -114,10 +114,11 @@ namespace OpenVulkano
} }
public: public:
MovingCubeAppImpl() : m_camera(90, 16, 9, 0.1, 1000) MovingCubeAppImpl() : m_camera(90)
{ {
m_morphableCameraControl.Init(&m_camera); m_morphableCameraControl.Init(&m_camera);
m_cameraControl.Init(&m_camera); m_cameraControl.Init(&m_camera);
m_camera.SetZoom(50);
} }
void Init() override void Init() override

View File

@@ -25,7 +25,8 @@ namespace OpenVulkano::Scene
Math::Vector4f m_camPosition{}; Math::Vector4f m_camPosition{};
float m_nearPlane, m_farPlane, m_width, m_height; float m_nearPlane, m_farPlane, m_width, m_height;
float m_fov = 0, m_aspect = 0, m_scaleFactor = 0, m_perPixelScaleFactor = 0; float m_fov = 0, m_aspect = 0, m_scaleFactor = 0, m_perPixelScaleFactor = 0;
std::array<uint8_t, 16> m_userData{}; float m_contentScaleFactor = 1, m_zoom = 1; // For use with ortho camera
std::array<uint8_t, 8> m_userData{};
Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {} Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {}
@@ -81,6 +82,17 @@ namespace OpenVulkano::Scene
return m_farPlane; return m_farPlane;
} }
void SetContentScaleFactor(float contentScale = 1)
{
m_contentScaleFactor = 1.0f / contentScale;
}
[[nodiscard]] float GetContentScaleFactor() const { return 1.0f / m_contentScaleFactor; }
void SetZoom(float zoom) { m_zoom = 1.0f / zoom; }
[[nodiscard]] float GetZoom() const { return 1.0f / m_zoom; }
virtual void UpdateProjectionMatrix() = 0; virtual void UpdateProjectionMatrix() = 0;
void UpdateViewProjectionMatrix() void UpdateViewProjectionMatrix()
@@ -141,7 +153,7 @@ namespace OpenVulkano::Scene
* The 16 byte of user data can be used to transmit additional data about the camera to the shader. * The 16 byte of user data can be used to transmit additional data about the camera to the shader.
* @return reference to the custom data array * @return reference to the custom data array
*/ */
[[nodiscard]] std::array<uint8_t, 16>& GetUserData() { return m_userData; } [[nodiscard]] std::array<uint8_t, 8>& GetUserData() { return m_userData; }
[[nodiscard]] float GetPixelScaleFactor() const { return m_perPixelScaleFactor; } [[nodiscard]] float GetPixelScaleFactor() const { return m_perPixelScaleFactor; }
}; };
@@ -245,7 +257,8 @@ namespace OpenVulkano::Scene
public: public:
void UpdateProjectionMatrix() final void UpdateProjectionMatrix() final
{ {
const float widthHalf = m_width * 0.5f, heightHalf = m_height * 0.5f; 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); m_projection = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
UpdateViewProjectionMatrix(); UpdateViewProjectionMatrix();
} }

View File

@@ -25,15 +25,15 @@ namespace OpenVulkano::Scene
PerspectiveCamera::UpdateProjectionMatrix(); PerspectiveCamera::UpdateProjectionMatrix();
if (m_morphState != 0) if (m_morphState != 0)
{ {
float aspectH = m_aspect; const float scale = 0.5f * m_contentScaleFactor * m_zoom;
float aspectV = 1; const float widthHalf = m_width * scale, heightHalf = m_height * scale;
m_orthoMatrix = Math::Utils::orthoRH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, m_nearPlane, m_farPlane);
if (m_morphState == 1) if (m_morphState == 1)
{ {
m_projection = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane); m_projection = m_orthoMatrix;
} }
else else
{ {
m_orthoMatrix = Math::Utils::orthoRH_ZO(-aspectH, aspectH, -aspectV, aspectV, m_nearPlane, m_farPlane);
m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState); m_projection = BlendMatrices(m_projection, m_orthoMatrix, m_morphState);
} }
UpdateViewProjectionMatrix(); UpdateViewProjectionMatrix();

View File

@@ -16,7 +16,7 @@ namespace OpenVulkano::Scene
Math::Matrix4f m_orthoMatrix; Math::Matrix4f m_orthoMatrix;
public: public:
MorphableCamera(float fovDegrees, float width, float height, float nearPlane, float farPlane) MorphableCamera(float fovDegrees, float nearPlane = 0.1f, float farPlane = 1000.0f, float width = 16, float height = 9)
: PerspectiveCamera(fovDegrees, nearPlane, farPlane, width, height), m_morphState(0.0f) : PerspectiveCamera(fovDegrees, nearPlane, farPlane, width, height), m_morphState(0.0f)
{ {
UpdateProjectionMatrix(); UpdateProjectionMatrix();