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?
thanks
Davide

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

1 Like

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()
{
    
    img.load("picture.jpg");
    
    //fbo.allocate(img.getWidth(), img.getHeight(), );
    fbo.allocate(img.getWidth(), img.getHeight(),GL_RGBA);
    
    
    fbo.begin();
    {
        ofClear(0,0,0,0);
    }
    fbo.end();
    
    fbo.begin();
    {
        ofClear(0,0,0,0);
        //ofBackground(255,255,255);
        ofSetColor(255);
        ofDrawEllipse(100, 100, 50, 50);
    }
    fbo.end();
    
    img.getTexture().setAlphaMask(fbo.getTexture());
}

//--------------------------------------------------------------
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)

5 Likes

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:

2 Likes

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.

1 Like

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…

1 Like