Nima Kalantari
CSCE 441 - Computer Graphics
Rasterization
Many slides from Ren Ng, Pradeep Sen, and Scott Schaefer
Rasterization Pipeline
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
So far…
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Now
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Rasterization
?
(2.2, 1.3)
(4.4, 11.0)
(15.3, 8.6)
Shape Primitives
3dgep.com
Outline
Problem
Problem
Problem
Special Lines - Horizontal
Special Lines - Horizontal
Special Lines - Vertical
Special Lines - Vertical
Special Lines - Diagonal
Special Lines - Diagonal
How about Arbitrary Lines?
Representing a Line
Representing a Line – Slope-Intercept
b: y-intercept
m: slope
Representing a Line – Slope-Intercept
b: y-intercept
m: slope
Representing a Line – Slope-Intercept
b: y-intercept
m: slope
Outline
Arbitrary Lines
Arbitrary Lines
Simple Algorithm
Simple Algorithm
Simple Algorithm
Simple Algorithm
Simple Algorithm
Only need to compute m!
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Example
Simple Algorithm - Problems
Outline
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint at
Midpoint Algorithm
Midpoint at
Representing a Line
Representing a Line – Implicit Form
Representing a Line – Implicit Form
PL
PH
Representing a Line – Implicit Form
PL
PH
T
Line Tangent Vector
Representing a Line – Implicit Form
(x, y)
(-y, x)
Perpendicular
Vector in 2D
Representing a Line – Implicit Form
PL
PH
T
N
Representing a Line – Implicit Form
PL
PH
N
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
PL
PH
P = (x , y)
N
V
Representing a Line – Implicit Form
A
B
C
Example
Example
Example
Example
Example
Example
Example
Midpoint Algorithm
Midpoint at
Midpoint Algorithm
Midpoint at
Midpoint Algorithm
Requires evaluating the line function
Too slow!
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
line equation
Midpoint Algorithm
line equation
Midpoint Algorithm
line equation
Midpoint Algorithm
line equation
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
What about the very first value?
Midpoint Algorithm
What about the very first value?
First Midpoint
= 0 (Point on the line)
Summary
Summary
Midpoint Algorithm
Requires evaluating the line function
Too slow!
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Midpoint Algorithm – Example
Outline
What Pixel Values Approximate a Triangle?
?
(2.2, 1.3)
(4.4, 11.0)
(15.3, 8.6)
Sampling a Function
� for( int x = 0; x < xmax; x++ )
output[x] = f(x);
Let’s Try Rasterization As 2D Sampling
Sample If Each Pixel Center Is Inside Triangle
Sample If Each Pixel Center Is Inside Triangle
Sample Locations
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
(0,h)
(w,h)
(0,0)
(w,0)
Sample location for pixel (x,y)
(x+1/2,y+1/2)
Rasterization = Sampling A 2D Indicator Function
� for( int x = 0; x < xmax; x++ )
for( int y = 0; y < ymax; y++ )
Image[x][y] = f(x + 0.5, y + 0.5);
Define Binary Function: inside(tri,x,y)
inside(t,x,y) =
1
0
(x,y) in triangle t
otherwise
Evaluating inside(tri,x,y)
How to evaluate inside function?
P0
P1
P2
Triangle = Intersection of Three Half Planes
P0
P1
P2
Each Line Defines Two Half-Planes
> 0
< 0
= 0
P0
P1
Point-in-Triangle Test: Three Line Tests
P0
P1
P2
Compute line equations
from pairs of vertices
Point-in-Triangle Test: Three Line Tests
P0
P1
P2
Point-in-Triangle Test: Three Line Tests
P0
P1
P2
Point-in-Triangle Test: Three Line Tests
P0
P1
P2
Point-in-Triangle Test: Three Line Tests
Sample point is inside the triangle if it is inside all three lines.
P0
P1
P2
Note that the vertices should be
visited in a CCW order!
Performance Improvement
Performance Improvement
Performance Improvement
Performance Improvement
P0
P1
P2
Edge Cases (Literally)
1
2
Incorrect solution #1
Incorrect solution #2
Correct Solution
T1
T2
T1
T2
Correct Solution
T1
T2
T1
T2
Correct Solution
T1
T2
T1
T2
Correct Solution
T1
T2
T1
T2
Correct Solution
T1
T2
T1
T2
Outline
Interpolation Across Triangles
Linear Interpolation Across Triangle
Computing Barycentric Coordinates
Barycentric Coordinate Formulas
Barycentric Coordinates
Outline
Problem
Painter’s Algorithm
[Wikipedia]
Painter’s Algorithm
Painter’s Algorithm
Painter’s Algorithm
Painter’s Algorithm
[Foley et al.]
Z-Buffer
Z-Buffer Example
Image credit: Dominic Alves, flickr.
Rendering
Depth buffer
Z-Buffer Algorithm
for (each triangle T)
for (each fragment (x,y) in T)
if (z < depthbuffer[x,y]) // closest sample so far
colorbuffer[x,y] = rgb; // update color
depthbuffer[x,y] = z; // update z
else
; // do nothing, this simple is not closest
Z-Buffer Algorithm
Z-Buffer Complexity
Rasterization Pipeline
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Transformations
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Triangles formed
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Rasterization
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Shading
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)
Visibility
Vertex Processing
Triangle Processing
Rasterization
Fragment Processing
Framebuffer Operations
Display
Application
1
2
3
4
Input: vertices in 3D space
Vertex Stream
Vertices positioned in screen space
Triangle Stream
Triangles positioned in screen space
Fragment Stream
Fragments (one per covered sample)
Shaded Fragments
Shaded fragments
Output: image (pixels)