PDA

View Full Version : Using the stencil buffer..



AltF4me
01-20-2012, 02:18 PM
Hello,
I've managed to use the stencil buffer to create a simple reflection, however I've decided to use it for clipping my snow particle system within a globe. However, at certain viewing angles it doesn't appear quite correct. As you can see in the video below, the snow lands as a square, but I don't want this to be visible. Any ideas? Thanks!.

http://youtu.be/wssS6NS-vEY

Ilian Dinev
01-20-2012, 05:49 PM
With a shader it would be simple: if the particle's Y (relative to the grass plane) is 0.0f, set the particle's alpha to 0 (alpha-test or blending required), or point_size=0.

With fixed-func, basically you want the particles to be hidden/discarded by the depth-buffer. Because there's no way to hide with stencil tests only those that are both at 0.0f Y and outside the grass disc.

Before drawing all the stuff you already do, add this:

1) clear depth+color+stencil
2) set stencil to replace_always to 0x2
3) draw the podium
4) disable color-write, set stencil to keep_always, fail if stencil == 2
5) draw a box (6 quads) with sizes [radius*4 : radius*4 : 0.02f], centered on the podium. Its color won't be visible, but it'll set such depth, that later it will discard all geometry whose Y is -0.01f ... 0.01f (relative to the podium). Those are the snowflakes you don't want. Except, it won't discard anything that's with stencil-value 2 (the particles on the podium)
6) disable depth-testing, disable color-writing, set stencil to replace_always with 0x1
7) draw the globe, to which you limit snowflakes (as you already have done in that video)
8) enable color-writing, enable depth-testing, disable depth-writing, set stencil to keep_always, fail if stencil != 0x1
9) draw snowflakes

10) enable depth-writing, clear depth+stencil, disable color-writing, disable stencil
11) draw the podium again. (color-writing is still disabled, we're just doing this for re-laying depth to a sane value, basically removing that box out of existence, as it'll be troublesome later)
12) enable color-writing;
13) draw everything else (the globe's glow etc) naturally now. (the Z-buffer has info that only the podium exists in the scene now, while the color-buffer shows the podium and nicely-clipped snowflakes on/above it)

Phew, seemed shorter when I imagined it. :)

AltF4me
01-21-2012, 06:01 AM
Wow the fixed func way seems a bit tedious and a little confusing :P. I have some shaders already but they're quite simple as I'm still very new to GLSL. I'm wanting the snow to land on the ground so setting the alpha value to 0 when hitting the ground would make it disappear altogether, which I don't want. Is there a simpler way using a shader? If not I will have a go with fixed func.

Thanks

AltF4me
02-02-2012, 07:04 AM
Bump :-)

I'm wanting the snow to land on the ground and visible, so simply setting the alpha to be 0 in the shader isn't ideal as this would make all snow disappear...

Ilian Dinev
02-04-2012, 03:12 AM
Re-read my post, as it's that long in order to handle that case.