修改 OpenGL 顶点缓冲区的正确方法是什么? [英] What is the proper way to modify OpenGL vertex buffer?

查看:29
本文介绍了修改 OpenGL 顶点缓冲区的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 OpenGL 中设置顶点缓冲区,如下所示:

I'm setting up a vertex buffer in OpenGL, like this:

int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);

稍后,如果我想向vertexData"添加或删除顶点,那么正确的方法是什么?甚至有可能吗?我假设我不能直接修改数组而不将其重新发送到 GPU.

Later, if I want to add or remove vertices to "vertexData", what is the proper way to do this? Is it even possible? I'm assuming I can't just modify the array directly without re-sending it to the GPU.

如果我修改了 vertexData 数组,则再次调用它:

If I modify the vertexData array, then call this again:

glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);

...这会用我的新数据覆盖旧缓冲区吗?还是我也必须删除旧的?有没有更好的办法?

...will that overwrite the old buffer with my new data? Or do I also have to delete the old one? Is there a better way?

推荐答案

当你调用 glBufferData 时,任何 OpenGL 缓冲区对象的大小都会被设置.也就是说,OpenGL 将分配您在 glBufferData(未在 OP 中列出)的第二个参数中指定的内存量.事实上,如果你调用例如 glBufferData( GL_ARRAY_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW ); OpenGL 将创建一个 bufferSize 字节的未初始化数据 字节的缓冲区em>.

The size of any OpenGL buffer object is set when you call glBufferData. That is, OpenGL will allocate the amount of memory you specify in the second argument of glBufferData (which isn't listed in the OP). In fact, if you call, for example glBufferData( GL_ARRAY_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW ); OpenGL will create a buffer of bufferSize bytes of uninitialized data.

您可以使用 glBufferSubDataglMapBuffer 或任何其他用于传递数据的例程来加载任意数量的数据(最多可达缓冲区的大小).调整缓冲区大小的唯一方法是使用相同缓冲区 ID 的新大小(从 glGenBuffers 返回的值)调用 glBufferData.

You can load any amount of data (up to the size of the buffer) using glBufferSubData, glMapBuffer, or any of the other routines for passing data. The only way to resize the buffer is to call glBufferData with a new size for the same buffer id (the value returned from glGenBuffers).

也就是说,您始终可以使用缓冲区中的数据子集(类似于删除顶点),如果使用 glDrawElements 进行渲染,则可以随机访问缓冲区中的元素.将顶点添加到缓冲区需要分配更大的缓冲区,然后您需要重新加载缓冲区中的所有数据.

That said, you can always use a subset of the data in the buffer (which would be akin to deleting vertices), and if you render using glDrawElements, you can randomly access elements in the buffer. Adding vertices to a buffer would require allocating a larger buffer, and then you'd need to reload all of the data in the buffer.

这篇关于修改 OpenGL 顶点缓冲区的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆