Objectives • Introduce basic implementation strategies • Clipping • Scan conversion
Dec 30, 2015
Objectives
• Introduce basic implementation strategies• Clipping • Scan conversion
Major tasks
(1) Modeling: results in sets of vertices that specify a group of geometric objects supported by the rest of the system
E.g. GL_POLYGON in OpenGL
Modeler is usually an application program that may be interactive
Major tasks
(2) Geometry Processing: • works with vertices• involves a number of transformations
Modeling transformations
Viewing transformation
Projection transformation
Clipping
Perspective division
NDC to viewport (window coordinates)
3D WORLD
3D World
3D camera
3D clip
3D clip
3D NDC
2D window (with depth)
“NormalizedViewVolume”
Projection Transformation
• OpenGL transforms vertices by a projection transformation to a normalized view volume (cube centered at origin, with side length 2). These coordinates are referred to as normalized device coordinates
• No matter how the viewing volume was specified by the user (e.g. glOrtho or gluPerspective)
11
11
11
z
y
x
Perspective Projection
z
y
Z=-near
Z=-far
z
y+1
+1 (far)
-1
-1(near)
Viewing frustum Normalized view volume
viewer
Normalized Device Coordinates
• (x,y) coordinates indicate the projected point on the final viewing window.
Orthogonal projection of the distorted shape in the normalized volume is equivalent to perspective projection of the original shape
• z coordinate is used for depth There is a reversal of z coordinates: the points closest to the
viewer have smaller z coordinates Z coordinates went through a nonlinear shortening, however
relative order of z coordinates are maintained—that is sufficient for hidden surface removal
Why normalization?
• To simplify clipping Now primitives are clipped with respect to the same
shape!
In fact, clipping takes place before the perspective division (division by w): still an axis aligned rectangular box.
• To avoid division operation for parts that are not going to be visible in the window at the end!
wzw
wyw
wxw
Clip coordinates
Clipping
• 2D against clipping window• 3D against clipping volume• Easy for line segments polygons• Hard for curves and text
Convert to lines and polygons first
Clipping 2D Line Segments
• Brute force approach: compute intersections with all sides of clipping window
Inefficient: one division per intersection
Cohen-Sutherland Algorithm
• Idea: eliminate as many cases as possible without computing intersections
• Start with four lines that determine the sides of the clipping window
x = xmaxx = xmin
y = ymax
y = ymin
The Cases
• Case 1: both endpoints of line segment inside all four lines
Draw (accept) line segment as is
• Case 2: both endpoints outside all lines and on same side of a line
Discard (reject) the line segment
x = xmaxx = xmin
y = ymax
y = ymin
The Cases
• Case 3: One endpoint inside, one outside Must do at least one intersection
• Case 4: Both outside May have part inside
Must do at least one intersection
x = xmaxx = xmin
y = ymax
Defining Outcodes
• For each endpoint, define an outcode
• Outcodes divide space into 9 regions• Computation of outcode requires at most 4 subtractions
b0b1b2b3
b0 = 1 if y > ymax, 0 otherwiseb1 = 1 if y < ymin, 0 otherwiseb2 = 1 if x > xmax, 0 otherwiseb3 = 1 if x < xmin, 0 otherwise
Using Outcodes
• Consider the 5 cases below• AB: outcode(A) = outcode(B) = 0
Accept line segment
Using Outcodes
• CD: outcode (C) = 0, outcode(D) 0 Compute intersection
Location of 1 in outcode(D) determines which edge to intersect with
Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two intersections
Using Outcodes
• EF: outcode(E) logically ANDed with outcode(F) (bitwise) 0
Both outcodes have a 1 bit in the same place
Line segment is outside of corresponding side of clipping window
reject
Using Outcodes
• GH and IJ: same outcodes, neither zero but logical AND yields zero
• Shorten line segment by intersecting with one of sides of window
• Compute outcode of intersection (new endpoint of shortened line segment)
• Reexecute algorithm
Efficiency
• In many applications, the clipping window is small relative to the size of the whole data base
Most line segments are outside one or more side of the window and can be eliminated based on their outcodes
• Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step
Cohen Sutherland in 3D
• Use 6-bit outcodes • When needed, clip line segment against planes
Liang-Barsky Clipping
• Consider the parametric form of a line segment (works in both 2- and 3-dimensions)
• We can distinguish between the cases by looking at the ordering of the values of where the line determined by the line segment crosses the lines that determine the window
p() = (1-)p1+ p2 1 0
p1
p2
Liang-Barsky Clipping
• In (a): 4 > 3 > 2 > 1
Intersect right, top, left, bottom: shorten
• In (b): 4 > 2 > 3 > 1
Intersect right, left, top, bottom: reject
Liang-Barsky Clipping
• Efficient implementation requires avoiding computing intersections until they are needed.
• We also want to avoid divisions when possible.
Liang-Barsky Clipping
• The intersection with top line (y= ymax) is at
• Rewrite equation as
• It is possible to restate all the rules in terms of
12
1max
yy
yy
max1max12 )( yyyyyy
.,, max etcyy
Liang-Barsky Clipping
• Many lines can be rejected before all 4 intersections are known.
E.g. if , reject0,0 max yy
Liang-Barsky Clipping
• Can accept/reject as easily as with Cohen-Sutherland
• Using values of , we do not have to use algorithm recursively as with C-S
• Extends to 3D We are now clipping lines against planes (six of
them)
Plane-Line Intersections
)(
)(
,0))((
)1()(
12
1
0
21
ppn
ppna
papn
appaap
o
Plane-line intersection
• The above equation requires six multiplications and divisions!
• However, the normalized viewing volume has axis aligned planes
Reduces to a single division just like in 2D. E.g.•
12
1max
yy
yya
Polygon Clipping
• Not as simple as line segment clipping Clipping a line segment yields at most one line
segment
Clipping a polygon can yield multiple polygons
• However, clipping a convex polygon can yield at most one other polygon
Tessellation and Convexity
• Either do not allow nonconvex polygons• or replace nonconvex (concave) polygons with a
set of triangular polygons (a tessellation)
Clipping as a Black Box
• Can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment
Pipeline Clipping of Line Segments
• Clipping against each side of window is independent of other sides
Can use four independent clippers in a pipeline
Pipeline Clipping of Polygons
• Clip edges of polygons successively• Three dimensions: add front and back clippers• Strategy used in SGI Geometry Engine• Small increase in latency
Therefore clipped polygon is P, Q, R, S.
Clipping to One Boundary
• Consider each polygon edge in turn• Four cases:
ENTER:
STAY IN:
LEAVE:
STAY OUT:
inside
P
Q
Output P, Q R
Output R
S
Output S
(no output)
Clipping Example
• Works for more nonconvex shapes.
Input Case Output1 start -2 stay in 23 leave A4 stay out -5 enter B, 56 leave C7 stay out -1 enter D, 1
2 A B 5 C D 1
1
4
6
5
3
2
7
A
D
CB
inside
Bounding Boxes
• Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent
Smallest rectangle aligned with axes that encloses the polygon
Simple to compute: max and min of x and y
Bounding boxes
Can usually determine accept/reject based only on bounding box
reject
accept
requires detailed clipping
Rasterization
• Rasterization (scan conversion) Shade pixels that are inside an object specified
by a set of vertices• Line segments• Polygons: scan conversion = fill
• Shades determined by color, texture, shading model
• Here we study algorithms for determining the correct pixels starting with the vertices
Scan Conversion of Line Segments
• Start with line segment in window coordinates with integer values for endpoints
• Assume implementation has a write_pixel function
y = mx + h
x
ym
DDA Algorithm
• Digital Differential Analyzer DDA was a mechanical device for numerical
solution of differential equations
Line y=mx+ h satisfies differential equation dy/dx = m = y/x = y2-y1/x2-x1
• Along scan line x = 1
For(x=x1; x<=x2, x++) { y+=m; write_pixel(x, round(y), line_color)}
Problem
• DDA = for each x plot pixel at closest y Problems for steep lines
Using Symmetry
• Use for 1 m 0• For m > 1, swap role of x and y
For each y, plot closest x
Bresenham’s Algorithm
• DDA requires one floating point addition per step
• We can eliminate all fp through Bresenham’s algorithm
• Consider only 1 m 0 Other cases by symmetry
• Assume pixel centers are at half integers• If we start at a pixel that has been written, there
are only two candidates for the next pixel to be written into the frame buffer
Candidate Pixels
1 m 0
last pixel
candidates
Note that line could havepassed through anypart of this pixel
Decision Variable
-
d = x(b-a)
d is an integerd > 0 use upper pixeld < 0 use lower pixel
Incremental Form
• To compute dk+1
More efficient if we use dk, the value of the decision variable at x = xk
122)1(2
1
)1(
kk
k
k
k
yhxmab
yya
yyb
hxmya
b
1kx
ky
1ky
y
Incremental Form
)12(2.2.2)(
122)1(2
hxyyxxyabxd
yhxmab
kkk
kk
a
b
1kx
ky
1ky
y
Constant (c )
Incremental Form
).(22
.2)1.(2
11
11
kkkk
kkk
yyxydd
cyxxyd
a
b
1kx
ky
1ky
y
Either 0 or 1 depending on the sign of dk
Incremental Form
dk+1= dk +2y, if dk < 0dk+1= dk +2(y- x), otherwise
•For each x, we need do only an integer addition and a test
Bresenham’s Algorithm
• (1) Input the two line endpoints and store the left endpoint in(x1, y1)
• (2)set the color for posiiton(x1,y2) and plot the first point• (3) calculate constants y, x, 2(y-x)• (4)calculate d1 = 2y-x• (5) At each xk, starting at k=1 do
if dk < 0, plot (xk +1, yk)dk+1= dk +2y,
Else plot(xk+1, yk+1)dk+1= dk +2(y- x)
Example
• Endpoints (20,10) and (30,18)x=10, y=8, d1=62y-2x=-4, 2y=16
Example
20 22 25 30
10
15
18
• Endpoints (20,10) and (30,18)
x=10, y=8, d1=6• 2y-2x=-4, 2y=16
20 22 25 30
10
15
18
(x1,y1)=(20,10)
P1=6
Next pixel (x2,y2): (21,11)
P2=6-4=2
20 22 25 30
10
15
18
(x2,y2)=(21,11)
P2=2
Next pixel (x3,y3): (22,12)
P3=2-4=-2
20 22 25 30
10
15
18
(x3,y3)=(22,12)
P3=-2
Next pixel (x4,y4): (23,12)
P4=-2+16=14
20 22 25 30
10
15
18
(x4,y4)=(23,12)
P4=14
Next pixel (x5,y5): (24,13)
P5=14-4=10
20 22 25 30
10
15
18
20 22 25 30
10
15
18
20 22 25 30
10
15
18
Aliasing
• Ideal rasterized line should be 1 pixel wide
• Choosing best y for each x (or visa versa) produces aliased raster lines
Antialiasing by Area Averaging
• Color multiple pixels for each x depending on coverage by ideal line
original antialiased
magnified