我正在繪製大約30-80個紋理平面(正方形) - 背景,球員,敵人,子彈等。
所有飛機移動和縮放,一些+旋轉和一些動畫紋理。
我認爲,CPU或GPU不能太難,但在較慢/較舊的設備上,它的性能相對較低 - 例如在Galaxy ACE上。
請你看看我的代碼,我在做什麼錯或髒?或者什麼可以優化?
謝謝。
這是我onSurfaceCreated,onSurfaceChanged和onDrawFrame:OpenGL ES 2.0 - 性能/最優化需要?
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
..
// load and prepare all textures
..
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
GLES20.glDisable(GL10.GL_DITHER);
GLES20.glDisable(GL10.GL_LIGHTING);
final float eyeX = 0.0f; final float eyeY = 0.0f; final float eyeZ = 1.5f;
final float lookX = 0.0f; final float lookY = 0.0f; final float lookZ = -3.0f;
final float upX = 0.0f; final float upY = 1.0f; final float upZ = 0.0f;
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
mProgramHandle = GLES20.glCreateProgram();
final String vertexShader =
"uniform mat4 u_MVPMatrix; \n"
+ "attribute vec4 a_Position; \n"
+ "attribute vec2 a_TexCoordinate; \n"
+ "attribute float a_AlphaValue; \n"
+ "varying vec2 v_TexCoordinate; \n"
+ "varying float v_AlphaValue; \n"
+ "void main() \n"
+ "{ \n"
+ " v_TexCoordinate = a_TexCoordinate; \n"
+ " v_AlphaValue = a_AlphaValue; \n"
+ " gl_Position = u_MVPMatrix * a_Position; \n"
+ "} \n";
final String fragmentShader =
"precision lowp float; \n"
+ "uniform sampler2D u_Texture; \n"
+ "varying vec2 v_TexCoordinate; \n"
+ "varying float v_AlphaValue; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = texture2D(u_Texture, v_TexCoordinate); \n"
+ " gl_FragColor.a *= v_AlphaValue; \n"
+ "} \n";
final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position", "a_TexCoordinate", "a_AlphaValue"});
GLES20.glUseProgram(mProgramHandle);
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
}
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width/height;
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 1, 1000);
}
public void onDrawFrame(GL10 glUnused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
updateGame();
// all planes are saved in Vector<Mesh> children
int size = children.size();
for (int i = 0; i < size; i++)
{
children.get(i).Draw(renderer);
}
}
而在 '平面' 對象繪製(渲染):
public void Draw(Renderer renderer)
{
if (!Visible) return; // no visible object, no need draw
mTextureCoordinateHandle = GLES20.glGetAttribLocation(renderer.mProgramHandle, "a_TexCoordinate");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBrickDataHandle);
mAlphaHandle = GLES20.glGetAttribLocation(renderer.mProgramHandle, "a_AlphaValue");
GLES20.glVertexAttrib1f(mAlphaHandle, alpha);
// texture animation
renderer.mPlaneTextureCoords[(byte)indexAnim].position(0);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, renderer.mPlaneTextureCoords[(byte)indexAnim]);
indexAnim++;
renderer.mPlanePositions.position(0);
GLES20.glVertexAttribPointer(Renderer.mPositionHandle, renderer.mPositionDataSize, GLES20.GL_FLOAT, false, 0, renderer.mPlanePositions);
GLES20.glEnableVertexAttribArray(Renderer.mPositionHandle);
if (angleZ == 0) // no rotating plane -> no need setRotateM
{
Matrix.setIdentityM(renderer.mModelMatrix, 0);
Matrix.translateM(renderer.mModelMatrix, 0, x, y, z);
Matrix.scaleM(renderer.mModelMatrix, 0, scaleX, scaleY, 1);
}
else // rotating plane
{
float[] mt = new float[16];
Matrix.setIdentityM(mt, 0);
Matrix.translateM(mt, 0, x, y, z);
Matrix.scaleM(mt, 0, scaleX, scaleY, 1);
Matrix.setRotateM(mRotZMatrix, 0, angleZ, 0, 0, 1);
Matrix.multiplyMM(renderer.mModelMatrix, 0, mt, 0, mRotZMatrix, 0);
}
Matrix.multiplyMM(renderer.mMVPMatrix, 0, renderer.mViewMatrix, 0, renderer.mModelMatrix, 0);
GLES20.glUniformMatrix4fv(renderer.mMVMatrixHandle, 1, false, renderer.mMVPMatrix, 0);
//Matrix.multiplyMM(renderer.mMVPMatrix, 0, renderer.mProjectionMatrix, 0, renderer.mMVPMatrix, 0);
Matrix.multiplyMM(renderer.mTemporaryMatrix, 0, renderer.mProjectionMatrix, 0, renderer.mMVPMatrix, 0); // little bit faster (?)
System.arraycopy(renderer.mTemporaryMatrix, 0, renderer.mMVPMatrix, 0, 16);
GLES20.glUniformMatrix4fv(renderer.mMVPMatrixHandle, 1, false, renderer.mMVPMatrix, 0); // pass in the combined matrix
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}
非常感謝您的回覆。我修復了你寫的所有錯誤/錯誤,除了第2點。 - 至少目前爲止, 也許現在重製當前項目已經太晚了...所以,我尋找TexturePacker,我會考慮這個改變:-) – Commanche