Advertisement

Transformation Troubles

Started by November 13, 2004 04:27 PM
4 comments, last by lc_overlord 20 years ago
Hello! First, I would like to apologize in advance--this is sort of a Direct3D question, or more of a question comparing the two. First off, we all know that if we want to translate some shape and then spin it, in OpenGL we do glTranslatef and then glRotatef. Translate moves the coordinate axes, and rotate then rotates them, causing a spinning globe or whatever we are drawing. But I have recently been toying with Managed DirectX, and have run into a difference. In Direct3D, you must do world.Transform=Matrix.Rotation*Matrix.Translation to achieve the same effect. Why is the order opposite? It seems that Rotation always rotates with respect to the origin. If this is the case, wouldn't this make rendering things like orbiting planets painful? I'm obviously missing something. If someone could explain the nuances of OpenGL's transformation in comparison with Direct3d's, it would greatly help. Thanks!
I don't know much about this subject, but one thing I've heard a lot of times is that with OpenGL you do the opposite of what you would normally do when you operate with matrices!
And that Direct3D does it in the normal way...

Someone else must fill in the details for me :D
Killers don't end up in jailThey end up on a high-score!
Advertisement
Quote: Original post by nife
And that Direct3D does it in the normal way...


No such think as normal way. As long as you are consistent. Math does not care.
To OP, lookup row and column major matrix (google for it).
First off, if you take a sphere, do a glTranslatef and then a glRotatef, you are gonna get dizzy.
IT will then orbit the camera at the translated distance

If you want to take an object and spinn it around it's own axis then do a glRotatef first, then a glTranslatef.

OpenGL and D3D does not differ on this, it's sort of universal.

In open gl you do not to things the oposite order, but if you are going to move the camera to the left, you translate to the right.
This is because your moving the vertics, not the camera.

For objects that are drawn in the center of the world but need to be placed somewhere else you need to translate in the oposite direction while rendering, then translate back right after.

and no, it wouldn't make rendering orbiting planets painfull,
First move your camera to the observing position(either by translating and rotating or by gluLookAt).

Then for every planet(not counting moons) do this.

1. save the state with glPushMatrix();
2. set the current spin of the planet with glRotatef.
3. translate forward the distance of the planets orbit.
4. rotate again, but this time the planets orbit around the sun.
5. render the planet
6. call glPopMatrix(); to reset the matrix to the camera position.

for moons

1. save the state with glPushMatrix();
2. set the current spin of the moon with glRotatef.
3. translate forward the distance of the moons orbit around the planet.
4. rotate again, this time the moons orbit around the planet.
5. translate forward the distance of the planets orbit.
6. rotate again, but this time the planets orbit around the sun.
7. render the moon.
8. call glPopMatrix(); to reset the matrix to the camera position.


Simple. :)
Are you sure? I'm pretty sure in OpenGL, if you Rotate, then Translate, it will orbit. But if you Translate, then Rotate, the object spins. In fact, I'm using this piece of code to zoom out a little bit and allow spinning of some object:

glTranslatef(0,0,zoom);
glRotatef(x_rot,1.0f,0,0);

Unless I am doign something odd, this is because GL translates the axes, and then rotates them at this translated point, right?

Anyways, my questions wasn't so much as how to draw orbiting planets, but why Direct3D and OpenGL differ in this order of transformation.

To Anonymous, I'll look up what you said. I suspected that it was due to some nuance of matrices.

Thanks for everyone's help!
my bad, seems i was wrong.
I suspect it has something to do with how the matrix is composed.
IF you read the manual you will see that

world.Transform=Matrix.Rotation*Matrix.Translation

is equal to

glTranslatef
glRotatef

because the matrix is multiplied like this

ModelViewMatrix=glTranslatef*ModelViewMatrix
ModelViewMatrix=glRotatef*ModelViewMatrix

witch in turn mean this

ModelViewMatrix=glRotatef*(glTranslatef*ModelViewMatrix).

IT's all slightly confusing, so i only use this way of rotating and translating for my camera class and for everything else i use my own inhouse matrix transformations.

This topic is closed to new replies.

Advertisement