-
walking through space
I'm making a small and simple 3D game, but I'm habing difficulties to walk around in space.
I made a structure which holds the position and the rotation of my camera. I also have a structure which holds the position and rotation of my object.
To draw this, I tried this code :
Code:
glLoadIdentity();
glTranslatef (-cam_pos[0], -cam_pos[1], -cam_pos[2]);
glRotatef (cam_rot[0], 1.0f, 0.0f, 0.0f);
glRotatef (cam_rot[1], 0.0f, 1.0f, 0.0f);
glRotatef (cam_rot[2], 0.0f, 0.0f, 1.0f);
glTranslatef (obj_pos[0], obj_pos[1], obj_pos[2]);
glRotatef(obj_rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(obj_rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(obj_rot[2], 0.0f, 0.0f, 1.0f);
glBegin(...);
glVertex3f(vert_pos[0], vert_pos[1],vert_pos[2]);
...etc
glEnd()
}
This code gives wrong results though.
Does anyone know what I'm doing wrong?
-
can you describe the "wrong results" your seeing? The problem could be a number of things.
*Are your vert_pos and obj_pos coords in world coordinates or are they relative to the camera's position? (because the vert_pos coords are being rendered relative to the object (obj_pos) which is rendered relative to the camera)
-
The objects seem to rotate around the origin (but I could be wrong) . The vert_pos coordinates are relative to the obj_pos coordinate. And obj_pos is in world coordinates.
The cam_pos is als in world coordinates.
-
Well, the way this works highly depends on the conventions you're using. My first guess would be to try performing the camera rotations before the translation.
If that doesnt work you'll have to trace through what is happening one step at a time (in terms of positioning and rotation). Note that a rotation followed by a translation will have a different effect than a translation followed by a rotation.
-
Transformation order is reverse of "logical" order. If you transform in this order:
Code:
glLoadIdentity();
glTranslate();
glRotate();
glScale();
The scale will be done first, then the rotation and last the translation.