From abdd7137abcebeca64b532fdc8c2b636f5d52f57 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Wed, 24 Jul 2024 22:06:01 +0200 Subject: [PATCH] Improved Resolution handling. --- samples/03_model_render/model_render.cpp | 61 ++++++++++++++++-------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/samples/03_model_render/model_render.cpp b/samples/03_model_render/model_render.cpp index 4531309..ce54756 100644 --- a/samples/03_model_render/model_render.cpp +++ b/samples/03_model_render/model_render.cpp @@ -122,9 +122,9 @@ main(int, char **) Camera camera = { .m_View = lookAt(vec3(0.0f, 2.0f, 2.0f), vec3(0.0f), vec3(0.0f, 1.0f, 0.0f)), .m_Perspective = glm::perspective( - 70_deg, Cast(internalResolution.width) / Cast(internalResolution.height), 0.1f, 100.0f), + 70_deg, Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height), 0.1f, 100.0f), .m_Position = vec4{0.0f, 2.0f, 2.0f, 1.0f}, - .m_AspectRatio = Cast(internalResolution.width) / Cast(internalResolution.height), + .m_AspectRatio = Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height), }; UniformBuffer ubo; @@ -273,7 +273,14 @@ main(int, char **) gui::Init(&context, &device, &window, swapchain.m_Format, Cast(swapchain.m_ImageViews.size()), queueAllocation.m_Family, commandQueue); bool rotating = false; - i32 currentInternalResolution[2] = {Cast(internalResolution.width), Cast(internalResolution.height)}; + bool lockToScreen = true; + i32 height = Cast(internalResolution.height); + vk::Extent2D inputResolution = internalResolution; + + swapchain.RegisterResizeCallback([&camera](vk::Extent2D extent) { + camera.m_AspectRatio = Cast(extent.width) / Cast(extent.height); + camera.m_Perspective = glm::perspective(70_deg, camera.m_AspectRatio, 0.1f, 100.0f); + }); Time::Init(); @@ -285,31 +292,45 @@ main(int, char **) gui::StartBuild(); gui::Begin("Settings"); - gui::Text("Window Resolution: %udx%ud", swapchain.m_Extent.width, swapchain.m_Extent.height); - gui::Text("FrameBuffer Resolution %dx%d", currentInternalResolution[0], currentInternalResolution[1]); - gui::InputInt("FrameBuffer Height", ¤tInternalResolution[1], 1, 10); - - camera.m_AspectRatio = Cast(swapchain.m_Extent.width) / Cast(swapchain.m_Extent.height); - currentInternalResolution[0] = Cast(camera.m_AspectRatio * currentInternalResolution[1]); - for (i32 &val : currentInternalResolution) + gui::Text("Window Resolution: %ux%u", swapchain.m_Extent.width, swapchain.m_Extent.height); + gui::Text("FrameBuffer Resolution %ux%u", internalResolution.width, internalResolution.height); + gui::Checkbox("Lock Resolution to Window", &lockToScreen); + if (!lockToScreen) { - val = eastl::clamp(val, 64, 7680); - } - camera.m_Perspective = glm::perspective(70_deg, camera.m_AspectRatio, 0.1f, 100.0f); - - if (gui::Button("Change Resolution")) - { - if (currentInternalResolution[0] != internalResolution.width || - currentInternalResolution[1] != internalResolution.height) + if (gui::InputInt("FrameBuffer Height", &height, 1, 10)) { - internalResolution = - vk::Extent2D{}.setWidth(currentInternalResolution[0]).setHeight(currentInternalResolution[1]); + height = eastl::clamp(height, 64, 4320); + } + + inputResolution.height = height; + inputResolution.width = Cast(camera.m_AspectRatio * Cast(inputResolution.height)); + + if (gui::Button("Change Resolution")) + { + if (inputResolution.width != internalResolution.width || + inputResolution.height != internalResolution.height) + { + internalResolution = inputResolution; + viewport.width = Cast(internalResolution.width); + viewport.height = -Cast(internalResolution.height); + viewport.y = Cast(internalResolution.height); + scissor.extent = internalResolution; + } + } + } + else + { + if (swapchain.m_Extent.width != internalResolution.width || + swapchain.m_Extent.height != internalResolution.height) + { + internalResolution = swapchain.m_Extent; viewport.width = Cast(internalResolution.width); viewport.height = -Cast(internalResolution.height); viewport.y = Cast(internalResolution.height); scissor.extent = internalResolution; } } + gui::Separator(); gui::Text("Delta: %0.6f ms", 1000.0f * Time::m_Delta); gui::Text("FPS: %0.6f", 1.0f / Time::m_Delta); gui::Checkbox("Rotate", &rotating);