Image stretching when the window is not a square

When I use a squared window (800×800 for example) the sprites work perfectly, but when I use a rectangle window(1280×720 for example) the texture I’m trying to render is stretched horizontally.


// Includes
#include "../Libraries/GLAD/glad.h"
#include "../Libraries/STB/stb_image.h"
#include <GLFW/glfw3.h>

#include "../../Header Files/Sprites/Shader.hpp"
#include "../../Header Files/Sprites/VertexArray.hpp"
#include "../../Header Files/Sprites/VertexBuffer.hpp"
#include "../../Header Files/Sprites/ElementBuffer.hpp"
#include "../../Header Files/Sprites/Texture.hpp"

// The Global variables of the project.
#include "../Header Files/Global.hpp"

// The vertices of our square.
GLfloat vertices[] =
{ //    COORDINATES        /     COLORS    //   TEXTURE COORDINATES
    -0.5f, -0.5f, 0.0f,     1.0f, 0.0f,  0.0f,     0.0f, 0.0f, // Lower left corner
    -0.5f,  0.5f, 0.0f,     0.0f, 1.0f,  0.0f,     0.0f, 1.0f, // Upper left corner
     0.5f,  0.5f, 0.0f,     0.0f, 0.0f,  1.0f,     1.0f, 1.0f, // Upper right corner
     0.5f, -0.5f, 0.0f,     1.0f, 1.0f,  1.0f,     1.0f, 0.0f, // Lower right corner
};

// The order the program will draw the indices of the square.
const GLuint indices[] =
{
    0, 2, 1,
    0, 3, 2,
};

// Get the amount of vertices from the vertices variable.
const GLint amountOfVertices = 6;

int main()
{
    // Initialize GLFW.
    if (!glfwInit())
    {
        return 0;
    }

    // Specify to GLFW the version of OpenGL. (v3)
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    // Specify to GLFW the profile we want to use. (Core to get the modern functions.)
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create the window.
    GLFWwindow* window = glfwCreateWindow(WINDOW::WIDTH, WINDOW::HEIGHT, WINDOW::TITLE.c_str(), NULL, NULL);

    // Check if the window was successfully created.
    if (window == NULL)
    {
        std::cout << "Couldn't create the window successfully..." << 'n';
        glfwTerminate();
        return 0;
    }

    // Make the window the main window for glfw to handle.
    glfwMakeContextCurrent(window);

    // Initialize GLAD.
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // Set the viewport of the window.
    glViewport(0, 0, WINDOW::WIDTH, WINDOW::HEIGHT);
    
    // Create the shader program of the triangle.
    Shader shaderProgram("Triangle.vert", "Triangle.frag");

    // Create the buffer manager so OpenGL can read the buffer.
    VertexArray vertexArray;
    // Bind it so the vertex buffer and element can be added to the vertex array.
    vertexArray.Bind();

    // Create the buffer so the sending info to the GPU is more efficient (VBO).
    VertexBuffer vertexBuffer(vertices, sizeof(vertices));
    // Create the buffer so we can draw the triangle with a specific order of indices.
    ElementBuffer elementBuffer(indices, sizeof(indices));

    // Link the vertex buffer to the vertex array so the CPU can read the vertex buffer.
    vertexArray.LinkAttribute(vertexBuffer, 0, 3, GL_FLOAT, 8 * sizeof(float), (void*)0);
    vertexArray.LinkAttribute(vertexBuffer, 1, 3, GL_FLOAT, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    vertexArray.LinkAttribute(vertexBuffer, 2, 2, GL_FLOAT, 8 * sizeof(float), (void*)(6 * sizeof(float)));

    // Unbind the AVO, EBO and VBO so we don't accidentally modify them when using the OpenGL functions.
    vertexArray.Unbind();
    vertexBuffer.Unbind();
    elementBuffer.Unbind();

    // Create the texture and add the parameters into it.
    Texture texture("Resource Files/Images/pop_cat.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGBA, GL_UNSIGNED_BYTE);
    
    // Bind the texture into the shader.
    texture.BindToShader(shaderProgram, "texture0");

    // Set the color of the background and clear the screen.
    glClearColor(1, 0.5f, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    // Swap the buffers of the window so we can draw things on screen.
    glfwSwapBuffers(window);

    // Create the game loop.
    while (!glfwWindowShouldClose(window))
    {
        // Set the color of the background and clear the screen.
        glClearColor(1, 0.5f, 1, 1);
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw the triangle on screen.
        // Activate the shader.
        shaderProgram.Activate();

        // Bind the texture so OpenGL knows which texture to draw.
        texture.Bind();

        // Set the main VAO. (Not necessary but just to make sure.)
        vertexArray.Bind();

        // Draw the triangle on screen.
        glDrawElements(GL_TRIANGLES, amountOfVertices, GL_UNSIGNED_INT, 0);

        // Swap the buffers each frame so we can see stuff on screen.
        glfwSwapBuffers(window);

        // Manage events like the close button of the window.
        glfwPollEvents();
    }

    // Delete all the shaders.
    vertexBuffer.Delete();
    vertexArray.Delete();
    shaderProgram.Delete();
    
    // Delete all the textures.
    texture.Delete();

    // Close the window.
    glfwDestroyWindow(window);

    // Terminate GLFW.
    glfwTerminate();
    return 0;
}

I just want a way to make the textures have their original aspect ratio independently of the window size. I also have no libraries installed so if I need one I would appreciate if you let me know.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật