From 979b531e9fff8ef044cf036d8ebd7fab3c17bec3 Mon Sep 17 00:00:00 2001 From: Jeremy Kniager Date: Fri, 22 Nov 2019 14:55:57 -0700 Subject: vkcube: Fix Cube Squishing on Resize Modified viewport creation in vkcube to keep the aspect ratio square and centered. This change prevents cube from squishing and warping when the window is resized. It now shrinks and grows. Change-Id: Ie2b3c5747293dfed968af5d83b581ac74d81bb10 --- cube/cube.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'cube/cube.cpp') diff --git a/cube/cube.cpp b/cube/cube.cpp index abe80a88..4af97856 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -850,9 +850,23 @@ void Demo::draw_build_cmd(vk::CommandBuffer commandBuffer) { commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline_layout, 0, 1, &swapchain_image_resources[current_buffer].descriptor_set, 0, nullptr); - - auto const viewport = - vk::Viewport().setWidth((float)width).setHeight((float)height).setMinDepth((float)0.0f).setMaxDepth((float)1.0f); + float viewport_dimension; + float viewport_x = 0.0f; + float viewport_y = 0.0f; + if (width < height) { + viewport_dimension = (float)width; + viewport_y = (height - width) / 2.0f; + } else { + viewport_dimension = (float)height; + viewport_x = (width - height) / 2.0f; + } + auto const viewport = vk::Viewport() + .setX(viewport_x) + .setY(viewport_y) + .setWidth((float)viewport_dimension) + .setHeight((float)viewport_dimension) + .setMinDepth((float)0.0f) + .setMaxDepth((float)1.0f); commandBuffer.setViewport(0, 1, &viewport); vk::Rect2D const scissor(vk::Offset2D(0, 0), vk::Extent2D(width, height)); -- cgit v1.2.3