简体   繁体   English

OpenGL绘制了数千个2D圆圈

[英]OpenGL drawing thousands of 2D circles

In my application, I am rendering thousands (~10k) 2D circles on top of a 3D scene. 在我的应用程序中,我在3D场景上渲染了数千(~10k)2D圆圈。 I have it working, but the performance is very slow when there are this many circles (the circles are small, ~16 pixels diameter). 我有它工作,但是当有这么多圆圈时(圆圈很小,直径约16像素),性能非常慢。 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? 还是有其他技术可以加快速度吗?

Thanks, Jeff 谢谢,杰夫

您可以生成一个圆圈,将其blit到纹理,然后只创建用该圆圈纹理化的(两个三角形)曲面。

I would think all of this 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);

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. 您只需要为对象平移/缩放推送和弹出矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM