opengl transformations

Started by
0 comments, last by ECKILLER 24 years ago
Hey, I translate an object out say 10 units and then i want to rotate it. The problem is, is that it doesn''t rotate around itself, but it rotates around the original origin. If i rotate first and then translate, it rotates around itself but then the translation only translates around the original axis. This is for a simple virtual world i''m making. Say the user presses the up arrow key to walk forward. I translate the world so it looks like he''s walking forward. Then he stops and wants to turn left, i rotate the world to the right but its making a rotation around the original origin. Basically i think i''m saying i need to translate the world, make that the new origin and then rotate around that new origin. Ugh i hope i explained myself My render function code will probably explain some more.... void Draw_Scene(void) { // clear the screen and the depth buffer glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // enable face culling glEnable(GL_CULL_FACE); // cull back facing triangles glCullFace(GL_BACK); glBindTexture(GL_TEXTURE_2D, tex); // rotate glRotatef(x_rot, 1.0f, 0.0f, 0.0f); glRotatef(y_rot, 0.0f, 1.0f, 0.0f); glRotatef(z_rot, 0.0f, 0.0f, 1.0f); // set object position in scene glTranslatef(move_left_right, move_up_down, move_in_out); Draw_Land_Scape(); //glPopMatrix(); // disable state glDisable(GL_CULL_FACE); // Swap buffers glutSwapBuffers(); } I''m sure this is a fundamental thing but i just can''t see it. ECKILLER
ECKILLER
Advertisement
It''s seems to me that it is the camera placement that you''re having problems with. Think of it like this: The camera is placed in the world at some coordinates and some orientation. In order to get the view from the cameras position you need to displace the world and camera so that the camera is aligned with the negative z-axis. You do this like so:

glTranslatef(-cam_xpos, -cam_ypos, -cam_zpos);
glRotatef(-cam_yaw, 0, 1, 0);
glRotatef(-cam_pitch, 1, 0, 0);
glRotatef(-cam_roll, 0, 0, -1);

I may be mistaken with the negative z-axis, but if my memory serves me right OpenGL has a right-handed coordinate system where the z-axis points out from the screen.

If you want to place an object in the world you should first rotate it around its origin and then translate it to its position:

glRotatef(obj_roll, 0, 0, -1);
glRotatef(obj_pitch, 1, 0, 0);
glRotatef(obj_yaw, 0, 1, 0);
glTranslatef(obj_xpos, obj_ypos, obj_zpos);

Do you see the similarities between the two? This is because one is the inverse transform of the other.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement