0% found this document useful (0 votes)
65 views103 pages

Rendering Part2 E

The document summarizes the 3D rendering pipeline. It discusses: 1) The geometry stage which includes model-view transformation, lighting, projection transformation, and clipping geometry against the view volume. 2) The clipping process removes portions of objects outside the view volume. Clipping happens after projection transformation during the geometry stage. 3) Rasterization converts geometry into pixels during the rasterization stage. It determines which pixels are inside each primitive through scan conversion.

Uploaded by

pietro fischetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views103 pages

Rendering Part2 E

The document summarizes the 3D rendering pipeline. It discusses: 1) The geometry stage which includes model-view transformation, lighting, projection transformation, and clipping geometry against the view volume. 2) The clipping process removes portions of objects outside the view volume. Clipping happens after projection transformation during the geometry stage. 3) Rasterization converts geometry into pixels during the rasterization stage. It determines which pixels are inside each primitive through scan conversion.

Uploaded by

pietro fischetti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

Rendering: part II

Rendering
is the ”engine” that
creates
images from
3D scenes and a
virtual camera
Rendering Pipeline
Geometry stage

Object space world space camera view space

Compute lighting projection clip viewport


Geometry stage: Clipping
CPU Application
Vertices
We need to clip scene against
Model View
sides of view volume
Transform

geometry Lighting Portions of the object


outside the view
Projection
Transform/clip volume are
Fragment generation removed
GPU asterization

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

– Use bounding volumes to trivially reject groups of objects at a time


Point Clipping
• The view volume is a cuboid that extends from -1 to 1 in
x and y and z
• Test components of vertices -1 <= x,y,z <=1

(-1, 1, 1) (-1, 1, -1)

y
(1, 1, 1)
(1, 1, -1)
z
(-1, -1, 1) (-1, -1,-1) x
Front clip plane
Back clip plane

(1, -1, 1) (1, -1, -1)

• Points falling within these values are saved, and vertices


falling outside get clipped
Line Clipping

Endpoint analysis for lines:

– if both endpoints in, can do “trivial acceptance”


– if one endpoint is inside, one outside, must clip
– if both endpoints out, don’t know
Cohen-Sutherland H1 H2
Line Clipping in 2D
• Divide plane into 9 regions 0110
1010 0010
H
• 4 bit outcode OC q
p
Records results of four bounds tests: 1000 0000 0100
H
Clip Rectangle
1001 0001 0101

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

• Determine OC for the line vertices (OCp , OCq )


• CASE 1:
Lines with OCp = 0000 and OCq = 0000 can be trivially accepted
• CASE 2:
Lines lying entirely in a half plane on outside of an edge
can be trivially rejected:
OCp AND OCq  0000
(i.e., they
H1 share anH“outside”
2 bit)
p q
Outcode of p : 1010

1010 0010 0110 Outcode of q : 0110


H3

1000 0000 0100 Outcode of [pq] : 0010


H4

Clipped because there is a 1


1001 0001 0101
• CASE 3:
External vertices but the line can not be rejected

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

1001 0001 0101 Not clipped


External vertices but the line can not be rejected
OCp AND OCq = 0000

•Consider one vertex with OC 0000;


•Determine the intersection between the line and the window edge
(corresponding to the bit  0 in the OC);
•If the intersection has OC=0000 then update the vertex
with the computed intersection vertex, otherwise reject segment
•Repeat test
H1 H2

Outcode of p : 1000
1010 0010 q 0110
Outcode of q : 0010
H3

1000
p
0000 0100
Outcode of [pq] : 0000
H4

1001 0001 0101


Cohen-Sutherland
Line Clipping in 3D
• Very similar to 2D
• Divide volume into 27 regions

• 6 bit outcode records results of 6 bounds tests:


First bit: outside back plane, behind back plane
Second bit: outside front plane, in front of front plane
Third bit: outside top plane, above top plane
Fourth bit: outside bottom plane, below bottom plane
Fifth bit: outside right plane, to right of right plane
Sixth bit: outside left plane, to left of left plane

• Lines with OC0 = 000000 and OC1 = 000000 can be trivially


accepted
• Lines lying entirely in a volume on outside of a plane can be trivially
rejected:
OC0 AND OC1  0 (i.e., they share an “outside” bit)
• Otherwise CLIP
Line – Plane Intersection
• Explicit (Parametric) Line Equation

P(t) = P0 + t (P1 – P0) P2


P(t) = (1-t) P0 + t P1

• How do we intersect?
Insert explicit equation of line into
the plane equation

• P(t)= (1 − t)P0 + tP1 n · (P(t) − P2) = 0


t= n·(P2−P0)/n·(P1−P0)
Then find the intersection point and shorten the line
Polygon Clipping
• What happens to a triangle during clipping?
- possible outcomes:

triangle triangle triangle quad triangle 5-gon

How many sides could a clipped triangle have?


Sutherland-Hodgman
Polygon Clipping
INPUT: v1, v2, ..vn ordered polygon vertices
OUTPUT: one or more polygons (for nonconvex)
Sutherland-Hodgman Polygon Clipping

Decompose the problem in


simple clip pipelined
subproblems:

-Clip right window boundary


-Clip bottom window boundary
-Clip left window boundary
-Clip top window boundary
For each window boundary:
• CASE 1: internal
polygon edge ⇒
add E to the final • CASE 2: add the
E
vertex list intersection point to
E Output
Intersection the final vertex list
Output
Vertex

• CASE 3: both • CASE 4: add


external vertices ⇒ E
E intersection point and
no vertex in final list First E to the final vertex list
Second Output
Output
Clipping pipeline on
No
output. the other window
boundaries
Sutherland-Hodgman Algorithm
Out In Out In Out In Out In
S
S E I S
E E E
S I

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

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

No output Output E Output I Output I,E

L
P1 a h
g
R
c P2

T
f d P3
P4 e • dchgfe
b
Rasterization (Scan Conversion
Fragment generation)
CPU Application
Vertices

Model View – Determine which pixels that are inside


Transform primitive specified by a set of vertices
OUTPUT: Produces a set of fragments
geometry Lighting – Fragments have a location (pixel location)
and other attributes such color and texture
Projection coordinates that are determined by
Transform/clip interpolating values at vertices
Fragment generation
GPU Rasterization Fragments
rasterization Fragment
Processing

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

Transform continuous primitive into


discrete samples

• Uniform thickness & brightness


• Continuous appearance
• No gaps
• Accuracy
• Speed
Finding next pixel:
Special case:
• Horizontal Line:
Draw pixel P and increment x coordinate value by 1 to get
next pixel.
• Vertical Line:
Draw pixel P and increment y coordinate value by 1 to get
next pixel.
• Diagonal Line:
Draw pixel P and increment both x and y coordinate by 1 to
get next pixel.
• What should we do in general case?
– Increment x coordinate by 1 and choose point closest to line.
– But how do we measure “closest”?
Strategy 1 –
Digital Differential Analyzer (DDA)
Equation of line that connects two points
y = mx + B ( x i + 1 , yi + m )
Starting with leftmost point,
•Increment xi by 1

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

• DDA = for each x plot pixel at closest y


– Problems for steep lines

0 ≤m≤1

Solution: if the slope is >1, step for y instead


Using Symmetry
• For m > 1, swap role of x and y
yi+1 = mxi+1 + B = m(xi + 1) + B= yi + m (0<m ≤ 1)
yi+1 = yi+1 xi+1 =(yi+1 - B)/m = xi + 1/m (m>1)

y and m are float type numbers,


it is hard to be implemented by hardware.
Strategy 2: Midpoint Algorithm (1985)
(Bresenham's Algorithm (1965))
• Select pixel vertically closest to line segment
– intuitive, efficient, pixel center always within 0.5
vertically
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)

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

Now, find a way to calculate on which side of line midpoint lies


Line p
F>0
F<0
Line equation as function y: F=0
F=0
• y = m x + B = dy/dx x + B p
M F>0
F<0
Line equation as implicit function:
• F(x, y) = a x + b y + c = 0
from above, y dx = dy x + B dx or
F(x, y) = dy x – y dx + B dx = 0
so a = dy, b = -dx, c = B dx, a > 0 for y1 < y2

• F(xm, ym) = 0 when any point M is on line


• F(xm, ym) < 0 when any point M is above line
• F(xm, ym) > 0 when any point M is below line
• Our decision will be based on value of function at midpoint
M (xm, ym) where (xm=xp+1, ym=yp+½)
Decision Variable
Decision Variable d:
• midpoint M at (xp + 1, yp + ½)
• We only need sign of F(xp + 1, yp + ½) to seepwhereF>0
line
lies, and then pick nearest pixel F=0
NE
• d = F(xp + 1, yp + ½) M
F<0
- if d > 0 choose pixel NE E
- if d < 0 choose pixel E
- if d = 0 choose either one consistently

How do we incrementally update d ?


- On basis of picking E or NE, figure out location of M for
that pixel, and corresponding value of d for next grid line
F>0
p
F=0
If E was chosen: M
F<0
Increment M by one in x direction
dnew = F(xp + 2, yp + ½)
= a(xp + 2) + b(yp + ½) + c
dold = a(xp + 1) + b(yp + ½) + c

• Subtract dold from dnew to get incremental difference E


dnew = dold + a
E = a = dy

• Derive value of decision variable at next step incrementally without


computing F(M) directly
dnew = dold + E = dold + dy

• E can be thought of as correction or update factor to take dold to dnew


• It is referred to as forward difference
F>0
p
F=0
If NE was chosen: M
F<0
Increment M by one in both x and y directions

dnew = F(xp + 2, yp + 3/2)


= a(xp + 2) + b(yp + 3/2) + c

• Subtract dold from dnew to get incremental difference


dnew = dold + a + b
NE = a + b = dy – dx

• Thus, incrementally,
dnew = dold + NE = dold + dy – dx
Summary

• At each step, algorithm chooses between 2 pixels based


on sign of decision variable calculated in previous
iteration.

• It then updates decision variable d by adding either E


or NE to old value depending on choice of pixel. Simple
additions only!

• First midpoint for first d = dstart is at (x0 + 1, y0 + ½):


F(x0 + 1, y0 + ½) = F(x0, y0) + a + b/2 = a+b/2
To eliminate fraction in dstart : =0, (x , y ) is on the line
0 0

redefine F by multiplying it by 2; F(x,y) = 2(ax + by + c)


Example Code
void MidpointLine(int x1, int y1,
int x2, int y2, int value) {
int dx = x2 – x1;
int dy = y2 – y1;
int d = 2 * dy - dx;
int incrE = 2 * dy;
int incrNE = 2 * (dy - dx);
int x = x1;
int y = y1;

writePixel(x, y, value);

while (x < x2) {


if (d <= 0) { // East Case
d = d + incrE;
} else { // Northeast Case
d = d + incrNE;
y++;
}
x++;
writePixel(x, y, value);
} /* while */
} /* MidpointLine */
Other Quadrants
• Note that this only applies
to lines with a positive -Swap y1 for y2
Step for y
gradient -Step for x
instead of y
instead of x
- Increment by -x

• But you can easily write a -Increment by -y


-Swap y1 for y2
separate case for each
other case
Increment by -y
Swap x1 for x2

• Also if the gradient is too


steep you need to step for
-Step for x
x instead of y (as we saw in - Swap x1 for x2
- Step for y instead of y
instead of x - Increment by x
DDA)
Polygon Rasterization
Polygon Filling
Polygon Rasterization
Polygon Filling
The basic rule for filling a polygon is:
– If a point is inside the polygon, color it with the
inside color.
– INSIDE/OUTSIDE TEST: triangle test/odd-even test
Polygon fill is a sorting problem, where we sort all
the pixels in the frame buffer into those that are
inside the polygon, and those that are not.
Polygon filling Algorithms:
– Flood fill
– Scan-line fill
Inside Triangle Test
A point is inside a triangle if it is in the
negative half-space of all three boundary lines
o Triangle vertices are ordered counter-clockwise
o Point must be on the left side of every boundary line
E ( x, y ) = ax + by + c =
B
= ( x − x0 )( y1 − y0 ) − ( y − y0 )( x1 − x0 )
= x ( y1 − y0 ) + y ( x0 − x1 ) + E1
E3 outward
P=(x,y) normal
+
a b
-
− x0 ( y1 − y0 ) + y0 ( x1 − x0 ) A C
E2
c
( x, y ) within triangle  Ei ( x, y )  0, i = 1,2,3
Triangle method works only for convex polygons!
Inside Polygon Rule
• How to tell inside from outside
– Convex easy, but..
– What is a good rule for which pixels are inside?
Odd-even test
Any ray from P to infinity crosses a number of edges
– Odd = inside polygon
– Even = outside polygon
Flood Fill Method
• Given an initial point(x,y) inside the polygon- a seed point -
then we can look at its neighbors recursively, coloring them
with the foreground color if they are not edge point.
• use black for rasterize edge
• Scan convert edges into buffer in edge/inside color (RED)

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”

Each span can be


processed independently
desired order
(parallel span processor)
Scan Line Fill Method
Proceeding from top to bottom, from left to right the
intersections are paired and intervening pixels are set to the
specified intensity
Algorithm
- Find the intersections of the scan line with all the edges in the polygon
- Sort the intersections by increasing X-coordinates
- Fill the pixels between pair of intersections
Scan Line Fill (for triangle)
For every triangle
Compute projection for vertices, compute the Ei
Compute bbox, clip bbox to screen limits
For all pixels x,y in bbox
Evaluate edge functions Ei
If all Ei < 0
Framebuffer[x,y ] = triangleColor

How do we get such a bounding box?


Xmin, Xmax, Ymin, Ymax of the projected
triangle vertices
Can we do better?
For every triangle
Compute projection for vertices
Compute bbox, clip bbox to screen limits
For all scanlines y in bbox
Evaluate all Ei’s at (x0,y): Ei = aix0 + biy + ci
For all pixels x in bbox
If all Ei<0
Framebuffer[x,y ] = triangleColor
Increment line equations: Ei += ai
Ei (x+1,y) = Ei (x,y) + ai

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

C4 determined by interpolating between C1 and C2


C5 determined by interpolating between C2 and C3
interpolate between C4 and C5 along span
C1

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)

B Color at x is affine combination of


color at three triangle vertices
A
aa
x color =  Acolor +  Bcolor +  Ccolor
Per-pixel attributes
Interpolate colors
■R = α0 R0+ α1R1+ α2R2
■G = α0G0+ α1G1+ α2G2
■B = α0B0+ α1B1+ α2B2
Interpolate normal vectors
■N = α0N0+ α1N1+ α2N2
Interpolate z-buffer depth values
■z = α0z0+ α1z1+ α2z2
Interpolate texture coordinates
■u = α0u0+ α1u1+ α2u2
■v = α0v0+ α1v1+ α2v2
Visibility
CPU Application

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?

Given a set of 3-D objects and a view specification (camera),


determine which lines or surfaces of the object are visible
Culling vs HSR

Culling
refers to the process of
determining and culling
polygons which are not
visible
Before HSR

HSR (hidden surface


removal) refers to the
process of determining
which parts of the
polygons are not
visible
Culling

• There are three common reasons to cull a


particular triangle
– If it doesn’t lie within the view volume
(view frustum culling)
– If it is facing ‘away’ from the viewer
(back-face culling)
– If it is degenerate (area=0)
• The first case is built automatically into the
clipping algorithm which we already covered
• In the third case normal n will be [0 0 0]
Back-face Culling

• By convention, the front side of the triangle is


defined as the side where the vertices are arranged
in a counterclockwise fashion
p2
n

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

Usually, back-face culling is


done before clipping, as it is
a very quick operation and
will affect a much larger
percentage of triangles than
clipping
Backface Culling

the projector from any


point P on polygon to the
center of view

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

Object space: algorithms which work in the view


coordinate system.
Complexity:
for k objects O(k2) since each object must be compared with all the
others

Image Space: algorithms which work in the screen


coordinate system;
Complexity:
for image nxm and k objects O(k)
Painter’s algorithm
• Draw back-to-front
• How do we sort objects?
• Can we always sort objects?

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

• Can we always sort objects?


– No, there can be cycles
– Requires to split polygons

A C

B
3D Depth-Sort Algorithm
– Handles errors/ambiguities of Z-sort:

1. Sort all objects’ zmin and zmax


2. If an object is uninterrupted (its zmin and zmax are
adjacent in the sorted list), it is fine
3. If 2 objects DO overlap
3.1 Check if they overlap in x
- If not, they are fine
3.2 Check if they overlap in y
- If not, they are fine
- If yes, need to split one
Ray Casting
• Maintain intersection with closest object

For each screen pixel:


determine the ray EYE-pixel
For each object in scene:
compute intersection ray/object
If (inters. Is closest)
Then the object is visible
store the pixel object;
color the pixel;
Z-Buffer Algorithm (Catmull 1975)
– Requires two “buffers” of the same sizes
Color Buffer —RGB pixel buffer
—initialized to background color
Depth (“Z”) Buffer —depth of scene at each pixel
—initialized to far depth = 255
- Polygons are scan-converted in arbitrary order. When pixels
overlap, use Z-buffer to decide which polygon “gets” that pixel
255 255 255 255 255 255 255 255 127 127 127 127 127 127 127 127 127 127 127 127 127 127 255

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 127 255 255 255 + 63


= 127 127 127 127 127 255 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

Example using integer Z-buffer with near = 0, far = 255


Usually, every pixel stores a depth (or z) value, in 32 bit fixed point format
Z-Buffer Algorithm
– draw every polygon that we can’t reject trivially
– If we find a pixel of a polygon that is closer to the
front, we paint over whatever was behind it
– Inizialize the FrameBuffer/zBuffer
void Init_zBuffer() {
int x, y;
for (y = 0; y < YMAX; y++)
for (x = 0; x < XMAX; x++) {
FrameBuffer (x, y, BACKGROUND_VALUE);
zBuffer (x, y, 1);
}
}
Rasterizer with Z-buffer
pseudo code
For every triangle
Compute Projection of vertices
Compute bbox, clip bbox to screen limits
Setup 3 line equations
For all pixels in bbox
Increment line equations
If all line equations<0 //pixel [x,y] in triangle
Compute barycentric coordinates
Compute currentZ
Compute currentColor
If currentZ < zBuffer[x,y] //pixel is visible
Framebuffer[x,y]=currentColor
zBuffer[x,y]=currentZ
Z-Buffer Pros
– Simplicity lends itself well to hardware implementations:
FAST -- used by all graphics cards
– Polygons do not have to be compared in any particular
order: no presorting in z necessary
– Only consider one polygon at a time
– Z-buffer can be stored w/ an image; allows you to
correctly composite multiple images w/o having to
merge models
• great for incremental addition to a complex scene
– Can be used for non-polygonal surfaces, CSGs
(intersect, union, difference), and any z = f(x,y)
Works for hard cases!

A C

MIT EECS 6.837, Cutler and Durand


Serena Morigi
Dipartimento di Matematica
[email protected]
http://www.dm.unibo.it/morigi

You might also like