0
votes

I've got this problem with clearing texture memory in OpenGL. I'm making a game in Android using OpenGL. Everything works just fine, but what I do is loading all the textures to memory on app start up. I clearly slows down speed of the game - I've got there like 12 monsters each with 10 animation frames 512x512 size plus environement textures - so I guess loading it all to memory at once is not a perfect idea.

What i want to do is a function onLoadLevel(int level) that will clear texture memory and load necessery textures on start of current level.

The problem is HOW TO CLEAR the textures memory in OpenGL? I think about 2 options 1 - clear it all -like deleting all the textures in memory and load everything I need for current level - but only textures for this level - It'll be slower but I guess easier to do? Just to reset the textures memory? 2 - delete just the textures I don't need any more and load next textures.

I have no idea as for now, how to do both of this - how to clear all the textures from memory at once or how to delete just one from the memory - or even if it is possible - I've seen some solutions but it was about pointing the texture id what seems to be a problem since I have no idea what is the texture id?

I'll show You below how I do the texture loading process:

public int mTextureDataHandleSample1;
//then I use loadTexture function from my TextureHelper class
mTextureDataHandleSample1 = TextureHelper.loadTexture(mActivityContext, R.drawable.sample1);
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

and loadTexture function looks like this:

public static int loadTexture(final Context context, final int resourceId)
{
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0)
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;   // No pre-scaling

        // Read in the resource
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();                       
    }

    if (textureHandle[0] == 0)
    {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}

So if You could show me or at least point me to a solution how to clear textures memory or delete just some of the textures previously loaded to memory I will be very grateful.

2

2 Answers

3
votes

So you're looking for a way to delete a texture. You may want to consider the glDeleteTextures function. It takes an array of one or more textures and deletes them.

1
votes

Totally agree with Nicol ¿Bolas? once you generate texture with glGenTextures(), you release memory with glDeleteTextures(), for example in your case, you should has a method for release like so:

public void free(GL10 gl){ gl.glDeleteTextures(1, textureHandle, 0); }

Then when you need to replace texture or re-instantiate the class should invoke the free() method.