何时在OpenGL中使用glBufferData [英] When to use glBufferData in OpenGL

查看:115
本文介绍了何时在OpenGL中使用glBufferData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习C ++中的顶点缓冲对象.我正在阅读一本有关OpenGL的书,其中说VBO渲染比其他形式的渲染更有效,因为数据存储在GPU上而不是堆上.但是,如果您仍然必须将数据数组从堆加载到GPU,我会感到困惑.每隔几秒钟,我就会更新程序的顶点数据,这意味着我必须随后使用glBufferData()刷新数据以更新到新状态.我没有看到这比仅正常渲染数组更有效.我想知道是否要调用glBufferData()的次数过多,或者是否有更好的方法直接在GPU上更新顶点数据.

解决方案

好吧,glBufferData (...)的作用超出您的想象.确实,它可以将数据提供给VBO,但更重要的一点是,它可以在服务器端(用于所有目的和目的的GPU)上分配内存以存储顶点.

在您的示例中,刷新数据时,顶点的数量以及因此存储它们所需的大小似乎都没有改变.您实际上应该做的是调用glBufferSubData (...)来更新数据,而无需为其重新分配空间.加上正确的使用标志(例如GL_DYNAMIC_DRAW),这比每次绘制内容时从客户端复制到服务器要有效得多.

glBufferData (...)视为malloc (...)memcpy (...)的组合.另一方面,glBufferSubData (...)memcpy (...).为此,最终,您甚至可以将VBO映射到应用程序的地址空间,而不必使用glMapBuffer (...)glUnmapBuffer (...)在客户端和服务器中分配存储,这与mmap (...)munmap (...)类似. /p>

I have just started learning about vertex buffer objects in C++. I am reading a book about OpenGL that says that VBO rendering is more efficient than other forms of rendering because the data is stored on the GPU instead of on the heap. However, I am confused how this could be if you still have to load an array of data from the heap to the GPU. Every few seconds, I update the vertex data of my program, which means that I must then use glBufferData() to refresh the data to update to the new state. I don't see how this is more efficient than just rendering the array normally. I was wondering if I am calling glBufferData() more than is necessary, or if there is a better way to update the vertex data directly on the GPU.

解决方案

Well, glBufferData (...) does more than you think. True it supplies data to a VBO, but the more important point is that it allocates memory on the server side (GPU for all intents and purposes) for vertex storage.

In your example, the number of vertices, and therefore size required to store them does not seem to change when you refresh your data. What you should actually be doing is calling glBufferSubData (...) to update the data without re-allocating space for it. Coupled with a correct usage flag (e.g. GL_DYNAMIC_DRAW) this can be much more efficient than copying from client to server everytime something is drawn.

Think of glBufferData (...) as a combination of malloc (...) and memcpy (...). glBufferSubData (...) on the other hand is memcpy (...). To this, end you can even do memory mapping of VBOs into your application's address space without having to allocate storage in both the client and server using glMapBuffer (...) and glUnmapBuffer (...), which are analogous to mmap (...) and munmap (...).

这篇关于何时在OpenGL中使用glBufferData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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