
主要是用模板緩沖區(qū)來(lái)實(shí)現(xiàn)。
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并不是一個(gè)沒(méi)有缺陷的技術(shù)。除了高像素填充率和陰影邊界檢測(cè)外,在繪制過(guò)程中還可能出現(xiàn)錯(cuò)誤。錯(cuò)誤的主要原因是當(dāng)一個(gè)幾何模型在計(jì)算自陰影時(shí),它的面通常被完全照亮或者完全變暗,這取決于這個(gè)面是否朝向光源。光照計(jì)算必須使用頂點(diǎn)法向而非表面法向。對(duì)于接近平行光源的面,它會(huì)被完全照亮或者完全變暗,而實(shí)際上應(yīng)該部分處于光源中。這是從stencil shadow volume中繼承來(lái)的問(wèn)題,在計(jì)算陰影時(shí)必須被考慮。這個(gè)問(wèn)題可以通過(guò)增加mesh密度來(lái)解決,這也增加了處理mesh的時(shí)間。頂點(diǎn)法向和面法向越接近,表面的問(wèn)題就越少。如果程序不能把問(wèn)題限制在可接受的范圍內(nèi),就必須考慮使用其他的算法,比如PRT或者Shadow Map。