使用 OpenGL VBO 绘制数千个多边形
Drawing thousands of polygons with OpenGL VBO
我正在尝试创建能够渲染超过 100000 个二维原始对象的 OpenGL 应用程序。
据我所知,使用现代 OpenGL 和 VBO 应该是可能的。
下面是代码(使用 Qt):
#include "paintwidget.h"
PaintWidget::PaintWidget(QGLWidget *parent) : QGLWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
timer->start(16);
}
GLuint indices[100000];
void PaintWidget::initializeGL()
{
VBOBuffer= new QGLBuffer(QGLBuffer::VertexBuffer);
VBOBuffer->create();
VBOBuffer->bind();
VBOBuffer->setUsagePattern(QGLBuffer::DynamicDraw);
VBOBuffer->allocate(100000 * 10 * sizeof(double));
// load data into VBO:
for(int i=0; i<100000; i++)
{
GLdouble vertices[] = {100 + (double)i * 100, 100 + (double)i * 100,
100 + (double)i * 100, 200 + (double)i * 100,
200 + (double)i * 100, 200 + (double)i * 100,
300 + (double)i * 100, 150 + (double)i * 100,
200 + (double)i * 100, 100 + (double)i * 100 };
VBOBuffer->write(i * 10 * sizeof(double), vertices, 10 * sizeof(double));
}
// fill indices array:
for(int i=0; i<100000; i+=10)
{
indices[i] = i;
indices[i+1] = i+1;
indices[i+2] = i+1;
indices[i+3] = i+2;
indices[i+4] = i+2;
indices[i+5] = i+3;
indices[i+6] = i+3;
indices[i+7] = i+4;
indices[i+8] = i+4;
indices[i+9] = i;
}
}
void PaintWidget::paintEvent(QPaintEvent*)
{
QPainter paint(this);
paint.beginNativePainting();
glEnable(GL_LINE_SMOOTH);
glEnable(GL_MULTISAMPLE);
glClearColor(0.1, 0.96, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_DOUBLE, 0, 0);
// draw my polygons:
for(int i=0; i<100000; i+=5)
{
glDrawArrays(GL_POLYGON, i, 5);
}
glDisableClientState(GL_VERTEX_ARRAY);
paint.endNativePainting();
}
void PaintWidget::updateTimer()
{
paintEvent(nullptr);
}
此代码每 16 毫秒渲染 100000 个多边形。
而且我对我的代码的性能不是很满意。它加载处理器相当多(尽管使用 VBO)。我可以提高效率吗,或者这是最好的性能?
谢谢。
那是你的问题:
// draw my polygons:
for(int i=0; i<100000; i+=5)
{
glDrawArrays(GL_POLYGON, i, 5);
}
您正在为单个 VBO 执行 100000 次绘制调用。这就是您 CPU 的负担。相比之下,最新的《毁灭战士》整个场景平均需要不到 1500 次绘制调用。
您只需调用 glDrawArrays
或 glDrawElements
即可绘制整个几何图形。顺便说一句:现代 OpenGL 不再支持 GL_POLYGON(唯一支持的原语 a GL_POINTS、GL_LINE* 和 GL_TRIANGLE*)。
如果您关心的是开始一个新的原语,with glDrawElements
you can specify a special index that restarts。或者(这实际上是首选方法)将其绘制为索引三角形。索引是高效缓存的关键,因此如果您想要最高性能,那就是最佳选择。
我正在尝试创建能够渲染超过 100000 个二维原始对象的 OpenGL 应用程序。
据我所知,使用现代 OpenGL 和 VBO 应该是可能的。
下面是代码(使用 Qt):
#include "paintwidget.h"
PaintWidget::PaintWidget(QGLWidget *parent) : QGLWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
timer->start(16);
}
GLuint indices[100000];
void PaintWidget::initializeGL()
{
VBOBuffer= new QGLBuffer(QGLBuffer::VertexBuffer);
VBOBuffer->create();
VBOBuffer->bind();
VBOBuffer->setUsagePattern(QGLBuffer::DynamicDraw);
VBOBuffer->allocate(100000 * 10 * sizeof(double));
// load data into VBO:
for(int i=0; i<100000; i++)
{
GLdouble vertices[] = {100 + (double)i * 100, 100 + (double)i * 100,
100 + (double)i * 100, 200 + (double)i * 100,
200 + (double)i * 100, 200 + (double)i * 100,
300 + (double)i * 100, 150 + (double)i * 100,
200 + (double)i * 100, 100 + (double)i * 100 };
VBOBuffer->write(i * 10 * sizeof(double), vertices, 10 * sizeof(double));
}
// fill indices array:
for(int i=0; i<100000; i+=10)
{
indices[i] = i;
indices[i+1] = i+1;
indices[i+2] = i+1;
indices[i+3] = i+2;
indices[i+4] = i+2;
indices[i+5] = i+3;
indices[i+6] = i+3;
indices[i+7] = i+4;
indices[i+8] = i+4;
indices[i+9] = i;
}
}
void PaintWidget::paintEvent(QPaintEvent*)
{
QPainter paint(this);
paint.beginNativePainting();
glEnable(GL_LINE_SMOOTH);
glEnable(GL_MULTISAMPLE);
glClearColor(0.1, 0.96, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_DOUBLE, 0, 0);
// draw my polygons:
for(int i=0; i<100000; i+=5)
{
glDrawArrays(GL_POLYGON, i, 5);
}
glDisableClientState(GL_VERTEX_ARRAY);
paint.endNativePainting();
}
void PaintWidget::updateTimer()
{
paintEvent(nullptr);
}
此代码每 16 毫秒渲染 100000 个多边形。
而且我对我的代码的性能不是很满意。它加载处理器相当多(尽管使用 VBO)。我可以提高效率吗,或者这是最好的性能?
谢谢。
那是你的问题:
// draw my polygons: for(int i=0; i<100000; i+=5) { glDrawArrays(GL_POLYGON, i, 5); }
您正在为单个 VBO 执行 100000 次绘制调用。这就是您 CPU 的负担。相比之下,最新的《毁灭战士》整个场景平均需要不到 1500 次绘制调用。
您只需调用 glDrawArrays
或 glDrawElements
即可绘制整个几何图形。顺便说一句:现代 OpenGL 不再支持 GL_POLYGON(唯一支持的原语 a GL_POINTS、GL_LINE* 和 GL_TRIANGLE*)。
如果您关心的是开始一个新的原语,with glDrawElements
you can specify a special index that restarts。或者(这实际上是首选方法)将其绘制为索引三角形。索引是高效缓存的关键,因此如果您想要最高性能,那就是最佳选择。