Not logged in, Join Here! or Log In Below:  
News Articles Search    

Submitted by Jon Olav Bjorndal, posted on December 05, 2000

Image Description, by Jon Olav Bjorndal

This is an image from my OpenGL demo called "LadyDeath and the Light Sources of Doom", or just "LightDemo" for short.

It is a windowed standard OpenGL application. No DirectX or extensions involved. Main feature is shadow volumes for global shadows using stencil buffer. Other than that relatively straight forward OpenGL lighting. Draws whole scene several times for each light source and uses blending to combine the results of each pass. No rough culling is involved. Only Z buffer. If used in a larger scene an additional rough culling algo would be needed.

Editor's Note: Download the demo + source code here: (428k)

Image of the Day Gallery


Message Center / Reader Comments: ( To Participate in the Discussion, Join the Community )
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

December 05, 2000, 02:07 PM

I'm too lazy to download the source, so I don't know if you answer it in there, but: Are you doing anything to combine the edges of your polygons in the shadow volume, or are you putting out 2+n polys per n-gon? Or are you taking the wimpy (and slow and inaccurate) GLU tesselator approach that Mark Kilgard advocates?


December 05, 2000, 02:10 PM

Here we go, first ever IOTD comment!
Nice screenshot you got there. Does your shadow volume technique using the stencil buffer is inspired by Mark killgard's (NVidia) document? I haven't red much about shadow volumes but, wouldn't it be possible to blur a little your shadows so you'd get soft shadows?


December 05, 2000, 02:14 PM

Francis: I've done what I consider a fair amount of work in shadow volumes (nothing pioneering, mind you), and aside from having a whole bunch of lightsources, there's no easy way to do any sort of blurring with the stencil buffer, since it's just an on-off bitmask (it doesn't modulate anything, the fragment is just on or off based on matching a bit operation in the stencil). So you can't just blur them or anything. :/

Francis Shanahan

December 05, 2000, 02:47 PM

For what it's worth, it looks great, nice job.


December 05, 2000, 02:49 PM

wow, looks nice nice.. but, i dont even try to run it. my ati currently says it doesnt has a stencilbuffer.. so it runs at bout 1 fps, and (i'm pretty sure) i cant even see a shadow..

have you read the nice demo with the depthbuffer-shadow with alphablending? they even look nice smoothed sometimes:) its really nice and it runs even smooth on my ati.. (ok.. in sometimes i ressieve a geforce 2 mx.. so there is no big problem, i think:)) then i test it and, i think my answer is just.. great:)

The Wolf

December 05, 2000, 04:03 PM

Nice shot, sexy model


December 05, 2000, 04:08 PM

Your screen is nice, but when I downloaded the demo, its a zip called that contains two zips, one of which is called which itself contains a



December 05, 2000, 04:15 PM

Okay, I have a complaint.
I downloaded your demo, and ran it. I got about 1 frame every 10 seconds, so I went to close it. When I closed the application, my computer rebooted.

I have a PIII 1GhZ, w/128 MB RAM and a GeForce3 GTS with 64MB DDR.
45 Gig HD, w 32G free.


December 05, 2000, 04:35 PM

"GeForce3 GTS"?


December 05, 2000, 04:37 PM

hehe, oops
GeForce2 GTS


December 05, 2000, 04:50 PM

Are you using a LODed down model for the lighting? the shadow looks very very very lowrez..... however it looks great!!! nice shot

John van der Burg

December 05, 2000, 04:56 PM

It looks nice!
But why are you rendering your scene multiple times?
Wouldn't it be cheaper to just render it once and rendering an alpha blended polygon on top of the in the masked stencil (shadowed) areas?

That wouldn't work with overlapping shadows probably (so, not that it gets even darker if you have overlapping shadows), but still, you don't need to render the scene multiple times (except the shadows, which you can do with silhouettes)

Maybe I understood wrong what you are doing.
But I know that there is some document from NVidia around, which describes how to do the stencil shadows, and they do render the scene multiple times, even for 1 light, doing some zbuffer trick and disable lighting in one of the passes, etc.
But that's probably slower as the way I am thinking of.

Anyway, however it's done, it looks nice :)

- John


December 05, 2000, 05:50 PM

Does it draws correct shadows when the camera is inside the shadow volume?


December 05, 2000, 06:33 PM

I just ran your demo . . looks really great with shadows moving all over... really neat, even :)

My desktop runs 1280x960x32 and when the demo starts it runs ok, but when maximized it runs fairly slow. Then i changed my desktop to 1024x786x16 hopeing for better performance. BUT i got a framerate of something like 0.3 fps and the shadows were really messed up. They looked like the whole world's z-buffer problems stuffed into one little demo :)
So I guess the desktop should be set to 32-bit in order to run correctly?

My system:
Athlon-650, 256MB ram, GeForce256 32MB DDR, WIN98

Soren Seeberg / ZeeGate


December 05, 2000, 06:42 PM

I have a P3-500, GeForce2 GTS (Asus7700), 128MB RAM and every thing ran fine under Win2k. You probably have the latest drivers from NVidia (Detonator3)?


December 05, 2000, 07:02 PM

Make sure you have your desktop set to 32-bit.

Soren Seeberg / ZeeGate


December 05, 2000, 07:09 PM

Thanks Zeegate. That turned out to be my problem too. I switched over to 32-bit and the demo ran nicely. Man, does she have a big ass.


December 05, 2000, 07:56 PM

It's looking very well. Nice work

Any idea how to fast cast shadows on quad tree based terrain ? with/without stencil

For the people that run the demo ..and got only 0.000001fps .....don't forget , that stencil buffer is available only when in 32bit color depth !!!
It's true for NVIDIA chips .. must true for the other brands ..i suppose.

Aproposito ...any know how to perform fast RAY collision detection on quad tree terrain ?


December 05, 2000, 08:02 PM

kewl, np :)

yup . . .nice big ass :)

Soren Seeberg / ZeeGate


December 05, 2000, 08:57 PM

I have been working on shadow volume stuff myself too once, but my problem was that I could not solve the camera-in-the-shadow problem, which is caused by the fact the the near plane clips the shadow volume. I made some quick hacks which included clipping the polygons by hand and convex hull code, but the solution was ugly and slow.

After browsing the source some time I couldn't be sure of the solution you used. The DetailObject class has a method IsInShadow, but it always returns false (zero). So I guess the correct answer is hidden somewhere between the rendering code :) Could you shed some light on the solution.

Anyway very nice stuff (both your Solace engine and this code).




December 06, 2000, 06:01 AM

A sexy POLYGON MESH with a big ass?
that's not even funny...that is as sick as a virtual girlfriend, come on!

oh..nice screenshot btw


December 06, 2000, 06:46 AM

Stencil-buffer shadows can be done by rendering an alpha blended poly over the screen. This works pretty well, and multiple lights can be combined by setting the colour of the screen poly depending on the light source colour. Hence 1 quad per light-source.

But, This is not technically correct, as objects in shadow will still have specular highlights (darkened though) whereas they shouldn't have.

This is why it is more correct to render the scene WITH lighting for stencil values out of the shadow and WITHOUT lighting for values in the shadow.

I'm personally using the 1 quad per lightsource method as it is quicker and still looks nifty :)

John van der Burg

December 06, 2000, 07:47 PM

Yeah exactly.
The gamer won't notice that there is specular inside the shadowed areas anyway. And even when it's there. Who carez :)
As long as it looks good and is faster, I think that's the way to go :)

- John

Jon Olav Bjørndal

December 07, 2000, 10:32 AM

Hi folks,

I will try to answer some of the questions:

Yes, I do something to combine edges. Notice that only edges that have one poly facing the light and one not facing the light need to generate SW surfaces. The others will either not generate any shadows at all (both polys facing away from lightsource), or generate 2 sw faces that eliminate each other (both polys facing the lightsource).

I have read Mark Kilgards document about Shadow Volumes. That was not the first place I read about it, but AFAIK I am using the algo described there.
As fluffy wrote, hard edges are a weakness if this algo.

Yeah, I know the model is sexy. Thank Paul Steed (previously) of id Software!

I don’t do any LODing, but when the shadows get large the relatively low poly resolution of a Q2 model gets very visible, of course.

For the world polys, the algo draws correct shadow volumes when the camera is inside the volume. But not for the model. You should be able to see errors because of that sometimes. But I found out it was not worth the performance hit to get rid of. When the camera is inside a volume you need to initialize the stencilbuffer to the number of volumes you are inside. Say if you are inside 5 volumes, all the values in the stencilbuffer must be initialized to 5. I do that by switching off the z-test and rendering all the volume faces.
Here is a sketch of that part of the algoritm:
1.Set culling to cull frontfaces. Set stencilfunc to add. Switch off z-test.
2.Draw shadow volumes.
3.Set culling to cull backfaces. Set stencilfunc to subtract. Switch off z-test.
4.Draw shadow volumes.
5.Set culling to cull backfaces. Set stencilfunc to add. Switch on z-test.
6.Draw shadow volumes.
7.Set culling to cull frontfaces. Set stencilfunc to subtract. Switch on z-test.
8.Draw shadow volumes.

Now it works even if you are inside the shadow volumes. It is an expensive algo though.

This thread contains 24 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.