For this project I am using GLFW, glad and glm. My window looks like this: The dimensions of the window are correct, but the viewport is stretched, making my square into a rectangle. How do I use a 16:9 resolution (1280x720) without my viewport stretching?

This is what the window should look like: I have a projection matrix set up in my code, and sent to my shader:

projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);
glUniformMatrix4fv(glGetUniformLocation(basicShader.ID, "projectionMatrix"), 1, GL_FALSE, glm::value_ptr(projection));

EDIT: So I update the viewport everytime the window is resized, to it's correct dimensions. But now, even with a 1:1 (600x600) aspect, the square remains a rectangle. So I guess it isn't the viewport's fault? So something else is causing the stretch, as far I know the vertices aren't at fault, seeing as they're correct:

std::vector<GLfloat> vert {
         1,  0,  0,   1.0, 0.0,
        -1,  0,  0,   0.0, 0.0,
        -1,  1,  0,   0.0, 1.0,
         1,  1,  0,   1.0, 1.0,
        -1,  1,  0,   0.0, 1.0,
         1,  0,  0,   1.0, 0.0

And the modelMatrix is set up correctly, without any changes being made to it.

My vertex shader:

#version 330 core
layout (location = 0) in vec3 Pos;
layout (location = 1) in vec2 TexCoord;

out vec2 texCoord;

uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main() {

    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(Pos.x, Pos.y, Pos.z, 1.0f);
    texCoord = TexCoord;
  • \$\begingroup\$ Do you make sure to update the viewport and projection matrix whenever your window is resized? \$\endgroup\$ Feb 27 '20 at 23:18
  • \$\begingroup\$ Yes, I have a method which gets the window's width and height and adjusts the projection matrix appropriately. Though my problem isn't that the viewport is being stretched, when the window is resized. My problem is that the viewport is already stretched at the start. \$\endgroup\$
    – egordiac
    Feb 28 '20 at 9:48
  • \$\begingroup\$ Can you post your vertex shader as well, please? \$\endgroup\$ Feb 28 '20 at 10:20
  • \$\begingroup\$ @MaximusMinimus added it \$\endgroup\$
    – egordiac
    Feb 28 '20 at 10:23
  • 1
    \$\begingroup\$ You're drawing a rectangle because your vertices define a rectangle, not a square. \$\endgroup\$
    – Magma
    Feb 28 '20 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.