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: zooming - with glulookat()?

  1. #1
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    3

    zooming - with glulookat()?

    hi guys,

    i've read several posts and tutorials regarding zooming in opengl now and yet i am still not able to get it to work.
    what i have: left bottom corner is at (-1, -1), right top corner is at (1, 1), and my code draws some simple GL_POINTS (all of them at z = 0); my canvas dimensions are (1024, 768).
    what i want to do: a simple zoom by a defined factor.

    currently i'm trying this approach, i have trouble understand which argument values to use though. if i try it like this, i see only the background color after a repaint.

    my code right now:
    Code :
    	zoom -= 0.1;
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective (zoom, 1024 / 768, 1, 3); 
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	gluLookAt(0, 0, 0, 0, 0, 0, 0, 1, -1); // eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz


    what is the easiest way achieve this?

    thanks :-)
    Last edited by rebal_; 06-20-2013 at 07:20 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    Quote Originally Posted by rebal_ View Post
    Code :
    	zoom -= 0.1;
    What was the value of "zoom" before you subtracted 0.1 from it?

    Quote Originally Posted by rebal_ View Post
    Code :
    	gluLookAt(0, 0, 0, 0, 0, 0, 0, 1, -1); // eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
    This places both the eye and the "look at" point at the origin, resulting in the forward vector being undefined, resulting in the matrix being undefined. These two points need to be different.

    If you're drawing points in the z=0 plane, eyeZ needs to be non-zero and centerZ-eyeZ needs to have the opposite sign to eyeZ so that you're looking toward the z=0 plane.

  3. #3
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    3
    thanks for the reply. the original value of zoom was/is 1.

    anyway, i tried another approach, and heureka - it works, at least kinda. the remaining problem is that the painted objects are only visible at every second zoom, which seems pretty strange to me. the code:

    Code :
            // Set gluPerspective (once)
     	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective (90, w / h, 1, 3); 
    	glOrtho(-1, 1, -1.0, 1, 1, 3); 
     
            // Do this every time the user wants to zoom
            zoom = 1 + event->delta() / MOUSEWHEEL_DELTA_NORMALIZATION_FACTOR;	
    	glOrtho(-zoom, zoom, -zoom, zoom, 1, 3);
    	glMatrixMode( GL_MODELVIEW );

    as i said, the zooming per se works. it's just that the painted content seems to disappear with every second zooming operation (visible at first - without zooming -, invisible after first zooming, visible after second zooming, ...). any ideas there?

    [EDIT] maybe the problem is related to glMatrixPush() / glMatrixPull()? that's just a suspicion though.
    Last edited by rebal_; 06-20-2013 at 05:06 PM.

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    498
    Quote Originally Posted by rebal_ View Post
    anyway, i tried another approach, and heureka - it works, at least kinda. the remaining problem is that the painted objects are only visible at every second zoom, which seems pretty strange to me. the code:

    Code :
     	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective (90, w / h, 1, 3); 
    	glOrtho(-1, 1, -1.0, 1, 1, 3);
    Using both gluPerspective() and glOrtho() doesn't make much sense. Normally you'd use one or the other, but not both.

    The glOrtho() call generates the matrix
    Code :
    1  0  0  0
    0  1  0  0
    0  0 -1  2
    0  0  0  1
    which is equivalent to calling
    Code :
         glTranslatef(0, 0, 2);
         glScalef(1,1,-1);
    Note that this flips the Z axis, which effectively turns the scene inside-out (e.g. front and back faces will be swapped for lighting and culling.

    Quote Originally Posted by rebal_ View Post
    Code :
            // Do this every time the user wants to zoom
            zoom = 1 + event->delta() / MOUSEWHEEL_DELTA_NORMALIZATION_FACTOR;	
    	glOrtho(-zoom, zoom, -zoom, zoom, 1, 3);
    	glMatrixMode( GL_MODELVIEW );
    This flips the Z axis with each call.

    Normalised device coordinates have +Z into the screen and -Z out of the screen. All of the standard projection matrices (glFrustum, glOrtho, gluPerspective, gluOrtho2D) place a negative value at matrix[2][2] to negate the Z coordinate so that the default coordinate system is right-handed (i.e. +Z is out of the screen).

    I suggest reverting to your original example and just setting centerZ correctly.

    Also, you need to decide what you mean by "zoom". The zoom on a camera works by changing the field-of-view angle. In computer graphics, it can be used to mean that, but it can also mean scaling, or it can mean moving toward or away from a target object (with a perspective projection, this will change the size of the object on screen).

    Each of those are different things, and the means to achieve them is different. Changing the field-of-view angle involves changing the first parameter (fovy) in a gluPerspective() call. Scaling is achieved via glScale(). Moving the viewpoint is achieved by changing the first three parameters (eyeX/Y/Z) in a gluLookAt() call or by calling glTranslate() (either appended to the projection matrix or prepended to the model-view matrix).

  5. #5
    Newbie Newbie
    Join Date
    Jun 2013
    Posts
    3
    ah, ok...the flipped Z axis explains that "flicker".

    Also, you need to decide what you mean by "zoom".
    honestly, i didn't put much thought into that. any working method suits me just fine.

    I suggest reverting to your original example and just setting centerZ correctly.
    thanks for your help! centerZ and eyeZ were the problem, it works flawlessly now.

Posting Permissions

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