Skip to content

Commit 36e157d

Browse files
author
pocdn
committed
corrected rescaling of window size dynamically
1 parent 7c4a3c9 commit 36e157d

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

examples/cybertruck/cybertruck.cpp

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ int main() {
4141
}
4242
window.dumpCaps(std::cout, fw.physicalDevice());
4343

44-
// https://learnopengl.com/Getting-started/Coordinate-Systems
45-
// https://www.khronos.org/opengl/wiki/Face_Culling
46-
// https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/
47-
// https://www.saschawillems.de/blog/2019/03/29/flipping-the-vulkan-viewport/
48-
// flip viewport to match opengl ( +x > Right, +y ^ UP, +z towards viewer from screen ), instead of vulkan default
49-
// also requires pipeline set with cullMode:BACK and frontFace:CounterClockWise
50-
auto viewport = vk::Viewport{
51-
.x = 0.0f, //Vulkan default:0
52-
.y = static_cast<float>(window.height()), //Vulkan default:0
53-
.width = static_cast<float>(window.width()), //Vulkan default:width
54-
.height = -static_cast<float>(window.height()),//Vulkan default:height
55-
.minDepth = 0.0f, //Vulkan default:0
56-
.maxDepth = 1.0f //Vulkan default:1
57-
};
58-
5944
////////////////////////////////////////
6045
//
6146
// Create Uniform Buffer
@@ -131,17 +116,35 @@ int main() {
131116

132117
auto pipelineLayout = plm.createUnique(device);
133118

134-
vku::PipelineMaker pm{window.width(), window.height()};
135-
pm.shader(vk::ShaderStageFlagBits::eVertex, vert)
136-
.shader(vk::ShaderStageFlagBits::eFragment, frag)
137-
.vertexBinding(0, sizeof(Vertex))
138-
.vertexAttribute(0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos))
139-
.viewport(viewport) // viewport set to match openGL, affects cullMode and frontFace
140-
.frontFace(vk::FrontFace::eCounterClockwise) // openGL default, GL_CCW face is front
141-
.cullMode(vk::CullModeFlagBits::eBack); // openGL default, GL_BACK is the face to be culled
142-
143-
// Create a pipeline using a renderPass built for our window.
144-
auto pipeline = pm.createUnique(device, fw.pipelineCache(), *pipelineLayout, window.renderPass());
119+
auto buildPipeline = [&]() {
120+
// https://learnopengl.com/Getting-started/Coordinate-Systems
121+
// https://www.khronos.org/opengl/wiki/Face_Culling
122+
// https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/
123+
// https://www.saschawillems.de/blog/2019/03/29/flipping-the-vulkan-viewport/
124+
// flip viewport to match opengl ( +x > Right, +y ^ UP, +z towards viewer from screen ), instead of vulkan default
125+
// also requires pipeline set with cullMode:BACK and frontFace:CounterClockWise
126+
auto viewport = vk::Viewport{
127+
.x = 0.0f, //Vulkan default:0
128+
.y = static_cast<float>(window.height()), //Vulkan default:0
129+
.width = static_cast<float>(window.width()), //Vulkan default:width
130+
.height = -static_cast<float>(window.height()),//Vulkan default:height
131+
.minDepth = 0.0f, //Vulkan default:0
132+
.maxDepth = 1.0f //Vulkan default:1
133+
};
134+
135+
vku::PipelineMaker pm{window.width(), window.height()};
136+
pm.shader(vk::ShaderStageFlagBits::eVertex, vert)
137+
.shader(vk::ShaderStageFlagBits::eFragment, frag)
138+
.vertexBinding(0, sizeof(Vertex))
139+
.vertexAttribute(0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos))
140+
.viewport(viewport) // viewport set to match openGL, affects cullMode and frontFace
141+
.frontFace(vk::FrontFace::eCounterClockwise) // openGL default, GL_CCW face is front
142+
.cullMode(vk::CullModeFlagBits::eBack); // openGL default, GL_BACK is the face to be culled
143+
144+
// Create a pipeline using a renderPass built for our window.
145+
return pm.createUnique(device, fw.pipelineCache(), *pipelineLayout, window.renderPass());
146+
};
147+
auto pipeline = buildPipeline();
145148

146149
////////////////////////////////////////
147150
//
@@ -157,11 +160,20 @@ int main() {
157160
glfwGetCursorPos(glfwwindow, &xpos, &ypos);
158161
};
159162

163+
static auto ww = window.width();
164+
static auto wh = window.height();
165+
if (ww != window.width() || wh != window.height()) {
166+
ww = window.width();
167+
wh = window.height();
168+
pipeline = buildPipeline();
169+
std::cout << "." << std::endl;
170+
}
171+
160172
Uniform uniform {
161-
.iResolution = glm::vec4(window.width(), window.height(), 1., 0.),
173+
.iResolution = glm::vec4(ww, wh, 1., 0.),
162174
.iTime = glm::vec4{iFrame/60.0f, 0.0, 0.0, 0.0},
163175
.iFrame = glm::ivec4{iFrame++, 0, 0, 0},
164-
.iMouse = glm::vec4{xpos, window.height()-ypos, state*xpos, state*ypos},
176+
.iMouse = glm::vec4{xpos, wh-ypos, state*xpos, state*ypos},
165177
};
166178

167179
window.draw(device, fw.graphicsQueue(),

0 commit comments

Comments
 (0)