Ivan Belyavtsev
22.01.2022
After vertex shader stage we have an NDC coordinates of each vertex of the triangle.
Next steps:
Could be done in homogeneous coordinates or cartesian
\[M_{ss} = \left[\begin{array}{cccc} width & 0 & 0 & 0 \\ 0 & - height & 0 & 0 \\ 0 & 0 & 1 & 0 \\ x & height + y & 0 & 1 \end{array}\right]\]
where \(x\) and \(y\) are offsets from the screen space \((0, 0)\) [1]
Let’s take two points \((X, Y)\) and \((X+dx, Y+dy)\) on an edge
Then, define an edge function:
\[E(x, y) = (x - X)dy - (y - Y)dx\]
[2]
If \(E(x, y)>0\) then \((x, y)\) on the “right” side
If \(E(x, y)<0\) then \((x, y)\) on the “left” side
If \(E(x, y)=0\) then \((x, y)\) on the edge
[2]
[2]
[3]
Lets compare \[E(x, y) = (x - X)dy - (y - Y)dx\]
and \[(u_1, u_2, 0) \times (v_1, v_2, 0) = (0, 0, u_1v_1-u_2v_1)\]
The edge function value is a twice area of the triangle
Let \(u = \frac{S_{CAP}}{S_{ABC}}\), \(v = \frac{S_{BCP}}{S_{ABC}}\), \(w = \frac{S_{ABP}}{S_{ABC}}\)
Therefore, \(u+v+w=1\), and \(P=uA+vB+wC\). The barycentric could be used for attribute interpolation [4]
Input: interpolated vertex attributes
Output: result pixel color
The pixel shader should be provided by graphics programmer [5]
cg::renderer::rasterizer::edge_function
methodpixel_shader
lambda for the instance of cg::renderer::rasterizer
Rasterization
and Pixel shader
stages to draw
method of cg::renderer::rasterizer