views:

73

answers:

2

Hi, I am programming in OpenGL and C++. I know 2 points on 1 line ( a diagonal line) and wish to rotate an object around that diagonal line. How can I go about doing this? I know how to use glrotatef to rotate it around the x, y or z axis but am not sure about this Thanks Jessica

A: 

glrotate does the rotation about an axis. One method is to perform transformations which align rotation axis with one of coordinate axis, perform the rotation, then reverse the first step. If you need speed you can combine the operations into a special transformation matrix and apply them in one step. There's a description here.

Jay
+2  A: 

The x, y and z parameters to glRotate can specify any arbitrary axis, not just the x, y and z axes. To find an axis passing through your line, just subtract the end-points of the line to get an axis vector: if the two points are (x1, y1, z1) and (x2, y2, z2), the axis you need is (x2-x1, y2-y1, z2-z1).

Edit: As @chris_l pointed out, this works only if the line passes through the origin. If not, first apply a translation of (-x1, -y1, -z1) so that the line passes through the origin, then apply the above rotation, and translate it back by (x1, y1, z1).

casablanca
This of course only works correctly, if the axis goes through the origin (0, 0, 0).
Chris Lercher
@chris_l: Thanks, updated.
casablanca
@chris, this can be fixed by subtracting the first point from the second and then using the result as your axis vector. Works as long as they aren't the same, but then they wouldn't be 2 different points on the line
DominicMcDonnell
thanks guys I am reading all your comments as they come in
Jessica