How would you use alpha masks with FBOs to make holes in an image?

Hello oF People,
I’m trying to create a perforated ‘layer’ through which I’ll see a background layer.

Conceptually, it’s like this:

  1. Make random array of circles to use as the holes
  2. Subtract the circles from a plane to create the perforated plane (I was assuming I could use an alpha mask to do this) Here’s an example hole mask
  3. Draw this to the screen above a background image
  4. See background image through holes Here’s the two composited together, it’s dead easy in Photoshop

I don’t want to use a pre-made image for the alpha mask because I want to be able to vary the hole size, quantity and position.

Should I be using an FBO for the behind image and for the perforated plane? Do I need a third FBO, like this?
FBO 1: plane
FBO 2: array of circles
FBO 3: background image

Should I be using glBlend to composite them together?

I’d be grateful for any suggestions of where to go next.

Take a look at examples/gl/AlphaMAskingShaderExample, it does a very similar operation.

1 Like

I’ve done this before by using glColorMask to draw just in the alpha plane of an FBO (with glColorMask, you can disable drawing to certain color channels, and draw just to alpha, for example), but this is also super easy to do with a shader and as @drakko mentions, there’s an example of this that comes with OF. I typically do this now with shaders, having one image or fbo that’s for the color info and another that’s for the alpha info and using a shader to mix them together as I draw.

1 Like

Thanks guys, that gives me a couple of new things to work through. I’d investigated the AlphaMaskingShader example but hadn’t clocked that it was relevant.