Graphics Reference
In-Depth Information
[domain("tri")]
DS_OUTPUT DSMAIN( const OutputPatch<HS_POINT_OUTPUT, 3> TriPatch,
float3 Coords : SVOomainLocation,
HS_PATCH_OUTPUT input )
{
DS_OUTPUT output;
// Interpolate world space position
float4 vWorldPos = Coords.x * TriPatch[6].position
+ Coords.y * TriPatch[l].position
+ Coords.z * TriPatch[2] .position;
// Calculate the interpolated normal vector
output.normal = Coords.x * TriPatch [0] .normal
+ Coords.y * TriPatch[l].normal
+ Coords.z * TriPatch[2].normal;
// Normalize the vector length for use in displacement
output.normal = normalize( output.normal );
// Interpolate the texture coordinates
output.tex = Coords.x * TriPatch[0] .tex
+ Coords.y * TriPatch[l] .tex
+ Coords.z * TriPatch[2].tex;
// Calculate the interpolated world space light vector.
output.light = Coords.x * TriPatch[0].light
+ Coords.y * TriPatch[l].light
+ Coords.z * TriPatch[2].light;
// Calculate MIP level to fetch normal from
float fHeightMapMIPLevel =
clamp( ( distance( vWorldPos.xyz, vEye.xyz ) - 100.0f ) / 100.0f,
0.0f,
3.0f);
// Sample the height map to know how much to displace the surface by
float4 texHeight =
HeightTexture.SampleLevel( LinearSampler, output.tex,
fHeightMapMIPLevel );
// Perform the displacement. The ' fScale' parameter determines the maximum
// world space offset that can be applied to the surface. The displacement
// is performed along the interpolated vertex normal vector.
const float fScale = 0.5f;
vWorldPos . xyz = vWorldPos . xyz + output.normal * texHeight.r * fScale;
// Transform world position with viewprojection matrix
output.position = mul( vWorldPos, ViewProjMatrix );
return output;
}
Listing 8.5. The domain shader program for implementing displacement mapping.
Search WWH ::




Custom Search