PDA

View Full Version : Translating an object



Robin Forster
02-06-2003, 09:56 AM
My OpenGL graphics engine allows a user to load an object that is specified relative to its own internal origin. The object may also be rotated about any of the three major axis. The object can then be painted (with texturing) at any model view location.

My problem is that the new origin say (vx,vy,vz) must also be rotated. I have done this by using the modelview matrix and multiplying it against the aforementioned vector (new origin). However this does not seem to rotate it properly. The following is the algorithm used:

1. rotate to the worlds rotation (rx, ry, rz)
2. translate to (0.0, 0.0, 0.0)
3. get the modelview matrix
4. multiply the matrix against the position
vector (vx,vy,vz) (Mv = p)
5. reset the modelview matrix to identity
6. rotate the object by its internal rotation
and the worlds rotation (three axis)
7. translate to the new previously
calculated position vector p.
8. If the object is inside the frustum then
paint it.

This does not seem to plave the object correctly with respect to the floor of my test world.

Any ideas?

Robin

JustHanging
02-06-2003, 10:03 AM
Sorry, I didn't understand you problem. Please select one of the following:

a. You want the objects to be rotated around the world's origin
b. You want them to be rotated around their internal origin, as coming from the modelling program
c. You want to rotate them around some other point
d. You need the transformed positions for frustum culling (determining what you need to draw)
e. None of the above, it's more like...

-Ilkka

Robin Forster
02-06-2003, 10:11 AM
The objects are being rotated about BOTH their own internal origin and about the real modelview origin. For example lets say that my WHOLE world is rotated 45 degrees about the Y axis and 30 degrees about the X axis. AND the object itself is rotated about the Y axis. This is the same as saying I have a box in a room but the box is rotated and the box is NOT at the origin. It is translated.

Understand?

Robin.

[This message has been edited by Robin Forster (edited 02-06-2003).]

JustHanging
02-06-2003, 10:29 AM
Yeah, I think I get it.

I suppose the whole world rotation is there to simulate the camera, and then the objects just are rotated in their own coordinate systems.

I think that it's the local coordinate system in opengl that's confusing you. Luckily it also makes this kind of things very simple. Change your algorithm to this:

1. Set camera transform, for example
glLoadIdentity
glRotatef(-yaw, 0, 1, 0)
glRotatef(-pitch, 1, 0, 0)
glTranslatef(-camx, -camy, -camz)

2. For each object do
glPushMatrix
glTranslatef(posx...
glRotatef(...
draw the object
glPopMatrix

All the objects get automatically rotated in their own coordinate systems, and the camera transform gets applied to them. Sorry if that was unclear, it's quite easy when you get the hang of it.

-Ilkka