Graphics Reference
In-Depth Information
context.OutputMerger
.SetDepthStencilState(oldDepthState, oldStencilRef);
context.Rasterizer.State = oldRSState;
}
20. For each of the lights in the previous for loop, we need to first choose the correct
shader based on the type of light, and update perLightBuffer with the current
light's parameters. As our G-Buffer has been stored in view-space, we will transform
the light parameters into the same space using the PerObject.View matrix before
updating the constant buffer resource.
PerLight light = Lights[i];
PixelShader shader = null; // Assign shader
if (light.Type == LightType.Ambient)
shader = psAmbientLight;
else if (light.Type == LightType.Directional)
shader = psDirectionalLight;
else if (light.Type == LightType.Point)
shader = psPointLight;
// Update the perLight constant buffer
// Calculate view space position and direction
Vector3 lightDir = Vector3.Normalize(Lights[i].Direction);
Vector4 viewSpaceDir = Vector3.Transform(lightDir,
PerObject.View);
light.Direction = new Vector3(viewSpaceDir.X,
viewSpaceDir.Y, viewSpaceDir.Z);
Vector4 viewSpacePos =
Vector3.Transform(Lights[i].Position, PerObject.View);
light.Position = new Vector3(viewSpacePos.X,
viewSpacePos.Y, viewSpacePos.Z);
context.UpdateSubresource(ref light, perLightBuffer);
context.PixelShader.SetConstantBuffer(4, perLightBuffer);
21. Now we check whether the light needs to be rendered full screen using the screen-
aligned quad, or only the region defined by a light volume. In our implementation,
directional and ambient lights will always be applied to the full G-Buffer (that is
rendered fullscreen). For a point light, we only want to use the full G-Buffer if the
bounding sphere is clipping the near- and far-clip plane of the frustum.
// Check if the light should be considered full screen
bool isFullScreen = light.Type == LightType.Directional ||
light.Type == LightType.Ambient;
 
Search WWH ::




Custom Search