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

我正在用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的大小;但我首先必须阅读规范,如果这是实际定义的,然后交叉指示所有实现都正确.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...