PDA

View Full Version : translation and rotation

h333
04-11-2004, 12:11 PM
hi all
i am trying to make a ball trundle or move
but i really didn't understand the translation and the rotation in opengl so could anybody help me in this problem

plasmonster
04-11-2004, 01:03 PM
Transformations can be frightfully trying at times, but with the right perspective, it gets much easier. For simplicity, if we despence with scaling, all we ever really do is rotate an object, then move it somewhere. For instance, if we want to position a basketball in space, we might want to give it some spin, then put it in the hoop. One way to do this is
glTranslatef( hoop.x, hoop.y, hoop.z );
glRotatef( spinAngle, spinAxis.x, spinAxis.y, spinAxis.z );

This tells gl that we first want to spin the ball at the current origin, then move it to its position. All transformations are applied in reverse order, keeep this in mind. So any tranform can be achieved in this way, just think of what you want to do to a single point, the ball in this example, and reverse it. If we had used

glRotatef( spinAngle, spinAxis.x, spinAxis.y, spinAxis.z );
glTranslatef( hoop.x, hoop.y, hoop.z );

the effect would be completely different. This says that I want to first move the ball in the current orientation by the vector ball, then rotate it. This would give you a solar system with the ball as the moon orbiting the Earth.

This could be the push order for your matrix:
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glTranslatef( hoop.x, hoop.y, hoop.z )
glRotatef( spin, spinAxis.x, spinAxis.y, spinAxis.z );
glPopMatrix();

We use a push, rather than glLoadIdentity(), because we're adding a transformation to the model view matrix, not creating a new matrix from scratch, although that is perfectly ok.

Now when you setup your model view matrix using lookAt(), or some other flavor, this is the first
transform on the stack, or the _last_ one to be applied. So, from above, after we position our ball, the last transform is to evaluate the ball in the camera's coordinate system, so we can see it. Our entire setup to draw the ball might be
glMatrixMode( GL_MODELVIEW );