rotating objects that are not centered around the origin

Started by
9 comments, last by derodo 18 years, 5 months ago
i'm trying to rotate my objects by mouse, that are not centered at the origin, but no luck so far, it just seams as my object are still rotated around their the origin(0,0,0). this is my viewing code gl.glLoadIdentity(); //z is up vector glu.gluLookAt(0,0,0, 6538.139, -8353.096, 780.3191, 0,0,1 ); //translate back to origin gl.glTranslated(-6538.139, 8353.096, -780.3191); //rotate object by mouse gl.glRotatef(xRot, 1, 0, 0); gl.glRotatef(yRot, 0, 0, 1); //translate back to orignal location gl.glTranslated(6538.139, -8353.096, 780.3191); //draw points render.renderPoints(); anyone shows a light;) cheers
Cosmic Keys to my Creations & Times
Advertisement
Don't know exactly what you want to see, but it seems to me that the problem is the last "translation" before the final "lookat".

I assume you want to see your object rotating arround the x and z axis, so it describe some kind of "circle" arround the world origin (0,0,0)...don't you?

As the GL uses a post multipliation matrix scheme, you must issue the operations in the reverse order, so what I would do is:

1) use the "look at" to make the camera point from the origin to the desired point
2) perform the rotation, this will allways make it rotate arround the origin (0,0,0) but as you have peformed a previous translation the (0,0,0) wont be the local object space, but the (0,0,0) in world space
3) perform the translation (this will move the object to some point)
4) render

As you may see, the order of the operations is "not logical" as its somehow reversed. What you would normaly think to make an object rotate arround the world origin would be "move to the point from wich the rotation is going to be done", then "rotate it", then make the camera look to somewhere, and finally render.

But the actual ordering in the code should be just the opposite (except for the render operation, of course :) due to the way the matrix operations are performed.

Hope this helps
Just forget my other post, now I think I understand your problem "your object allways rotates arround the origin" :)

OK, the problem is basically the same. You need to think in "reverse order" so if you want to "move object, rotate it, then move it again to the new place where you want the object to be rotating around, and finally set the camera to look at it from the origin", just do the opposite:

1) Perform the "look at"
2) Perform a translation to the point in space you want the object be rotating around
3) Perform the rotation
4) Perform the translation that will give you the rotation "radius"

So, for example, if you want your object rotating arround the (100,100,100), describing circles of 50 units radius, just do:

1) Perform the "look at" to position the camera wherever you wat
2) Perform a translation to (100,100,100
3) Perform a rotation (rot,0,1,0) (this will make it rotate arround the Y axis)
4) Perform a translation to (50,0,0)
5) Then render

It should work :)
Not really what i basically want is that a model rotates around it own origin, sorry it was pretty early when i first posted this. When i move my mouse, so when moving my mouse in horizontal direction the model rotates around it's Z axis( z is up here ) and when moving the mouse up or down the model rotates around it x axis.

something like this, ( the pocketwatch demo )
http://www.sulaco.co.za/opengl2.htm



Cosmic Keys to my Creations & Times
And what are you getting instead?

If you just perform:

1) "look at"
2) rotation
3) render

It sound work fine. You should be able to see the object centered in the world origin (0,0,0) and rotating around its own center.

Maybe the problem is you are not setting the camera right. Just try to set the camera so it points to (0,0,0) from a quite distant point (0,0,500).
The camera will be positioned just in front of your object, at 500 units of distance, aligned with the world Y axis (that is, looking straight forward).

If you want your object rotating around its center, but positioned anywhere in the world, just do:

1) "look at"
2) translation to the point you want the object to be
3) rotation
4) render

No matter where you put the camera, your object will be at the position sated in step 2, rotating arround its center.

derodo has basically spelled it out for you, but to give you an aditional example here's my testing code to rotate the earth around its origin:

                camera.view();		// the earth				GL11.glPushMatrix();		bindTexture(texture3);		GL11.glTranslatef(0f, 6280000.0f, 0f); // some point in space to                                      // position the earth to illustrate                                     // rotation around a point that is not                                      //(0,0,0)		GL11.glRotatef(23.5f, 1.0f, 0.0f, 0.0f); // tilt the earth		GL11.glRotatef(rot*5, 0.0f, -1.0f, 0.0f);		createSphere(12800000,50);				GL11.glPopMatrix();


So after you position the camera or whatever, do a translation to where in space the origin of the rotating object should be, then do the rotation, then draw the object.
Just one thing more, i'm not sure, but I think GL uses a default left handed coordinate system.

So if you do nothing to change it (that is, setting your custom transformation), the standard "up" basis vector should be (0,1,0). The basis (1,0,0) would point "right", and (0,0,1) would point forward away.

That's why I wrote those numbers in the post before, asuming (0,0,0) to be the world origin, and (0,0,500) a position just 500 units away (not 500 units up).

If you don't know what your reference system is, then you may think your doing things wrong, while they're just right. I mean, if you call gluLookAt with an UP vector being (0,0,1), while using a left handed coordinate system, the camera will be rotated -90 degrees arround the Z axis, so if you just render an object against a black background, apparently, the rotations about its X axis would look as if they were done about it's Y axis...¿do you know what I mean?

If you do nothing, the default reference system is somethign like:

Y (0,1,0)^|  Z (0,0,1)|  /| /|/+-------> X (1,0,0)


At least, that's how it works with all the programs I have made. And it really helps to know what your base reference is.

Hope this helps a little bit.
--removed as I'm no longer sure myself--
But I think OpenGL was right-handed..

Edit: OpenGL is right-handed by default after all, and the "forward" Z axis is the negative direction, so the above diagram would be incorrect.
You're righ gorgorath. I was just looking at the wrong hand when I wrote the post :P

The only thing I'm not sure now is the Z forward direction...I think its along the positive axis...shoulnd't a right handed coordinate system look like?

         Y(0,1,0)         ^Z(0,0,1) |      \  |       \ |        \|  <------+X(1,0,0)


At least that's what I get if render three lines from (0,0,0) to (100,0,0), (0,100,0) and (0,0,100)...
If you meant me, I'm definitely not gorgorath :P

Math is sometimes confusing to me (I shouldn't admit that when I try to do 3d graphics I guess). I used this site for reference to figure this out: http://mathworld.wolfram.com/Right-HandRule.html

Also I looked at the OpenGL faq at http://www.opengl.org/resources/faq/technical/transformations.htm

Quote:
9.150 Can I make OpenGL use a left-handed coordinate space?

OpenGL doesn't have a mode switch to change from right- to left-handed coordinates. However, you can easily obtain a left-handed coordinate system by multiplying a negative Z scale onto the ModelView matrix. For example:

glMatrixMode (GL_MODELVIEW);

glLoadIdentity ();

glScalef (1., 1., -1.);

/* multiply view transforms as usual... */

/* multiply model transforms as usual... */


also on the same page:
Quote:
Eye Coordinates result from transforming Object Coordinates by the ModelView matrix. The ModelView matrix contains both modelling and viewing transformations that place the viewer at the origin with the view direction aligned with the negative Z axis.


So what does all this mean in our case? I don't know. :-) The matrix stack in OpenGL still confuses me. But a coordinate system that has "forward" Z negative seems to be right-handed.

Edit: I'll steal your diagram to illustrate what I think the whole system should look like by default in OpenGL:
         Y(0,1,0)         ^         |         |         |         |         +------->        /        X(1,0,0)       /      /     Z(0,0,1) 

Which is the same as that last diagram of yours, albeit rotated to show that the abscissa really does point to "the right".

This topic is closed to new replies.

Advertisement