PDA

View Full Version : gluLookAt doesn't work



Sarah22
04-20-2009, 02:54 AM
Can you please check my codes? Why it doesn't work?

Init()

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(54.0f,(GLfloat)800/(GLfloat)600,1.0f,500.0);
glMatrixMode(GL_MODELVIEW);


Draw()

gluLookAt (locx, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
videodriver->draw3DTriangle(vector3df(0,0,-5),vector3df(1,1,-4),vector3df(2,0,-5));
locx++;

BionicBytes
04-20-2009, 05:46 AM
Not quite sure what you are trying to achieve and what exactly does not work!
Have you set gldisable (GL_CULL_FACE) so that both front <u>and</u> back polys are visible?

Also, you seem to be drawing a triangle at the origin, but the Z value is behind where the camera is looking & positioned. Try changing the triangle to use +ve Z instead and set the camera's eyeposition further away,

glulookat (x,0,-10 , 0,0,0, 0,1,0)

Now you should see your trangle infront of the camera, athough you have not specified any colour at each vertex or texture coords?

This is a bit of an odd thing to do in a draw loop, because you are moving the camera position (possibly 1000+ times a second) away from your object and it may be better to just fix the eyeposition for now.

glulookat (0,0,-10 , 0,0,0, 0,1,0)

Sarah22
04-20-2009, 10:21 AM
I found the problem. The glLoadIdentity(); on draw function is the problem. I changed it to glPushMatrix()/glPopMatrix() so it works correctly now.


This is a bit of an odd thing to do in a draw loop, because you are moving the camera position (possibly 1000+ times a second) away from your object and it may be better to just fix the eyeposition for now.

What do you it's a bit of an odd thing to do it in a draw loop? Is it bad to do gluLookAt(); in draw function? What if I want to use it as a player's camera. So I have a variable for each. like the code below I found on a book. It is called in the draw function.


gluLookAt(m_whiteViewPos.x, m_whiteViewPos.y, m_whiteViewPos.z, 4.0, 0.0, 4.0, 0.0, 1.0, 0.0);

dletozeun
04-20-2009, 02:12 PM
It is up to you, gluLookAt just affects the current active matrix stack. You are not forced to call this function each frame as long as the modelview matrix is not set to identity. You can save camera matrix state with glPush/glPop calls as you seem to do.