New demo

Veteran

I've been experimenting a little the latest days with stencil shadows and Carmacks reversed algoritm. I've got it working quite well now and have uploaded a demo of it on my site. It's got dynamic per-pixel lighting with with proper shadow overlapping. Source is included too for those interested. I'll try to move this stuff into my primary project to get some shadows there too. Will probably kill framerate though

Veteran

Carmacks reversed algoritm is a way to implement stencil shadows. The convential method is to use volumes that stretch to infinity, and then increment the stencil when you enter the volume (front facing polygons) and decrement it when you leave (back facing polygons) if the depth test passes. If you end up with zero you're not in the shadow, otherwise you are. It's quite intuitive. However, it has the problem that it doesn't work if you're within the shadow volume, and to make it work properly you'd need to cap the volume to the near clipping plane, which is a quite complex task and prone to glitches.
Carmacks reversed on the other hand takes a similar but not as intuitive approach, it's sort of doing everything the other way. When the depth test fails for back facing polygons you increase the stencil, and decrease for front facing polygons. This way the problem is moved from the near clipping plane to the far clipping plane, but if you're using attenuated light you can just cap the shadow volumes far enough where no shadows from that light would go. This makes this approach much simplier to implement and is more robust. It's was described in one of nVidia's whitepapers. Here's link where Carmack himself describes hes algorithm:

ModeratorVeteran

Carmacks reversed algoritm is a way to implement stencil shadows. The convential method is to use volumes that stretch to infinity,

Click to expand...

..I always used to limit them for fill rate reasons...

and then increment the stencil when you enter the volume (front facing polygons) and decrement it when you leave (back facing polygons) if the depth test passes. If you end up with zero you're not in the shadow, otherwise you are. It's quite intuitive. However, it has the problem that it doesn't work if you're within the shadow volume,

Click to expand...

I'm just going to be pedantic here for the sake of the listeners. It's easy enough to adjust if the camera is "entirely" inside the volume. The problem Humus is describing occurs if the front image/clipping plane is only partially inside the volume.

and to make it work properly you'd need to cap the volume to the near clipping plane, which is a quite complex task and prone to glitches.

Click to expand...

Actually there are two easy ways to "cap" a clipped volume. One was described by Deipenbach in his Phd Thesis - this simply duplicates every post-clipping polygon and 'flattens' the copy onto the front clipping plane.
The second method is cheaper and is used in Elan - only the chopped off sections are modified. I think that method will be public soon.

Carmacks reversed on the other hand takes a similar but not as intuitive approach, it's sort of doing everything the other way. When the depth test fails for back facing polygons you increase the stencil, and decrease for front facing polygons. This way the problem is moved from the near clipping plane to the far clipping plane, but if you're using attenuated light you can just cap the shadow volumes far enough where no shadows from that light would go. This makes this approach much simplier to implement and is more robust. It's was described in one of nVidia's whitepapers. Here's link where Carmack himself describes hes algorithm:

I haven't read that, but it seems like an interesting approach. I suppose as long as you stick to either "indoor" type lights or can guarantee a "perpendicular" cut-off point for "sunlight" it should be ok. Obviously you'd have to make sure that your far clip plane is further away than any of your volumes' far cap/limits or else you'd be back to the same problem as the near plane clipping , but I suspect that wouldn't be much of a limitation.

Veteran

Try redownloading it. I've just updated it. There was a bug causing it to call the glBlendFuncSeparateEXT() function instead of the normal glBlendFunc(), and since nVidia doesn't support the GL_EXT_blend_func_separate it caused a NULL pointer exception. I've fixed that now though.

Anyway, do you guys get very bright screen on GF3/4? I was sent a screenshot that clearly indicates it's very bright on GF3's, but this time it can't be the same as the extreme brightness bug in my previous demo.

ModeratorVeteran

"Obviously you'd have to make sure that your far clip plane is further away than any of your volumes' far cap/limits or else you'd be back to the same problem as the near plane clipping , but I suspect that wouldn't be much of a limitation."

This is the crux of the reversal basically you take the problem at the near plane and turn it into a problem at the far plane where it's easier to basically ignore the issue.
Geforce 4's allow you to clamp Z/W values at the near or far plane in order to make the capping problem easier. Which in effect will give you something similar to what you described as the Elan solution.

ModeratorVeteranSubscriber

It is also supported under GeForce 3, but unfortunately there's no way to use it under Direct3D 8.

The new whitepaper from NVIDIA talking about pushing far plane to infinity looks promising. I'll try to modify my omnidx program (which uses preset far clip plane and shadow volume length) with that method if I have some free time.

Veteran

looks great, works fine as long as aniso is off, just like with your other proggy. (on my GF3)
I get framerates from ~50 to ~65, hovering around ~55
with 2xAA, i get framerates from ~38 to ~50, hovering around ~43

Veteran

I tried it on my Kyro II and it turned my monitor off, what resolution is this app set for by default?

Click to expand...

1024x768x32 @ 85Hz.
It'll pick the closest available if it's not supported and priorities bpp first, then width, height, and last refreshrate.

When you say "turned off", does that mean the monitor actually turned off or did it just go black? It can just be that it goes into software mode and you'll have to wait like 20 seconds for the first frame to appear.

ModeratorVeteran

The new whitepaper from NVIDIA talking about pushing far plane to infinity looks promising. I'll try to modify my omnidx program (which uses preset far clip plane and shadow volume length) with that method if I have some free time.

Click to expand...

Are you saying that Nvidia are now suggesting moving the far clip plane to infinity? That's been done on on PowerVR since before PCX1.
I wrote a white paper on this ages ago which shows that you only lose a fraction of a bit of Z accuracy (i.e. insignificant) but save clipping and projection costs. Apparently this is available on the 'devlopers' part of the DevRel site.

ModeratorVeteranSubscriber

The main reason to put far clip plane to infinity in that whitepaper is to extrude and cap the shadow volume at infinity. This solves the far plane clipping problem. The downside is that it works only with perspective projection, and may waste some extra fill rate.

Unfortunately, there is no way to specify W component of a pre-transform vertex in Direct3D 8 without using a vertex shader. This is a bit annoying.

About Us

Beyond3D has been around for over a decade and prides itself on being the best place on the web for in-depth, technically-driven discussion and analysis of 3D graphics hardware. If you love pixels and transistors, you've come to the right place!