24

And then have display correctly? An example would be having a round ball in a rectangle while being able to see another texture in the background.

edit: At the moment, when I load the texture the transparent pixels from the source image are displayed as black.

leppie
  • 115,091
  • 17
  • 196
  • 297
Dimitris
  • 682
  • 3
  • 14
  • 19

2 Answers2

31

For iPhone and N95 this works:

If you are loading texture from raw data, set internal and source format to GL_RGBA.

glTexImage2D(GL_TEXTURE_2D, 0, 
    GL_RGBA,
    textureWidth,
    textureHeight, 
    0, 
    GL_RGBA,
    GL_UNSIGNED_BYTE,
    pointerToPixels);

And when rendering, enable alpha blend:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
Virne
  • 1,205
  • 11
  • 11
  • That works thanks! As a followup, how would I increase texture transparency after this? – Dimitris Mar 04 '09 at 16:23
  • 2
    Use color modulation. glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); Then set alpha component of the color to desired. glColor4f(255, 255, 255, alpha); – Virne Mar 04 '09 at 17:10
13

The answer provided by @Virne is correct, and I was able to use it for Android devices with a few small modifications. The myImage object I used is a standard .png image with transparency.

I created the texture using this:

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

And (like Virne), I enabled alpha blend when rendering:

gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
theisenp
  • 8,639
  • 5
  • 39
  • 47
  • If you are decoding the bitmap before setting it with texImage2d, don`t forget to specify that is a ARGB bitmap.: BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bitmap, opts); – jonathanrz Sep 18 '13 at 17:36