Sign in to follow this  

Rotating on the spot (OpenGL)

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

I use this code for rendering and am using glut.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(-2.0f, 0.0f, 0.0f);
glRotatef(rot1, 0.0f, 0.0f, 1.0f);

glBegin(GL_TRIANGLES);
  glVertex3f( 0.0f, 1.0f, -10.0f);
  glVertex3f(1.0f, -1.0f, -10.0f);
  glVertex3f( -1.0f, -1.0f, -10.0f);
glEnd();

glLoadIdentity();
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef(rot2, 0.0f, 0.0f, 1.0f);

glBegin(GL_QUADS);
  glVertex3f(-1.0f, 1.0f, -10.0f);
  glVertex3f(1.0f, 1.0f, -10.0f);
  glVertex3f(1.0f, -1.0f, -10.0f);
  glVertex3f(-1.0f, -1.0f, -10.0f);
glEnd();

rot1 += 5.0f;
rot2 -= 5.0f;

glutSwapBuffers();
}

I can rotate on the Z axis and keep the object in the same place but when I rotate around any other axis it goes all over the place. How would I rotate it on the spot for x and y? Thanks alot.

Share this post


Link to post
Share on other sites

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glRotatef(rot1, 0.0f, 0.0f, 1.0f);
glTranslatef(-2.0f, 0.0f, 0.0f);


glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -10.0f);
glVertex3f(1.0f, -1.0f, -10.0f);
glVertex3f( -1.0f, -1.0f, -10.0f);
glEnd();

glLoadIdentity();
glRotatef(rot2, 0.0f, 0.0f, 1.0f);
glTranslatef(2.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, -10.0f);
glVertex3f(1.0f, 1.0f, -10.0f);
glVertex3f(1.0f, -1.0f, -10.0f);
glVertex3f(-1.0f, -1.0f, -10.0f);
glEnd();

rot1 += 5.0f;
rot2 -= 5.0f;

glutSwapBuffers();
}



Rotations are done around an axis. So, if you translate first, the rotation will move around the axis you translated off of. So rotate first, so it rotates around the axis you are on, then translate.

That should work for you.

Share this post


Link to post
Share on other sites
What I mean is that the center stays still. How my code is before the center of the triangle is always in the same place and it rotates around it, I want to do that but on other axis too.

Thanks for the help :)

Share this post


Link to post
Share on other sites
I told you. It is because you are translating first. Do all your rotations first, then your translations.

So :

Rotate X
Translate X
Push New State
Rotate Y
Translate Y
Push New State
Rotate Z
Translate Z
Render
Pop State
Pop State
Pop State

That should work

Share this post


Link to post
Share on other sites
^ Just to add to that, You want to do it in the heirarchy of the object. So basically say you have a wheel. So you would first rotate it to the amount you need to too roll, then rotate for turning, then move it to the car's position, rotate it for the car's rotation, then finally move it to the position relative to the car it should be.

Hope that made sense.

Share this post


Link to post
Share on other sites
The correct order:
You want to rotate the object first, and then translate it.
After that, you want to translate the camera, and then rotate the camera.

However, transformations happen in the opposite order of how you call them. So to achieve this order, it's like this:

glLoadIdentity

glRotate(camera)
glRotate(camera)
glRotate(camera)
glTranslate(camera)

glTranslate(object)
glRotate(object)
glRotate(object)
glRotate(object)

Share this post


Link to post
Share on other sites
I agree with erissian, however I think it is confusing to say that "transformations happen in the opposite order of how you call them" (we are talking about OpenGL). I think it is easier to think of it like this: OpenGL tranformations transform the local (or current) coordinate system rather than the vertexes themselves, and vertexes are placed according to the local system.

Thus, glTranslate followed by glRotate means: translate the origin and then rotate around the new origin. The object is drawn in the current coordinate system. On the other hand, glRotate followed by glTranslate means: rotate around the current origin and then translate according the new rotated system. Anyway, the "Red Book" explains it pretty well.

As for mengha's code, I don't see anything obviously wrong or anything that would explain the cause of his problem.

Share this post


Link to post
Share on other sites

This topic is 4341 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this