PDA

View Full Version : OpenGL ES 2.0 Enabling Alpha Buffers for Destination Blending



hhenderson
01-23-2015, 02:38 PM
I am working with the OpenGL ES 2.0 Platform on Android and I am having difficulty using the destination alpha blending functions because I dont think I am requesting an alpha buffer when I create the GL context.

I am trying to use a black and white texture to mask a foreground texture so that certain parts of the foreground texture do not appear and the background texture is blended into the foreground texture.

The Method I am using is as follows:


glEnable(GL_BLEND);
// Use a simple blendfunc for drawing the background
glBlendFunc(GL_ONE, GL_ZERO);
// Draw entire background without masking
drawQuad(backgroundTexture);
// Next, we want a blendfunc that doesn't change the color of any pixels,
// but rather replaces the framebuffer alpha values with values based
// on the whiteness of the mask. In other words, if a pixel is white in the mask,
// then the corresponding framebuffer pixel's alpha will be set to 1.
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
// Now "draw" the mask (again, this doesn't produce a visible result, it just
// changes the alpha values in the framebuffer)
drawQuad(maskTexture);
// Finally, we want a blendfunc that makes the foreground visible only in
// areas with high alpha.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
drawQuad(foregroundTexture);

But it seems the blendfunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); does not work because I cannot blend the destination alpha with android without requesting an alpha buffer when I make the context.

Alfonse Reinheart
01-23-2015, 03:18 PM
There's no such thing as an "alpha buffer" to my knowledge. If you're talking about having an alpha channel in the current framebuffer, then you're correct. You actually need to have a destination alpha before you can do anything useful with it. And, unless you're rendering to an FBO, you'll need to create your OpenGL context with an alpha channel.

Dark Photon
01-23-2015, 07:14 PM
Right. Specifically, if you're talking EGL, you need to allocate an EGLSurface (window/pbuffer/etc.) using an EGLConfig that has an alpha channel, and bind that to your context.

hhenderson
01-25-2015, 04:08 PM
Thank you guys! So I am still having the same problem but I am fairly certain that I am using an EGLConfig that has an alpha channel now! Which is one step closer. So what I did was call the function:



setEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize);

//Actual Code Used
setEGLConfigChooser(8, 8, 8, 8, 16, 0);


And I used an alphaSize of 8 bits similar to the size of the RGB Channels. I also called the function before I set my Renderer.

But the problem I am having is that OpenGL doesn't seem to be blending the textures as I would expect. This is my code and textures as of right now:



glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
//Draw Background Elements
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
//Draw Mask Texture: I draw this the same as the other elements. Not sure if I should call different draw functions since this is just a mask, but I am under the impression that the glBlendFuncSeperate(); should take care of everything.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//Draw Foreground Texture


Background Texture: 1605

Shadow Mask Texture: 1606

Foreground Texture: 1607

Current Result: 1609

Final Expected Result: 1608

I know it would probably be easier to use a custom shader to calculate the mask, but I will need to move the mask around to create the shadow illusion. So I want to simply draw the mask every frame in a new position instead of dealing with the position on the shader side since I am very unfamiliar with writing shader code.

Thanks Ahead of Time!