
主要是用模板緩沖區來實現。
DepthStencilState StencilShadow
{
DepthEnable = true;
DepthWriteMask = ZERO;
DepthFunc = LESS;
StencilEnable = true;
StencilReadMask = 0xFFFFFFFF;
StencilWriteMask = 0xFFFFFFFF;
FrontFaceStencilFunc = ALWAYS;
FrontFaceStencilPass = INCR;
FrontFaceStencilFail = Keep;
BackFaceStencilFunc = ALWAYS;
BackFaceStencilPass = DECR;
BackFaceStencilFail = Keep;
};
BlendState AdditiveBlending
{
AlphaToCoverageEnable = FALSE;
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA ;
DestBlend = INV_SRC_ALPHA ;
BlendOp = ADD;
SrcBlendAlpha = ZERO;
DestBlendAlpha = ZERO;
BlendOpAlpha = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
Rendering Shadows
At the top level, the rendering steps look like the following:
- If ambient lighting is enabled, render the entire scene with ambient only.
- For each light in the scene, do the following:
- Disable depth-buffer and frame-buffer writing.
- Prepare the stencil buffer render states for rendering the shadow volume.
- Render the shadow volume mesh with a vertex extruding shader. This sets up the stencil buffer according to whether or not the pixels are in the shadow volume.
- Prepare the stencil buffer render states for lighting.
- Prepare the additive blending mode.
- Render the scene for lighting with only the light being processed.
Shadow Volume并不是一個沒有缺陷的技術。除了高像素填充率和陰影邊界檢測外,在繪制過程中還可能出現錯誤。錯誤的主要原因是當一個幾何模型在計算自陰影時,它的面通常被完全照亮或者完全變暗,這取決于這個面是否朝向光源。光照計算必須使用頂點法向而非表面法向。對于接近平行光源的面,它會被完全照亮或者完全變暗,而實際上應該部分處于光源中。這是從stencil shadow volume中繼承來的問題,在計算陰影時必須被考慮。這個問題可以通過增加mesh密度來解決,這也增加了處理mesh的時間。頂點法向和面法向越接近,表面的問題就越少。如果程序不能把問題限制在可接受的范圍內,就必須考慮使用其他的算法,比如PRT或者Shadow Map。