# opengl rotate

hi i want to know how to rotate a sphere in any angle with opengl thanks

Let’s find your answer by taking the dot product of the fact that I’m just a noob in this matter with your question being a little generic what do you mean by “rotate a sphere”?
I presume it’s a model like any other and you want to spin it by yaw, pitch and roll amounts, so you’ll have to rotate all its vertices.
I don’t know whether OpenGl has a built-in function for this but here’s what needs to be done:
for each vertex

• (rotate by x, rotate by y, rotate by z), that is get the total rotation matrix by multiplying the rotation matrices per axis, in the reversed order
Totalrot = Zrot . Yrot . Xrot. A faster algorithm is
``````
mat4 rotByEulers(float angle_x, float angle_y, float angle_z)
{
mat4 TotalRot;
float A = cos(angle_x);
float B = sin(angle_x);
float C = cos(angle_y);
float D = sin(angle_y);
float E = cos(angle_z);
float F = sin(angle_z);

float AD = A * D;
float BD = B * D;
TotalRot = C * E;
TotalRot = -C * F;
TotalRot = D;
TotalRot = BD * E + A * F;
TotalRot = -BD * F + A * E;
TotalRot = -B * C;
TotalRot = -AD * E + B * F;
TotalRot = AD * F + B * E;
TotalRot = A * C;
TotalRot = TotalRot = TotalRot = TotalRot = TotalRot = TotalRot = 0;
TotalRot =  1;

}
``````

OR

• rotate it by arbitrary axis uvw, by angle a (quaternion style)
``````
mat4 rotByAxisAngle(vec3 axis, float a)
{
mat4 TotalRot;
rcos = cos(a);
rsin = sin(a);
TotalRot = rcos + axis.u * axis.u * (1-rcos);
TotalRot = axis.w * rsin + axis.v * axis.u * (1-rcos);
TotalRot = -axis.v * rsin + axis.w * axis.u * (1-rcos);
TotalRot = -axis.w * rsin + axis.u * axis.v * (1-rcos);
TotalRot = rcos + v*v*(1-rcos);
TotalRot = axis.u * rsin + axis.w * axis.v * (1-rcos);
TotalRot = axis.v * rsin + axis.u * axis.w * (1-rcos);
TotalRot = -axis.u * rsin + axis.v * axis.w * (1-rcos);
TotalRot = rcos + axis.w * axis.w * (1-rcos);
TotalRot = 1;
TotalRot = TotalRot = TotalRot = TotalRot = TotalRot = TotalRot = 0;
TotalRot =  1;