This project is about the process of creating a triangle primitive in 2D space, hence forth known as NDC (Normalized Device Coordinates). The range of NDC by default goes from -1 and +1 on the X axis, -1 and +1 on the Y axis, -1 and +1 on the Z axis
Software Used
Languages Used
Visual Studio
C++
Explanations
Working in Screen/Pixel Space
Working in 3D Space
3D Normalized Device Coordinates
Adding Flexibility: Shaders
Triangle Rasterization
Barycentric Coordinates
Brute Triangle
Lari’s Parametric Triangle
Barycentric Interpolation
Code
Main
Defines
3D Math
Shaders
Raster Functions
Code Clean
Rotating triangle (lines only)
Rotating triangle (filled to a solid color with white border lines)
Rotating triangle (interpolated colors with white border lines)
Working in Screen/Pixel Space
Pros
Cons
Working in 3D Space
Pros
Cons
The Screen in 3D Normalized Device Coordinates
A.K.A The Canonical View Volume.
This coordinate system is always the same regardless of what you set your screen pixel width & height.
Notice how the origin is now in the center and that we have a Z axis available.
As we start to work in 3D this system will be much easier to deal with.
Using 3D to Our Advantage
Adding Flexibility: Shaders
Triangle Rasterization
Barycentric Coordinates
Defines a Non-Orthogonal coordinate system describing the space of a triangle.
Any position on this plane can be described with the following equation:
P = β( B – A ) + γ( C – A ) + A
Terms Reordered:
P = ( 1 - β - γ )A + β( B ) + γ( C )
α = ( 1 - β - γ )
P ( α, β, γ ) = αA + βB + γC
Finding the Barycentric Coordinates of a point relative to a triangle
β = ImplicitLineEquation ( B, line AC )
γ = ImplicitLineEquation ( C, line BA )
α = ImplicitLineEquation ( A, line CB )
b = ImplicitLineEquation ( P, line AC )
y = ImplicitLineEquation ( P, line BA )
a = ImplicitLineEquation ( P, line CB )
Pβγα = ( b / β , y / γ, a / α )
Could we use the Barycentric coordinates to detect if we are inside or outside the triangle? If so… how?
Brute Triangle
FOR ALL PIXELS
bya = FindBarycentric ( CurrX, CurrY )
IF b >=0 && b <= 1 &&
y >=0 && y <= 1 &&
a >=0 && a <= 1
THEN
PlotPixel ( CurrX, CurrY )
Better Brute Triangle
StartX = MIN ( X1, X2, X3 )
StartY = MIN ( Y1, Y2, Y3 )
EndX = MAX ( X1, X2, X3 )
EndY = MAX ( Y1, Y2, Y3 )
FOR StartY to EndY
FOR StartX to EndX
bya = FindBarycentric ( CurrX, CurrY )
IF b >=0 && b <= 1 &&
y >=0 && y <= 1 &&
a >=0 && a <= 1
THEN
PlotPixel ( CurrX, CurrY )
Lari’s Parametric Triangle
Barycentric Interpolation