0

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.

Karl Adler
  • 15,780
  • 10
  • 70
  • 88

1 Answers1

1

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]);
//...
datenwolf
  • 159,371
  • 13
  • 185
  • 298
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
  • Matrices don't have to be "transposed" for OpenGL, OpenGL just uses a memory layout (column major) that looks "unintuitive" when used naively with C-style multidimensional arrays. However column major matrices have huge benefits in computer graphics, because most of the time you're interested in the column vectors more than in the row vectors. – datenwolf Jul 05 '14 at 17:21
  • isn't it somehow the same like transposing it? I just wanted to do it that way, so I could use the same matrices a mentioned in the document above. – Karl Adler Jul 05 '14 at 17:36
  • If you feed those matrices into a library that uses row major layout, well then yes. However most math packages out there actually use column major order. I just want to emphase on that OpenGL does not intentionally "transposes" the matrices. – datenwolf Jul 05 '14 at 19:05