Jump to content
  • Advertisement
Sign in to follow this  
Aliii

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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?smile.png

Share this post


Link to post
Share on other sites

Your projection matrix

 

= 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?smile.png

    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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!