CG-lecture05 - Computer Graphics Lecture 5 Clipping The...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Graphics Lecture 5 Clipping The Rendering Pipeline Transform Illuminate ModelWorld WorldCamera Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display Why clip? We don’t want to waste time rendering objects that are We outside the viewing window (or clipping window) viewing Why Clip? Bad idea to rasterize outside of framebuffer Bad bounds Also, don’t waste time scan converting pixels outside window outside What is clipping? Analytically calculating the portions of primitives Analytically within the view window within Clip to what? View Window Up Eye position (focal point) Back Towards Right Frustum Clip to what? Frustum Why illuminate before clipping? ModelWorld Transform Illuminate WorldCamera Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display Why World Camera before clipping? ModelWorld Transform Illuminate WorldCamera Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display After Clipping ModelWorld Transform Illuminate WorldCamera Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display Canonical View Volume A standardized viewing volume representation Parallel (Orthogonal) Parallel x or y Front Plane -1 -1 x or y Back Plane 1 Perspective -z Front Plane x or y = +/- z Back Plane -z Why do we care? Canonical View Volume Permits Standardization • Clipping – Easier to determine if an arbitrary point is enclosed in Easier volume volume – Consider clipping to six arbitrary planes of a viewing Consider volume versus canonical view volume volume • Projection – Projection is simplified Projection Normalization One additional step of standardization • Convert perspective view volume to orthogonal view volume Convert to further standardize camera representation to – Convert all projections into orthogonal projections by Convert distorting points in 3D space distorting Distort objects using transformation matrix Projection Normalization Building a transformation Building matrix matrix • How do we build a matrix that – Warps any view volume to Warps canonical orthographic view volume volume – Permits rendering with Permits orthographic camera orthographic All scenes rendered with orthographic camera Projection Normalization - Persp Perspective Normalization is Trickier x = az y = bz x = −a y = −b Perspective Normalization Consider N= 1 0 0 0 00 10 0α 0 −1 0 0 β 0 After multiplying: x ' = x • p’ = Np y' = y z ' = αz + β w' = − z Consider d=1. When the viewing angle is fixed, the d only affect the size of image. Perspective Normalization After dividing by w’, p’ -> p’’ x =x ' y =y ' z = αz + β ' w = −z ' x x =− z y '' y =− z '' z =− α + ( '' β z ) Perspective Normalization Quick Check x '' x =− z y '' y =− z z =− α + ( '' • If x = az – x’’ = -a • If y = bz – x’’ = -b β z ) Perspective Normalization z=-1 the projection image is invariant z=-1 Perspective Normalization What about z? • if z = zmax • if z = zmin z '' = − α + ( z =− α + ( '' β z max β z min • Solve for α and β such that Solve zmin -1 and zmax 1 • Resulting z’’ is nonlinear, but Resulting preserves ordering of points preserves – If z1 < z2 … z’’1 < z’’2 z = −(α + '' ) β z ) 0 z1 ) z2 +∞ +∞ Z 0 z2 z1 1/Z +∞ 0 z1 z2 -1/Z Perspective Normalization We did it. Using matrix, N • Perspective viewing frustum transformed to cube • Orthographic rendering of cube produces same image as Orthographic perspective rendering of original frustum perspective Projection Normalization - Ortho Normalizing Orthographic Normalizing Cameras Cameras • Not all orthographic cameras Not define viewing volumes of right size and location (canonical view volume) volume) • Transformation must map: xmin → 1 − xmax → 1 y min → 1 − y max → 1 z min → 1 z max → 1 − Projection Normalization - Ortho • Scale volume to cube with sides Scale =2 – Scale x by 2/(xmax – xmin) – Scale y by 2/(ymax – ymin) • Compose these transformation Compose matrices matrices – Resulting matrix maps Resulting orthogonal volume to canonical canonical Aspect ratio between vertical and horizontal viewing angles in projection matrix is introduced in this step. Trivial Accepts Big optimization: trivial accept/rejects Big How can we quickly determine whether a line segment is How entirely inside the view window? entirely A: test both endpoints. Trivial Rejects How can we know a line is outside view window? A: if both endpoints on wrong side of same edge, A: same can trivially reject line can Clipping Lines To Viewport Combining trivial accepts/rejects • Trivially accept lines with both endpoints inside all edges of the view window Trivially accept inside • Trivially reject lines with both endpoints outside the same edge of the view Trivially window window • Otherwise, reduce to trivial cases by splitting into two segments by Cohen-Sutherland Line Clipping • Divide view window into regions defined by window edges Divide view window • Assign each region a 4-bit outcode: Assign outcode – Bit 1 indicates y-value of points are above ymax 1001 1000 1010 0001 0000 0010 0101 0100 0110 ymax xmax Cohen-Sutherland Line Clipping For each line segment • Assign an outcode to each vertex Assign • If both outcodes = 0, trivial accept – Same as performing if (bitwise OR = 0) OR Same • Else – bitwise AND vertex outcodes together bitwise AND – iif result ≠ 0, trivial reject f Cohen-Sutherland Line Clipping • If line cannot be trivially accepted or rejected, subdivide so If that one or both segments can be discarded that – Pick an edge of view window that the line crosses (how?) – Intersect line with edge (how?) – Discard portion on wrong side of edge and assign new Discard outcode to new vertex outcode – Apply trivial accept/reject tests; repeat if necessary Apply Cohen-Sutherland Line Clipping If line cannot be trivially accepted or rejected, subdivide so that If one or both segments can be discarded one Pick an edge that the line crosses • Check against edges in same order each time – For example: right, top, left, bottom For top, C B A D E Cohen-Sutherland Line Clipping Intersect line with edge (how?) C B A D E Cohen-Sutherland Line Clipping Discard portion on wrong side of edge and assign outcode to new vertex new Apply trivial accept/reject tests and repeat if necessary Apply C B A D Cohen-Sutherland Line Clipping Discard portion on wrong side of edge and assign outcode to new vertex new Apply trivial accept/reject tests and repeat if necessary Apply C B A View Window Intersection Code • (x1, y1), (x2, y2) intersect with vertical edge at xright – yintersect = y1 + m(xright – x1) where m=(y2-y1)/(x2-x1) • (x1, y1), (x2, y2) intersect with horizontal edge at ybottom – xintersect = x1 + (ybottom – y1)/m where 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 viewing window 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 (t) t, – x(t) = x0 + (x1 - x0) t – y(t) = y0 + (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: Define • P0(t)…Pn-1(t) Define each edge of view window in parametric Define form: form: • PL(t), PR(t), PT(t), PB(t) Perform Cohen-Sutherland intersection tests Perform using appropriate view window edge and line using Line / Edge Clipping Equations Faster line clippers use parametric equations Line 0: View Window Edge L: • x0 = x00 + (x01 - x00) t0 • xL = xL0 + (xL1 - xL0) tL • y0 = y00 + (y01 - y00) t0 • 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 – P L, N L Cyrus-Beck Algorithm Find t such that PL NL [P(t) - PL] = 0 P(t) P0 Substitute line equation for P(t): Substitute • NL [P0 + (P1 - P0) t - PL] = 0 Solve for t • t = NL [PL – P0] / -NL [P1 - P0] Inside NL 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) 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 so we can keep Note it around it Cyrus-Beck Algorithm Compute PE with largest t Compute Compute PL with smallest t Clip to these two points PE PE P 0 PL PL P 1 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 Clipping the individual lines the • Input: polygon • Output: original polygon, new polygon, or nothing The biggest optimizer we had was trivial accept or The reject… reject… When can we trivially accept/reject a polygon as When opposed to the line segments that make up the polygon? polygon? Why Is Clipping Hard? What happens to a triangle during clipping? What 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-Hodgman Clipping Basic idea: • Consider each edge of the view window individually • Clip the polygon against the view window edge’s equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman Clipping Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation Sutherland-Hodgman 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-Hodgman Clipping Input/output for algorithm: • Input: list of polygon vertices in order Input: • Output: list of clipped polygon vertices consisting of Output: old vertices (maybe) and new vertices (maybe) old Note: this is exactly what we expect from the Note: clipping operation against each edge clipping Sutherland-Hodgman Clipping Sutherland-Hodgman basic routine: • Go around polygon one vertex at a time • Current vertex has position p Current • Previous vertex had position s, and it has been added to Previous and the output if appropriate the Sutherland-Hodgman Clipping Edge from s to p takes one of four cases: Edge takes (Orange line can be a line or a plane) inside outside inside outside inside s p p output s i output p outside p inside p s no output i output p output outside s Sutherland-Hodgman Clipping Four cases: • s inside plane and p inside plane – Add p to output Add – Note: s has already been added Note: • s inside plane and p outside plane – Find intersection point i Find – Add i to output Add Sutherland-Hodgman Clipping • s outside plane and p outside plane outside – Add nothing • s outside plane and p inside plane – Find intersection point i Find – Add i to output, followed by p Add Point-to-Plane test A very general test to determine if a point p is “inside” very a plane P, defined by q and n: defined (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 (θ ) Remember: |p| θ = angle between p and n angle q q n p P q n p P p P n Finding Line-Plane Intersections Edge intersects plane P where E(t) is on P (t) Edge • q is a point on P • n is normal to P (L(t) - q) • n = 0 (L0 + (L1 - L0) t - q) • n = 0 t = [(q - L0) • n] / [(L1 - L0) • n] • The intersection point i = L(t) for this value of t The ...
View Full Document

This note was uploaded on 06/12/2011 for the course ECON 101 taught by Professor Professor during the Spring '10 term at Cisco Junior College.

Ask a homework question - tutors are online