Jump to content
  • Advertisement
Sign in to follow this  
thomas3711

Stencil shadows + Per pixel lighting in shader

This topic is 3094 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

Hello, this will be very silly question indeed, but I've been looking on the internet for about week now, without any success... I've got stencil shadows implemented (without shaders) and per pixel lighting shader implemented. And I want to put it all together...but my problem is that I don't know how to acess stencil buffer in my lighting shader and I don't want to pass it as some texture, or do I have to ? Or is there some way to bypass stencil buffer and use the shadow volumes in my per pixel lighting sgader ? So can someone please explain this to me or point me to some sources where it is. Thanks !

Share this post


Link to post
Share on other sites
Advertisement
The stencil process should work regardless of the shader. What API are you using, and how are you drawing?
If you just do the stencil pass as before, but bind your shader instead of setting the fixed-function states before drawing the visible light, what errors do you get?

Provided that you do it the normal way, 1 draw ambient, 2 draw stencil occluders, 3 draw additive lighting on top of the ambient. If that's not how you do it, you might want to try it this way. You are talking about shadow volumes right?

Share this post


Link to post
Share on other sites
Hmm...thanks for hints, this got me thinking...

I do steps 1.) and 2.) in my shadow implementation (yes we are talking about shadow volumes).

Hmm...maybe the problem will be that I do the complete lighting computation (ambient+diffuse+specular so on) in my per pixel lighting shader. Well so how do I do that 3.) step ? Wouldn't it be enough to turn on my lighting shader on the 1.) ambient pass ?

maybe Iam too confused, please stay with me...

p.s. I use OpenGL ...

Share this post


Link to post
Share on other sites
The best way is generally to first draw your scene with a dark color, then the shadow volume, and then draw the light color where the stencil test says there are no shadows. This is generally the correct way, as you add light to wherever the light reaches. It also works nicely for multiple lights, you just redraw the shadow volumes from the next light, and add the light from it on top of the previous light. (Use additive blending, GL_ONE, GL_ONE).
So if all your shadows should be at color (0.5, 0.5, 0.5) for example, you first draw the whole scene with (0.5, 0.5, 0.5) ambient lighting, then the shadow volume, then with additive blending draw the actual light on top with stencil testing, so that it's only added to places that are not in shadow.

Share this post


Link to post
Share on other sites
but this leads us to my first question...how to determine where to draw light and where not to...I don't have stencil buffer information in shader.

So I won't be able to blend the lighting only on places where I want it to. And to make this more complicated, in my lighting shader I compute bump mapping and parallax mapping ... so I want to add these effects on shadowed areas as well.

hmm...

or maybe I can do something like this:
Create a shadow mask -> grey where the shadow is (or shade of grey depending on number of shadows from multiple lights) and white where the light is supposed to be and then blend it over lighted scene...perhaps by passing it to shader...but wouldn't this be an overkill ? or is this completely stupid idea?

anyway thanks a lot for helping!

Share this post


Link to post
Share on other sites
Quote:
Original post by thomas3711
but this leads us to my first question...how to determine where to draw light and where not to...I don't have stencil buffer information in shader.
You don't need the stencil information in the shader. Set up the shader test to discard all pixels with the stencil bit set, and then the shader will only render where the stencil bit is not set.

Share this post


Link to post
Share on other sites
Quote:
Original post by thomas3711
hmmm and how do I do that ? (I use OpenGL an Cg)
glEnable(GL_STENCIL_TEST), and then figure out what glStencilFunc() you need...

Share this post


Link to post
Share on other sites
hmm...yeah that is how to enable stencil testing...

you mean that shader will be applied only on those areas that pass stencil testing ? the others will not ?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!