Rendering Part2 E
Rendering Part2 E
Rendering
is the ”engine” that
creates
images from
3D scenes and a
virtual camera
Rendering Pipeline
Geometry stage
rasterization Fragment
Processing
Z-Buffer
Visibility Test
Final Image
in FRAME BUFFER
Clipping: when?
VCS NDC
CPU Application projection
Vertices
Model View
Transform
Proj. Transformation
geometry Lighting
(Perspective / Orthographic)
Projection
Transform/clip Clipping
Fragment generation
GPU Rasterization Projection
(to Screen Space)
rasterization Fragment
Processing Screen Space
NDC
Z-Buffer
Visibility Test projection
Final Image
in FRAME BUFFER
Scan Conversion (Rasterization)
CPU Application
Vertices
Model View
Transform – Determine which pixels that are inside
primitive specified by a set of vertices
geometry Lighting – Produces a set of fragments
– Fragments have a location (pixel location)
Projection and other attributes such color and texture
Transform/clip coordinates that are determined by
Fragment generation
interpolating values at vertices
GPU Rasterization
rasterization Fragment
Processing
Z-Buffer
Visibility Test
Final Image
in FRAME BUFFER
Visibility / Display
CPU Application
Vertices
Model View
Transform
geometry Lighting
Projection
Transform/clip
Fragment generation
GPU Rasterization
- Fragment processing (texturing)
rasterization Fragment
Processing
- Draw the closest object
Z-Buffer
(depth buffer)
Visibility Test
Final Image
in FRAME BUFFER
Clipping Against View Volume
– Polyhedra transformed to the normalized world are clipped against bounds of
canonical view volume, one polygon at a time
– Polygons are clipped one edge at a time
– Intersection calculations are trivial because of normalized planes of canonical
view volume
– New vertices are created where objects are clipped
y
(1, 1, 1)
(1, 1, -1)
z
(-1, -1, 1) (-1, -1,-1) x
Front clip plane
Back clip plane
First bit: x<xmin outside halfplane of left edge, to left of left edge
Second bit: x>xmax outside halfplane of right edge, to right of right edge
Third bit: y>ymax outside halfplane of top edge, above top edge
Fourth bit: y<ymin outside halfplane of bottom edge, below bottom edge
H1 H2
OCp AND OCq = 0000
q
Outcode of p : 1000
1010 0010 0110
Outcode of q : 0010
H3
p
1000 0000 0100
Outcode of [pq] : 0000
H4
Outcode of p : 1000
1010 0010 q 0110
Outcode of q : 0010
H3
1000
p
0000 0100
Outcode of [pq] : 0000
H4
• How do we intersect?
Insert explicit equation of line into
the plane equation
P1
P2
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1
• P4 P3 P2 P1
R
P2
B
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1
• P4 P3 P2 P1
R
P2
B
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4 P3 P2
R(a)
P2 P2 a
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4 P3 P2
R(a)
P2 P2 a
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4 P3
R(a)
P2 P3 P2 a
P3
P4
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4 P3
R(a)
P2 P3 P2 a
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4
R(a)
P2 bP3 P2 a
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L( P1 )
P1 a • P4
R(a)
P2 bP3 P2 a
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R(a)
P2 • bP3 P2 a
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R(a)
c P2 • bP3 P2
B(c)
c
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R(a)
c P2 • bP3
B(c)
c
P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R(a)
c P2 •b
B(c)
bdc
d P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R(a)
c P2 •
B(c)
abdc
d P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
B(c)
• abdc
d P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
B(c)
• abd
T (d )
d
d P3
P4 b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
B(c)
• ab
T (d )
ed
d P3
P4 e
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
B(c)
•a
T (d )
afed
f d P3
P4 e
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
B(c)
•
T (d )
cafed
f d P3
P4 e
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
T (d )
• cafed
f d P3
P4 e
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
T (d )
• cafe
f d P3
P4 e e
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
R
c P2
T (d )
• caf
f d P3
P4 e fe
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a
g
R
c P2
T (d )
• ca
f d P3
P4 e gfe
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a h
g
R
c P2
T (d )
•c
f d P3
P4 e chgfe
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a h
g
R
c P2
T (d )
•
f d P3
P4 e dchgfe
b
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I
L
P1 a h
g
R
c P2
T
f d P3
P4 e • dchgfe
b
Rasterization (Scan Conversion
Fragment generation)
CPU Application
Vertices
Z-Buffer
Visibility Test
Final Image
in FRAME BUFFER
Fragment processing
CPU Application
Vertices
Model View
Transform – Fragment Processing
Pixel colors determined later using color,
geometry Lighting texture, and other vertex properties
(Texture Mapping)
Projection
Transform/clip
Fragment generation
GPU Rasterization
rasterization Fragment
Processing
Z-Buffer
Visibility Test
Final Image
in FRAME BUFFER
Rasterization
• Geometric primitives
(point, line, polygon, circle, polyhedron, sphere... )
• Primitives are continuous; screen is discrete
• Rasterization: algorithms for efficient generation of the
samples occupied by a geometric primitive
– It enumerates the fragments covered by the primitive
– It interpolates values, called attributes, across the primitive
Line rasterization
• Compute the boundary pixels
75
Polygon Rasterization
• Compute the boundary pixels
• Polygon Filling:
– Fill the spans
– Flood fill
Scan Converting:
how to draw 2D Line Segments
1. Given two points on the screen (with integer coords.)
2. Determine which pixels should be drawn in between
these to display a unit width line…
– Line-Drawing Algorithms:
DDA, Midpoint (Bresenham’s) Algorithm
77
Line Rasterization Requirements
yi + 1 = mxi + 1 + B ( xi , yi )
= m( xi + 1 ) + B m = y / x
= mxi + B + m x = x2 − x1
= yi + m
Float
Increment
y = y2 − y1
•Color pixel at drawpixel [ x ,round( y )]
Problem
0 ≤m≤1
NE
E
Bresenham Step
• Which pixel to choose: E or NE?
– Choose E if segment passes below or
through middle point M
– Choose NE if segment passes above M
NE NE
M M
E E
• Thus, incrementally,
dnew = dold + NE = dold + dy – dx
Summary
writePixel(x, y, value);
flood_fill(int x, int y) {
if(read_pixel(x,y)== WHITE) {
write_pixel(x,y,RED);
flood_fill(x-1, y);
flood_fill(x+1, y);
flood_fill(x, y+1);
flood_fill(x, y-1);
} }
Scan Line Fill Method
Incremental algorithm to find spans for each
scanline (top-to-bottom), and determine
Span Scanline
“insideness”
We save ~two multiplications and two additions per pixel when the triangle
is large
Scanline for concave polygons:
tessellator
Convert everything into triangles
then scan convert the triangles
Framebuffer[x,y ] = Color(?)
• We store data (such as color, etc.) on the vertices
of triangles, and subsequently use interpolation to
compute values of this data on the interior of the
triangle
For example:
•Specify an (R,G,B) color on each vertex of a triangle
•For each pixel inside the triangle, compute the interpolation
coordinates for that pixel
•Then use these interpolation coordinates to compute an
interpolated (R,G,B) color value for that pixel
Per-pixel color:
linear Interpolation
C1 C2 C3 specify Color or by vertex shading
C3
scan line C4
C5
span
C2
Per-Pixel color:
barycentric interpolation
Triangle’s color at point x?
1
C area xBC 2 E BC (x) EBC (x)
aa = = 1 =
area ABC 2 EBC (A) EBC (A)
ECA (x)
=
ECA (B)
aa
E AB (x)
=
E AB (C)
Vertices
Model View
Transform • hidden surface removal
geometry
algorithms: update the frame
Lighting
buffer with the closest object
Projection
Transform/clip
• Fragment processing
Fragment Generation/
GPU Rasterization
rasterization Fragment
Processing
Z-Buffer
Visibility Test
Final Image
in FRAME BUFFER
Visibility
How do we know which parts are visible/in front?
Culling
refers to the process of
determining and culling
polygons which are not
visible
Before HSR
p0
p1
• Most renderers allow triangles to be defined as one
or two sided. Only one-sided triangles need to be
backface culled.
Backface Culling
Any back facing triangles
should be culled as early as
possible, as it would be
expected that up to 50% of
the triangles in a scene
would be back facing
view direction
v
Backface Culling
view direction
v
Backface Culling
n v = cos( ) 0 , draw polygon
− 90 90
view direction
v
Backface Culling
n v 0 , cull polygon
view direction
v
Visible Surface determination
Two approaches:
Object space vs. image space
5
3
4
7
6
1
1. Sort polygons by smallest Z
2. Solve polygons overlapping problems (in z)
3. For each polygon, draw its pixels
Painter’s algorithm (Depth-Sort)
A
A C
B
3D Depth-Sort Algorithm
– Handles errors/ambiguities of Z-sort:
255 255 255 255 255 255 255 255 127 127 127 127 127 127 127 127 127 127 127 127 255 255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
+ 127
127
127
127
127
127
127
127
127 = 127
127
127
127
127
127
127
127
127
255
255
255
255
255
255
255
255 255 255 255 255 255 255 255 127 127 127 127 127 127 255 255 255 255 255
255 255 255 255 255 255 255 255 127 127 127 127 255 255 255 255 255 255
255 255 255 255 255 255 255 255 127 127 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
127 127 127 127 127 127 127 255 127 127 127 127 127 127 127 255
127 127 127 127 127 127 255 255 127 127 127 127 127 127 255 255
127 127 127 127 255 255 255 255 63 127 127 127 255 255 255 255
63 63
127 127 127 255 255 255 255 255 63 63 127 255 255 255 255 255
63 63 63
127 127 255 255 255 255 255 255 63 63 63 255 255 255 255 255
127 255 255 255 255 255 255 255 63 63 63 63 63 63 63 63 255 255 255 255
255 255 255 255 255 255 255 255 63 63 63 63 63 63 63 63 63 63 255 255 255
A C