Results 1 to 4 of 4

Thread: Help with zoom camera

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    2

    Help with zoom camera

    Hi!! Im trying to implement a zoom camera using the mouse, and glulookat. Here is my code:
    Code :
    float eyesx = 0.0, eyesy = 0.0, eyesz = 20.0, zoom = 0.01, lasty;
    void mouse(int button, int state, int x, int y)
    {
    	float direccion = 1;
    	float ydelta = lasty - y;
    	if(ydelta>0){
    		direccion*=-1;
    	}else{
    		direccion*=1;
    	}
        if (button == GLUT_LEFT_BUTTON) {
            if (state == GLUT_DOWN) {
    			lasty = y;
    			if(direccion>0){
    				//eyesx+=eyesx*zoom;
    				//eyesy+=eyesy*zoom;
    				eyesz+=eyesz*zoom;
    			}else{
    				//eyesx-=eyesx*zoom;
    				//eyesy-=eyesy*zoom;
    				eyesz-=eyesz*zoom;
    			}
            } else {
    			lasty = y;
            }
        }
    	glutPostRedisplay();
    }
     
    void display(void)
    {
    	gluLookAt (eyesx, eyesy, eyesz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	printf("eyesx, eyesy, eyesz: %f %f %f \n", eyesx, eyesy, eyesz);
    	drawCube();
    	glutSwapBuffers();
    }
    When I run it, no mather what I do, even tough eyesz increases or diminishes; the scene still zooms out.
    Some help is welcomed!!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: Help with zoom camera

    As it stands, the code appears to be repeatedly multiplying gluLookAt matrices. Whenever you call gluLookAt, the previous value of MODELVIEW is replaced with MODELVIEW*M_gluLookAt. You usually set the MODELVIEW matrix to identity then call gluLookAt, only then do you begin drawing your scene... try adding the two lines before gluLookAt:
    Code :
    void display(void)
    {
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
    	gluLookAt (eyesx, eyesy, eyesz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
     
    	printf("eyesx, eyesy, eyesz: %f %f %f \n", eyesx, eyesy, eyesz);
     
            glPushMatrix();
            //glTranslate/glScale/glRotate etc to locate the cube
    	drawCube();
            glPopMatrix();
    	glutSwapBuffers();
    }
    And to draw a set of object you usually put each object in a push/pop pair.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    2

    Re: Help with zoom camera

    Thanks!! It work ok!
    But now my pan functions that previously worked, are not working anymore. I hope you can help me. Here is my code where I integrate pan and zoom:
    Code :
    void display(void)
    {
    	glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    	gluLookAt (eyesx, eyesy, eyesz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	printf("eyesx, eyesy, eyesz: %f %f %f \n", eyesx, eyesy, eyesz);
    	glPushMatrix();
    		dibujarSuperficie();
    	glPopMatrix();
    	glutSwapBuffers();
    }
     
    void mouse(int button, int state, int x, int y)
    {
    	float direccion1 = 1;
    	float ydelta = lasty - y;
    	if(ydelta>0){
    		direccion1*=-1;
    	}else{
    		direccion1*=1;
    	}
        if (button == GLUT_LEFT_BUTTON) {
            if (state == GLUT_DOWN) {
    			lasty = y;
    			if(direccion1>0){
    				eyesx+=eyesx*zoom;
    				eyesy+=eyesy*zoom;
    				eyesz+=eyesz*zoom;
    			}else{
    				eyesx-=eyesx*zoom;
    				eyesy-=eyesy*zoom;
    				eyesz-=eyesz*zoom;
    			}
            } else {
    			lasty = y;
    			lastx = x;
            }
        }
    	else if (button == GLUT_RIGHT_BUTTON) {
            if (state == GLUT_DOWN) {
    			lasty = y;
    			glRotatef(lastx - x, 0, 0, 1);
    			lastx = x;
    			glRotatef(lasty - y, 0, 1, 0);
    			lasty = y;
            } else {
    			lasty = y;
    			lastx = x;
            }
        }
    	glutPostRedisplay();
    }

    This is my original code, where the pan worked:
    Code :
    void display(void)
    {
    	glPushMatrix();
    		gluLookAt (0, 0, 20, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	glPopMatrix();
    	//printf("eyesx, eyesy, eyesz: %f %f %f \n", eyesx, eyesy, eyesz);
    	glPushMatrix();
    		dibujarSuperficie();
    	glPopMatrix();
    	glutSwapBuffers();
    }
     
    void motion(int x, int y)
    {
    	float xrotrad, yrotrad;
        if (downL) {
    		glRotatef(lastx - x, 0, 0, 1);
    		lastx = x;
    		glRotatef(lasty - y, 0, 1, 0);
    		lasty = y;
            glutPostRedisplay();
        }
    }
     
    /* ARGSUSED3 */
    void mouse(int button, int state, int x, int y)
    {
        if (button == GLUT_LEFT_BUTTON) {
            if (state == GLUT_DOWN) {
                lastx = x;
    		    lasty = y;
                downL = 1;
            } else {
                downL = 0;
            }
        }
    }
    What am I doing wrong here?

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: Help with zoom camera

    You may get some benefit looking at GltZPR. The source is a single C-file that you could look at to get some ideas or just use.

Similar Threads

  1. Replies: 0
    Last Post: 01-06-2015, 09:42 AM
  2. Zoom In, Zoom Out, Pan using Mouse
    By VishwaSwamiG in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-16-2014, 11:09 PM
  3. Change zoom from dolly to "real" zoom
    By Rainer Wahnsinn in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-01-2013, 05:48 PM
  4. zoom in and zoom out with gl4java and Java
    By vamsee kanth in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-03-2004, 02:32 AM
  5. Replies: 0
    Last Post: 03-15-2001, 02:41 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