简体   繁体   中英

VAO/VBO management - change all VBO data

I have a VAO with 3 VBOs, containing a model with vertexes, normals and texture coordinates.

I intend to change all the data in those VBOs quite often, roughly from a 500ms to a 20ms update frequency. The new model downloaded to the VBO can have less or more triangles than the previous one. So the buffer size will also change.

I'm not an expert in OpenGL so I would like some tips if possible on how to improve my code. For now, the program is implemented as follows:

glBindVertexArray(*vao);

if (mesh->HasPositions()) {
    glBindBuffer(GL_ARRAY_BUFFER, vbo_pos);
    glBufferData(
        GL_ARRAY_BUFFER,
        3 * *point_count * sizeof (GLfloat),
        points,
        GL_DYNAMIC_DRAW
        );
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);
    free(points); // free our temporary memory
}
if (mesh->HasNormals()) {
    glBindBuffer(GL_ARRAY_BUFFER, vbo_norm);
    glBufferData(
        GL_ARRAY_BUFFER,
        3 * *point_count * sizeof (GLfloat),
        normals,
        GL_DYNAMIC_DRAW
        );
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(2);
    free(normals); // free our temporary memory
}
if (mesh->HasTextureCoords(0)) {
    glBindBuffer(GL_ARRAY_BUFFER, vbo_tex);
    glBufferData(
        GL_ARRAY_BUFFER,
        2 * *point_count * sizeof (GLfloat),
        texcoords,
        GL_DYNAMIC_DRAW
        );
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(1);
    free(texcoords); // free our temporary memory
}

EDIT:

This current solution have the issue that the drawing is not what it should be.

If my first model is a big one (50Mb) and I load another one a bit smaller (25Mb) there's still a part of the previous model that is drawn.

If the first model is the small one (25Mb) and then I changed it for the big one (50Mb) the drawing isn't changed (or at least it doesn't seem to change). If I charge after an even smaller one (216kb) the drawing changes (but there's still a part remaining).

So I suppose there's something wrong with my VBO management?

ok i found the issue

I was updating vbo data in a thread. I didn't know that it doesn't work :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM