OpenGL三角形旋转错误

OpenGL triangle rotates wrong

我想围绕他自己的轴旋转我的三角形,但它是围绕相机旋转的。 我必须从教程中编写代码,唯一的区别是我使用的是 C 而不是 C++。所以我必须使用 "cglm"-lib 而不是 "glm"-lib。 (我也使用过剩和高兴)

全局定义:

mat4  model;
mat4 projection;
mat4 view;
mat4 modelViewProj;

主要是:

glm_mat4_identity(model); //Einheitsmatrix
glm_mat4_identity(view);
glm_scale(model, (vec3) {1.2f, 1.2f, 1.2f});

glm_perspective(glm_rad(45.0f), 4.0f/3.0f, 0.1f, 100.0f, projection);

glm_translate(view, (vec3) {0, 0, -5.0f});

glm_mat4_mul(model, projection, modelViewProj);
glm_mat4_mul(modelViewProj, view, modelViewProj);

modelMatrixLocation = glGetUniformLocation(shader1.shaderId, "u_modelViewProj");

在循环中:

//In the void display()
glUniformMatrix4fv(modelMatrixLocation,1, GL_FALSE, &modelViewProj[0][0]); 

//In the void update() ->glutTimerFunc()
glm_rotate(model, 0.08f, (vec3){0, 1, 0});

glm_mat4_mul(model, projection, modelViewProj);
glm_mat4_mul(modelViewProj, view, modelViewProj);

矩阵乘法的顺序错误。

必须是:

modelViewProj = projection * view * model;

在 C:

glm_mat4_mul(view, model, modelViewProj);
glm_mat4_mul(projection, modelViewProj, modelViewProj);

注意,void glm_mat4_mul(mat4 m1, mat4 m2, mat4 dest) 计算:

dest = m1 * m2;

Matrix multiplication is not Commutative (in compare to Arithmetic Multiplication)。顺序很重要。