Advertisement

Stencil shadow volumes

Started by November 06, 2020 12:30 AM
11 comments, last by taby 4 years, 2 months ago

There are quite a few online resources for stencil shadow volumes. Do you have a favourite code? Right now I have a copy of OpenGL 4 Shading Language Cookbook, and I grabbed the source from the Packt website. It looks really complicated – it requires three shader programs, one with a geometry shader.

Or do you recommend shadow maps, for beginners like me when it comes to shadows?

taby said:
Do you have a favourite code?

No, but i remember Frictional Games being proud on their robust implementation, so maybe that's one more resource to look up: https://github.com/FrictionalGames/HPL1Engine

taby said:
Or do you recommend shadow maps, for beginners like me when it comes to shadows?

Well, anybody has switched to SM, even gfx seniors : )

So why do you consider volumes at all? Do you need pixel perfect shadows?
If so, there is another alternative:
Per SM, for each texel store a list of intersecting triangles (using conservative rasterization).
Then at the shadow lookup you can loop over all triangle and intersect with the ray.

This technique is probably better for detailed geometry, while volumes work better for low poly meshes, but not sure.
NV made it a game works effect, and it has been used for some games, e.g. The Division 2. It's practical for only one light source (son), they say. Related search terms are ‘irregular Z-Buffer’, IIRC.

That said, we are at the dawn of ray traced shadows, so personally i would recommend: Do not waste time on the SM rabbit hole, implement only a simple SM technique, wait some years until everybody has RT GPU, replace with raytraced shadows.
But other people are still into SM, and there's still active research.

Advertisement

Wow, thanks for your time and effort. I decided to try shadow mapping first, since it seems to be simpler. Simple is good when you’re just starting out, right?

I am having a bit of a tough time implementing shadow maps correctly. I am using the source gotten from OpenGL 4 Shading Language Cookbook.

As you can see, it's almost working. Any hints as to what I may be doing wrong?

Looks you have not implemented a bias yet?
One way to avoid this (for now) would be to draw the back faces for the SM, so there is no z fighting form projecting shadow front faces to the same visible front faces.

@JoeJ Those sound like good ideas to try, but I'm not sure how to implement them. :(

Advertisement

You shouldn't necessarily just follow what everyone else is doing (unique approaches are good to), but you should definitely know that stencil shadows have almost completely fallen out of use. They were popular for a while in the 2000's, but shadow maps are now the overwhelmingly popular solution. Off the top of my head, these are the main issues stencil shadows:

  1. Extruding edges is complicated, and can be expensive. Earlier implementations usually did pre-processing and/or CPU processing of meshes, which doesn't scale up very well to higher geometric complexity or things like tessellated/displaced surfaces. Geometry shaders can be used, but can have severe performance issues when used to amplify geometry.
  2. By default they produce pixel-perfect hard shadows, which is nice but it's not straightforward to use that to approximate softer/filtered shadows (and forget about accurate area light shadows with contact hardening)
  3. They're very heavy on stencil fillrate which can hurt at higher resolutions
  4. They don't really offer a path for integrating shadows with ray-marched volumetrics
  5. I'm not sure if there's a good way to handle alpha-tested geometry

Thanks for the information.

I'm still stuck with the shadow maps. Any ideas? :(

Edit:
removed non-working code.

These 3 lines are ultra-important:

vec3 lightPos = vec3(10.0f, 10.0f, 10.0f);  // World coord

lightFrustum.orient(lightPos, vec3(0.0f), vec3(0.0f, 1.0f, 0.0f));
lightFrustum.setPerspective(45.0f, 1.0f, 1.0f, 25.0f);

Apparently I can't place the light too far from the models. I was using vec3 lightPos = vec3(100.0f, 100.0f, 100.0f) and that wasn't working. Glad to know what the problem was.

The whole code is at https://github.com/sjhalayka/opengl4_stlview_shadow_map

This topic is closed to new replies.

Advertisement