I've been reading up on this tutorial: Tutorial 41: Multiple Light Shadow Mapping (rastertek.com) from RasterTek on how to do multiple light shadow mapping. They do it in a pretty brute force style. Ie. they decided to add a second shadow casting light so they're just adding everywhere a lightPos2
, lightViewMatrix2
, lightProjectionMatrix2
, shadowTexture2
, … 2.
I have already implemented shadow mapping for 1 light source in a way that is satisfactory to me. I was in search of a way to expand this, by using multiple shadow casting lights and just have them “add” their contributions to the finalColor
of the final Pixel Shader. How do I do this? Particularly in the Shader side.. I believe I know how to do it in C++ side. But how to have the shader code, the vertex shader and pixel shaders in particular. How should they accumulate all these shadow & light contributions without having to hardcode how many lights, and textures, and constant buffers and parameters there would be (because I don't know beforehand - it depends on the scene and how many lights I add to it).
To elaborate further.
- The current state of my Vertex Shader outputs a
posLightSpace
variable, which is basically (as you'd have guessed) the vertex pos in the light source's space. But this is only 1 light source. How do I factor in multiple light sources? - The current state of my Pixel Shader gets that
posLightSpace
variable which I use tocalculateShadowLevel()
and I useSamplerComparisonState
SampleCmpLevelZero
with hardware PCF to do the sampling from the single shadow mapTextureCube
. Later I proceed to use that single's Light source vectors to do my lighting calculations. So this is a single shadowMap, a single light source parameters/vectors. How do I factor in multiple light source contributions and multiple shadow maps?
I should note, if it hasn't been clear, that I'm using forward rendering. I haven't progressed into deferred territory yet.
I must be missing a critical step, I can't believe the messy RasterTek's article approach should be the only way here.
How do I make the shader code scale to multiple sources?
I hope I made myself clear, if not let me know. Help please. Thank you.
Using DirectX, HLSL, C++.