OpenGL Rotation

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!
I think mathematically is the way to go.

φ is the angular rotation around the x-axis
θ is the angular rotation around the y-axis
ψ is the angular rotation around the z-axis

Your rotational matrix will look like:
1
2
3
4
double rotation[3][3] = 
{{ cos(θ)*cos(ψ), -cos(φ)*sin(ψ)+sin(φ)*sin(θ)*cos(ψ),  sin(φ)*sin(ψ)+cos(φ)*sin(θ)*cos(ψ) },
 { cos(θ)*sin(ψ),  cos(φ)*cos(ψ)+sin(φ)*sin(θ)*sin(ψ), -sin(φ)*cos(ψ)+cos(φ)*sin(θ)*sin(ψ) },
 {-sin(θ)       ,  sin(φ)*cos(θ)                     ,  cos(φ)*cos(θ)                      }};


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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void CreateRotationalMatrix(double matrix[3][3], double phi, double theta, double psi)
{
    matrix[0][0] =  cos(theta)*cos(psi);
    matrix[1][0] = -cos(phi)*sin(psi)+sin(phi)*sin(theta)*cos(psi);
    matrix[2][0] =  sin(phi)*sin(psi)+cos(phi)*sin(theta)*cos(psi);
    matrix[0][1] =  cos(theta)*sin(psi);
    matrix[1][1] =  cos(phi)*cos(psi)+sin(phi)*sin(theta)*sin(psi);
    matrix[2][1] = -sin(phi)*cos(psi)+cos(phi)*sin(theta)*sin(psi);
    matrix[0][2] = -sin(theta);
    matrix[1][2] =  sin(phi)*cos(theta);
    matrix[2][2] =  cos(phi)*cos(theta);
}

void RotatePoint(double point[3], double rotation[3][3])
{
    // 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;
}
Last edited on
Wow, that was more help than I could ask for. I will try and implement this and get back to you. Thanks a lot :)
Topic archived. No new replies allowed.