Filling intersections with a different color.

Started by Aisaaax, July 20, 2021, 03:57:55

Previous topic - Next topic

Aisaaax

Hello.
I'm drawing a bunch of shapes on the screen with GL_BLEND enabled and GL_DEPTH_TEST disabled. I want places where those shapes intersect in screen space to be filled with a different color.

Can I achieve that without making a second render pass for intersections?

I thought that some clever blending could do the trick, however I discovered that because you can only set up factors for destination and source, and those factors are clamped to [0,1], that means that the mixing is always substractive. i.e. I can dim the color in the src/dest, but I can't make it brighter. So if I start with some color like (0.6, 0.3, 0, 1), which is yellow-ish color, I can't make it bright red, because if I set up factor to (1, 0, 0, 1), at the very most I can get the color (0.6, 0, 0, 1), which is dark red. And I can't make that color blue either, because the starting value is 0, so no matter what I do I can't increase it.

Am I missing something? Is there a way to color the intersections differently during blending or rendering through a single pass? Or perhaps somehow take the end result frame buffer and change certain color in it to a different color?

Thank you.

jakethesnake

Blend only works with the alpha to my knowledge. So you could render your shapes with low opacity and have the intersections become brighter.
I can't think of a simple solution to do what you want. I can only think of rendering two passes, with an int texture as the destination that increments each time you render a pixel. You'd the do another pass where you read from this and pick a color based on the value.

Aisaaax

Actually that gives me an idea. Can I render the first shape to texture, and then feed that texture as an input to the shader when rendering the second shape, and pick the color based on the texture data. Then render the result to the same texture, effectively over-writing it. And then takethat texture and mix it with a framebuffer?

bentoemanuel

I cant post, sorry this is off-topic but every time i try to post i get 500 internal server error, i need help

jakethesnake

I'm not sure, I think this would result in undefined behaviour when reading and writing to the same texture .

check this out, there seems to be a solution:
https://stackoverflow.com/questions/11410292/opengl-read-and-write-to-the-same-texture