Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ECKILLER

OpenGL opengl transformations

This topic is 6800 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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!