OpenGL Rotation of an object around a line
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 diago开发者_JAVA技巧nal 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.
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)
.
Hey, how about doing some quaternions / vector maths? =) I've done that using small "patch" on my Vector class:
double NumBounds(double value)
{
if (fabs(value) < (1 / 1000000.0f))
return 0; else
return value;
}
class Vector
{
private:
double x, y, z;
public:
Vector(const Vector &v)
{
x = NumBounds(v.x); y = NumBounds(v.y); z = NumBounds(v.z);
}
Vector(double _x, double _y, double _z)
{
x = NumBounds(_x); y = NumBounds(_y); z = NumBounds(_z);
}
Vector Normalize()
{
if (Length() != 0)
return Vector(x / Length(), y / Length(), z / Length()); else
return *this;
}
double operator[](unsigned int index) const
{
if (index == 0)
return NumBounds(x); else
if (index == 1)
return NumBounds(y); else
if (index == 2)
return NumBounds(z); else
return 0;
}
void operator=(const Vector &v)
{
x = NumBounds(v.x); y = NumBounds(v.y); z = NumBounds(v.z);
}
Vector operator+(const Vector &v)
{
return Vector(x + v.x, y + v.y, z + v.z);
}
Vector operator-(const Vector &v)
{
return Vector(x - v.x, y - v.y, z - v.z);
}
double operator*(const Vector &v)
{
return NumBounds((x * v.x) + (y * v.y) + (z * v.z));
}
Vector operator*(double s)
{
return Vector(x * s, y * s, z * s);
}
Vector DotProduct(const Vector &v)
{
double k1 = (y * v.z) - (z * v.y);
double k2 = (z * v.x) - (x * v.z);
double k3 = (x * v.y) - (y * v.x);
return Vector(NumBounds(k1), NumBounds(k2), NumBounds(k3));
}
Vector Rotate(Vector &axis, double Angle)
{
Vector v = *this;
return ((v - axis * (axis * v)) * cos(angle)) + (axis.DotProduct(v) * sin(angle)) + (axis * (axis * v));
}
};
Using this class you can easily rotate any vector around any other one:
Vector a(1.0f, 0.0f, 0.0f), b(0.0f, 1.0f, 0.0f), c(0.0f, 0.0f, 0.0f);
c = a.Rotate(b, M_PI / 2.0f); // rotate vector a around vector b for 90 degrees (PI / 2 radians): should be Vector(0, 0, 1);
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.
精彩评论