OpenGl帧缓冲性能问题

时间:2014-01-31 21:42:04

标签: java opengl 2d jogl framebuffer

我目前正在尝试为我在JAVA中开发的游戏创建一个简单的照明引擎。我正在使用帧缓冲对象将光源的图像绘制到环境色上,并将其与先前渲染的场景组合以获得简单的光照。一切都运行得很好,除了帧速率减半,从稳定的60 FPS到波动的30 FPS。

这是我目前的做法:

(这在我的主显示循环中完成)

        int[] params1 = new int[1];
        gl2.glGenTextures(1, params1, 0);
        int textureID = params1[0];
        gl2.glBindTexture(GL.GL_TEXTURE_2D, textureID);
        gl2.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
        gl2.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl2.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, Game.RES_WIDTH, Game.RES_HEIGHT, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, null);

        int[] params2 = new int[1];
        gl2.glGenFramebuffers(1, params2, 0);
        int frameBufferID = params2[0];
        gl2.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, frameBufferID);
        gl2.glClear(0);
        gl2.glFramebufferTexture2D(GL2.GL_DRAW_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, textureID, 0);

        DrawUtils.fillRect(0, 0, Game.WIDTH, Game.HEIGHT, getAmbientLight()); 

        gl2.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_DST_ALPHA);    

        //Code for drawing the light sources here...

        gl2.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0);
        gl2.glBlendFunc(GL2.GL_DST_COLOR, GL2.GL_ONE_MINUS_SRC_ALPHA);

        gl2.glEnable(GL2.GL_TEXTURE_2D);
        gl2.glPushMatrix();
        gl2.glColor3f(1.0F, 1.0F, 1.0F);
        gl2.glBindTexture(GL2.GL_TEXTURE_2D, textureID);
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glNormal3i(0, 0, 1);
        gl2.glTexCoord2i(0, 0);
        gl2.glVertex2i(0, 0);
        gl2.glTexCoord2i(1, 0);
        gl2.glVertex2i(0 + Game.WIDTH, 0);
        gl2.glTexCoord2i(1, 1);
        gl2.glVertex2i(Game.WIDTH, Game.HEIGHT);
        gl2.glTexCoord2i(0, 1);
        gl2.glVertex2i(0, Game.HEIGHT);
        gl2.glEnd();
        gl2.glPopMatrix();
        gl2.glDisable(GL2.GL_TEXTURE_2D);

        gl2.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);

        gl2.glDeleteTextures(1, params1, 0);
        gl2.glDeleteFramebuffers(1, params2, 0);

1 个答案:

答案 0 :(得分:0)

将帧缓冲区创建移出我的循环确实解决了问题:

public void init(GL2 gl2)
{
    int[] params1 = new int[1];
    gl2.glGenTextures(1, params1, 0);
    textureID = params1[0];
    gl2.glBindTexture(GL.GL_TEXTURE_2D, textureID);
    gl2.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
    gl2.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
    gl2.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, Game.RES_WIDTH, Game.RES_HEIGHT, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, null);

    int[] params2 = new int[1];
    gl2.glGenFramebuffers(1, params2, 0);
    frameBufferID = params2[0];
    gl2.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, frameBufferID);
    gl2.glFramebufferTexture2D(GL2.GL_DRAW_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, textureID, 0);

    gl2.glBindFramebuffer(GL2.GL_DRAW_FRAMEBUFFER, 0);
}