Accumulative Fragment Shader

Started by
4 comments, last by rewolfer 13 years, 6 months ago
I'm trying to write a fragment shader that performs
accumulation of colors as the render progresses.
ie I render a first polygon in the background
with a color c0=(0.5,0,0) then another polygon
in the front of it with a color c1=(0,0.5,0)
and I would like the final output
to be c0+c1=(0.5,0.5,0)

But I don't seem to be able to get the previously
computed fragment value.
Is there any way to get it ?
Advertisement
Have you tried enabling glEnable(GL_BLEND) and setting the blend function to glBlendFunc(GL_ONE,GL_ONE)

This will add a new fragment's colour to the colour currently at that location. That way you don't even need to read the current colour within the fragment shader as blending is done after the fragment shader.
well i omitted to say that the addition is kind of a special case,
I need to do a max for R, min for G and add for B...
so basically i need to get the color value ...
Ergh.. i see

Well I can only think of using an FBO. Render your background to a texture attached to the FBO. Then bind that texture and read it in your fragment shader when drawing your foreground. Hopefully you're not wanting to have multiple layers of backgrounds because you'd need a pass for each.
yes well, i would need LOTS of layer i think :)
there's no real bg/fg polygons , just lots of them
and I want to apply it to all fragments ...

but i guess i might be out of luck on this one
You could alternatively perform three renders per primitive. The first render you setup glBlendFunc and glBlendEquation to the operations you need for the red channel ; as well as setting glColorMask for GL_TRUE only for red channel.
Then you do a similar thing for green and blue.

Unfortunately it requires three renders per primitive, but I think it should get the job done and is definitely faster than the FBO technique

This topic is closed to new replies.

Advertisement