我正在绘制大约 30 - 80 个带纹理的平面(正方形) - 背景、玩家、敌人、子弹等。
所有平面都移动和缩放,有些 + 旋转,有些有动画纹理。
我认为,对于 CPU 或 GPU 来说,这并不太难,但在较慢/较旧的设备上,它的性能相对较低——例如在 Galaxy ACE 上。
拜托,你能看看我的代码,我在做什么错或脏吗?或者可以优化什么?
谢谢你。
这是我的 onSurfaceCreated、onSurfaceChanged 和 onDrawFrame:
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);
}