Platform: Android Version: 2.2 and above Acceleration: OpenGL ES 2.0 Language: Java IDE: ADT - Eclipse
I am trying to utilize OpenGL ES 2.0 to rapidly accelerate the drawing of 2D Sprites for a game I'm creating, however I am not getting the speed I need from OpenGL ES 2.0 for Android. Below is my code related that is related to drawing in OpenGL. When using the profiler most of the time per frame is being taken up by the Draw method.
Also, because of the nature of the game CreateModelMatrix() has be be called each frame before drawing because the objects are constantly moving and rotating.
How can I optimize the Draw method to make it more effecient?
void Create(float[] ModelData, float[] CoordData)
{
//Count Verts
Verts = ModelData.length / 3;
//Create Buffer outside Java VM
FloatBuffer ModelBuffer;
ModelBuffer = ByteBuffer.allocateDirect(ModelData.length* BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
ModelBuffer.put(ModelData).position(0);
FloatBuffer CoordBuffer;
CoordBuffer = ByteBuffer.allocateDirect(CoordData.length* BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
CoordBuffer.put(CoordData).position(0);
//Create OpenGL Buffer
final int buffers[] = new int[2];
GLES20.glGenBuffers(2,buffers,0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, ModelBuffer.capacity() * 4, ModelBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, CoordBuffer.capacity() * 4, CoordBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,0);
VertHandle = buffers[0];
FrameHandle.add( buffers[1] );
//Set empty for Garbage Collection
ModelBuffer.limit(0);
ModelBuffer = null;
CoordBuffer.limit(0);
CoordBuffer = null;
ModelMatrix = new float[16];
Matrix.setIdentityM(ModelMatrix, 0);
//ModelMatrix[3] = 0.5f;
}
void CreateModelMatrix() {
Matrix.setIdentityM(ModelMatrix, 0);
Matrix.translateM(ModelMatrix,0,x,y,0.0f);
Matrix.rotateM(ModelMatrix, 0, rot, 0.0f, 0.0f, 1.0f);
Matrix.translateM(ModelMatrix,0,-this.OriginX,-this.OriginY,0.0f);
}
void Draw()
{
//final int stride = (POSITION_DATA_SIZE + NORMAL_DATA_SIZE + TEXTURE_COORDINATE_DATA_SIZE) * BYTES_PER_FLOAT;
// Pass in the position information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VertHandle);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, POSITION_DATA_SIZE * BYTES_PER_FLOAT, 0);
// Pass in the texture information
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, FrameHandle.get((int)Frame));
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINATE_DATA_SIZE, GLES20.GL_FLOAT, false,
TEXTURE_COORDINATE_DATA_SIZE * BYTES_PER_FLOAT, 0);
// Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
//Pass the Model Matrix
GLES20.glUniformMatrix4fv(mMMatrixHandle, 1, false, ModelMatrix, 0);
//Set Texturing
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
GLES20.glUniform1i(mTextureUniformHandle, 0);
// Draw the cubes.
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, Verts);
}