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