Graphics Reference
In-Depth Information
We will update the
CommonTess.hlsl
shader code to perform barycentric interpolation,
to calculate the attributes for new vertices in a triangle domain, and bilinear interpolation
to interpolate between the four attributes within the quad domain.
Getting ready
We will continue from the previous recipe,
Preparing the vertex shader and buffers
for tessellation
.
For this recipe, we will also re-use the triangle and quad renderers from the recipe
Rendering
primitives
in
Chapter 2
,
Rendering with Direct3D
, along with the
Texture2.png
texture.
1.
Add the
Texture2.png
texture, and the
TriangleRenderer.cs
and
QuadRenderer.cs
renderers to your project.
2.
Update the quad and triangle renderers to use the
Vertex
structure for the vertices,
passing in the vertex positions and UV coordinates as appropriate.
3.
The vertex winding will also need to be reversed, as these renderers were created for
a left-handed coordinate system.
Remember to update the vertex binding in order to use the size
of the
Vertex
structure.
The completed project for this recipe can be found in
Ch05_01TessellationPrimitives
within the downloaded companion code.
How to do it…
We will begin by creating the interpolation functions necessary for the
tri
and
quad
domains
within
Shaders\CommonTess.hlsl
. We will then implement the hull and domain shaders,
and finally update the renderers. Perform the following steps:
1.
First add a new HLSL function
BarycentricInterpolate
to
CommonTess.hlsl
.
As we may need to interpolate
float2
,
float3
, and
float4
values, we will add a
number of overloaded versions of this function. For convenience, we can accept the
inputs as individual parameters or as an array. The following code snippet shows the
float3
version; you need to do the same using
float2
and
float4
:
float3 BarycentricInterpolate(
float3 v0
,
float3 v1
,
float3 v2
, float3 barycentric)
{
return barycentric.z * v0 + barycentric.x * v1 +