0
votes

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?

1
The projection matrix looks weird (and also has a weird name: the projection goes from world coordinates to screen coordinates). Why is only [0][0] and [1][1] set?pingul
@pingul don't look at the names, I still have to change everything. This projection matrix should just change my coordinates from the screen to OpenGL coordinates. So that the -1,-1 in OpenGL is my 0,0 and 1,1 is my Width,Height. I then calculated that to apply this kind of transformation I need to set just these two variables of the matrixAlessandro Lioi
I think you're thinking about it the wrong way. The point of the projection matrix is to go from your world coordinates (i.e. the 3D world you're creating) to the screen coordinates. Essentially we're projecting points into 3D space to our 2D screen. The simplest possible projection matrix is the identity matrix (i.e. no projection). Have you tried using that?pingul
Set the diagonal matrix to 1, i.e. [0][0] = 1, [1][1] = 1, etc.pingul
Will try this when I can. By the way, is it correct the way I apply the matrix in my shader?Alessandro Lioi

1 Answers

1
votes

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.