PDA

View Full Version : Help with zoom camera



linker85
10-30-2009, 06:42 PM
Hi!! Im trying to implement a zoom camera using the mouse, and glulookat. Here is my 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!!

marshats
10-30-2009, 10:07 PM
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:


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.

linker85
10-31-2009, 07:27 PM
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:


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:


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?

marshats
10-31-2009, 11:39 PM
You may get some benefit looking at GltZPR (http://www.nigels.com/glt/gltzpr/). The source is a single C-file that you could look at to get some ideas or just use.