Jump to content
  • Advertisement
Sign in to follow this  
k_ounu_eddy

Problems with gluLookAt()

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody!I have problems with understanding what gluLookAt() really does. For help,I 'build' a 3d scene with some boxes(cubes),with diferent colours.When you use a,s,d,w, you can move.Now comes the problem.I wrote code for the eveniment coresponding pressing key g,and when this happens I call the function gluLookAt().Here's the code: #include <windows.h> #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> #include <gl\glut.h> #include<math.h> void init(void); void display(void); void keyboard(unsigned char, int, int); void resize(int, int); void drawcube(int, int, int); int is_depth; int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(600, 600); glutInitWindowPosition(40, 40); glutCreateWindow("The Cube World"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutReshapeFunc(resize); glutMainLoop(); return 0; } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); is_depth = 1; glMatrixMode(GL_MODELVIEW); } void display(void) { if (is_depth) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); else glClear(GL_COLOR_BUFFER_BIT); /* draw the floor */ glBegin(GL_QUADS); glColor3f(0.2f, 0.2f, 0.2f); glVertex3f(-100.0, 0.0, -100.0); glColor3f(0.4f, 0.4f, 0.4f); glVertex3f(-100.0, 0.0, 100.0); glColor3f(0.6f, 0.6f, 0.6f); glVertex3f(100.0, 0.0, 100.0); glColor3f(0.8f, 0.8f, 0.8f); glVertex3f(100.0, 0.0, -100.0); glEnd(); /* draw 12 cubes with different colors */ drawcube(75, 57, 2); drawcube(-65, -12, 3); drawcube(50, -50, 1); drawcube(-56, 17, 2); drawcube(67, 12, 3); drawcube(-87, 32, 1); drawcube(-26, 75, 2); drawcube(57, 82, 3); drawcube(-3, 12, 1); drawcube(46, 35, 2); drawcube(37, -2, 3); glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'a': case 'A': glTranslatef(5.0, 0.0, 0.0); break; case 'd': case 'D': glTranslatef(-5.0, 0.0, 0.0); break; case 'w': case 'W': glTranslatef(0.0, 0.0, 5.0); break; case 's': case 'S': glTranslatef(0.0, 0.0, -5.0); break; case 't': case 'T': if (is_depth) { is_depth = 0; glDisable(GL_DEPTH_TEST); } else { is_depth = 1; glEnable(GL_DEPTH_TEST); } case 'g': case 'G': gluLookAt(0.1,0,0 ,0.2,0,0, 0,1,0); } display(); } void resize(int width, int height) { if (height == 0) height = 1; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, width / height, 1.0, 400.0); glTranslatef(0.0, -5.0, -150.0); glMatrixMode(GL_MODELVIEW); } void drawcube(int x_offset, int z_offset, int color) { float x_big = (float)x_offset + 5; float z_big = (float)z_offset + 5; float x_small = (float)x_offset - 5; float z_small = (float)z_offset - 5; switch(color) { case 1: glColor3f(1.0,0.0,0.0); break; case 2: glColor3f(0.0,1.0,0.0); break; case 3: glColor3f(0.0,0.0,1.0); break; } glBegin(GL_QUADS); glVertex3f(x_small,10.0,z_big); /* front */ glVertex3f(x_small,0.0,z_big); glVertex3f(x_big,0.0,z_big); glVertex3f(x_big,10.0,z_big); glVertex3f(x_big,10.0,z_small); /* back */ glVertex3f(x_big,0.0,z_small); glVertex3f(x_small,0.0,z_small); glVertex3f(x_small,10.0,z_small); glVertex3f(x_big,10.0,z_big); /* right */ glVertex3f(x_big,0.0,z_big); glVertex3f(x_big,0.0,z_small); glVertex3f(x_big,10.0,z_small); glVertex3f(x_small,10.0,z_small); /* left */ glVertex3f(x_small,0.0,z_small); glVertex3f(x_small,0.0,z_big); glVertex3f(x_small,10.0,z_big); glVertex3f(x_small,10.0,z_big); /* top */ glVertex3f(x_big,10.0,z_big); glVertex3f(x_big,10.0,z_small); glVertex3f(x_small,10.0,z_small); glVertex3f(x_small,0.0,z_small); /* bottom */ glVertex3f(x_big,0.0,z_small); glVertex3f(x_big,0.0,z_big); glVertex3f(x_small,0.0,z_big); glEnd(); } When I press g(if I'm correct) all the scene is rotate at an angle of 90 degrees.Why this?The results that I'm expecting ,are like I'm moving 0.1 at right,but isn't that.Can you explain me?Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I dont see you setting up the projection matrix in your render so the camera is looking the default axis (IIRC -Z Axis).

You dont actually move until you hit the G key

Edit - Ah I see on the resize you are

Share this post


Link to post
Share on other sites
I know that until I don't press the g button I don't move.But when I press g the whole scene is rotating at 90 degrees ,not at as I expect to move with 0.1 to the right.

Share this post


Link to post
Share on other sites
I'm not sure why you're expecting the results that you're expecting, but you're wrong to.

Don't use OpenGL to keep track of where your virtual camera is. Keep track of that in your own code, and then simply use glLoadIdentity to clear out the previous transformation, followed by glTranslate and glRotate to put the camera there. If you're doing an FPS-style camera, you shouldn't need to use gluLookAt at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!