axis,rotation

I have drawn a rod with its centre at 0.0.0.When I use rotate it is rotated at its centre. Is there a way to rotated about an axis at the 2/3 of its length ,without changing the way it is drawn (with 0.0.0 at its center) ?

Yes, certainly. The trick is to use a composition of translations and a rotation. Let’s say you’re in the standard GL coordinate system, i.e.:

a. positive-x axis is to the right
b. positive-y axis is upwards
c. negative-z axis is into the screen

Say you’re at (0, 0, 5) and looking in the negative-z direction, so that you can see the origin.

At the origin there is a vertical line going from (0, -3, 0) to (0, 3, 0).

If you call:

glRotatef(90, 1, 0, 0);

before drawing the line, the line will be rotated 90 degrees about the x-axis.

Now, you want to rotate the line (rod) about an axis somewhere other than the center. Suppose you want to rotate it 90 degrees about the line that is parallel to the x-axis and passes through the point (0, 1.5, 0).

This can be accomplished by:

glTranslatef(0, 1.5, 0);
glRotatef(90, 1, 0, 0);
glTranslatef(0, -1.5, 0);

// code to draw the line

Remember that modelview transformations are applied in the reverse order that they appear. So the above code:

  1. translates everything down 1.5 units; in other words, the axis you want to rotate about has now been moved down to the x-axis

  2. then rotates 90-degrees about the x-axis

  3. finally translates the model back up 1.5 units

I suggest drawing a picture … hope that helps.

Eric

Thanx Eric.