仅更新所选三角形的颜色,VBO更新

时间:2017-03-24 00:32:18

标签: c++ opengl vbo

实际上我对使用glBufferSubData和glMapBuffer有点困惑。在我的程序中,我加载了一个三角形网格,所以我创建了一个顶点结构,并使用整个网格数据初始化了我的VertexBuffer。我实现了光线投射和光线三角形交叉。我想要做的是,如果我的光标与网格相交,相交的三角形会随着它相交而改变颜色。它现在正在工作,但是当我尝试加载具有50.000+顶点的更大网格时,它不再起作用了。我用glBufferData更新我的整个VBO,因为我不明白如何只用glBufferSubData更新三角形的颜色。有人可以解释我如何只更新我的VBO的特定部分(例如Color)吗?

struct Vertex
{
    glm::vec3  Pos;
    glm::vec3  Normal;
    glm::vec3  Color;
};

VertexBuffer(void* vertices, size_t size)
{
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_DYNAMIC_DRAW);

    glBindVertexArray(0);
}

void Render()
{
    glBindVertexArray(vertexBuffer->VAO);
    glUseProgram(Fill->program);

    glUniformMatrix4fv(glGetUniformLocation(Fill->program, "model"), 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(glGetUniformLocation(Fill->program, "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(Fill->program, "projection"), 1, GL_FALSE, glm::value_ptr(proj));

    for (int j = 0; j < numVertices; j += 3){

        if (intersectPlane(this->Vertices[j], this->Vertices[j + 1], this->Vertices[j + 2], ray, orig)){

            glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->buffer);
            this->Vertices[j].Color = glm::vec3(1.0, 0.0, 0.0);
            this->Vertices[j + 1].Color = glm::vec3(1.0, 0.0, 0.0);
            this->Vertices[j + 2].Color = glm::vec3(1.0, 0.0, 0.0);
            glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), Vertices, GL_DYNAMIC_DRAW);
        }
        else{
            glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->buffer);
            this->Vertices[j].Color = glm::vec3(0.695f, 0.695f, 0.695f);
            this->Vertices[j + 1].Color = glm::vec3(0.695f, 0.695f, 0.695f);
            this->Vertices[j + 2].Color = glm::vec3(0.695f, 0.695f, 0.695f);
            glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), Vertices, GL_DYNAMIC_DRAW);
        }
    }

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->buffer);

    GLuint posLoc = glGetAttribLocation(Fill->program, "Position");
    GLuint norm = glGetAttribLocation(Fill->program, "Normal");
    GLuint colo = glGetAttribLocation(Fill->program, "Color");

    glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Pos));
    glVertexAttribPointer(norm, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Normal));
    glVertexAttribPointer(colo, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Color));

    glEnableVertexAttribArray(posLoc);
    glEnableVertexAttribArray(norm);
    glEnableVertexAttribArray(colo);

        glDrawArrays(GL_TRIANGLES, 0, numVertices);
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glUseProgram(0);

}

1 个答案:

答案 0 :(得分:1)

glBufferSubData更新缓冲区的部分(或整个),而无需重新创建它。

在你的情况下,你可以做一些微积分来知道当前所选边的缓冲区内的位置(所谓的“偏移”),并更新缓冲区仅用于那对顶点。

glMapBuffer为您提供了一个指向缓冲区的(虚拟)指针。您可以使用C / C ++函数直接读取或写入该地址。完成后请注意“取消映射”。

现在,您的缓冲区布局是Pos,Normal,Color和repeat。所以:PxPyPzNxNyNzCrCgCbPxPyPzNxNyNzCrCgCbPxPyPzNxNyNzCrCgCb ...“交错数据”。您必须小心更新的位置(字节)。