Graphics Reference
In-Depth Information
proper texturing and a more realistic lighting model. The above code makes references to
the Sobel() function, which is shown in Listing 9.9.
cbuffer sampleparams
{
// xy = pixel dimensions
// zw = geometry dimensions
float4 heightMapDimensions;
}
float3 Sobel( float2 tc )
{
// Useful aliases
float2 pxSz = float2( 1.0f / heightMapDimensions.x, 1.0f
/ heightMapDimensions.y );
// Compute the necessary offsets:
float2 000 = tc + float2( -pxSz.x, -pxSz.y );
float2 o10 = tc + fioat2( 0.0f, -pxSz.y );
float2 o20 = tc + float2( pxSz.x, -pxSz.y );
ftoat2 o01 = tc + fioat2( -pxSz.x, 0.0f
);
float2 o21 = tc + float2( pxSz.x, 0.0f
);
float2 002 = tc + float2( -pxSz.x,
pxSz.y );
float2 ol2 = tc +
float2(
0.0f,
pxSz.y );
float2 o22 = tc + float2( pxSz.x,
pxSz.y )j
// Use of the sobel filter requires the eight samples
// surrounding the current pixel:
float h00 = SampleHeightMap(o00); // NB: Definition provided in listing 9.8
float hl0 = SampleHeightMap(ol0)j
float h20 = SampleHeightMap(o20);
float h01 = SampleHeightMap(o01);
float h21 = SampleHeightMap(o21);
float h02 = SampleHeightMap(o02)j
float hl2 = SampleHeightMap(o12)j
float h22 = SampleHeightMap(o22);
// Evaluate the Sobel filters
float Gx = h00 - h20 + 2.0f * h01 - 2.0f * h21 + h02 - h22;
float Gy = h00 + 2.0f * hl0 + h20 - h02 - 2.0f * hl2 - h22;
// Generate the missing Z
float Gz = 0.01f * sqrt( max(0.0f„ 1.0f - Gx * Gx - Gy * Gy ) );
// Make sure the returned normal is of unit length
return normalize( float3( 2.0f * Gx, Gz, 2.0f * Gy ) );
}
Listing 9.9. Definition of the Sobel () function.
 
Search WWH ::




Custom Search