setAlphaMask example failing


I would like to mask a picture before drawing it onscreen. I copied the code suggested in this thread

but my picture gets drawn with random pixels on it, like this:

and the console prints this error: [ error ] ofFbo: FRAMEBUFFER_UNSUPPORTED

All of this with oF0.9.3, xCode 7.3.1 and OSX 10.11.5

Any idea about what I could be doing wrong?

PS the code for my test is here if anyone wants to give it a try emptyExample (33.5 KB)


Hey, I re-wrote your code so it alpha masks with a circle. The main difference is that I am calling setAlphaMask after drawing into the fbo.

void ofApp::setup()
    //fbo.allocate(img.getWidth(), img.getHeight(), );
    fbo.allocate(img.getWidth(), img.getHeight(),GL_RGBA);
        ofDrawEllipse(100, 100, 50, 50);

void ofApp::draw()
    img.draw(100, 100);

If you want to do this with a shader in future it may be helpful to check out the alpha masking example (examples/shader/05_alphaMasking)

Mask image with a ofPath shape

Thank you so much, now it works!

Comparing your solution with mine (that’s not really mine, I took it from an older example and the code is basically the same of the how-to here) the two important points are

  1. the FBO must be allocated with GL_RGBA
  2. the background must be black and the mask (where the image will be seen) white (or gray)

Fixing these two things made the trick. And, of course, there is no need to redraw the fbo every frame so it should be drawn once in setup()

And yes it could be done with shaders but this way is much simpler (once you got it working), pulling a shader would be overkill (unless masking with setAlphaMask were done via CPU pixel manipulation, but I hope this is not the case).

Thanks again, you made my day :slight_smile:


While trying to use an image as mask I also noticed that the experimented behavior is different from what I was expecting.

The background must not be black but transparent, and the mask must be white where you want to see the pixels. And, of course, the size of the mask and the one of the image being drawn must match.


Thanks to both of you for this thread! I have been reading and experimenting on this topic on and off for a few days now, and this is the first place I have found amongst several threads and the How-To that actually show a working example that really clips to the shape, and does the other steps needed to actually draw it. The example on the HowTo should, I think, be edited to include this information. I’ve only been learning OF recently or I’d feel I should go ahead and edit it…