Problems displaying transparent textures with OpenGL ES on Android
I've invested now more than a day looking for a solution on how to render transparent textures with OpenGL on Android. Obviously others had more luck than I but after trying various solutions (e.g. Transparent texture in OpenGL ES for Android) offered I still cannot get it done.
Here is my basic setup:
gl.gl开发者_Go百科Hint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, -ratio, ratio, -1, 1);
The textures are loaded via ...
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);
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);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
... and there are hints that this leads to problems due to pre-multiplied alpha. But ReplicaIsland does use the GLUtils function as well and the game does not seem to have problems with transparent images.
Now if I draw textures with alpha channel (PNG) they appear solidly black. Textures without alpha are drawn correctly in their respective color). I applied various glBlendFunc calls without luck, i.e.
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
I also gave this one a try
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
The result is always that the textures are no visible at all (fully transparent?).
Images for testing are used from the ReplicaIsland project, these are standard PNGs. This also means that the textures used conform to the power-of-2-rule - another solution offered in various posts. I see one difference which is that ReplicaIsland seems mostly to rely on the draw_texture extension whereas I have to use VBOs (in order to be able to apply rotations).
Help would be greatly appreciated! Thanks Ben
Ok, another day of debugging goes by and finally I found my error. It had nothing to do with the color scheme of the bitmap, glBlendFunc
or something like this.
I turned out to be a sloppy use of GL_FIXED
and GL_FLOAT
in two texture function calls.
精彩评论