如何将glRotatef()转换为glMultMatrixd()的乘法矩阵

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

本文关键字:glMultMatrixd 转换 glRotatef      更新时间:2023-10-16

我需要用不同的openGL函数执行一些操作。

我有一个初始化为glList的多维数据集。我要做的是对标准函数的变换我喜欢对矩阵乘法做完全相同的操作。但是我被旋转函数困住了。我想让立方体绕x轴旋转,例如90°:

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

应替换为:

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);

我发现这个非常有用的网站,但是它的功能与上面的不完全相同。是否有一个通用的原则如何将glRotatef()函数转换为gl变换矩阵?

更新:对不起,我错过了文档开头的重要说明,即文档中的矩阵需要转置才能在openGL中使用。

在x轴

     |  1  0       0       0 |
 M = |  0  cos(A) -sin(A)  0 |
     |  0  sin(A)  cos(A)  0 |
     |  0  0       0       1 |
在Y-Axsis

     |  cos(A)  0   sin(A)  0 |
 M = |  0       1   0       0 |
     | -sin(A)  0   cos(A)  0 |
     |  0       0   0       1 |
在Z-Axsis

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

注意:OpenGL中的矩阵使用列主内存布局

的例子:

#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]);
//...