OpenGL: Using shaders to solve depth test / alpha problem
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 ;)
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).
[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).
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
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
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.
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.
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.
Isn't there more to it. Cause thats my code atm... without any depth sorting and such.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement