Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: opengl rotate

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    3

    opengl rotate

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

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2013
    Posts
    11
    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
    Code :
    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[0][0] = C * E;
     TotalRot[0][1] = -C * F;
     TotalRot[0][2] = D;
     TotalRot[1][0] = BD * E + A * F;
     TotalRot[1][1] = -BD * F + A * E;
     TotalRot[1][2] = -B * C;
     TotalRot[2][0] = -AD * E + B * F;
     TotalRot[2][1] = AD * F + B * E;
     TotalRot[2][2] = A * C; 
     TotalRot[0][3] = TotalRot[1][3] = TotalRot[2][3] = TotalRot[3][0] = TotalRot[3][1] = TotalRot[3][2] = 0;
     TotalRot[3][3] =  1;
     
     return TotalRot;
    }

    OR

    - rotate it by arbitrary axis uvw, by angle a (quaternion style)

    Code :
    mat4 rotByAxisAngle(vec3 axis, float a)
    {
     mat4 TotalRot;
     rcos = cos(a);
     rsin = sin(a);
     TotalRot[0][0] = rcos + axis.u * axis.u * (1-rcos);
     TotalRot[1][0] = axis.w * rsin + axis.v * axis.u * (1-rcos);
     TotalRot[2][0] = -axis.v * rsin + axis.w * axis.u * (1-rcos);
     TotalRot[0][1] = -axis.w * rsin + axis.u * axis.v * (1-rcos);
     TotalRot[1][1] = rcos + v*v*(1-rcos);
     TotalRot[2][1] = axis.u * rsin + axis.w * axis.v * (1-rcos);
     TotalRot[0][2] = axis.v * rsin + axis.u * axis.w * (1-rcos);
     TotalRot[1][2] = -axis.u * rsin + axis.v * axis.w * (1-rcos);
     TotalRot[2][2] = rcos + axis.w * axis.w * (1-rcos); 
     TotalRot[3][3] = 1;
     TotalRot[0][3] = TotalRot[1][3] = TotalRot[2][3] = TotalRot[3][0] = TotalRot[3][1] = TotalRot[3][2] = 0;
     TotalRot[3][3] =  1;
     
    return TotalRot;
    }

    There might be a little problem with the algorithms being row/column-major, as I'm confused which is which.
    I may have answered to your question and not answered to your problem but this is all I know. I hope there's a shorter way, like this kind of functions even quaternions are already in libraries. I myself would like to know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •