PowerPoint

advertisement
CS 445 / 645
Introduction to Computer Graphics
Lecture 9
Clipping
Assignment 1
Do a writeup (call it README)
• Indicate which version of Visual Studio you used
• Also indicate what you do for extra credit
• Also, if your program doesn’t work, explain what you did to help us provide
partial credit
Copy all code (including source) required to run your program to
• username/Assignment1
Name executable
• assign1.exe
Review: Polygon Rasterization
For scanline, determine all intersections of polygon edges with scanline
Sort edge intersections in least to greatest order
Use parity count to determine when pixels are drawn
Horizontal lines do not contribute to parity count
Ymin endpoints do contribute to parity count
Ymax endpoints do not contribute to parity count
Not drawn because H is max of AH
And HG does not contribute
H
F
Not drawn because D is min of ED
And increments counter to 2.
DC doesn’t contribute
G
E
D
A
C
B
Bottom edge drawn because A is min of AH. AB does not contribute
Next Topic: Clipping
We’ve been assuming that all primitives (lines, triangles,
polygons) lie entirely within the viewport
• In general, this assumption will not hold:
Clipping
Analytically calculating the portions of primitives
within the viewport
Why Clip?
Bad idea to rasterize outside of framebuffer
bounds
Also, don’t waste time scan converting pixels
outside window
Clipping
The naïve approach to clipping lines:
for each line segment
for each edge of viewport
find intersection point
pick “nearest” point
B
if anything is left, draw it
What do we mean by “nearest”?
How can we optimize this?
D
C
A
Trivial Accepts
Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is
entirely inside the viewport?
A: test both endpoints.
Trivial Rejects
How can we know a line is outside viewport?
A: if both endpoints on wrong side of same edge,
can trivially reject line
Clipping Lines To Viewport
Combining trivial accepts/rejects
• Trivially accept lines with both endpoints inside all edges of the viewport
• Trivially reject lines with both endpoints outside the same edge of the
viewport
• Otherwise, reduce to trivial cases by splitting into two segments
Cohen-Sutherland Line Clipping
Divide viewplane into regions defined by viewport
edges
Assign each region a 4-bit outcode:
1001
1000
1010
0001
0000
0010
0101
0100
0110
Cohen-Sutherland Line Clipping
Assign an outcode to each vertex of line to test
• Bit 1 = sign bit of (ymax – y)
• If both outcodes = 0, trivial accept
– bitwise OR
• bitwise AND vertex outcodes together
– if result  0, trivial reject
Cohen-Sutherland Line Clipping
If line cannot be trivially accepted or rejected, subdivide
so that one or both segments can be discarded
Pick an edge that the line crosses (how?)
Intersect line with edge (how?)
Discard portion on wrong side of edge and assign
outcode to new vertex
Apply trivial accept/reject tests; repeat if necessary
Cohen-Sutherland Line Clipping
If line cannot be trivially accepted or rejected, subdivide
so that one or both segments can be discarded
Pick an edge that the line crosses
• Check against edges in same order each time
– For example: top, bottom, right, left
D
C
B
A
E
Cohen-Sutherland Line Clipping
Intersect line with edge (how?)
D
C
B
A
E
Cohen-Sutherland Line Clipping
Discard portion on wrong side of edge and assign outcode to new vertex
D
C
B
A
Apply trivial accept/reject tests and repeat if necessary
Viewport Intersection Code
• (x1, y1), (x2, y2) intersect with vertical edge at xright
– yintersect = y1 + m(xright – x1), m=(y2-y1)/(x2-x1)
• (x1, y1), (x2, y2) intersect with horizontal edge at
ybottom
– xintersect = x1 + (ybottom – y1)/m, m=(y2-y1)/(x2-x1)
Cohen-Sutherland Review
• Use opcodes to quickly eliminate/include lines
– Best algorithm when trivial accepts/rejects are common
• Must compute viewport clipping of remaining lines
– Non-trivial clipping cost
– Redundant clipping of some lines
More efficient algorithms exist
Solving Simultaneous Equations
Equation of a line
• Slope-intercept (explicit equation): y = mx + b
• Implicit Equation: Ax + By + C = 0
• Parametric Equation: Line defined by two points, P0 and P1
– P(t) = P0 + (P1 - P0) t, where P is a vector [x, y]T
– x(t) = x0 + (x1 - x0) t
– y(t) = x0 + (y1 - y0) t
Parametric Line Equation
Describes a finite line
Works with vertical lines (like the viewport edge)
0 <=t <= 1
• Defines line between P0 and P1
t<0
• Defines line before P0
t>1
• Defines line after P1
Parametric Lines and Clipping
Define each line in parametric form:
• P0(t)…Pn-1(t)
Define each edge of viewport in parametric form:
• PL(t), PR(t), PT(t), PB(t)
Could perform Cohen-Sutherland intersection
tests using appropriate viewport edge and line
Line / Edge Clipping Equations
Faster line clippers use parametric equations
Line 0:
• x0 = x00 + (x01 - x00) t0
• y0 = y00 + (y01 - y00) t0
Viewport Edge L:
• xL = xL0 + (xL1 - xL0) tL
• yL = yL0 + (yL1 - yL0) tL
x00 + (x01 - x00) t0 = xL0 + (xL1 - xL0) tL
y00 + (y01 - y00) t0 = yL0 + (yL1 - yL0) tL
• Solve for t0 and/or tL
Cyrus-Beck Algorithm
We wish to optimize line/line intersection
• Start with parametric equation of line:
– P(t) = P0 + (P1 - P0) t
• And a point and normal for each edge
– PL, NL
Cyrus-Beck Algorithm
Find t such that
PL
NL [P(t) - PL] = 0
P(t)
Inside
P0
Substitute line equation for P(t)
Solve for t
• t = NL [PL – P0] / -NL [P1 - P0]
NL
P1
Cyrus-Beck Algorithm
Compute t for line intersection with all four edges
Discard all (t < 0) and (t > 1)
Classify each remaining intersection as
• Potentially Entering (PE)
• Potentially Leaving (PL)
NL [P1 - P0] > 0 implies PL
NL [P1 - P0] < 0 implies PE
• Note that we computed this term when computing t
Cyrus-Beck Algorithm
Compute PE with largest t
Compute PL with smallest t
Clip to these two points
PL
PL
PE
PE
P0
P1
Cyrus-Beck Algorithm
Because of horizontal and vertical clip lines:
• Many computations reduce
Normals: (-1, 0), (1, 0), (0, -1), (0, 1)
Pick constant points on edges
solution for t:
• -(x0 - xleft) / (x1 - x0)
• (x0 - xright) / -(x1 - x0)
• -(y0 - ybottom) / (y1 - y0)
• (y0 - ytop) / -(y1 - y0)
Comparison
Cohen-Sutherland
• Repeated clipping is expensive
• Best used when trivial acceptance and rejection is possible for most lines
Cyrus-Beck
• Computation of t-intersections is cheap
• Computation of (x,y) clip points is only done once
• Algorithm doesn’t consider trivial accepts/rejects
• Best when many lines must be clipped
Liang-Barsky: Optimized Cyrus-Beck
Nicholl et al.: Fastest, but doesn’t do 3D
Clipping Polygons
Clipping polygons is more complex than clipping
the individual lines
• Input: polygon
• Output: original polygon, new polygon, or nothing
When can we trivially accept/reject a polygon as
opposed to the line segments that make up the
polygon?
Why Is Clipping Hard?
What happens to a triangle during clipping?
Possible outcomes:
triangle  triangle
triangle  quad
triangle  5-gon
How many sides can a clipped triangle have?
How many sides?
Seven…
Why Is Clipping Hard?
A really tough case:
Why Is Clipping Hard?
A really tough case:
concave polygon  multiple polygons
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the viewport edge’s equation
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping:
The Algorithm
Basic idea:
• Consider each edge of the viewport individually
• Clip the polygon against the edge equation
• After doing all edges, the polygon is fully clipped
Sutherland-Hodgeman Clipping
Input/output for algorithm:
• Input: list of polygon vertices in order
• Output: list of clipped poygon vertices consisting of
old vertices (maybe) and new vertices (maybe)
Note: this is exactly what we expect from the
clipping operation against each edge
Sutherland-Hodgeman Clipping
Sutherland-Hodgman basic routine:
• Go around polygon one vertex at a time
• Current vertex has position p
• Previous vertex had position s, and it has been added to
the output if appropriate
Sutherland-Hodgeman Clipping
Edge from s to p takes one of four cases:
(Orange line can be a line or a plane)
inside
outside
inside
outside
inside
outside
p
s
p
p output
s
i output
p
inside
p
s
no output
i output
p output
outside
s
Sutherland-Hodgeman Clipping
Four cases:
• s inside plane and p inside plane
– Add p to output
– Note: s has already been added
• s inside plane and p outside plane
– Find intersection point i
– Add i to output
• s outside plane and p outside plane
– Add nothing
• s outside plane and p inside plane
– Find intersection point i
– Add i to output, followed by p
Point-to-Plane test
A very general test to determine if a point p is “inside”
a plane P, defined by q and n:
(p - q) • n < 0:
p inside P
(p - q) • n = 0:
p on P
(p - q) • n > 0:
p outside P
Remember: p • n = |p| |n| cos (q)
q = angle between p and n
q
q
q
n
p
n
p
p
P
P
P
n
Finding Line-Plane Intersections
Edge intersects plane P where E(t) is on P
• q is a point on P
• n is normal to P
(L(t) - q) • n = 0
t = [(q - L0) • n] / [(L1 - L0) • n]
• The intersection point i = L(t) for this value of t
Line-Plane Intersections
Again, lots of opportunity for optimization
Download