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 5 of 5

Thread: noob question - modelview and projection matrices

  1. #1
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    77

    noob question - modelview and projection matrices

    Sorry, but I must know the answer to this and tried to google alot, without finding the exact answer...

    Q: Why does this work?
    Code :
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = float(width)/float(height)
        gluPerspective(40., aspect, 0.1, 40.)
     
        glMatrixMode(GL_MODELVIEW)
        gluLookAt(0,0,10,  0,0,0,  0,1,0)
    When this doesn't work?
    Code :
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = float(width)/float(height)
        gluPerspective(40., aspect, 0.1, 40.)
     
        glMatrixMode(GL_MODELVIEW)
        gluLookAt(0,0,10,  0,0,0,  0,1,0)
    I thought the projection and the modelview matrices have absolutely nothing to do with each other, so why does the order of the operations/matrix stuff matter ???

    Why doesn't it give the same result ???

    Noob question, I know... But I don't understand this...

  2. #2
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    352

    Re: noob question - modelview and projection matrices

    they look like pretty the same to me.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Member Regular Contributor
    Join Date
    Apr 2010
    Posts
    493

    Re: noob question - modelview and projection matrices

    Huh? I must be blind, but the two look like literally the same code, where is the difference hiding?
    BTW, what does "it doesn't work" mean here? That is usually not a very useful problem description (i.e. one that makes a good answer possible), take a look at Forum Posting Guidelines for suggestions how to improve it.

    Assuming the second code block was meant to look like
    Code :
    glMatrixMode(GL_MODELVIEW)
    gluLookAt(0,0,10,  0,0,0,  0,1,0)
     
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    aspect = float(width)/float(height)
    gluPerspective(40., aspect, 0.1, 40.)

    I'd then guess the problem is that you leave the projection matrix active, so any calls to glTranslate, glRotate, etc. affect your projection matrix, which is usually not what you want.

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    381

    Re: noob question - modelview and projection matrices

    Also, you most likely want to reset the model-view matrix to identity before calling gluLookAt:
    Code :
    glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = float(width)/float(height)
        gluPerspective(40., aspect, 0.1, 40.)
     
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity(); // <===== add this line
        gluLookAt(0,0,10,  0,0,0,  0,1,0)
    Since gluLookAt simply multiplies the current matrix by the "look at" matrix, and if you don't reset it, the transformation will stack each time you render.

  5. #5
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    77

    Re: noob question - modelview and projection matrices

    ARRGH!

    Thanks a lot, both of you! You're both right... Ofcourse it was a really stupid copy/paste error I made, I had to remove a lot of comments so my mind was focused on removing comments instead of showing what I meant to show... And what I meant to show was:
    Code :
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = float(width)/float(height)
        gluPerspective(40., aspect, 0.1, 40.)
     
        glMatrixMode(GL_MODELVIEW)
        gluLookAt(0,0,10,  0,0,0,  0,1,0)
    When this doesn't work?
    Code :
        glMatrixMode(GL_MODELVIEW)
        gluLookAt(0,0,10,  0,0,0,  0,1,0)
     
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        aspect = float(width)/float(height)
        gluPerspective(40., aspect, 0.1, 40.)

    You're both absolutely right:

    1) Ofcourse it was stupid of me, not to end with an extra switch to the gl_modelview-matrix. This actually solved my main problem and confirms that I think I understand the difference between the two matrices, thanks!

    2) Also thanks a lot for the glLoadIdentity-comment - I've now added that. Makes good sense...

    Stupid of me - I actually knew the importance of ending with the modelview matrix, but somehow I've forgot...

    Thanks all (especially for quick answers)!

Posting Permissions

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