Shader blending ( Get previous pixel )

Started by
15 comments, last by haegarr 10 years ago


gl_FragColor = (behindpixel * 0.5 ) + (tex * 0.5 );

What you're trying to do is pretty much standard alpha blending. GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA, then output 0.5 for your alpha component.

I'm not sure exactly what you're trying to explain with your picture above, but it kind of seems like you want the areas around the tree leaves to be completely transparent? The same alpha blending flags will work for that too - except that you'll still be writing to the depth buffer for those completely transparent pixels, so they'll block stuff from being drawn behind them.

In that case, what you're looking for is alpha testing, not alpha blending.

Advertisement

The one reason why i really wanted to get that pixel what is behind.
Tried all those blending flags what are in the page you gave.

You have to disable the depth buffer for this:



GL_CHECK(glDisable(GL_DEPTH_TEST));
GL_CHECK(glDepthMask(false));

and afterwards reenable



GL_CHECK(glEnable(GL_DEPTH_TEST));
GL_CHECK(glDepthMask(true));

Never use the zbuffer with alpha-blended stuff, it just doesn't work.

EDIT:

You can actually leave on GL_DEPTH_TEST and just set glDepthMask to false, otherwise your alphablended stuff will probably overlap your opaque geometry. Also, if you have a texture that is eigther fully opague and on some pixels fully transparent, you can just leave z-buffering like normal and just use alpha-testing by adding this line to your shader after the texture read:


if(tex.a <= 0.0001f)
discard;

Thank you all, it is working!
I still have one more question. Does the directx allow me to do that what i asked here?

( that getting pixel behind the thing ... )

Never worked with directx but after reading some source code and information from internet i did not found anything about saying yes or no.
More like no because the thing what im looking for doesnt exists maybe or i just dont know how do search because i dont really know how its called what im looking for.

It's still unclear exactly what feature you're talking about, but all the things talked about on this thread (alpha blending, alpha test, render targets) are supported in DirectX, so I'm sure you'll be able to do pretty much the same thing.

What if i render all the opaque triangles, create texture a from rendered screen,

then render transparent triangle, create texture b from screen where that one triangle were rendered and then

render quad with the texture a blended with b.

( so i have then control over the pixels what are behind the transparent object, i can control everything )
I will continue this thing with the ohter transparent triangles, one by one.
Need to find a way how do i know that the transparent triangle is or should be visible but thats really small problem right now and im having some ideas yet they are useless right now.

How bad is this idea?
Should i do it or find the other way?

Thank you all, it is working!
I still have one more question. Does the directx allow me to do that what i asked here?

( that getting pixel behind the thing ... )

Never worked with directx but after reading some source code and information from internet i did not found anything about saying yes or no.
More like no because the thing what im looking for doesnt exists maybe or i just dont know how do search because i dont really know how its called what im looking for.

If I understand correctly, you're asking if there's any way to do a fully programmable blend. Sadly the answer is no. There's a very full explanation here: http://fgiesen.wordpress.com/2011/07/12/a-trip-through-the-graphics-pipeline-2011-part-9/ (halfway down, "Aside: Why no programmable blend"), but basically, it's a restriction of the GPU design rather than a restriction imposed by OpenGL/D3D. In fact, the entire http://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/ series is rather excellent, and recommended reading to all.

Actually, I believe fully programmable blends are possible on certain PowerVR chips, for instance the one used by the Vita, but that uses a very different architecture to your desktop GPU.

Ultimately, the standard fixed function blend options, while limited, are really very powerful, and you should make every effort to fully understand what can be achieved by them before thinking about ping-ponging between buffers as suggested in your last post.


AFAIK fragment shaders cannot read framebuffers directly

Oh well, today I stumbled over an extension for OpenGLES, originating from Apple but nowadays being an EXT extension that allows the fragment shader to read the active framebuffer at the current fragment location:

EXT_shader_framebuffer_fetch

Perhaps not being an option for the OP (and obviously not needed anyhow), it shows that actually a programmed blending is possible on some platforms.

This topic is closed to new replies.

Advertisement