在 OpenGL ES 2 中更新 VBO 的顶点

Updating the vertices of a VBO in OpenGL ES 2

如果我想更改 VBO 的顶点,我是否必须重复下面代码中显示的过程,但使用新的顶点数组?

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo);

GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,
                    vertexBuffer.capacity() * BYTES_PER_FLOAT,
                    vertexBuffer, GLES20.GL_STATIC_DRAW);

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

或者有更好、更高效的方法吗?我相信每次我想更改顶点时都调用上面的代码不会太理想。感谢任何答案,谢谢。

如果要更改现有缓冲区对象的数据,则 glBufferSubData should be used. While glBufferData creates a new data store for the buffer object, glBufferSubData 更新缓冲区对象的数据存储。这避免了重新分配数据存储的成本。

OpenGL ES 2.0 Full Specification, 2.9. BUFFER OBJECTS, page 24:

BufferData deletes any existing data store, and sets the values of the buffer object’s state variables as shown in table ...

.....

To modify some or all of the data contained in a buffer object’s data store, the client may use the command

void BufferSubData( enum target, intptr offset, sizeiptr size, const void *data );


此外,您应该根据需要设置 glBufferDatausage 参数。

OpenGL ES 2.0 Full Specification, 2.9. BUFFER OBJECTS, page 23:

void BufferData( enum target, sizeiptr size, const void *data, enum usage );

....

usage is specified as one of three enumerated values, indicating the expected application usage pattern of the data store. The values are:

  • STATIC_DRAW The data store contents will be specified once by the application, and used many times as the source for GL drawing commands.

  • DYNAMIC_DRAW The data store contents will be respecified repeatedly by the application, and used many times as the source for GL drawing commands.

  • STREAM_DRAW The data store contents will be specified once by the application, and used at most a few times as the source of a GL drawing command.