微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更新 OPENGL 中的顶点和索引

如何解决更新 OPENGL 中的顶点和索引

大家好,我是 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,(GLvoid*)offsetof(vertex,normal));
    // Vertex Texture Coords
    glEnabLevertexAttribArray(2);
    glVertexAttribPointer(2,2,uv));

    glBindVertexArray(0);

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

解决方法

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。