Jump to content
  • Advertisement
Sign in to follow this  
Kimau

OpenGL OpenGL: Using shaders to solve depth test / alpha problem

This topic is 4557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I'm just getting to that stage of pulling my hair out. Can anyone tell me how or point me in direction of a solution. Can you use shaders to compute alpha to avoid having to do depth sorting. The reasons I ask is I have a particle engine and I want to add alpha. How do I do it? Is this a stupid method? What should I be using? thanks in advance ;)

Share this post


Link to post
Share on other sites
Advertisement
Given my severe lack of knowledge when it comes to fragment shaders (hooray for GF2s), I think it's possible. You'll need to check the fragment depth against the new one to determine if the new fragment's infront or behind the old one, and scale the colour appropriately (scale the old colour down if the new fragment's infront, or scale the new colour down if it's behind), then add the results (or multiply them, depending on what you're trying to achieve). Then again, I could be horribly wrong, but I don't feel like writing up something to test it when I'd have to wait around for a few minutes to see anything worth reporting...

[EDIT]
Don't forget that a fragment that hasn't been written to could hold any colour/depth value, so make sure you clear them (or use a skybox or something for the colour value).

Share this post


Link to post
Share on other sites
Sadly, not really possible.

The good news is that if you're using additive alpha, you don't need to sort (the math works out okay either order). Otherwise you're stuck with sorting (using frame coherence, the end result isn't actually that horrific), or just ignoring the artefacts (which is an entirely valid solution :) ).

The only 'shader driven' solution for fixing the depth-order involves using depth-peeling.. but doing 200 passes to depth-peel correctly isn't really feasible.

Allan

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also remember to turn off Z-write, if you're gonna do the no-sort strategy.

Share this post


Link to post
Share on other sites
So can you tell me how to do alpha with shaders and no sort then... or link to url.


Thanks

Share this post


Link to post
Share on other sites
1. disable Z write
glDepthMask(FALSE);
2. disable lighting (not needed for most particles)
glDisable(GL_LIGHTING);
3. enable alpha
glEnable(GL_BLEND);
4. set additive blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

5. draw particles

6. reset to default z, lighting and blending values.

Share this post


Link to post
Share on other sites
So then how do I manage the shader to do the alpha calcs..... is that all I do?
Isn't there more to it. Cause thats my code atm... without any depth sorting and such.

Share this post


Link to post
Share on other sites
not really; on particles you're usually going to hit fillrate limitations pretty quickly, so you want to keep any clever stuff you do in the PS to a minimum.

The previous poster's usage of depth-writing probably wouldn't work (at least not by any docs I've read), so your best bet is to avoid anything that would negatively affect performance.

If you want to do other kinds of modulation, note that his will look a bit weird. You'll need to do a sort by distance from camera to make things work.

It's not magic, sadly... :)

Allan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!