Graphics Reference
In-Depth Information
Figure 11.6. Reconstructing surface position using view ray and camera distance.
vector will vary, depending on how far that geometry is from the camera, but the direction
is always the same. This is very similar to the method used for casting primary rays in a
ray tracer: for a pixel on the near or far clip plane, get the direction from the camera to that
pixel, and check for intersections. What this ultimately means is that if we have the screen
space pixel position and the camera position, we can figure out the position of the triangle
surface if we have the distance from the camera to the surface. Refer to Figure 11.6 for an
illustration of this concept.
We're now ready to work out a simple implementation of this concept in a deferred
Tenderer. The first step will be in our g-buffer pass, where we'll store the distance from
the camera to the triangle surface in a single floating-point value. The simplest and cheap-
est way to do this is to transform the vertex position to view space in the vertex shader
(since doing this implicitly makes the position relative to the camera position, as described
above) and then compute the magnitude of the resulting position vector in the pixel shader.
Listing 11.9 provides the g-buffer shader code for calculating camera distance.
II -- G-Buffer vertex shader
// Calculate view space position of the vertex and pass it to the pixel
// shader
output. PositionVS = mul(input.PositionOSj WorldViewMatrix).xyz;
// -- 6-Buffer pixel shader --
// Calculate the length of the view space position to get the distance from
// camera->surface
output.Distance.x = length(input.PositionVS);
Listing 11.9. G-buffer shader code for camera distance calculation.
Search WWH ::




Custom Search