PDA

View Full Version : whats wrong with gluLookAt?



ks554
01-26-2009, 03:13 PM
following gluLookat does not work....can any1 see the problem?



#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

#define ESCAPE 27 //use escape key to exit program

GLint Xsize = 400;
GLint Ysize = 400;
GLint window_name; //name of our window


GLvoid InitWindow(GLfloat Width, GLfloat Height)
{

glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,Width/Height,0.0, 200.0);
glMatrixMode(GL_MODELVIEW);

}


GLvoid ResizeScene(GLint Width, GLint Height)
{
if (Height == 0) Height = 1; //prevent division by zero
if (Width == 0) Width = 1;
InitWindow(Width, Height); //reset perspective projection
}



GLvoid DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -100.0f);
glColor3ub(255, 0, 0);
glutSolidSphere(3.0f, 15, 15);
glutSwapBuffers();
}



void NormalKey(GLubyte key, GLint x, GLint y)
{
switch( key)
{
case ESCAPE:
printf("Escape pressed, exiting..\n");
glutDestroyWindow(window_name);
exit(0);
break;

case 'd':
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10.0f, 0.0f, -100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f, 1.0f);
break;

default:
break;
}
}


int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(Xsize, Ysize);
glutInitWindowPosition(0,0);
window_name = glutCreateWindow("Title");

//Initialise window
InitWindow(Xsize, Ysize);

//Callback functions (Resize & Draw screen)
glutReshapeFunc(ResizeScene);
glutDisplayFunc(DrawScene);
//glutIdleFunc(DrawScene);
//Callback function when a key is pressed
glutKeyboardFunc(NormalKey);

glutMainLoop();
return 1;

}

satan
01-26-2009, 04:11 PM
AFAICS you are resetting your modelview matrix in your draw function, overwriting gluLookat's changes to it.

ks554
01-27-2009, 03:22 AM
OK, I understand, but the thing is I have now removed the glutIdleFunc, and the problem is that if I remove glLoadIdentity() from the drawfunction it works fine, but if I include it, then it does not work....any ideas why this happens?

_NK47
01-27-2009, 03:51 AM
"then it does not work"
and what happens? consecutive gluLookAt calls will keep multiplying new matrix with old one. glLoadIdentity prevents this. if it doesnt work that your camera isnt moving its because maybe your values are static for gluLookAt and in the other case they get concatenated together. something like that.

ks554
01-27-2009, 05:34 AM
It seems to me that your confused with what the problem actually is...

my gluLookAt works...

the problem is that it only works if I don't use glLoadIdentity() function in my draw function.

As far as I know, glLoadIdentity() simply resets the matrix, so why does my program not work when I use glLoadIdentity()?

datsua
01-27-2009, 05:59 AM
One way of "fixing" this is to preserve the matrix set up by gluLookAt by using the matrix stack.



GLvoid DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -100.0f);
glColor3ub(255, 0, 0);
glutSolidSphere(3.0f, 15, 15);
glPopMatrix();
glutSwapBuffers();
}

ks554
01-27-2009, 06:28 AM
yea, thanks...i solved it by using push and pop matrix

but one thing I don't understand is what's the difference between glLoadIdentity() and pushing and popping???....I thought they were the same thing?

_NK47
01-27-2009, 06:33 AM
i think you didnt understood my post. from the start.

"so why does my program not work when I use glLoadIdentity()?"
because your params to gluLookAt are static! if glLoadIdentity resets the matrix and you give again those values your camera will be at same position as it was.

"As far as I know, glLoadIdentity() simply resets the matrix"
correct. more correct, it sets the matrix to "identity".

"my gluLookAt works..."
if it works without glLoadIdentity then because you keep "multiplying" the previous lookat matrix with the new one. thus old values are "concatenated" with the old ones and you get the camera movement.

to fix this either do it without glLoadIdentity or add/substract from gluLookAt values on key hit.

gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, oldValue += 1.0f, 0.0f, 1.0f, 0.0f);

"glLoadIdentity() and pushing and popping???"
saves the current matrix with push and restores it later with pop. glLoadIdentity sets it to identity matrix so no transformation takes place.