2
votes

I am a starter of Iphone opengl ES programming. I have two textures, the first one is the background and occupies the full screen. I am printing the second picture on top of the first image but the white background of the second image covers part of the background. I want the background to be visible where the foreground picture has no color(or White). I am unable to figure out how to use the glBlendFunc correctly.

before printing the second image I am using Blending with following:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

Images at: http://www.flickr.com/x/t/0097002/photos/vjv2010/

1
make sure you call glEnable(GL_BLEND) before rendering anything. Also, unless your texture is semitransparent, you can use alpha test to cut out pixels instead of alpha blend. Alpha test will be faster.SigTerm
@SigTerm: I was thinking that too, but Apple documents it can adversely affect performance.. maybe blending is faster on iPhone?Stringer
@Stringer Bell: Yes, blending is pretty slow (you can even bring PC to knees with excessive alpha blending), but to use alpha, you'll have to either enable alphaBlend (glEnable(GL_BLEND)), or enable alphaTest(glEnable(GL_ALPHA_TEST)). Otherwise, alpha channel will have zero effect. If you don't need semi-transparent surfaces, alpha test is all you'll ever need. There is a way to simulate semi-ransparency with alphatest+dithering or glPolygonStripple, but a small screen (iPhone) it is bound to look very ugly.SigTerm
@SigTerm, about glPolygonStripple it's not part of OpenGL-ES AFAIK. On the other hand you have glSampleCoverage which could be use from faking transparency! khronos.org/opengles/documentation/opengles1_0/html/…Stringer

1 Answers

0
votes

I want the background to be visible where the foreground picture has no color(or White).

You have two choices, the simplest one is to use alpha testing and add an alpha channel for your foreground image so that each white pixel (or no color zones) has a special value, let' say 0.0f and all other 1.0f.

Then when rendering the foreground you enable alpha testing with glEnable(GL_ALPHA_TEST) and set alpha function with glAlphaFunc(GL_GREATER, 0.5f) This will accept/draw only fragments that have an alpha value greater than 0.5f and discard/not draw all fragments that have an alpha value less than 0.5f.

Second choice is to use blending as you did, but you will need also to add extra alpha channel to your foreground and set glBlendFunc properly.

More here: http://iphone-3d-programming.labs.oreilly.com/ch06.html