GLM 旋转无法正常工作

glm rotation not working properly

本文关键字:工作 常工作 旋转 GLM      更新时间:2023-10-16

我正在尝试围绕 X 和 Y 轴旋转立方体。我不知道为什么它不适用于这两个,它只在 Z 轴周围工作。我不工作的意思是立方体也在移动并且它没有在原地旋转。

我使用的相关代码是:

void rotateZ(float angle) {
        _modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(0,0,1));
    }
    void rotateY(float angle) {
        _modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(0,1,0));
    }
    void rotateX(float angle) {
        _modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(1,0,0));
    }
    void translate_to_origin() {
        _modelMatrix = glm::translate(_modelMatrix, glm::vec3(-(_x + _n/2), -(_y +  _n/2), -(_z + _n/2)));
    }   
    void translate_to_position() {
        _modelMatrix = glm::translate(_modelMatrix, glm::vec3(_x + _n/2, _y + _n/2, _z + _n/2));
    }
    void translate (float x, float y, float z) {
        _modelMatrix = glm::translate(_modelMatrix, glm::vec3(x, y, z));
    }
    void create(float x, float y, float z, float n) {

                std::vector<MyVertexFormat>verts;
                std::vector<glm::uvec3>indx;

                verts.push_back(MyVertexFormat(x,   y,  z,  1,0,0));
                verts.push_back(MyVertexFormat(x + n,   y,  z,  1,0,0));
                verts.push_back(MyVertexFormat(x + n,   y,  z - n,  0,1,0));
                verts.push_back(MyVertexFormat(x,   y,  z - n,  0,0,1));
                verts.push_back(MyVertexFormat(x,   y + n,  z,  1,0,0));
                verts.push_back(MyVertexFormat(x + n,   y + n,  z,  1,0,0));
                verts.push_back(MyVertexFormat(x + n,   y + n,  z - n,  1,0,1));
                verts.push_back(MyVertexFormat(x,   y - n,  z - n,  1,1,1));

        indx.push_back(glm::uvec3(0,1,2));
                indx.push_back(glm::uvec3(2,3,0));
        indx.push_back(glm::uvec3(4,5,6));
                indx.push_back(glm::uvec3(6,7,4));

        indx.push_back(glm::uvec3(0,1,4));
                indx.push_back(glm::uvec3(4,5,1));

        indx.push_back(glm::uvec3(1,2,5));
                indx.push_back(glm::uvec3(5,6,2));

        indx.push_back(glm::uvec3(0,3,4));
                indx.push_back(glm::uvec3(4,7,3));

        indx.push_back(glm::uvec3(2,3,7));
                indx.push_back(glm::uvec3(7,6,2));

        //vao
                glGenVertexArrays(1, &mesh_vao);
                glBindVertexArray(mesh_vao);
                //vbo
                glGenBuffers(1, &mesh_vbo);
                glBindBuffer(GL_ARRAY_BUFFER, mesh_vbo);
                glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertexFormat) * verts.size(), &verts[0], GL_STATIC_DRAW);
                //ibo
                glGenBuffers(1, &mesh_ibo);
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_ibo);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indx.size() * 3, &indx[0], GL_STATIC_DRAW);
                int pipe = glGetAttribLocation(_glProgramShader, "in_position");
                glEnableVertexAttribArray(pipe);
                glVertexAttribPointer(pipe, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertexFormat), (void*)0);
                pipe = glGetAttribLocation(_glProgramShader, "in_color");
                glEnableVertexAttribArray(pipe);
                glVertexAttribPointer(pipe, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertexFormat), (void*)sizeof(glm::vec3));
                mesh_num_indices = indx.size() * 3;
    }

要旋转立方体,我使用:

cube->translate_to_position();
cube->rotateY(angle);
cube->translate_to_origin();

我解决了问题!我没有正确构造幼崽。正确的方法是

            verts.push_back(MyVertexFormat(x,   y,  z,  1,0,0));
            verts.push_back(MyVertexFormat(x + n,   y,  z,  1,0,0));
            verts.push_back(MyVertexFormat(x + n,   y,  z + n,  0,1,0));
            verts.push_back(MyVertexFormat(x,   y,  z + n,  0,0,1));
            verts.push_back(MyVertexFormat(x,   y + n,  z,  1,0,0));
            verts.push_back(MyVertexFormat(x + n,   y + n,  z,  1,0,0));
            verts.push_back(MyVertexFormat(x + n,   y + n,  z + n,  1,0,1));
            verts.push_back(MyVertexFormat(x,   y + n,  z + n,  1,1,1));