Search code examples
iphoneopengl-esmaskingblending

Masking in OpenGL ES


reading #20 tutorial on Nehe and a question here about masking, I implemented masking using b/w image and opaque image, however the result is completely not what is expected (I'm using OpenGL ES 1 on iPhone, so can't use shaders).
Source+Mask=Result
As can be seen on the result picture what is white on mask isn't transparent and what is black isn't source color, but white (!) Here is the source code I use to render it:

    // we render from one big texture using coordinates
    // so changing rectangle will change the drawn image
    _pressedFrame = maskRect;
    glBlendFunc(GL_DST_COLOR,GL_ZERO);
    [super render:sender];

    _pressedFrame = normalRect;
    glBlendFunc(GL_ONE,GL_ONE);     
    [super render:sender];

Thanks for any help!


Solution

  • For the technique from the NeHe tutorial to work correctly, the background of your source image (i.e. anywhere where the mask image is white) needs to be full black. Although it’s not obvious from your first image exactly what the source is, I suspect that it isn’t black in the places that are brightening in the final image.

    However, doing masking this way isn’t necessary in OpenGL ES 1.1. You can render an image with an alpha mask in a single pass using multitexturing. Furthermore, if your mask and your image are always drawn together, you’re better off just baking them into a single RGBA texture—no multitexturing required.