Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Clipping.

Similar presentations


Presentation on theme: "Computer Graphics Clipping."— Presentation transcript:

1 Computer Graphics Clipping

2 Clipping Analytically calculating the portions of primitives within the viewport

3 Why Clip? Bad idea to rasterize outside of framebuffer bounds Also, don’t waste time scan converting pixels outside window

4 Clipping The naïve approach to clipping lines:
for each line segment for each edge of viewport find intersection point pick “nearest” point if anything is left, draw it What do we mean by “nearest”? How can we optimize this? A B C D

5 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.

6 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

7 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

8 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

9 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

10 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

11 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 E D C B A

12 Cohen-Sutherland Line Clipping
Intersect line with edge (how?) A B D E C

13 Cohen-Sutherland Line Clipping
Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests and repeat if necessary D C B A

14 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)

15 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

16 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?

17 How many sides can a clipped triangle have?
Why Is Clipping Hard? What happens to a triangle during clipping? Possible outcomes: triangle  quad triangle  triangle triangle  5-gon How many sides can a clipped triangle have?

18 How many sides? Seven…

19 Why Is Clipping Hard? A really tough case:

20 Why Is Clipping Hard? A really tough case:
concave polygon  multiple polygons

21 Sutherland-Hodgeman Clipping
Basic idea: Consider each edge of the viewport individually Clip the polygon against the viewport edge’s equation

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 Sutherland-Hodgeman Clipping
Edge from s to p takes one of four cases: (Orange line can be a line or a plane) inside outside s p p output inside outside s p i output inside outside s p no output inside outside s p i output p output

34 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 Add i to output, followed by p


Download ppt "Computer Graphics Clipping."

Similar presentations


Ads by Google