【问题标题】:OpenGL triangle rotates wrongOpenGL三角形旋转错误
【发布时间】:2020-01-02 22:23:52
【问题描述】:

我想围绕他自己的轴旋转我的三角形,但它围绕着相机旋转。 我必须从教程中编写代码,唯一的区别是,我使用的是 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);

【问题讨论】:

    标签: c opengl rotation glut glm-math


    【解决方案1】:

    矩阵乘法的顺序错误。

    必须是:

    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 不是Commutative(与Arithmetic Multiplication 相比)。顺序很重要。

    【讨论】:

    • 谢谢,它工作正常,但为什么呢?我在学校学到,我可以改变乘法乘积的顺序,它仍然是一样的。
    • @SouperFishɗɦʅ 不,Matrix multiplication 不是 Commutative(与 Arithmetic Multiplication 相比)。顺序很重要。
    • 好吧,我想我得等一会儿,直到我在学校里有了矩阵和向量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2014-11-06
    相关资源
    最近更新 更多