加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为glBufferData添加新点的最佳方法是什么?

发布时间:2020-12-16 09:54:16 所属栏目:百科 来源:网络整理
导读:我正在用OpenGL进行实验,以找到经常向glBufferData添加新数据的最佳/最有效的方法. 为此,我编写了一个小的2D绘图程序,并在移动鼠标时简单地添加点. 整个函数看起来像这样: void addPoint(double x,double y){ glBindBuffer(GL_ARRAY_BUFFER,pointVertBuffOb
我正在用OpenGL进行实验,以找到经常向glBufferData添加新数据的最佳/最有效的方法.

为此,我编写了一个小的2D绘图程序,并在移动鼠标时简单地添加点.

整个函数看起来像这样:

void addPoint(double x,double y)
{
    glBindBuffer(GL_ARRAY_BUFFER,pointVertBuffObj);
    if (arrayOfPointCapacity < numOfPoints + 1) {
        U32 size = (arrayOfPointCapacity + 8) * sizeof(Point2);
        Point2 *tmp = (Point2*)realloc(arrayOfPoints,size);
        arrayOfPoints = tmp;
        arrayOfPointCapacity += 8;
    }
    arrayOfPoints[numOfPoints].x = x,arrayOfPoints[numOfPoints].y = y;
    U32 offset = numOfPoints * sizeof(Point2);
    glBufferData(GL_ARRAY_BUFFER,numOfPoints * sizeof(Point2),arrayOfPoints,GL_DYNAMIC_DRAW);
    numOfPoints++;
}

每次添加一个点时必须用新数据重置glBufferData似乎绝对疯狂.我想过使用glBufferData分配大量的点并用glBufferSubData设置这些点.当缓冲区的大小变得太小时,我再次调用glBufferData来增加缓冲区的大小,并将现有的点复制回来.

理想情况下,我宁愿避免将点数据存储在计算机内存中,并将所有内容保存在GPU内存中.但是当我调整缓冲区的大小时,我必须将数据从缓冲区复制回CPU,然后调整缓冲区的大小,最后将数据从CPU复制回缓冲区.所有这些,也似乎效率低下.

任何的想法?什么是最佳做法?

解决方法

When the size of the buffer becomes too small,then I call glBufferData again increasing the size of the buffer,and copying back existing points to it.

不错的主意.事实上,这是做这些事情的推荐方式.但是不要让块太小.

Ideally,I would prefer to avoid storing the point data in the computer memory and keep everything in the GPU memory.

这不是OpenGL的工作原理.可以根据需要在CPU和GPU内存之间自由交换缓冲区对象的内容.

But when I would resize the buffer,I would have to copy the data back from the buffer to the CPU,then resize the buffer,and finally copy the data back to the buffer from the CPU. All this,also seems inefficient.

正确.您希望避免OpenGL和宿主程序之间的副本.这就是为什么OpenGL-3.1和更高版本的功能glCopyBufferSubData在缓冲区之间复制数据的原因.当您需要调整缓冲区大小时,您也可以创建一个新的缓冲区对象并从旧的缓冲区复制到新的缓冲区对象^ 1.

[1]:也许你也可以通过利用名称孤儿来在同一个缓冲区对象名称中调整copys的大小;但我首先必须阅读规范,如果这是实际定义的,然后交叉指示所有实现都正确.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读