OpenGL绘图数千2D圈 [英] OpenGL drawing thousands of 2D circles
问题描述
在我的应用程序,我使成千上万的(〜10K)的二维圆上的3D场景之上。我有工作,但性能是很慢的时候有这么多的圈子(圈子都很小,〜16像素的直径)。我的code绘制圆的样子:
In my application, I am rendering thousands (~10k) 2D circles on top of a 3D scene. I have it working, but the performance is very slow when there are this many circles (the circles are small, ~16 pixels diameter). My code for drawing the circles looks like:
for ( int i = 0; i < numCircles; i++) {
int attributeMask = GL.GL_DEPTH_BUFFER_BIT | GL.GL_TRANSFORM_BIT
| GL.GL_VIEWPORT_BIT | GL.GL_CURRENT_BIT
| GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT
| GL.GL_ENABLE_BIT | GL.GL_LIGHTING_BIT;
gl.glPushAttrib(attributeMask);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glDisable(GL.GL_LIGHTING);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0d, dc.getView().getViewport().width, 0d, dc.getView()
.getViewport().height, -1, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glDisable(GL.GL_DEPTH_TEST);
Vec4 screenPt // this is calculated for each circle
double size = 16;
gl.glTranslated(screen.x, screen.y, 0d);
gl.glScaled(size, size, size);
gl.glCallList(fillListId);
gl.glCallList(outlineListId);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glPopAttrib();
}
清单会产生如下:
The lists are generated as follows:
NUM_SEGMENTS = 18;
double[][] vertices = new double[2][NUM_SEGMENTS];
for (int i = 0; i < NUM_SEGMENTS; i++)
{
double rad = -2 * Math.PI * ((double) i) / ((double) NUM_SEGMENTS);
double x = Math.cos(rad);
double y = Math.sin(rad);
vertices[0][i] = x;
vertices[1][i] = y;
}
gl.glNewList(id, GL.GL_COMPILE);
gl.glBegin(GL.GL_LINE_LOOP); // 2 lists are actually created, the first time GL.GL_LINE_LOOP IS is used for the outline, and the second time GL.GL_POLYGON is used for the fill
for (int j = 0; j < vertices[0].length; j++)
{
gl.glVertex2d(vertices[0][j], vertices[1][j]);
}
gl.glEnd();
gl.glEndList();
有什么我做显然是错在这里放慢渲染?我应该考虑使用顶点缓冲区在这里?还是有其他的技术来加速这个吗?
Is there something I'm doing obviously wrong here to slow down the rendering? Should I consider using vertex buffers here? Or are there other techniques to speed this up?
谢谢, 杰夫
推荐答案
我想这一切code
int attributeMask = GL.GL_DEPTH_BUFFER_BIT | GL.GL_TRANSFORM_BIT
| GL.GL_VIEWPORT_BIT | GL.GL_CURRENT_BIT
| GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT
| GL.GL_ENABLE_BIT | GL.GL_LIGHTING_BIT;
gl.glPushAttrib(attributeMask);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glDisable(GL.GL_LIGHTING);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0d, dc.getView().getViewport().width, 0d, dc.getView()
.getViewport().height, -1, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glDisable(GL.GL_DEPTH_TEST);
而这一切code
and all of this code
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glPopAttrib();
可以在循环之外进行,而且只进行一次。
could be done outside of the loop, and just done once.
您将只需要推动和弹出一个矩阵对象平移/缩放。
You would just need to push and pop a matrix for the object translate/scale.
这篇关于OpenGL绘图数千2D圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!