PDA

View Full Version : Moving around my 3d environment



02-14-2002, 03:28 PM
Hi guys,

I'm new to OpenGL and am have just completed implementing my first 3d environment. However I'm not too sure on how to implement the funtions to allow the user to move around the world using the cursor keys. I've been going through the tutorials on nehe and used the system the desribed where you would have a key listener in winmain which would change a variable in the draw() function thus transforming the the entire world. But as I need to implement glLoadIdentity() before drawing each object the viewpoint essentially gets stuck as the transformation is cancelled out e.g.

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -0.25f;
GLfloat sceneroty = 360.0f - yrot;

glRotatef(lookupdown,1.0f,0,0); //Transform everything to display movement
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);

glLoadIdentity();
drawCube(0.0f, 0.0f, 0.0f);
glLoadIdentity();
drawCube(1.0f, 0.0f, 0.0f);
glLoadIdentity();
drawCube(3.0f, 0.0f, 0.0f);

return TRUE;
}

If anyone could give me any tips on how to resolve this I'd be very grateful

Cheers

Kam

chowe6685
02-14-2002, 04:27 PM
Why are you calling LoadIdentity before drawing, you say that you need to, but why?

Elixer
02-14-2002, 05:09 PM
Yeah, either reset the matrix in the begining, then do your rotations/transforms, THEN draw, or do your transforms then rotations then draw. (Depends on what your doing.)

Or, use glpush & pop to save /restore matrix inbetween draw calls or whatever.

MrShoe
02-14-2002, 05:22 PM
Have you thought about using gluLookAt?
Goes like this:
glPushMatrix();
gluLookAt(xpos, ypos, zpos, xlook, ylook, zlook, 0.0, 1.0, 0.0);
...
glPopMatrix();

I find it easier to keep track of things this way than rotationg the entire world around the player and translating.