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

查看:92
本文介绍了修改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个字节的未初始化数据的缓冲区.

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天全站免登陆