Hey everyone, I have been looking online to find a solution to this but can't seem to find one. I have created a shape in OpenGl and have rotated it about its center by translating the camera to the center, glRotate by the angle and the translating it back to where it was. This is all great and it rotates perfect but it means that the coordinates of the shape haven't changed. Is there a way I can convert them to the new ones or if not would I have to rotate it mathematically without using glRotate? Also, I am very new to OpenGl, hence the question, so it would be great if you could explain it simply. Thanks a lot :) Any help would be much appreciated!
This function will rotate any point or vector. Just stick all of your points in here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void RotatePoint(double point[3], double phi, double theta, double psi)
{
// Define a rotational matrix
double rotation[3][3] =
{{ cos(theta)*cos(psi), -cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi), sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi) },
{ cos(theta)*sin(psi), cos(phi)*cos(psi)+sin(phi)*sin(theta)*sin(psi), -sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi) },
{-sin(theta) , sin(phi)*cos(theta) , cos(phi)*cos(theta) }};
// Multiply the matrix with the point
double x = rotation[0][0]*point[0] + rotation[1][0]*point[1] + rotation[2][0]*point[2];
double y = rotation[0][1]*point[0] + rotation[1][1]*point[1] + rotation[2][1]*point[2];
double z = rotation[0][2]*point[0] + rotation[1][2]*point[1] + rotation[2][2]*point[2];
// Write to the output
point[0] = x;
point[1] = y;
point[2] = z;
}
Edit: Of course re-calculating that rotational matrix can be processor heavy so if we need to do multiple points, we could calculate the matrix once only by sticking it in it's own function and making it an argument in the rotation: