# OpenGL Projection Matrix problem

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im new to the modern OpenGL. Ive set up the view matrix, and it worked fine. Then I added a basic projection matrix. This one:

        GLfloat projection_matrix[16] =
{
1.0, 0.0,  0.0, 0.0,
0.0, 1.0,  0.0, 0.0,
0.0, 0.0,  1.0, 1.0,
0.0, 0.0,  0.0, 0.0
};

....and it worked fine. I tried to use a proper matrix and its not working. The camera looks into the wrong direction and the movement and rotation is mirrored:

(I use 90 deg perspective angle and 1.0 aspect ratio to keep it simple)

    float
n = 0.5,        //near
f = 100.0,      //far
w2 = 0.5,       //width  / 2
h2 = 0.5;       //height / 2

GLfloat projection_matrix[16] =
{
n/w2,   0.0,    0.0,             0.0,
0.0,    n/h2,   0.0,             0.0,
0.0,    0.0,    -((f+n)/(f-n)), -1.0,
0.0,    0.0,    -(2*f*n)/(f-n),  0.0
};

I basically copied this from the OpenGL Programming Guide 8, so I dont get why is this not working. Any help would be appreciated.

Here is the shader source BTW:

#version 330 core

layout(location = 0) in vec3 vertex_pos;
layout(location = 1) in vec3 vertex_color;

uniform mat4 V_trans;
uniform mat4 V_rot;
uniform mat4 P;

flat out vec4 color_VOUT;

void main(){

color_VOUT =  vec4( vertex_color, 1);
vec4 pos_H =  vec4( vertex_pos,   1);
gl_Position = P * (V_rot * V_trans) * pos_H;

} 

##### Share on other sites

Normally -z is "forward", into the screen. And you probably calculate gl_Position wrong, usually the model transform is translation * rotation. I think you want this: gl_Position = P * (V_trans * V_rot) * pos_H.

Edited by Sponji

##### Share on other sites

M = T * R is for the Model Matrix. For the view matrix its R * T because those are inverse matrices. I dont use Model Matrix yet.

[quote name='Sponji' timestamp='1380484792' post='5097650'

##### Share on other sites

= T * R is for the Model Matrix. For the view matrix its R * T because those are inverse matrices. I dont use Model Matrix yet.

Normally -z is "forward", into the screen

....what do you mean by that?

##### Share on other sites

= T * R is for the Model Matrix. For the view matrix its R * T because those are inverse matrices. I dont use Model Matrix yet.

Normally -z is "forward", into the screen

....what do you mean by that?

GLfloat projection_matrix[16] =
{
n/w2,   0.0,    0.0,             0.0,
0.0,    n/h2,   0.0,             0.0,
0.0,    0.0,    -((f+n)/(f-n)), -1.0,
0.0,    0.0,    -(2*f*n)/(f-n),  0.0
};

This projection matrix that you have set up has +z comming towards you. The default operation on matrices in GLSL is in column major. Therefore your worldMatrix will be Translation*Rotation instead of the other way around.

##### Share on other sites

Things get confused in this thread. So ...

GLfloat projection_matrix[16] =
{
n/w2,   0.0,    0.0,             0.0,
0.0,    n/h2,   0.0,             0.0,
0.0,    0.0,    -((f+n)/(f-n)), -1.0,
0.0,    0.0,    -(2*f*n)/(f-n),  0.0
};

This piece of code generates the standard OpenGL perspective projection matrix (AFAIS) for a view but symmetric in both horizontal and vertical dimensions, and lays it out in column major order when being meant as column vector matrix, or else lays it out in row major order when meant as row vector matrix.

One has to distinguish the use as row vs. column vector matrices (what calls for attention w.r.t. the order of multiplication, because the matrix product is not commutative), and the row vs. column major layout of the 2D construct (of a matrix) in 1D computer memory. The memory layout has absolutely nothing to do with the mathematical meaning of the order of matrices when being multiplied (but it has w.r.t. the implementation of the matrix product, of course).

Notice that row major layout of row vector matrices as well as column major layout of column vector matrices look the same when written down as done above. Historically OpenGL uses column vector matrices, stored in column major layout (but nowadays it can be used in another way, too).

Edited by haegarr

• 13
• 18
• 29
• 11