OpenGL Blend Question

Started by
4 comments, last by gold 18 years, 3 months ago
Hi all, I'm new to OpenGl so please bear with me. I'm trying to clear out alpha in a section of the frame-buffer and then later draw to only sections where alpha is 1. I'm using OpenGL ES 1.1 which doesn't allow masking of individual color channels. So I figured I'd accomplish this by drawing sprites.

// Clear Alpha Pass
// Sprite color = (0,0,0,1)
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
Does that look okay so far? I'm testing it by drawing another sprite over it which should only draw in areas where the frame buffer has an alpha of 1. So I drew the sprite with 0,0,1,0:

// Test Pass, nothing should show up
// Sprite color = (0,0,1,0)
glBlendFunc(GL_DST_ALPHA, GL_ONE);
The problem is this sprite draws when I think it shouldn't. Am I wrong? If I change the src factor in my test pass to GL_ONE_MINUS_DST_ALPHA, then the sprite doesn't draw...almost like its the reverse of what I expect. Thanks, BennyW
Advertisement
instead of glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR); try
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);... that's the blend function most commonly used, i hope that helps.
I'm using blending to clear out the alpha channels in a region of the frame-buffer not to write color values. Basically creating a mask of sorts.

I'm finding that if I draw a test sprite with this:
glBlendFunc(GL_DST_ALPHA, GL_ONE);

on a screen I just cleared, it shows up. It shouldn't though, right? B/c the dest alpha should be 0 which should effectively cancel out the color contribution of my test sprite.

[edit: added]
As I test, I tried this with the nehe tutorial that just draws a colored triangle and a quad.
This isn't perhaps the most efficient manner for masked rendering. The stencil buffer might be more suited to your needs. If you are just trying to render a masked sprite, you can build the alpha mask right into the sprite texture and use the alpha test - no blending is required, and it can be done in a single pass. Of course blending can smooth the edges if you have coverage values other than zero and one.
Not sure I understand what you're saying.

I can't build the alpha mask into the sprite, b/c this mask could potentially change per-frame due to occlusion. I need the alpha values to modulate with a glare-sprite. I'll try to think up some new ways.

I'm starting to think that there is no alpha channel in my frame buffer. Any pixels I read back always have an alpha of 1 despite the fact that I clear it to 0.
When you create the window and choose the pixel format, make sure you request alpha bits as well as color bits. Also make sure your desktop is 32-bit, not 24. I'm not sure I understand exactly what you are trying to do, would the stencil buffer be an appropriate solution?

Edit - I just noticed you are using OpenGL ES. You may not have the ability to request alpha planes, depending on your device. Hopefully stencil planes are supported.

This topic is closed to new replies.

Advertisement