views:

99

answers:

2

Hi, I'm playing with OpenGL ES on iPhone and I'm trying to rotate a model by panning with the finger. I discovered the open source app Molecules that let's you do that and I'm looking at that code, but when it comes to rotate a model of mine I'm able to rotate it only around a point distant in the space (like it was in orbit as a satellite and I am the fixed planet). Any suggestion on what can be wrong? I can post the code later , maybe on demand (many lines) For the most part refer to Molecules you can find it here MOLECULES

+4  A: 

Hi,

If my memory serves me correctly, I think you need to translate the model to the origin, rotate, and then translate back to starting position to get the effect you are after.

I think there is a glTranslate() function, Say the object is at 1,0,0. You should then translate by -1,0,0 to go to origin. That is translate by a vector going from the center of the object to the origin.

S.C. Madsen
how can i translate it to the origin? I guess I am at 0,0,0 and the object stands at some coordinates, I tried to translate even inside the model (i see only half of it) but it keeps rotating at some dinstance.Pardon me I'm a total noob.
rano
I updated my answer above with an example translation. If you consider yourself an OpenGL newbie, I remember learning a lot from the "NeHe" tutorials when first using OpenGL. Lesson 10 ( http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=10 ) shows how to move around the 3D world (scroll past the bit about loading models)
S.C. Madsen
@S.C. Madsen: I got your point so all I have to do is translate back of the quantity I first translate it. But I can't actually do it, I load the model (so I guess that his vertexes have some defined coordinates and he is not put into the origins) than I do some gltranslate() and glRotate to move 'the camera' (I know the scene is moving) in order to have the model in the view of the device. So I guess that I'm doing something wrong about the perspective or the coordinate axis system.
rano
Now I managed somehow to rotate it correctly (I was doing a glRotate more than necessary in the draw() ). But as I load another model I notice that I I need to set other values as transformations plus its rotation pivot point relays on the bottom point (it is a cylinder) and even if i translate it along the y axis it cointinues to rotate around that pivot. Is it something related to each specific model?
rano
The points of the model need not be stored at the origin. You could calculate the "center" point (add all vertices as vectors and divide by the number of vertices) and try to visualize that by drawing something at the center point using an easy identifiable color and/or shape.
S.C. Madsen
Ok thanks for all, I noticed that the rotation point is a model property changable from a program like Blender
rano
+2  A: 

The draw code probably looks roughly like this:

glLoadIdentity();
glTranslate(0, 0, -10);
glRotate(...);
drawMolecule();

Now it's important to realize that these transformations are applied in reverse order. If, in drawMolecule, we specify a vertex, then this vertex will first be rotated about the axis given to glRotate (which by definition passes through the local origin of the molecule), and then be translated 10 units in the −z direction.

This makes sense, because glTranslate essentially means: "translate everything that comes after this". This includes the glRotate call itself, so the result of the rotation also gets translated. Had the calls been reversed, then the result of the translation would have been rotated, which results in a rotation about an axis that does not pass through the origin anymore.

Bottom line: to rotate an object about its local origin, put the glRotate call last.

Thomas
It sounds like all the transform are going into a stack
rano
Yes and no. When you write it like this, what happens is that the current transform matrix is simply multiplied by the rotation/translation matrix that you specified, and there's no way to get back to the previous matrix (unless you apply the inverse transform, but with roundoff error, there's no way to be sure). However, OpenGL *does* have a "matrix stack": you can explicitly save and restore the current matrix using `glPushMatrix` and `glPopMatrix`. The matrix on top of the stack is the current matrix; matrices below that are not looked at, at all.
Thomas
Ok I got it, so the fact that the order matters is to be found in the premultiplication of two matrixes which has not the commutative property
rano
Exactly. So if v is your vertex (as a column vector) and M is the current modelview matrix, the transformed vertex is Mv. If you specify first A, then B, then C, then M = ABC (as opposed to CBA) so the transformed vertex is ABCv. From that, you can also see that C is applied first, then B, then A.
Thomas
^^ thanks for your support and explanations, I'm making the above answer as the correct one even if both helped since I do not know how to sign multiple answers and he has a lower rating than you : )Thanks again
rano
That's alright, my answer was intended to complement S.C. Madsen's. You're welcome :)
Thomas