   Multiple lights and Shadow volumes...
  hi all,I''m trying to (for learning purposes) put together a simple lighting demo - probably something that will get added to as/when I learn more techniques. This is being done with MS D3D9.Currently I''ve got shadowing working with a single light source, I use the following algorithm (may not be the best, but it works!):1. Render all geometry2. Clear Stencil Buffer3. Extrude shadow caster geometry (done in SW, no GPU work)4. Render shadow volume to stencil buffer   - increment Stencil on clockwise tri''s   - decrement Stencil on counter-clockwise tr''s5. Render a full-screen Black quad to the frame buffer where Stencil==1Basically I stole it from the DX-SDK, so whilst it aint my work as such I understand it / gave myself a pat on the back Now, I''m wondering about implementing a system with more than 1 light. I haven''t been able to find a simple n-step   guide (like I wrote above) about how to do this; I''ve got the basic idea - it involves doing multiple passes, but the grand-scheme of things just aren''t clicking I understand that I''ll need a shadow volume extruded for each object and each light (1 object, 3 lights = 3 volumes / 10 objects, 2 lights = 20 volumes). I gather that I''ll also have to render as many passes as there are lights - or at least, that many passes to the stencil buffer. what I really dont get is how the final part (rendering a black(??) quad to the screen) will work  ..Should each quad drawn be the same colour and add/subtracted - so as where there are more shadows its darker than where theres only 1?as a template algorithm:1. render the scene as normal to the z/colour buffer2. generate shadow volumes for all lights/objects3. For each light in the scene:   a. clear the stencil buffer   b. render the relevent shadow volumes (to stencil)   c. draw quad to color bufferis that how its done?any hints/tips/confirmation?? cheers,Jack   DirectX 4 VB: All you need for multimedia programming in Visual BasicFormula 1 Championship Manager, My Game Project. 
  You''ve got the beginning correct, not the end. You no longer use a full-screen quad. You should enable the stencil test, so that the test passes only outside the shadow. Enable additive blending (since light is additive), and add the contribution of the light to the scene. You''re right that you''ll need as many passes as you have lights.Y.;  ah, so if I understand correctly... when using multiple lights you do the opposite of the last part?instead of darkening the shadowed areas, you lighten the non-shadowed areas..?if I have that right, surely the ''1. render the scene as normal to the z/colour buffer''part is wrong? because the lights contributions will already have been calculated/stored in the colour buffer?Or, do you have to render first with Z writes, no colour... then for each subsequent pass (after stencil writes) render the whole scene with only that light enabled?sorry for my stupidity ! Jack ;  No, you render the scene with an ambient light, or just clear everything to black if you have no ambient (Doom3). But yeah, you''ve got it: you light non-shadowed areas.Y.;  You might be interested in this:http://www.beyond3d.com/interviews/carmackdoom3/ -* So many things to do, so little time to spend. *-;quote:yeah, you''ve got it: you light non-shadowed areasgreat so, as a revised algorithm - something like this? ::1. render the entire scene to the depth buffer (no textures, only ambient lights)2. For each light in the scene a. clear the stencil buffer b. generate/render shadow volumes to stencil buffer c. render the entire scene with ONLY the current light enabled, and ONLY render where stencil==0, and when rendered use addition for colour.That should in theory allow any number of lights to be rendered in the scene, and presumably it also gets around the typical only-8-hardware-lights at a time limit on the hardware?If someone could just confirm that proposed algorithm I''ll get around to trying it out cheers,Jack DirectX 4 VB: All you need for multimedia programming in Visual BasicFormula 1 Championship Manager, My Game Project. ;  Yes.This algorithm is explained, with a source code and a demo, both on NVidia''s developer site, and there:ClickyLook into the "research project" section, the two articles about per-pixel lighting.It exactly implements that.It''s a bit old now, as it still uses register combiners for Geforce2 support, but it''s exactly the same theory with vertex and pixel shaders.Y.;  cheers, am having a look at those sites now...Jack 