Graphics Reference
In-Depth Information
float2 texcoord = patch[0] .texCoord * (1.0f-uv.x) * (1.0f-uv.y)
+ patchfl] .texCoord * uv.x * (1.0f-uv.y)
+ patch[2]. texCoord * (1.0f-uv.x) * uv.y
+ patch[3] .texCoord * uv.x * uv.y
;
II Determine the height from the texture
finalVertexCoord.y = SampleHeightMap(texcoord);
// We then need to transform the world-space
// coord to be a proper projection space output
// that the rasterizer can deal with. Could delegate
// to the GSj but no need this time!
o.position = mul( float4( finalVertexCoord, 1.0f ), mViewProj );
I/ Perform a sobel filter on the heightmap to determine an appropriate
// normal vector
float3 normal = Sobel( texcoord );
normal = normalize( mul( float4(normal, 1.0f)j mlnvTposeWorld ).xyz );
o.colour = min(0.75f, max(0.0f, dot( normal, float3( 0.9f, 1.0f, 0.0f ) ) ) );
return o;
}
Listing 9.8. The domain shader.
Listing 9.8 is all that is necessary to build the final terrain geometry that will be ren-
dered. The domain shader shown above does not really implement a specific part of Greg
Snook's original algorithm; rather, it implements a simple form of displacement mapping.
The output of the hull shader and fixed-function tessellator stages essentially just de-
fine a pattern on the XZ plane for where the heightmap texture should be sampled. Unlike
more trivial terrain Tenderers, which generate a uniform grid of sample locations, this im-
plementation generates an increasing density of sample locations, according to the LOD
scheme in use. The domain shader simply takes these new points as being the correct ones
to generate more detail for—the above code does not actively decide or influence the dis-
tribution of geometric detail.
The domain shader presented above can be broken down into three fundamental sec-
tions. First, the UV coordinate is decoded into a final world-space position on the XZ plane.
This is a straightforward interpolation that follows the diagram in the preceding section.
The same set of equations is used to determine the texture coordinate for this new vertex,
which is then used to look up a value from the heightmap texture. These values are put
together into the final vertex position and then transformed into projection space for the
rasterizer.
The final section is a trivial lighting model to give the rendered terrain a more aes-
thetically pleasing appearance. Naturally, a more robust implementation would implement
Search WWH ::




Custom Search