Trick
Trick

Reputation: 3849

Effective clean at opengl animation in android?

My Renderer looks like this:

public void onDrawFrame(GL10 gl) {
    if(draw) {
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glFrontFace(GL10.GL_CW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                textures[i]);

        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        bitmap.recycle();
    } else {
        gl.glDeleteTextures(textures.length, textures, 0);
        gl.glFlush();       
    }
}

Is this all that I can do to clean after animation is over? I use this renderer all the time and after a few minutes I always get out of memory.

Upvotes: 0

Views: 308

Answers (1)

datenwolf
datenwolf

Reputation: 162297

I use this renderer all the time and after a few minutes I always get out of memory.

You're not supposed to create all the textures anew every frame. You create them once and reuse them. If you want to update the picture while keeping format and dimensions use glTexSubImage2D on an existing texture object.

Upvotes: 1

Related Questions