Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

Slides:



Advertisements
Similar presentations
Rezanje črt in poligonov. World window & viewport window viewport screen window world window.
Advertisements

CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
Computer Graphics Lecture 8 Arbitrary Viewing II: More Projection, Clipping and Mathematics of 3D Viewing.
10/10/02 (c) 2002 University of Wisconsin, CS 559 Last Time Finished viewing: Now you know how to: –Define a region of space that you wish to view – the.
CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA.
CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons.
CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.
David Breen, William Regli and Maxim Peysakhov
Dr. Scott Schaefer Clipping Lines. 2/94 Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
1 Clipping. 2 Transformation Sequence X Y Z X Y Z X Y Z X Y Z Object Coords. Eye Coords. Clip Coords. Normalized Device Coords. Screen Coords. Implementation:
Two-Dimensional Viewing Jehee Lee Seoul National University.
Computer Graphics Viewing.
Viewing & Clipping In 2D. 2 of 44 Contents Windowing Concepts Clipping –Introduction –Brute Force –Cohen-Sutherland Clipping Algorithm Area Clipping –Sutherland-Hodgman.
CMPE 466 COMPUTER GRAPHICS Chapter 8 2D Viewing Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition by Donald Hearn,
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
March 1, 2009Dr. Muhammed Al-Mulhem1 ICS 415 Computer Graphics Clipping Dr. Muhammed Al-Mulhem March 1, 2009 Dr. Muhammed Al-Mulhem March 1, 2009.
Projection Matrix Tricks Eric Lengyel Outline  Projection Matrix Internals  Infinite Projection Matrix  Depth Modification  Oblique Near Clipping.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Clipping Week 8, Wed Mar.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Clipping II, Hidden Surfaces.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Interpolation Clipping.
Informationsteknologi Thursday, November 22, 2007Computer Graphics - Class 111 Today’s class Clipping Parametric and point-normal form of lines Intersecting.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Texturing, Clipping Week 9, Mon 27 Oct 2003
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Clipping.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Now Playing: California Stars Billy Bragg & Wilco from Mermaid Avenue
Graphics Pipeline Clipping CMSC 435/634. Graphics Pipeline Object-order approach to rendering Sequence of operations – Vertex processing – Transforms.
2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view.
UBI 516 Advanced Computer Graphics Two Dimensional Viewing ( Chapter 6 ) Aydın Öztürk Two Dimensional.
CS 325 Introduction to Computer Graphics 03 / 03 / 2010 Instructor: Michael Eckmann.
2-Dimension Viewing and Clipping
Clipping Apart from clipping to the view volume, clipping is a basic operation in many other algorithms –Breaking space up into chunks –2D drawing and.
Transformations Aaron Bloomfield CS 445: Introduction to Graphics
Basics of Rendering Pipeline Based Rendering –Objects in the scene are rendered in a sequence of steps that form the Rendering Pipeline. Ray-Tracing –A.
CS 551 / 645: Introductory Computer Graphics
CS 376 Introduction to Computer Graphics 02 / 12 / 2007 Instructor: Michael Eckmann.
02/26/02 (c) 2002 University of Wisconsin, CS 559 Last Time Canonical view pipeline Orthographic projection –There was an error in the matrix for taking.
CS 325 Introduction to Computer Graphics 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
CSE Real Time Rendering Week 9. Post Geometry Shaders Courtesy: E. Angel and D. Shreiner – Interactive Computer Graphics 6E © Addison-Wesley 2012.
David Luebke11/14/2015 CS 551 / 645: Introductory Computer Graphics David Luebke
Windows, Viewports, and Clipping
Basic Perspective Projection Watt Section 5.2, some typos Define a focal distance, d, and shift the origin to be at that distance (note d is negative)
10/19/04© University of Wisconsin, CS559 Fall 2004 Last Time Clipping –Why we care –Sutherland-Hodgman –Cohen-Sutherland –Intuition for Liang-Barsky Homework.
1 Computer Graphics Clipping Fall FCC Line Clipping What happens when one or both endpoints of a line segment are not inside the specified drawing.
Transformations: Projection CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Clipping Primitives. Clipping line Clipping rectangle: – x min to x max – y min to y max A point (x,y) lies within a clip rectangle and thus displayed.
Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.
David Luebke2/23/2016 CS 551 / 645: Introductory Computer Graphics Color Continued Clipping in 3D.
Computer Graphics Viewing. 2 of 30 Viewing in 2D Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
CS552: Computer Graphics Lecture 12: 3D Clipping.
Computer Graphics Clipping.
CS 551 / 645: Introductory Computer Graphics
Transformations contd.
CS 551 / 645: Introductory Computer Graphics
Computer Graphics Shading in OpenGL
Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
3D Clipping.
Implementation I Ed Angel
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Lecture 13 Clipping & Scan Conversion
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
© University of Wisconsin, CS559 Fall 2004
Clipping Polygons Dr. Scott Schaefer.
Clipping University of British Columbia CPSC 314 Computer Graphics
Implementation I Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by David Luebke)

2 Outline  Review Clipping Basics Cohen-Sutherland Line Clipping Clipping Polygons Sutherland-Hodgman Clipping Perspective Clipping

3 Recap: Homogeneous Coords Intuitively: The w coordinate of a homogeneous point is typically 1 Decreasing w makes the point “bigger”, meaning further from the origin Homogeneous points with w = 0 are thus “points at infinity”, meaning infinitely far away in some direction. (What direction?) To help illustrate this, imagine subtracting two homogeneous points: the result is (as expected) a vector

4 Recap: Perspective Projection When we do 3-D graphics, we think of the screen as a 2-D window onto the 3-D world: How tall should this bunny be?

5 Recap: Perspective Projection The geometry of the situation: Desired result: P (x, y, z)X Z View plane d (0,0,0) x’ = ?

6 Recap: Perspective Projection Matrix Example: Or, in 3-D coordinates:

7 Recap: OpenGL’s Persp. Proj. Matrix OpenGL’s gluPerspective() command generates a slightly more complicated matrix: Can you figure out what this matrix does?

8 Projection Matrices Now that we can express perspective foreshortening as a matrix, we can composite it onto our other matrices with the usual matrix multiplication End result: can create a single matrix encapsulating modeling, viewing, and projection transforms Though you will recall that in practice OpenGL separates the modelview from projection matrix (why?)

9 Outline Review  Clipping Basics Cohen-Sutherland Line Clipping Clipping Polygons Sutherland-Hodgman Clipping Perspective Clipping

10 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

11 Clipping Analytically calculating the portions of primitives within the viewport

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

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

14 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. x min x max y max y min

15 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 x min x max y max y min

16 Outline Review Clipping Basics  Cohen-Sutherland Line Clipping Clipping Polygons Sutherland-Hodgman Clipping Perspective Clipping

17 Cohen-Sutherland Line Clipping Divide viewplane into regions defined by viewport edges Assign each region a 4-bit outcode: x min x max y max y min

18 Cohen-Sutherland Line Clipping To what do we assign outcodes? How do we set the bits in the outcode? How do you suppose we use them? x min x max y max y min

19 Cohen-Sutherland Line Clipping Set bits with simple tests x > x max y < y min etc. Assign an outcode to each vertex of line If both outcodes = 0, trivial accept bitwise AND vertex outcodes together If result  0, trivial reject As those lines lie on one side of the boundary lines y max y min

20 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

21 Outcode tests and line-edge intersects are quite fast (how fast?) But some lines require multiple iterations: Clip top Clip left Clip bottom Clip right Fundamentally more efficient algorithms: Cyrus-Beck uses parametric lines Liang-Barsky optimizes this for upright volumes Cohen-Sutherland Line Clipping

22 Outline Review Clipping Basics Cohen-Sutherland Line Clipping  Clipping Polygons Sutherland-Hodgman Clipping Perspective Clipping

23 Clipping Polygons We know how to clip a single line segment How about a polygon in 2D? How about in 3D? Clipping polygons is more complex than clipping the individual lines Input: polygon Output: polygon, or nothing When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?

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

25 A really tough case: Why Is Clipping Hard?

26 A really tough case: Why Is Clipping Hard? concave polygon  multiple polygons

27 Outline Review Clipping Basics Cohen-Sutherland Line Clipping Clipping Polygons  Sutherland-Hodgman Clipping Perspective Clipping

28 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

29 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

30 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

31 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

32 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

33 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

34 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

35 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

36 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped

37 Sutherland-Hodgman Clipping Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped Will this work for non-rectangular clip regions? What would 3-D clipping involve?

38 Sutherland-Hodgman Clipping Input/output for algorithm: Input: list of polygon vertices in order Output: list of clipped polygon vertices consisting of old vertices (maybe) and new vertices (maybe) Note: this is exactly what we expect from the clipping operation against each edge This algorithm generalizes to 3-D Show movie…

39 Sutherland-Hodgman Clipping We need to be able to create clipped polygons from the original polygons 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

40 Sutherland-Hodgman Clipping Edge from s to p takes one of four cases: (Purple line can be a line or a plane) insideoutside s p p output insideoutside s p no output insideoutside s p i output insideoutside s p i output p output

41 Sutherland-Hodgman 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

42 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 P n p q P n p q P n p q

43 Point-to-Plane Test Dot product is relatively expensive 3 multiplies 5 additions 1 comparison (to 0, in this case) Think about how you might optimize or special- case this

44 Finding Line-Plane Intersections Use parametric definition of edge: E(t) = s + t(p - s) If t = 0 then E(t) = s If t = 1 then E(t) = p Otherwise, E(t) is part way from s to p

45 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 (E(t) - q) n = 0 (s + t(p - s) - q) n = 0 t = [(q - s) n] / [(p - s) n] The intersection point i = E(t) for this value of t

46 Line-Plane Intersections Note that the length of n doesn’t affect result: t = [(q - s) n] / [(p - s) n] Again, lots of opportunity for optimization

47 Outline Review Clipping Basics Cohen-Sutherland Line Clipping Clipping Polygons Sutherland-Hodgman Clipping  Perspective Clipping

48 3-D Clipping Before actually drawing on the screen, we have to clip (Why?) Can we transform to screen coordinates first, then clip in 2D? Correctness: shouldn’t draw objects behind viewer What will an object with negative z coordinates do in our perspective matrix?

49 Recap: Perspective Projection Matrix Example: Or, in 3-D coordinates: Multiplying by the projection matrix gets us the 3-D coordinates The act of dividing x and y by z/d is called the homogeneous divide

50 Clipping Under Perspective Problem: after multiplying by a perspective matrix and performing the homogeneous divide, a point at (-8, -2, -10) looks the same as a point at (8, 2, 10). Solution A: clip before multiplying the point by the projection matrix I.e., clip in camera coordinates Solution B: clip after the projection matrix but before the homogeneous divide I.e., clip in homogeneous screen coordinates

51 Clipping Under Perspective We will talk first about solution A: Clip against view volume Apply projection matrix and homogeneous divide Transform into viewport for 2-D display 3-D world coordinate primitives Clipped world coordinates 2-D device coordinates Canonical screen coordinates

52 Recap: Perspective Projection The typical view volume is a frustum or truncated pyramid x or y z

53 Perspective Projection The viewing frustum consists of six planes The Sutherland-Hodgeman algorithm (clipping polygons to a region one plane at a time) generalizes to 3-D Clip polygons against six planes of view frustum So what’s the problem? The problem: clipping a line segment to an arbitrary plane is relatively expensive Dot products and such

54 Perspective Projection In fact, for simplicity we prefer to use the canonical view frustum: x or y 1 z Front or hither plane Back or yon plane Why is this going to be simpler? Why is the yon plane at z = -1, not z = 1?

55 Clipping Under Perspective So we have to refine our pipeline model: Note that this model forces us to separate projection from modeling & viewing transforms Apply normalizing transformation projection matrix; homogeneous divide Transform into viewport for 2-D display 3-D world coordinate primitives 2-D device coordinates Clip against canonical view volume

56 Clipping Homogeneous Coords Another option is to clip the homogeneous coordinates directly. This allows us to clip after perspective projection: What are the advantages? Clip against view volume Apply projection matrix Transform into viewport for 2-D display 3-D world coordinate primitives 2-D device coordinates Homogeneous divide

57 Clipping Homogeneous Coords Other advantages: Can transform the canonical view volume for perspective projections to the canonical view volume for parallel projections Clip in the latter (only works in homogeneous coords) Allows an optimized (hardware) implementation Some primitives will have w  1 For example, polygons that result from tesselating splines Without clipping in homogeneous coords, must perform divide twice on such primitives

58 Clipping Homogeneous Coords So how do we clip homogeneous coordinates? Briefly, thus: Remember that we have applied a transform to normalized device coordinates x, y  [-1, 1] z  [0, 1] When clipping to (say) right side of the screen (x = 1), instead clip to (x = w) Can find details in book or on web

59 Clipping: The Real World In some renderers, a common shortcut used to be: But in today’s hardware, everybody just clips in homogeneous coordinates Projection matrix; homogeneous divide Clip in 2-D screen coordinates Clip against hither and yon planes Transform into screen coordinates