Alessandro Lioi
Alessandro Lioi

Reputation: 63

OpenGL GLSL - Projection matrix not working

I've got a basic OpenGL application and I want to use my projection matrix.

This is my matrix:

WorldCoordinates.m[0][0] = 2.0f / Width - 1.0f; WorldCoordinates.m[0][1] = 0; WorldCoordinates.m[0][2] = 0, WorldCoordinates.m[0][3] = 0;
WorldCoordinates.m[1][0] = 0; WorldCoordinates.m[1][1] = 2.0f / Height - 1.0f; WorldCoordinates.m[1][2] = 0, WorldCoordinates.m[1][3] = 0;
WorldCoordinates.m[2][0] = 0; WorldCoordinates.m[2][1] = 0; WorldCoordinates.m[2][2] = 0, WorldCoordinates.m[2][3] = 0;
WorldCoordinates.m[3][0] = 0; WorldCoordinates.m[3][1] = 0; WorldCoordinates.m[3][2] = 0, WorldCoordinates.m[3][3] = 0;

(WorldCoordinates is the Matrix4 struct that contains just a variable called m that is a float[4][4])(Width and Height are two ints). I then apply this coordinates to my vertex shader using this:

shader.Bind();
glUniformMatrix4fv(glGetUniformLocation(shader.GetProgramID(), "worldCoordinates"), 1, GL_TRUE, &WorldCoordinates.m[0][0]);

(Shader is a class and has got a Bind() method that is just glUseProgram).

This is my Vertex Shader GLSL

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;

out vec3 Color;
out vec2 TexCoord;

uniform mat4 worldCoordinates;

void main()
{
    gl_Position = worldCoordinates * vec4(position, 1.0f);
    Color = color;
    TexCoord = texCoord;
}

Using this, it doesn't work. But changing the gl_Position call to this:

gl_Position = vec4(vec3(position.x * 1/400 -1, position.y * 1/300 -1, 1.0f), 1.0f);

it renders as expected. Why is that?

Upvotes: 0

Views: 1070

Answers (1)

246tNt
246tNt

Reputation: 2170

This is how you build a orthogonal projection matrix :

static void
mat4_ortho(mat4_t m, float left, float right, float bottom, float top, float near, float far)
{
        float rl = right -  left;
        float tb =   top - bottom;
        float fn =   far -  near;

        mat4_zero(m);

        m[ 0] =  2.0f / rl;
        m[ 5] =  2.0f / tb;
        m[10] = -2.0f / fn;
        m[12] = -(left +  right) / rl;
        m[13] = -( top + bottom) / tb;
        m[14] = -( far +   near) / fn;
        m[15] = 1.0f;
}

For you case, you'd set left=0, right=width, bottom=0, top=height, near and far don't matter, just set -1.0 and 1.0 for instance.

With such a matrix, the vertex coordinates you use for drawing will map 1:1 with the pixels on screen.

Upvotes: 1

Related Questions