Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Correct Reshape Functinon

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2002
    Location
    Burlington, VT
    Posts
    13

    Correct Reshape Functinon

    Hi, I have a reshape() function that sets up
    a screen via glFrustum(), then after I do
    that I setup my camera via gluLookAt() function. Is it a correct way of doing it?
    Should I do my camera setup in reshape()?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2001
    Posts
    184

    Re: Correct Reshape Functinon

    you need to make sure your gluLookAt() call is when you are in MODELVIEW mode. if you are changing the MODELVIEW matrix each frame, then you need to call gluLookAt() before you draw your geometry (i think)

    b

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Correct Reshape Functinon

    If the values I give gluLookAt could change, I'll use it ever frame. (Simulating a moving camera for instance.) If I would just be using the same values for gluLookAt ever frame, I put it in my reshape, and then in my display code use a glPushMatrix/glPopMatrix around all the other transformations.

    Also, as coredump said, make sure to use it on the MODELVIEW matrix, not the projection matrix. If I'm using gluLookAt in the reshape function it generally looks somehwhat like so...

    Code :
    void reshape(int x, int y)
    {
       glViewport(0, 0, x, y);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective(60.0, (float)x/(float)y, 0.1, 100.0);
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
       gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    }
    Note the use of the glLoadIdentity calls above. Those are important so that you don't continue to multiply to the current projection and modelview matrices.
    Deiussum
    Software Engineer and OpenGL enthusiast

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2002
    Location
    Burlington, VT
    Posts
    13

    Re: Correct Reshape Functinon

    Originally posted by Deiussum:
    If the values I give gluLookAt could change, I'll use it ever frame. (Simulating a moving camera for instance.) If I would just be using the same values for gluLookAt ever frame, I put it in my reshape, and then in my display code use a glPushMatrix/glPopMatrix around all the other transformations.

    Also, as coredump said, make sure to use it on the MODELVIEW matrix, not the projection matrix. If I'm using gluLookAt in the reshape function it generally looks somehwhat like so...

    Code :
    void reshape(int x, int y)
    {
       glViewport(0, 0, x, y);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective(60.0, (float)x/(float)y, 0.1, 100.0);
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
       gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    }
    Note the use of the glLoadIdentity calls above. Those are important so that you don't continue to multiply to the current projection and modelview matrices.

    Why do you use gluPerspective here?

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Correct Reshape Functinon

    I find it to be a bit more intuitive to use. It basically does the same thing as glFrustum, but lets you specify it in a bit different way.
    Deiussum
    Software Engineer and OpenGL enthusiast

  6. #6
    Senior Member Regular Contributor
    Join Date
    Dec 2001
    Location
    Belmont, CA, USA
    Posts
    216

    Re: Correct Reshape Functinon

    The reshape function should not include a call to gluLookAt since it doesn't change the position or orientation of the camera.

    Generally, glLoadIdentity and gluLookAt are the first calls in the display function.

  7. #7
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: Correct Reshape Functinon

    One additional thing, beware of a divide by zero when your window is minimised :

    Code :
    void reshape(int x, int y)
    {
       glViewport(0, 0, x, y);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective(60.0, (float)x/(float)((y==0)?1.0f:y), 0.1, 100.0);
       glMatrixMode(GL_MODELVIEW);
       glLoadIdentity();
       gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    }

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Correct Reshape Functinon

    Originally posted by Jambolo:
    The reshape function should not include a call to gluLookAt since it doesn't change the position or orientation of the camera.

    Generally, glLoadIdentity and gluLookAt are the first calls in the display function.
    That's your opinion. If I'm writing something with a "fixed camera" I really don't need to use gluLookAt every frame. The reshape or init functions are as good as anyplace else to put gluLookAt in those cases.
    Deiussum
    Software Engineer and OpenGL enthusiast

  9. #9
    Senior Member Regular Contributor
    Join Date
    Mar 2001
    Posts
    184

    Re: Correct Reshape Functinon

    Why do you use gluPerspective here?
    gluPerspective sets up the PROJECTION matrix. gluLookAt sets up the MODELVIEW matrix. the simple case, you have a sphere at 0,100,0. without gluLookAt, you wouldn't be able to see the sphere. adding gluLookAt(0,0,0,0,1,0,0,0,1) actually sets the MODELVIEW matrix so all the geometry in your scene gets transformed, and you see the sphere.

    b

  10. #10
    Senior Member Regular Contributor
    Join Date
    Dec 2001
    Location
    Belmont, CA, USA
    Posts
    216

    Re: Correct Reshape Functinon

    Originally posted by Deiussum:
    If I'm writing something with a "fixed camera" I really don't need to use gluLookAt every frame. The reshape or init functions are as good as anyplace else to put gluLookAt in those cases.

    It shouldn't go in the reshape function because it doesn't have anything to do with the viewport. Putting it in the display function might slow the frame update by a couple microseconds but that's a better place to put it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 01-10-2017, 07:37 AM
  2. reshape function for correct proportion between window and viewport
    By Lee_Jennifer_82 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 10-21-2015, 12:11 PM
  3. Can't reshape subwindow
    By wmelgaard in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-30-2015, 11:21 AM
  4. reshape
    By chign in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-12-2010, 02:32 PM
  5. Reshape window...
    By Boubou in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 12-04-2000, 06:33 AM

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