• Create Account

# Projection Matrix problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Aliii  Members   -  Reputation: 1455

Like
0Likes
Like

Posted 29 September 2013 - 01:40 PM

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;

} 

### #2Sponji  Members   -  Reputation: 2058

Like
0Likes
Like

Posted 29 September 2013 - 01:59 PM

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, 29 September 2013 - 02:00 PM.

Derp

### #3Aliii  Members   -  Reputation: 1455

Like
0Likes
Like

Posted 30 September 2013 - 04:36 AM

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'

### #4Aliii  Members   -  Reputation: 1455

Like
0Likes
Like

Posted 30 September 2013 - 04:38 AM

= 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?

### #5BornToCode  Members   -  Reputation: 1165

Like
0Likes
Like

Posted 30 September 2013 - 11:56 PM

= 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.

### #6haegarr  Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 01 October 2013 - 05:11 AM

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, 01 October 2013 - 05:13 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS