# OpenGL viewport is stretched

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);


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.

#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;
}

• Do you make sure to update the viewport and projection matrix whenever your window is resized? – Nathan Wride Feb 27 '20 at 23:18
• 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. – egordiac Feb 28 '20 at 9:48
• Can you post your vertex shader as well, please? – Maximus Minimus Feb 28 '20 at 10:20
• @MaximusMinimus added it – egordiac Feb 28 '20 at 10:23
• You're drawing a rectangle because your vertices define a rectangle, not a square. – Magma Feb 28 '20 at 12:50
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.22012849152088165, "perplexity": 4069.3910566792724}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-10/segments/1614178358798.23/warc/CC-MAIN-20210227084805-20210227114805-00132.warc.gz"}