From Vertices To Fragments: Rasterization: Frame Buffer
From Vertices To Fragments: Rasterization: Frame Buffer
Vertices to Fragments:
Rasterization
Reading Assignment: Chapter 7
Frame Buffer
• Special memory where pixel colors are stored.
System Bus
Illumination
(Shading)
Viewing Transformation
(Perspective / Orthographic)
Clipping
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Visibility / Display
Modeling Transformations
Modeling
Transformations
• 3D models defined in their own
Illumination
coordinate system (object space)
(Shading)
• Modeling transforms orient the
Viewing Transformation
(Perspective / Orthographic) models within a common
coordinate frame (world space)
Clipping
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Object space World space
Visibility / Display
Illumination (Shading) (Lighting)
Modeling • Vertices lit (shaded) according to
Transformations
material properties, surface
Illumination
(Shading) properties (normal) and light sources
Viewing Transformation • Local lighting model
(Perspective / Orthographic) (Diffuse, Ambient, Phong, etc.)
Clipping
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Visibility / Display
Viewing Transformation
Modeling
Transformations
• Maps world space to eye space
Illumination • Viewing position is transformed
(Shading)
to origin & direction is oriented
Viewing Transformation
(Perspective / Orthographic) along some axis (usually z)
Clipping
Eye space
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Viewing Transformation
(Perspective / Orthographic)
Projection
Modeling
Transformations
• The objects are projected to the
Illumination
2D image place (screen space)
(Shading)
Viewing Transformation
(Perspective / Orthographic)
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Visibility / Display
Scan Conversion (Rasterization)
Modeling
Transformations
• Rasterizes objects into pixels
Illumination • Interpolate values as we go
(Shading)
(color, depth, etc.)
Viewing Transformation
(Perspective / Orthographic)
Clipping
Projection
(to Screen Space)
Scan Conversion
(Rasterization)
Visibility / Display
Visibility / Display
Modeling
Transformations
• Each pixel remembers the
Illumination
closest object (depth buffer)
(Shading)
Viewing Transformation
(Perspective / Orthographic)
Rasterization
• How to draw primitives?
– Convert from geometric definition to pixels
– Rasterization = selecting the pixels
• Will be done frequently
• Must be fast:
• use integer arithmetic
• use addition instead of multiplication
Next
• Line-drawing algorithm
– naïve algorithm
– Bresenham algorithm
• Circle-drawing algorithm
– naïve algorithm
– Bresenham algorithm
(x2, y2)
(x1, y1)
Line Rasterization Requirements
• Transform continuous primitive into
discrete samples
• Uniform thickness & brightness
• Continuous appearance
• No gaps (x2, y2)
• Accuracy
• Speed
(x1, y1)
Simple Line
Simple approach:
increment x, solve for y
(x1, y1)
Floating point arithmetic
required
Naive Line Rasterization Algorithm
• Simply compute y as a function of x
– Conceptually: move vertical scan line from x1 to x2
– What is the expression of y as function of x?
– Set pixel (x, round (y))
x − x1
y = y1 + ( y 2 − y1)
(x2, y2) x 2 − x1
= y1 + m( x − x1)
y
dy
m=
dx
(x1, y1)
Efficiency
• Computing y value is expensive
y = y1 + m( x − x1)
(x2, y2)
y(x+1)
y(x+1) m
y(x)
y(x)
x x+1
(x1, y1)
x x+1
Does it Work?
• It seems to work okay for
lines with a slope of 1 or less
• Doesn’t work well for lines
with slope greater than 1 –
lines become more
discontinuous in appearance
and we must add more than 1
pixel per column to make it
work.
• Solution? - use symmetry.
Bresenham's Algorithm
• Observation:
– If we're at pixel P (xp, yp), the next pixel must be
either E (xp+1, yp) or NE (xp+1, yp+1)
– Why?
NE
P
E
Bresenham Step
• Which pixel to choose: E or NE?
• Error associated with a pixel:
NE
Error pixel NE
Error pixel E
E
Bresenham Step
• How to compute the error?
• Line defined as y = mx + h
• Vertical distance from line to pixel (x, y):
e(x, y) = mx+h-y e <0
– negative if pixel above L e =0
– zero on L
e >0
– positive below L
L
e is called the error function.
Bresenham's Algorithm
• How to compute the error?
• Error Function: e’
e(x, y) = mx+h-y e
• On each iteration:
update x: x' = x+1
update e: e' = e + m
if (e ≤ 0.5): y' = y (choose pixel E)
if (e > 0.5): y' = y +1 (choose pixel NE) e' = e - 1
Summary of Bresenham
• Initialize e = 0
• for (x = x1; x ≤ x2; x++)
NE
– plot (x,y)
– update x, y, e
E
Line Anti-aliasing
• Note: brightness can vary with slope √2 * L
– What is the maximum variation?
• How could we compensate for this?
L
– Answer: antialiasing
How do we remove aliasing?
• Solution 1 - Area Sampling:
– Treat line as a single-pixel wide rectangle
– Color pixels according to the percentage of each pixel
that is “covered” by the rectangle
• For triangles:
glEnable(GL_POLYGON_SMOOTH);
Next:
• Circle Rasterization
Circle Rasterization
• Generate pixels for 2nd octant only
• Slope progresses from 0 → –1
• Analog of Bresenham
Segment Algorithm
d(x, y) = x2 + y2 – R2
SE
• On each iteration: R
update x: x' = x+1
update e: e' = e + 2x + 3
if (e < 0): y' = y (choose E)
if (e ≥ 0): y' = y - 1 (choose SE), e' = e’-2y+2
2D Scan Conversion
• Geometric primitive
– 2D: point, line, polygon, circle...
– 3D: point, line, polyhedron, sphere...
• Primitives are continuous; screen is discrete
Use line rasterization
• Compute the boundary pixels
Scan-line Rasterization
• Compute the boundary pixels
• Fill the spans
• Requires some initial setup to prepare
For modern graphics cards
• Triangles are usually very small
• Setup cost troublesome
• Brute force is tractable
Modern Rasterization
For every triangle
ComputeProjection
Compute bbox, clip bbox to screen limits
For all pixels in bbox
If pixel in triangle
Framebuffer[x,y]=triangleColor