Rotation

Started by Daslee, May 22, 2012, 19:05:57

Previous topic - Next topic

Daslee

Hello. I just tried to do rotation and i have problem. It rotates my object around all world, but i want that it rotates my object at its center. Im using glPushMatrix and glPopMatrix between object rendering and after glPushMatrix this: glRotatef(angle, 1, 0, 0); Here is little image about that:


CodeBunny

Have the model centered on the origin, and rotate THEN translate it to the desired location.

Daslee

Could you give me example with code?  :-\

pfolder

If I am thinking of this correctly, you are supposed to first glPushMatrix() in order to make a new matrix layer.  Then, you do glTranslatef(x,y,z) in order to make the center of what you are creating.  So say the center is at 0, 5, 5, you would put glTranslate(0,5,5), then you would do whatever rotation you want to put.  Then lastly, you glPopMatrix() back to normal.  Hopefully that works because I don't know for sure if that is what you do.
So:
glPushMatrix()
glTranslatef(x,y,z)
glRotate()
glPopMatrix()

CodeBunny

Close, but not quite. For one thing, if you used that code and rendered the object directly afterwards, no translation or rotation would be used.

The code to use should be something like:

glPushMatrix();

glRotatef(/*  rotational values  */);
glTranslatef(/*  translational values  */);

// render the object!
myObject.render();

glPopMatrix();


Calling glPopMatrix() resets everything to when you last called glPushMatrix().