我看过关于这个主题的类似帖子here,但是,我的问题有点不同。
我有一个2D图,它将由不同大小的不同位置的圆圈组成。目前,我的渲染方案使用显示列表来存储预先绘制的圆,用户可以使用glScalef / glTranslatef主动调整大小和翻译。但是,因为我渲染了数千个圆圈,所以调整大小和绘图变得非常慢。每个圆圈可以有不同的半径和颜色,所以这些东西必须在循环内完成。
当用户更改说圆圈的大小时,我可以尝试提高圆圈渲染速度的一些内容?我已经像上面的链接那样研究了VBO,但是对于这种类型的应用程序,我的对象不断变化的大小,我会收到多少性能提升。
答案 0 :(得分:1)
因为我正在渲染数千个圆圈,调整大小和绘图变得非常慢
只有顶点数组,在拥有10,000个圆圈的英特尔高清显卡3000上,每帧大约需要60毫秒:
// g++ -O3 circles.cpp -o circles -lglut -lGL
#include <GL/glut.h>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
// returns a GL_TRIANGLE_FAN-able buffer containing a unit circle
vector< float > glCircle( unsigned int subdivs = 20 )
{
vector< float > buf;
buf.push_back( 0 );
buf.push_back( 0 );
for( unsigned int i = 0; i <= subdivs; ++i )
{
float angle = i * ((2.0f * 3.14159f) / subdivs);
buf.push_back( cos(angle) );
buf.push_back( sin(angle) );
}
return buf;
}
struct Circle
{
Circle()
{
x = ( rand() % 200 ) - 100;
y = ( rand() % 200 ) - 100;
scale = ( rand() % 10 ) + 4;
r = rand() % 255;
g = rand() % 255;
b = rand() % 255;
a = 1;
}
float x, y;
float scale;
unsigned char r, g, b, a;
};
vector< Circle > circles;
vector< float > circleGeom;
void init()
{
srand( 0 );
for( size_t i = 0; i < 10000; ++i )
circles.push_back( Circle() );
circleGeom = glCircle( 100 );
}
void display()
{
int beg = glutGet( GLUT_ELAPSED_TIME );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -100 * ar, 100 * ar, -100, 100, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 2, GL_FLOAT, 0, &circleGeom[0] );
for( size_t i = 0; i < circles.size(); ++i )
{
Circle& c = circles[i];
c.scale = ( rand() % 10 ) + 4;
glPushMatrix();
glTranslatef( c.x, c.y, 0 );
glScalef( c.scale, c.scale, 0 );
glColor3ub( c.r, c.g, c.b );
glDrawArrays( GL_TRIANGLE_FAN, 0, circleGeom.size() / 2 );
glPopMatrix();
}
glDisableClientState( GL_VERTEX_ARRAY );
glutSwapBuffers();
int end = glutGet( GLUT_ELAPSED_TIME );
double elapsed = (double)( end - beg );
cout << elapsed << "ms" << endl;
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "Circles" );
init();
glutDisplayFunc( display );
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
答案 1 :(得分:0)
ARB_instanced_arrays
- 基于实例化可能是最干净的。
您将拥有一个包含M个顶点的圆圈,您将绘制N次,将每个圆圈的x / y位置,半径和颜色存储为顶点属性并适当地使用glVertexAttribDivisor()
。
会变得更加棘手。你可能不得不深入研究几何着色器。
答案 2 :(得分:0)
其次使用带有glDrawElementsInstanced或glDrawArraysInstanced的实例化数组作为一个干净的解决方案,可以很好地转移到其他类型的几何体。
如果你想/需要坚持OpenGL 2(例如必须在iThing上运行)并且你只需要圆圈,也可以考虑点精灵。每个圆的原点是点顶点值。将半径存储为纹理坐标的S值,曲面法线的X值,等等。启用混合,GL_PROGRAM_POINT_SIZE,可能指向平滑;并编写一个顶点着色器,只需将gl_PointSize设置为您想要的半径。即时圈子。