Use ofGraphics shape as image mask

Hello

What is the simplest way to use ofCircle, ofRectangle etc as mask for underlying ofImage.

I can see how to do with an ofMesh, but I would like to benefit from the simplicity of 2D drawing with ofGraphics (there is no simple “draw circle” function in ofMesh).

Thanks!

do you want to draw a “hole” in an image, ie, draw a circle to mask out an image?

sorry I think I understand…

I usually do this via a shader, you can create an FBO which is the same size as the image, then draw into it what you want to pass through, then use a shader to use the color of one texture as the alpha of the pictures color.

there are also tricks to do this with color masking in opengl –

this (almost a decade) old thread might have some info:

you can also get a mesh for a circle (or any other shape) really eas using ofPath:

ofPath path;
path.arc(x,y,r,r,0,360);
ofMesh mesh = path.getTessellation();

also in 0.9.0 you can set an alpha mask texture to another texture like:

ofTexture tex, mask;
tex.allocate(w,h,GL_RGBA);
mask.allocate(w,h,GL_LUMINANCE); // or GL_RED in opengl 3+
tex.setAlphaMask(mask);

when drawing tex it’ll me be masked by mask using the values in the mask as alpha values. so you can use an ofFbo allocated with GL_LUMINANCE (or GL_RED), draw into it and set it’s texture as mask of another texture

setAlphaMask is really great :slight_smile: so many years of doing this by hand with pixels…

Ok I took the shader way with intermediate fBO, before reading the answer of arturo :smile:
The setAlphaMask is a nice new feature! I will use it next time.