Barycentric coordinates
Rendering a triangle using barycentric coordinates as introduced in Pineda 88 has given the main theoretical ground to gpu design and shader programming since.
Observations
- Since barycentric coordinates interpolation is implemented on the GPU, this chapter mainly serves a theoretical purpose. However, it is recommended to study this topic prior to shader programming.
- All the points display on the sketches below are interactive. You may click and drag them to get a better insight of what’s been discussed.
Geometric motivation
Given points , and in 2D Euclidean space, we know that the determinant gives the signed area of the parallelogram spanned by the vectors and :
The sign of the determinant tells to which side of edge tu point p lies.
Edge functions
By developing and rearranging its terms the above determinant can be expressed in terms of point as:
which is defined as the edge function of and simply denoted as: , i.e.,
If the remaining triangle vertex is considered it’s also possible to define the other two triangle edge functions as:
Point p lies inside △ tuv iff the sign of the three edge functions are the same. This sole fact gives means to automate triangle rasterization.
Barycentric coordinates
The triangle (unnormalized) barycentric coordinates are defined directly from the edge functions as:
Normal form
The area is used to define the normalized barycentric coordinates as:
where,
Properties
It’s easy to see that (interact with the sketch above):
- if then , , and .
- If , or , or lies along edge then .
- , i.e., the normalized barycentric values measures the relative weights of the vertices t, u, and v required to interpolate p.
Shading
Not only positions but any vertex data, e.g., color, depth, texture coordinates, may be interpolated by using property 3 above.
Color interpolation
(software raster demo)
Color is one of the most common proporties associated to a vertex.
(click on the canvas and press any key)
TODO: sketch="/sketches/rasterize_color.js" quadrille=“true”
Color interpolations greatly alleviates the appearance of mach bands.
(click on the canvas and press any key)
TODO: sketch="/sketches/rasterize_strip.js" quadrille=“true”
Depth interpolation
(hardware raster demo)
Depth interpolation produces the z-buffer which solves the visibility problem. Vertex depth values are computed using a projection matrix.
(click on the canvas and press any key)
TODO: sketch="/sketches/zbuffer.js"
Texture mapping
(software raster demo)
Vertex texture coordinates is defined in image space (left frame).
TODO: sketch="/sketches/rasterize_texture.js" quadrille=“true”
Conclusion
Barycentric coordinates are crucial for implementing rasterization through dedicated hardware, as they can easily be parallelized, effectively address the visibility problem and also provide means to programmatically implement shading.