0

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?

Alessandro Lioi
  • 63
  • 1
  • 12
  • 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 May 26 '16 at 18:20
  • @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 matrix – Alessandro Lioi May 26 '16 at 18:25
  • 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 May 26 '16 at 18:30
  • Set the diagonal matrix to 1, i.e. `[0][0] = 1`, `[1][1] = 1`, etc. – pingul May 26 '16 at 18:31
  • Will try this when I can. By the way, is it correct the way I apply the matrix in my shader? – Alessandro Lioi May 26 '16 at 18:56
  • Yeah, that is correct. I wrote a longer answer here http://stackoverflow.com/questions/36703985/viewing-pipeline-and-3d-transformations/36708507#36708507 that you could look at and see if it is useful in your case. – pingul May 26 '16 at 20:10

1 Answers1

1

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.

246tNt
  • 2,122
  • 1
  • 16
  • 20
  • It is better than what I had before, but still isn't what I want. It now render the upper right quarter of the screen, but it should render a 32x32 quad in the left bottom corner. Why is that? PS: using the last code in my question, it works as I want it to work. – Alessandro Lioi May 27 '16 at 14:34
  • Okay, now it works. Apparently I set the variables wrong in my matrix. Thank you very much! – Alessandro Lioi May 28 '16 at 07:09