Hello, I’ve started learning openGL and got to the matrices part. I am trying to display meshes in the right aspect ratio (so they’re not stretched anymore) using glm::ortho but when I try to do this nothing is visible. When I pass an identity matrix instead of glm::ortho it does work. It also works when I only pass the transform matrix or no matrices at all.
here is the code
vertex shader:
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec3 aCol;
layout (location = 2) in vec2 aTexCoord;
out vec3 Color;
out vec2 TexCoord;
uniform mat4 Transform;
uniform mat4 Projection;
void main()
{
gl_Position = Projection * Transform * vec4(aPos, 1.0, 1.0);
Color = aCol;
TexCoord = aTexCoord;
}
I have a Camera struct with a Viewport struct inside, and I update the windowWidth and height in an update loop:
struct Camera
{
struct Viewport
{
int windowWidth = 500;
int windowHeight = 500;
float left{ 0 }, right{ 0 }, top{ 0 }, bottom{ 0 };
};
Viewport viewport;
glm::vec2 position = glm::vec2(0, 0);
void SetProjection()
{
viewport.left = -viewport.windowWidth * 0.5f;
viewport.right = viewport.windowWidth * 0.5f;
viewport.top = viewport.windowHeight * 0.5f;
viewport.bottom = -viewport.windowHeight * 0.5f;
projection = glm::mat4(1.0f);
/* doesn't work when I uncomment this
projection = glm::ortho(
viewport.left + position.x,
viewport.right + position.x,
viewport.bottom + position.y,
viewport.top + position.y,
.1f, 1.f);
*/
}
glm::mat4 GetProjection()
{
return projection;
}
private:
glm::mat4 projection;
};
updating the resolution:
SDL_GL_GetDrawableSize(window, &camera.viewport.windowWidth, &camera.viewport.windowHeight);
glViewport(0, 0, camera.viewport.windowWidth, camera.viewport.windowHeight);
Thanks in advance!