1

我正在开发一款游戏(此处为 beta 版),我正在使用我拥有的图像绘制爆炸。随着爆炸的老化,它的大小会增加并变得更加透明,直到它消失为止。

我的问题是这似乎是我的代码的瓶颈。我很容易达到 60FPS,但是当爆炸开始发生时,这很容易下降到 30FPS。如果我只禁用爆炸,我会得到恒定的 60FPS。

我可以用 OpenGL 做些什么来更有效地渲染这些内容吗?

我按顺序绘制它们,我只绑定纹理和顶点一次。

对于优化 OpenGL 调用,我应该注意什么一般的东西?

我的代码是用 Java 编写的,用本机代码重写这部分有帮助吗?

我有一个绘制所有爆炸的主要绘制函数

public void draw() {
    GLES20.glUniform1i(GraphicsEngine.uEnableVortex, 0);
    for (Boom boom : mBooms) {
        boom.drawCloud();
    }
    mHugeBoom.drawCloud();
    GLES20.glUniform1i(GraphicsEngine.uEnableVortex, 1);

    // Prepare the data
    GLES20.glVertexAttribPointer(GraphicsEngine.aPositionHandle, 3,
            GLES20.GL_FLOAT, false, 12, mVerticesBuffer);
    GraphicsEngine.clearShape();

    // Disable textures
    GLES20.glUniform1i(GraphicsEngine.uEnableTextureHandle, 0);

    GLES20.glLineWidth(2.0f);

    for (Boom boom : mBooms) {
        boom.drawSparks();
    }
    mHugeBoom.drawSparks();
}

然后是“繁荣”

public void drawCloud(){
        if (mActive) {
            float pDone = mTotalTime / 600.0f / mSize;
            if (pDone <= 1.0f) {
                mBurst.setColor(mRGBA);
                mBurst.draw(mMMatrix);
            }
        }
    }

    public void drawSparks(){
        if (mActive) {
            for (Particle particle : mParticles) {
                particle.draw();
            }
        }
    }

实物图

public void draw(float[] MMatrix, boolean setColor) {
    // Prepare the triangle data
    GraphicsEngine.setShape(mShape);

    if (mTextureId != -1) {
        GraphicsEngine.bindTexture(mTextureId);
        if (mCustomTex) {
            GLES20.glVertexAttribPointer(GraphicsEngine.aTexCoordHandle, 2,
                    GLES20.GL_FLOAT, false, 8, mTextureBuffer);
            GLES20.glEnableVertexAttribArray(GraphicsEngine.aTexCoordHandle);
            GraphicsEngine.clearTexture();
        } else {
            GraphicsEngine.setTexture(mShape);
        }
    } else {
        GLES20.glUniform1i(GraphicsEngine.uEnableTextureHandle, 0);
    }

    // Apply a ModelView Projection transformation
    GLES20.glUniformMatrix4fv(GraphicsEngine.uMMatrixHandle, 1, false,
            MMatrix, 0);

    if (setColor)
        GLES20.glUniform4fv(GraphicsEngine.uColorHandle, 1, mRGBA, 0);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES,
            GraphicsEngine.getNumIndicies(mShape),
            GLES20.GL_UNSIGNED_SHORT, GraphicsEngine.getIndicies(mShape));

}

防止重新加载已加载纹理的代码

public static void setShape(int i) {
    if (mState.shape != i) {
        GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT,
                false, 12, mShapes[i].vertices);
        mState.shape = i;
    }
}

public static void setTexture(int i) {
    if (mState.texture != i) {
        GLES20.glVertexAttribPointer(GraphicsEngine.aTexCoordHandle, 2,
                GLES20.GL_FLOAT, false, 8, mShapes[i].texture);
        mState.texture = i;
    }
}
4

0 回答 0