Rotation problem

Started by
5 comments, last by skow 19 years, 9 months ago
Hi guys Need some help regarding rotating an object about its axis. Currently I am creating a 3D object(made up of many triangles) from a wavefront(Obj) file. But when I tried to rotate it, it flew all over the screen instead of just about its position. The object is situated deep down the z-axis, how should rotate it in this way? Does glRotatef rotate only at the origin?
Advertisement
The glRotatef() function rotates about the current local axis, so if you translate first, it will rotate about the new coords. The problem could be that the vertices in the OBJ are from the origin, not about a local axis. If so, simply redo the file with corrected vertices, otherwise, if you translate to the position of the object, you maybe either loading the identity matrix, or modifying it afterwards, or you may simply be rotating before you translate. You have to tranlate, then rotate, then draw.


Quote:Original post by kburkhart84
The glRotatef() function rotates about the current local axis, so if you translate first, it will rotate about the new coords. The problem could be that the vertices in the OBJ are from the origin, not about a local axis. If so, simply redo the file with corrected vertices, otherwise, if you translate to the position of the object, you maybe either loading the identity matrix, or modifying it afterwards, or you may simply be rotating before you translate. You have to tranlate, then rotate, then draw.


You just gotten me more confused. :) What do you mean by local axis? Please pardon a complete newbie here.
OpenGL is based on a local coordinate system. When you call glTranslate you are not moving ojects in a fixed coordinate system, you are moving the coordinate system itself.

To visualise this, hold a ruler in your hand, so you are gripping the centre of the ruler. Imagine to start with that you are using a fixed coordinate system (the opposite of what OpenGL uses) and that you are at the origin of the coordinate system. Translate objects by moving your hand forwards, backwards, sideways, etc. but do not move yourself. Rotate by swinging your whole arm in the direction you wish to rotate. Note that if you move the ruler to arms length and then rotate it the ruler ends up in a different position, as well as a different orientation.

Now start again, but this time imagine that your hand is always the origin of the coordinate system. You cen translate as before by moving your hand, but this time forwards is always the direction your fingers are pointing, up is the direction your palm is pointing, etc. Rotate by rotating your hand. Note that if you move the ruler to arms length and then rotate it it changes orientation but not position.

Hopefully that all makes sense, but it's slightly more complicated than that. The object also has a coordinate system. Where you hold the ruler defines where the origin of the ruler's coordinate system is. If you hold the ruler in the centre and rotate it 180º it looks pretty much the same and occupies almost exactly the same space it did before. If on the other hand you hold the ruler by one end and rotate it 180º then it ends up in a different position.

To get your object to rotate around its centre you have to properly define where its centre is. This means that when the object is created in wavefront it should be centred about the origin.

Hope that clears it up a bit for you.

Enigma
Quote:Original post by Enigma
OpenGL is based on a local coordinate system. When you call glTranslate you are not moving ojects in a fixed coordinate system, you are moving the coordinate system itself.

To visualise this, hold a ruler in your hand, so you are gripping the centre of the ruler. Imagine to start with that you are using a fixed coordinate system (the opposite of what OpenGL uses) and that you are at the origin of the coordinate system. Translate objects by moving your hand forwards, backwards, sideways, etc. but do not move yourself. Rotate by swinging your whole arm in the direction you wish to rotate. Note that if you move the ruler to arms length and then rotate it the ruler ends up in a different position, as well as a different orientation.

Now start again, but this time imagine that your hand is always the origin of the coordinate system. You cen translate as before by moving your hand, but this time forwards is always the direction your fingers are pointing, up is the direction your palm is pointing, etc. Rotate by rotating your hand. Note that if you move the ruler to arms length and then rotate it it changes orientation but not position.

Hopefully that all makes sense, but it's slightly more complicated than that. The object also has a coordinate system. Where you hold the ruler defines where the origin of the ruler's coordinate system is. If you hold the ruler in the centre and rotate it 180º it looks pretty much the same and occupies almost exactly the same space it did before. If on the other hand you hold the ruler by one end and rotate it 180º then it ends up in a different position.

To get your object to rotate around its centre you have to properly define where its centre is. This means that when the object is created in wavefront it should be centred about the origin.

Hope that clears it up a bit for you.

Enigma


Thanks for the detailed explanation Enigma. So do you mean I have to locate the center of my model and do a translation to that coordinate before using the rotation command?

Using some 3d software, I managed to locate the center as 1.57027, 0.61322, -1019.58966 but somehow it still doesn't work.
"-1019.58966"

Bingo.

If you have some way to manipulate the file, I suggest trying for a center of 0, 0, 0. Typically, models will have their center at the origin, with it a little love thrown in the positive direction of the y-axis so the model isn't stuck in the floor.

Imagine a big square block rotating about the origin. Now imagine your model with it's wonky far off center at the end of that block, rotating with it.

 ------------|            ||            ||     O     M||            ||            | ------------Where O is the origin and M is your model.


Hopefully I didn't add any more confusion into the world with the previous example.

(This is Not A Good Thing(tm), but you could use glTranslatef(-1.57027, -0.61322, 1019.58966) to place your model at the origin, but you're better off changing the center of your model)



-Jojo
Yes, by default it rotates around 0,0,0. You idealy want to have the object your rotating centered at this point. Then if you have it move 20 units you can still easily rotate about the center of the object...

glTranslatef(0,0,-80.0f);
glPushMatrix();
glRotatef(180.0f,0,1.0f,0);
//draw object
glPopMatrix;

This would translate it 80 units then rotate it about the objects center, instead of rotating around 0,0,0 (what would happen without the push/pop matrix).

This topic is closed to new replies.

Advertisement