如何增加OpenGL缓冲区? [英] How do I grow a buffer OpenGL?

查看:109
本文介绍了如何增加OpenGL缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在OpenGL中增加缓冲区?

Is it possible to grow a buffer in OpenGL?

假设我要使用实例渲染.每次在世界上产生一个新对象时,我都必须使用实例数据更新缓冲区.

Let's say I want to use instanced rendering. Everytime a spawn a new object in the world I would have to update the buffer with the instanced data.

在这种情况下,我有3个浮点数的缓冲区

In this case I have a buffer of 3 floats

  std::vector<GLfloat> offsets = {0.0f,0.5f,1.0f};
  auto offset_buffer = buffer::makeBuffer(BufferDraw::STATIC_DRAW, offsets);

如果我想拥有4个元素怎么办?我必须调用更大的glBufferData吗?但是旧数据会发生什么情况,是否将其复制过来?还是我必须完全删除缓冲区并创建一个新缓冲区?

what if I want to have 4 elements? Do I have to call glBufferData with a bigger size? But what happens with the old data, does it get copied over? Or do I have to delete the buffer entirely and create a new buffer?

推荐答案

您绝对不使用新大小调用glBufferData (...)来执行此操作.这将孤立旧缓冲区,并为您提供一个具有更大存储量的新缓冲区(其内容与先前分配的内存无关).

You definitely do not call glBufferData (...) with a new size to do this. That is going to orphan the old buffer and give you a new buffer with larger storage (whose contents are unrelated to the previous allocated memory).

只要任何需要它的先前OpenGL命令仍在管道中排队,旧数据将继续存在.这些命令完成后,OpenGL允许回收内存-您不必担心泄漏,但是就地增长不会自动发生.

The old data will continue to exist for as long as any prior OpenGL commands that need it are still queued up in the pipeline. After those commands finish, the memory is allowed to be reclaimed by OpenGL - you do not have to worry about leaking, but in-place growth does not happen automatically.

GL 3.1引入了复制缓冲区的概念,它将帮助您更有效地完成此操作.

GL 3.1 introduced the concept of copy buffers, which will help you do this a lot more efficiently.

// Bind the old buffer to `GL_COPY_READ_BUFFER`
glBindBuffer (GL_COPY_READ_BUFFER, old_buffer);

// Allocate data for a new buffer
glGenBuffers (1, &new_buffer);
glBindBuffer (GL_COPY_WRITE_BUFFER, new_buffer);
glBufferData (GL_COPY_WRITE_BUFFER, ...);

// Copy `old_buffer_size`-bytes of data from `GL_COPY_READ_BUFFER`
//   to `GL_COPY_WRITE_BUFFER` beginning at 0.
glCopyBufferSubData (GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, old_buffer_size);

在实际情况下,您可以将缓冲区数据从任何绑定位置复制到任何其他绑定位置.新的GL_COPY_..._BUFFER绑定点很方便,但不是必需的,因为它们不会干扰任何其他绑定.

In real-world situations, you can copy buffer data from any binding location to any other binding location. The new GL_COPY_..._BUFFER binding points are convenient, but not necessary, because they don't interfere with any other bindings.

这篇关于如何增加OpenGL缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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