I'm porting a game from WP7 XNA to Android; everything was working using Canvas but it was too slow so I've switched to OpenGL ES, following the examples of Replica Island and SpriteMethodTest.
My problem is that my textures aren't being coloured or made transparent when I set glColor immediately before drawing the texture - it is completely ignored and the texture is drawn in the state it was loaded. All the colour and transparency information inherent in the texture itself is applied correctly. I'm also generating some bitmaps programatically using setPixels and I have exactly the same issue there.
I'm still at the "OpenGL is just magic spells" stage of understanding :-( which makes following the existing documentation, which seems to be aimed at mid- or high-level coders, rather frustrating. I'd hoped that copy/pasting from those samples would work, but I must have missed something and I can't work out what it is. I've got to the point where I'm code-blind and can't tell the difference between their code and mine.
glColor4 clearly works in Replica Island, when I run that in the same emulator all the text fades in and out when the opacity changes, just as it should. But it doesn't work in my code :-(
Here is my GL initialisation code:
surfaceCreated: gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 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.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); sizeChanged: gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f); gl.glDisable(GL10.GL_DEPTH_TEST); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glShadeModel(GL10.GL_FLAT); gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); gl.glEnable(GL10.GL_TEXTURE_2D);
I commented out disabling Dither and Lighting in case that was it... but no. It makes no difference.
Bitmaps are loaded from resources thus:
gl.glGenTextures(1, mTextureNameWorkspace, 0); textureName = mTextureNameWorkspace; 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_REPLACE); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, srcBitmap, 0);
And finally my draw code:
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName); gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop(src), 0); gl.glColor4f(tint.R(), tint.G(), tint.B(), tint.A()); ((GL11Ext) gl).glDrawTexfOES(dst.left, screenHeight - dst.bottom, 0.0f, dst.width(), dst.height());
The range of the tint members is 0.0..1.0 as it should be. If I set an invalid texture:
...then OpenGL obligingly draws rectangles of the right shade and opacity in the right location.
I must have tried every combination of possible parameters for glBlendFunc - some of them partly affect the transparency, but none of them work properly and none of them cause the glColor tint to have any effect.
All I'm trying to do is the equivalent of:
PorterDuff.Mode mode = PorterDuff.Mode.MULTIPLY; if (tint.A() < 1.0f && tint.R() == 1.0 && tint.G() == 1.0 && tint.B() == 1.0) mode = PorterDuff.Mode.DST_IN; Paint paint = new Paint(); PorterDuffColorFilter cf = new PorterDuffColorFilter(tint, mode); paint.setColorFilter(cf); canvas.drawBitmap(texture.bitmap, srcRect, dstRect, Paint(tint));