我正在制作游戏(beta here),我正在使用我拥有的图像进行爆炸。随着爆炸的增加,它会逐渐增大,并且在它消失之前变得更加透明。
我的问题是,这似乎是我的代码瓶颈。我很容易达到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;
}
}