Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: OpenGL 3.x: perspective projection problem

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    6

    OpenGL 3.x: perspective projection problem

    Lately, I've been writing a matrix implementation of my own to replace the built-in matrix stack that is deprecated as of OpenGL 3.0 and finally removed in core 3.3 / 4.0. The exact profile I'm developing for is core OpenGL 3.2 / GLSL 150.

    Referring to the detailed instructions provided here, I've set up my perspective projection matrix as follows (symmetric viewing frustum):



    I figured the only way to clearly display matrices on the forums would be to typeset them with LaTeX . The values -2.22 and -1.22 (obviously) suffer from truncation.

    Anyway, however, the rendering result doesn't quite match the one I'm expecting to see (the primitive is deformed beyond recognition; vertices at the boundaries of the viewing volume seem to be approaching infinity). More often than not I would initially see an completely black scene, and with a simple keyboard control mechanism was, on occasion, able to get at least something to show up.


    Now, please take a moment to look at the actual code I'm working with. Here are the relevant bits (btw, I'm using the Eigen library, which by default uses column-major storage order):

    Code :
    uni_modelview_loc = glGetUniformLocation(programHandle, "ModelView");
    uni_projection_loc = glGetUniformLocation(programHandle, "Projection");
     
    ...
     
    //The matrices projection, model and view have been initialized as shown above
     
    //premultiply these two
    Eigen::Matrix4f modelview = model * view;
     
    glUniformMatrix4fv(uni_modelview_loc, 1, GL_FALSE, modelview.data());
    glUniformMatrix4fv(uni_projection_loc, 1, GL_FALSE, projection.data());
     
    /* set up attribpointers and draw scene */
    Code :
    // VERTEX SHADER
     
    #version 150
     
    in vec3 in_position;
    in vec3 in_normal;
     
    uniform mat4 ModelView;
    uniform mat4 Projection;
     
    out vec3 vvertex;
    out vec4 vnormal;
     
    void main(void)
    {
    	gl_Position = Projection * ModelView * vec4(in_position, 1.0); 
    	vnormal = vec4(in_normal, 1.0);
    }
    Without providing any matrix uniforms to the shader, the primitive is rendered correctly. Weirdly enough, reversing the order in which the matrices ModelView and Projection are multiplied (again, in the shader) sometimes yields a result one could consider a perspective projection of sorts, but still not the one I would have expected (i.e. one similar to the default projection).

    Please point out the error(s) I may be failing to see here. If required, images demonstrating this issue can also be arranged.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,725

    Re: OpenGL 3.x: perspective projection problem

    Here are the relevant bits
    So where are the other relevant bits? Like how you create your matrices?

    Code :
    Eigen::Matrix4f modelview = model * view;

    That's the wrong order. The world-to-camera comes after the model-to-world matrix, so it goes on the left.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    6

    Re: OpenGL 3.x: perspective projection problem

    I wanted to keep the original post as math library-independent as I possibly could, so I chose not to give too much specific info on that. Also, I was very much inclined to think the problem lay not in my actual code, but rather in my algebra. However, if there are - other than the model * view order, thanks! - no obvious errors to be found, I will gladly go ahead and elaborate.

    All matrices used in this program are globally declared, and in a typical OpenGL init function, the following is done:
    Code :
    view = Eigen::Matrix4f::Identity();
     
    model = Eigen::Matrix4f::Identity();
    model(3,2) = 4.0; 
     
    projection << r/n, 0, 0, 0,
                  0, r/t, 0, 0,
                  0, 0, -(n+f)/(f-n), -(2*f*n)/(f-n),
    	      0, 0, -1.0, 0.0;
    When the contents of e.g. projection.data() are printed in a loop, the following sequence is given:
    Code :
    for (int i = 0; i < 16; i++)
    	printf("%f ", projection.data()[i]);
     
    output: 
    1.6 0.0 0.0 0.0 0.0 1.6 0.0 0.0 0.0 0.0 -1.22 -1.0 0.0 0.0 -2.2 0.0
    which appears to be both column-major and correct (figures truncated for clarity).

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    6

    Re: OpenGL 3.x: perspective projection problem

    After extensive testing, I have come to the (fortunate) conclusion that I had, in fact, had a correct perspective projection all along; only the znear/zfar planes were too close to each other for the program to render anything other than merely a slice or a cross-section of the primitive (and for me to recognize the shape). Scaling down the mesh fixed this issue.

    Cheers.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    2
    check this website to understand the concept of prepective projection
    http://www.mathdisk.com/pub/safi/wor...ive_Projection

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    2
    check this website to understand the prespective projection
    http://www.mathdisk.com/pub/safi/wor...ive_Projection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •