Sign in to follow this  
majestik_

Accumulative Fragment Shader

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this