Results 1 to 6 of 6

Thread: this might be obviouse to you but I don't get it

  1. #1
    Guest

    this might be obviouse to you but I don't get it

    here is the reshape funcion I have, I know it works but I just dont understand how??

    void reshape(int w, int h)
    {
    glPushMatrix();
    glViewport(0, 0, (GLsizei)w, (GLsizei)h) ;
    glMatrixMode(GL_PROJECTION) ;
    glLoadIdentity() ;
    gluPerspective(45, (GLfloat)w/(GLfloat)h, 0.1, 100.0) ;
    glMatrixMode(GL_MODELVIEW) ;
    glLoadIdentity() ;
    glPopMatrix();
    }

    in the entire program, this is the only place where gluPerspective appears and sets the matrix in perspective mode, but below that line there is glLoadIdentity, which resets the matrix, then theres glPopMatrix, which pops the matrix stored at the beginning of reshape function, so howcome when I enter the display function and draw something:

    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();

    glRotatef(g_lookupdown,1.0f,0,0); //look up and down
    glRotatef(sceneroty,0,1.0f,0); //look side to side
    glTranslatef(xtrans, ytrans, ztrans); //moving all directions

    glTranslatef(0,0,-5.0) ;
    glColor4f(1.0, 1.0, 0.0, 1.0);
    glutWireCube(1);
    ........
    .......
    glutSwapBuffers();

    they are in perspective mode and not reset??

    I also need better explaination of
    glMatrixMode(GL_PROJECTION) ;
    and
    glMatrixMode(GL_MODELVIEW) ;
    those tutorials and documents do not explain enough and I think understanding these will explain my question above. I know these store the matrix configuation made by the lines of code above it, but when and how are they "popped" back into effect?

    How does GL_PROJECTION and GL_MODELVIEW differ and can I just use one in the reshape function and not the other? how does that change the way my objects look?

    Any help would be greatly appreciated.

    Newbie

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    763

    Re: this might be obviouse to you but I don't get it

    Hello,

    the reason why the code works is because you are pushin/popping and applying gluPerspective to _different_ matricies.

    Assuming your matrix mode is GL_MODELVIEW when reshape() is called, then;

    glPushMatrix();

    pushes the modelview matrix

    glMatrixMode(GL_PROJECTION) ;

    changes to projection matrix

    glLoadIdentity() ;

    sets the projection matrix to I

    gluPerspective(45, (GLfloat)w/(GLfloat)h, 0.1, 100.0) ;

    sets the projection matrix to I * perspective

    glMatrixMode(GL_MODELVIEW) ;

    chnages BACK to modelview

    glLoadIdentity() ;

    sets modelview matrix to idenity

    glPopMatrix();

    and pops the modelview matrix.

    The only thing wrong with that code is you're pushing an identity onto the stack, and then popping it off again, so it all has no effect. (You probably want to axe the push/pop commands in the reshape function)

    cheers,
    John

  3. #3
    Guest

    Re: this might be obviouse to you but I don't get it

    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h) ;
    glMatrixMode(GL_PROJECTION) ;
    glLoadIdentity() ;
    gluPerspective(45, (GLfloat)w/(GLfloat)h, 0.1, 100.0) ;
    glMatrixMode(GL_MODELVIEW) ;
    glLoadIdentity() ;
    }
    Hi
    You were right, without the pushMatrix and popMatrix it works just the same,so then without the push and pop, when I am drawing my cubes, what mode am I in?, GL_PROJECTION? or GL_MODELVIEW?
    since the perspective is applied to GL_PROJECTION(stated in above post), I must be in GL_PROJECTION since my cubes are in perspective, correct? Then does that make the call to glMatrixMode(GL_MODELVIEW) useless? Also, what exactly does glLoadIdentity do? I thought it just resets everything to default, but my cubes are in perspective. Did I somehow set the default to perspective in the lines above? because the last line of the reshape function is glLoadIdentity().

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,574

    Re: this might be obviouse to you but I don't get it

    You don't seem to understand the OpenGL matrix functionality.

    When you call a matrix generation function (gluLookAt, gluPerspective, glRotate, glPushMatrix), it operates on the current matrix stack. You have 3 matrix stacks: GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE.

    When your geometry is transformed and rendered, the first thing that happens is that the GL_MODELVIEW matrix is applied to it. Then, the GL_PROJECTION matrix is applied to the results. It doesn't matter what glMatrixMode is; this is how geometry is transformed.

    The current matrix mode after leaving this function is GL_MODELVIEW. Since matrix mode is global, I would suggest making sure that all functions, upon exiting, make sure that the matrix mode is GL_MODELVIEW. Basically, any function (like this one) where the matrix mode is changed should restore the matrix mode to GL_MODELVIEW.

  5. #5
    Guest

    Re: this might be obviouse to you but I don't get it

    thank you, that cleared things up. So I just called up the projection matrix just to alter it, same goes to the modelview matrix, but they are always affecting the way my objects are projected onto the screen. Now that makes much more sense.

    glMatrixMode(GL_PROJECTION) ;

    //changes to projection matrix>>request to modify projection matrix

    glLoadIdentity() ;

    //sets the projection matrix to I>> resets projection matrix to default projection matrix

    gluPerspective(45, (GLfloat)w/(GLfloat)h, 0.1, 100.0) ;

    //sets the projection matrix to I * perspective>>adds perspective to projection matrix

    glMatrixMode(GL_MODELVIEW) ;

    //chnages BACK to modelview>>request to modify modelview matrix

    glLoadIdentity() ;

    //sets modelview matrix to idenity>>resets the modelview matrix to default modelview matrix


    so now any rotation or translation commands are altering the modelview matrix, and that is why my cubes are in perspective!!!

    Why George, I think he's got it!!!

    Newbie

  6. #6
    Guest

    Re: this might be obviouse to you but I don't get it

    This has been most EDUCATIONAL!!!
    thanks everyone for your help


    Newbie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean