0

大家好,我是 OPENGL 的新手,我想在翻译 3d 对象时更新顶点和索引,我制作了顶点向量和索引向量,这些向量保存了 3d 对象的所有数据,这可能是我使用的代码:

    // Create buffers/arrays
    glGenVertexArrays(1, &this->VAO);
    glGenBuffers(1, &this->VBO);
    glGenBuffers(1, &this->EBO);

    glBindVertexArray(this->VAO);
    // Load data into vertex buffers
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    // A great thing about structs is that their memory layout is sequential for all its items.
    // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
    // again translates to 3/2 floats which translates to a byte array.
    glBufferData(GL_ARRAY_BUFFER,this->vertices.size() * sizeof(vertex), &this->vertices[0], GL_STATIC_DRAW);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(int), &this->indices[0], GL_STATIC_DRAW);

    // Set the vertex attribute pointers
    // Vertex Positions
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)0);
    // Vertex Normals
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, normal));
    // Vertex Texture Coords
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, uv));

    glBindVertexArray(0);

我不知道这段代码是否更改了顶点和索引,或者它只是复制这两个向量并渲染我想要的不是复制这个向量并渲染我想直接使用这个向量并改变这个向量是这可能

4

1 回答 1

0

通常在图形上,我们使用所谓的变换矩阵来进行空间操作,例如平移/旋转/缩放。

这允许我们避免在模型特定数据(例如顶点、法线等)和他在 3D 世界中的状态(变换)之间混合数据。

所以我的建议是使用变换矩阵并更新顶点着色器中的模型位置。

您可以在这里找到更多信息https://learnopengl.com/Getting-started/Transformations

于 2021-02-18T10:58:09.617 回答