PDA

View Full Version : Replacing textures



DannyG
12-06-2010, 10:40 AM
Hi,

for an Android OpenGL app I have some quads which are textured based on a user configuration. If the configuration changes, the textures need to change accordingly. I wrote a function to load these:

private void loadTextures(Bitmap[] resources) {

mGL.glDeleteTextures(mTextures.length, mTextures, 0);
mTextures = new int[resources.length];
mGL.glGenTextures(resources.length, mTextures, 0);

Matrix flip = new Matrix();
flip.postScale(1f, -1f);

for(int i = 0; i<resources.length; i++){

Bitmap bmp = Bitmap.createBitmap(resources[i], 0, 0, resources[i].getWidth(), resources[i].getHeight(), flip, true);
resources[i].recycle();

mGL.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[i]);

mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

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

bmp.recycle();
}
}

The first time, the textures load fine, but a second call to it turns the screen white.

Am I overlooking something?

DannyG
12-06-2010, 01:45 PM
After some debugging I found out that the first time, the mTextures array consists of [1, 2, 3]. Every subsequent call sets it to [0, 0, 0], so it seems glGenTextures is having trouble assigning id's? glGetError() traces 3766488 (after more debugging this seems to be an arbitrary number, therefore gluErrorString returns NULL)

mhagain
12-06-2010, 04:10 PM
You should probably delete[] mTextures before recreating it.

DannyG
12-06-2010, 04:50 PM
How would I go about doing that?

My guess is that I don't even have to recreate the int[] as the glGenTextures should overwrite the values? (you tell it to offset from 0). Of course the array would contain unused values if the next call would contain less textures, but in my situation there's always 3 textures.

Im really stumped by the value glGetError returns.. it's like the function is totally broken..

mhagain
12-07-2010, 04:36 AM
How would I go about doing that?Before:
mGL.glDeleteTextures(mTextures.length, mTextures, 0);
mTextures = new int[resources.length];
mGL.glGenTextures(resources.length, mTextures, 0);
After:
mGL.glDeleteTextures(mTextures.length, mTextures, 0);
delete[] mTextures; // <----------- added
mTextures = new int[resources.length];
mGL.glGenTextures(resources.length, mTextures, 0);
You should definitely recreate the int as otherwise you're building in an assumption that your value of resources.length is always the same. It may be smaller, it may be larger, and if you ever come to reuse this code you'll be bitten by it.

DannyG
12-07-2010, 05:14 AM
As stated it's true the resources.length is always the same in this case, however I do agree on the reuse part. Though delete[] is not recognized by Eclipse as being a valid java statement.

mhagain
12-07-2010, 05:32 AM
Though delete[] is not recognized by Eclipse as being a valid java statement. Ooops, I missed that bit.