Overlapping Shapes

I’m new to Open GL and I’ve been assigned a project where if there are overlapping circles, the overlapping portion must be a specific color (specified by an external source). Is there anyway to specify this or implement this? I’ve played around with glBlendFunc but that’s not doing the trick.
I’ve considered drawing another shape over that area, but the math for that isn’t simple because the circles could dynamically be located anywhere, and have any radius.

glBlendFunc could theoretically used, especially with careful control of the blend mode and blend color.

Alternatively you could look into using a stencil buffer. Then you could clear the stencil buffer to a given value, and increment the buffer when pixels are rendered by the base circles. Then you render a fullscreen quad where the stencil test is set to only render to pixels that have a stencil value of at least 2 (so at least 2 circles are overlapping).

Things get a little trickier if the number of overlaps need to use different colors, but then you could render multiple quads where you update the stencil test to select increasingly higher values, where each quad has a different color.

OK, I will look into stencil buffers. As I’ve stated, I’m pretty new to Open GL, so if you have any examples/snippets or a link that can help me with the stencil buffer implementation that would be extremely helpful.

Thanks.

Yes, you’d have to either “hack” it together with color blending, or use stencil buffers. The basic idea of the stencil buffer is that each pixel has a “counter”. When you draw, you can specify an operation on this counter, like “add 1”, “set to 0” and various other operators, even bit-wise operators. You can use it to count overlaps by setting it to “add 1”.

The stencil buffer obviously worthless if you can’t do things with it after changing values in it. This is where the stencil test comes in. Here you can specify a stencil check, similar to the depth test if you have any experience with that. Things here include only pass if “pixel counter is equal to a specific number”, “pixel counter is less than a specific number”, e.t.c.

I’d do this by drawing all the circles and increase the stencil counter of all covered pixels by 1. When I’m done rendering, I’d draw a quad covering the whole screen with the overlapping color, with the stencil test enabled to only pass if the pixel counter is over 1 (2 or more overlaps).