Search code examples
c++openglmatrixglutglu

How to convert glRotatef() to multiplication matrice for glMultMatrixd()


I need to perform some operations with different openGL functions.

There for I have a cube initialized as glList. What I'm doing is some transformation with glu standard functions and I like to do exactly the same operations with matrix multiplication. But I got stuck with the rotation function. I want to rotate the cube around the x achsis e.g. 90°:

glRotatef(90.0, 1.0f, 0.0f, 0.0f);

should be replaced by:

GLdouble m[16] ={1.0, 0.0,  0.0,  0.0,
                 0.0, 0.0,  1.0,  0.0,
                 0.0,-1.0,  0.0,  0.0,
                 0.0, 0.0,  0.0,  1.0 };
glMultMatrixd(m);

I found this very usefull site but some how it's not doing exactly the same as the above function. Is there a generell principle how to transform the glRotatef() function to a gl transformation matrix?

UPDATE: sorry, I missed the important note a the beginning of the document that the matrices from the document needed to be transposed for use in openGL.


Solution

  • In X-Axis

         |  1  0       0       0 |
     M = |  0  cos(A) -sin(A)  0 |
         |  0  sin(A)  cos(A)  0 |
         |  0  0       0       1 |
    

    In Y-Axsis

         |  cos(A)  0   sin(A)  0 |
     M = |  0       1   0       0 |
         | -sin(A)  0   cos(A)  0 |
         |  0       0   0       1 |
    

    In Z-Axsis

         |  cos(A)  -sin(A)   0   0 |
     M = |  sin(A)   cos(A)   0   0 |
         |  0        0        1   0 |
         |  0        0        0   1 |
    

    NOTE: Matrices in OpenGL use column major memory layout

    Example:

    #include <math.h>
    #define PI 3.14159265
    
    // Rotate -45 Degrees around Y-Achsis
    GLdouble cosA = cos(-45.0f*PI/180);
    GLdouble sinA = sin(-45.0f*PI/180);
    
    // populate matrix in column major order
    GLdouble m[4][4] = {
      { cosA, 0.0, -sinA,  0.0}, // <- X column
      { 0.0,  1.0,   0.0,  0.0}, // <- Y column
      { sinA, 0.0,  cosA,  0.0}, // <- Z column
      { 0.0,  0.0,   0.0,  1.0}  // <- W column
    };
    
    //Apply to current matrix
    glMultMatrixd(&m[0][0]);
    //...