From Vertices to Fragments

Slides:



Advertisements
Similar presentations
Computer Graphics - Rasterization -
Advertisements

CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
Rezanje črt in poligonov. World window & viewport window viewport screen window world window.
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S Andries van Dam September 30, D Clipping 1/14 Clipping (pages , )
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
03/12/02 (c) 2002 University of Wisconsin, CS559 Last Time Some Visibility (Hidden Surface Removal) algorithms –Painter’s Draw in some order Things drawn.
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.
Computer Graphics Viewing.
Different types of Polygons Simple Convex Simple Concave Non-simple : self-intersecting With holes ConvexConcaveSelf-intersecting.
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.
Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters  Hill, Chapter 10.
Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley Mohan Sridharan Based on Slides.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Implementation III Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Hidden Surface Elimination Wen-Chieh (Steve) Lin Institute of Multimedia Engineering I-Chen Lin’ CG Slides, Rich Riesenfeld’s CG Slides, Shirley, Fundamentals.
Vertices and Fragments III Mohan Sridharan Based on slides created by Edward Angel 1 CS4395: Computer Graphics.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Introduction to 3D Graphics John E. Laird. Basic Issues u Given a internal model of a 3D world, with textures and light sources how do you project it.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 7: From Vertices to Fragments Ed Angel Professor of Computer Science, Electrical.
CS 325 Introduction to Computer Graphics 03 / 08 / 2010 Instructor: Michael Eckmann.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Objectives Introduce basic implementation strategies Clipping Scan conversion.
From Vertices to Fragments Chapter 7. Part I Objectives Introduce basic implementation strategies Introduce basic implementation strategies Clipping Clipping.
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
From Vertices to Fragments II Software College, Shandong University Instructor: Zhou Yuanfeng
1Computer Graphics Implementation III Lecture 17 John Shearer Culture Lab – space 2
Introduction to Computer Graphics with WebGL
CS 480/680 Computer Graphics Implementation III Dr. Frederick C Harris, Jr. Fall 2011.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Week 3 Lecture 1: Implementation Based on Interactive Computer Graphics (Angel) - Chapter.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1Computer Graphics Implementation II Lecture 16 John Shearer Culture Lab – space 2
Implementation II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CS 325 Introduction to Computer Graphics 03 / 22 / 2010 Instructor: Michael Eckmann.
Implementation II.
10/19/04© University of Wisconsin, CS559 Fall 2004 Last Time Clipping –Why we care –Sutherland-Hodgman –Cohen-Sutherland –Intuition for Liang-Barsky Homework.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Computer Graphics I, Fall 2010 Implementation II.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Computer Graphics CC416 Week 14 Filling Algorithms.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
Computer Graphics I, Fall 2010 Scan conversion algorithms.
Chapter 71 Computer Graphics - Chapter 7 From Vertices to Fragments Objectives are: How your program are processed by the system that you are using, Learning.
Computer Graphics Clipping.
Computer Graphics Implementation II
Introduction to Polygons
Transformations contd.
Computer Graphics Shading in OpenGL
University of New Mexico
Implementation III.
Introduction to Computer Graphics with WebGL
3D Rendering Pipeline Hidden Surface Removal 3D Primitives
Implementation II Ed Angel Professor Emeritus of Computer Science
Course code: 10CS65 | Computer Graphics and Visualization
Rasterization and Antialiasing
Lecture 13 Clipping & Scan Conversion
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Computer Graphics Implementation III
Rasterization and Antialiasing
(c) 2002 University of Wisconsin, CS559
Introduction to Computer Graphics with WebGL
From Vertices To Fragments
Clipping University of British Columbia CPSC 314 Computer Graphics
Implementation III Ed Angel Professor Emeritus of Computer Science
Implementation II Ed Angel Professor Emeritus of Computer Science
From Vertex to Fragment
From Vertex to Fragment
Presentation transcript:

From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com

Review Shading in OpenGL; Lights & Material; From vertex to fragment: Cohen-Sutherland Black box Projection Fragments Clipping Shading Surface hidden Texture Transparency

Objectives Geometric Processing: Cyrus-Beck clipping algorithm Liang-Barsky clipping algorithm Introduce clipping algorithm for polygons Rasterization: DDA & Bresenham

Cohen-Sutherland In case IV: o1 & o2 = 0 Intersection: Clipping Lines by Solving Simultaneous Equations

Solving Simultaneous Equations Equation of a line: Slope-intercept: y = mx + h difficult for vertical line Implicit Equation: Ax + By + C = 0 Parametric: Line defined by two points, P0 and P1 P(t) = P0 + (P1 - P0) t x(t) = x0 + (x1 - x0) t y(t) = x0 + (y1 - y0) t

Parametric Lines and Intersections For L1 : x=x0l1 + t(x1l1 – x0l1) y=y0l1 + t(y1l1 – y0l1) For L2 : x=x0l2 + t(x1l2 – x0l2) y=y0l2 + t(y1l2 – y0l2) The Intersection Point: x0l1 + t1 (x1l1 – x0l1) = x0l2 + t2 (x1l2 – x0l2) y0l1 + t1 (y1l1 – y0l1) = y0l2 + t2 (y1l2 – y0l2)

Cyrus-Beck Algorithm Cyrus-Beck algorithm (1978) for polygons Mike Cyrus, Jay Beck. "Generalized two- and three-dimensional clipping". Computers & Graphics, 1978: 23-28. Given a convex polygon R: P1 P2 A N R 0≤t≤1 ,then is inside of R; is on R or extension; is outside of R. para te para ts How to get ts and te

Cyrus-Beck Algorithm Intersection: NL ● (P(t) – A) = 0 Substitute line equation for P(t) P(t) = P0 + t(P1 - P0) Solve for t t = NL ● (P0 – A) / -NL ● (P1 - P0) A NL P(t) Inside Outside P0

Cyrus-Beck Algorithm Compute t for line intersection with all edges; Discard all (t < 0) and (t > 1); Classify each remaining intersection as Potentially Entering Point (PE) Potentially Leaving Point (PL) (How?) NL●(P1 - P0) < 0 implies PL NL●(P1 - P0) > 0 implies PE Note that we computed this term in when computing t

Cyrus-Beck Algorithm For each edge: Compute PE with largest t para te para ts t5 Compute PE with largest t Compute PL with smallest t Clip to these two points

Cyrus-Beck Algorithm When ; then if Then P0P1 is invisible; Then go to next edge;

Programming: Input: If (P0 = P1 ) for(k edges of clipping polygon) { solve Ni·(p1-p0); solve Ni·(p0-Ai); if ( Ni·(p1-p0) = = 0 ) //parallel with the edge if ( Ni·(p0-Ai) < 0 ) break; //invisible else go to next edge; } else // Ni·(p1-p0) != 0 solve ti; if ( Ni·(p1-p0) < 0 ) Input: If (P0 = P1 ) Line is degenerate so clip as a point; Output: if ( ts > te ) return nil; else return P(ts) and P(te) as the true clip intersections;

Liang-Barsky Algorithm (1984) The ONLY algorithm named for Chinese people in Computer Graphics course books Liang, Y.D., and Barsky, B., "A New Concept and Method for Line Clipping", ACM Transactions on Graphics, 3(1):1-22, January 1984.

Liang-Barsky Algorithm (1984) Because of horizontal and vertical clip lines: Many computations reduce Normals Pick constant points on edges solution for t: tL=-(x0 - xleft) / (x1 - x0) tR=(x0 - xright) / -(x1 - x0) tB=-(y0 - ybottom) / (y1 - y0) tT=(y0 - ytop) / -(y1 - y0) PE PL P1 P0 (0, -1) (1, 0) (-1, 0) (0, 1)

Liang-Barsky Algorithm (1984) Edge Inner normal A P1-A Left x=XL (1,0) (XL,y) (x1-XL, y1-y) Right x=XR (-1,0) (XR,y) (x1-XR,y1-y) Bottom y=YB (0,1) (x,YB) (x1-x,y1-YB) Top y=YT (0,-1) (x,YT) (x1-x,y1-YT)

Liang-Barsky Algorithm (1984) When rk<0, tk is entering point; when rk>0, tk is leaving point. If rk=0 and sk<0, then the line is invisible; else process other edges Let ∆x=x2-x1,∆y=y2-y1:

Comparison Cohen-Sutherland: Cyrus-Beck: 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 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

Clipping and Normalization General clipping in 3D requires intersection of line segments against arbitrary plane Example: oblique view

Plane-Line Intersections

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?

Normalized Form top view before normalization after normalization Normalization is part of viewing (pre clipping) but after normalization, we clip against sides of right parallelepiped Typical intersection calculation now requires only a floating point subtraction, e.g. is x > xmax

Clipping Polygons Clipping polygons is more complex than clipping the individual lines Input: polygon Output: polygon, or nothing

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 One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation) Also makes fill easier (we will study later) Tessellation code in GLU library, the best is Constrained Delaunay Triangulation

Pipeline Clipping of Polygons Three dimensions: add front and back clippers Strategy used in SGI Geometry Engine Small increase in latency

Sutherland-Hodgman Clipping Ivan Sutherland, Gary W. Hodgman: Reentrant Polygon Clipping. Communications of the ACM, vol. 17, pp. 32-42, 1974 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 planes, the polygon is fully clipped

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

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

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

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

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

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

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

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?

Sutherland-Hodgman 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-Hodgman 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-Hodgman Clipping Edge from s to p takes one of four cases: (Gray 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

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

Sutherland-Hodgman Clipping

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

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 Ellipsoid collision detection

Rasterization Rasterization (scan conversion) Determine which pixels that are inside primitive specified by a set of vertices Produces a set of fragments Fragments have a location (pixel location) and other attributes such color, depth and texture coordinates that are determined by interpolating values at vertices Pixel colors determined later using color, texture, and other vertex properties.

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

Scan Conversion of Line Segments One pixel

DDA Algorithm Along scan line Dx = 1 Digital Differential Analyzer (1964) DDA was a mechanical device for numerical solution of differential equations Line y=mx+h satisfies differential equation dy/dx = m = Dy/Dx = y2-y1/x2-x1 Along scan line Dx = 1 For(x=x1; x<=x2, x++) { y += m; //note:m is float number 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 (OpenGL has this definition) Bresenham, J. E. (1 January 1965). "Algorithm for computer control of a digital plotter". IBM Systems Journal 4(1): 25–30.

Bresenham’s Algorithm Observing: 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 candidates last pixel Note that line could have passed through any part of this pixel

Decision Variable d = △x(a-b) d is an integer d < 0 use upper pixel d > 0 use lower pixel A B C How to compute a and b? b-a =(yi+1–yi,r)-( yi,r+1-yi+1) =2yi+1–yi,r–(yi,r+1) = 2yi+1–2yi,r–1 ε(xi+1)= yi+1–yi,r–0.5 =BC-AC=BA=B-A = yi+1–(yi,r+ yi,r+1)/2

Incremental Form if ε(xi+1) ≥ 0, yi+1,r= yi,r+1, pick pixel D, the next pixel is ( xi+1, yi,r+1) if ε(xi+1) < 0, yi+1,r= yi,r, pick pixel C, the next pixel is ( xi+1, yi,r) yi,r yi,r+1 A B xi xi+1 D C d1 d2 yi,r yi,r+1 A xi xi+1 D C d1 d2

Improvement anew = alast – m anew = alast – (m-1) bnew = blast + m bnew = blast + (m-1) - - - - d = △x(a-b)

Improvement More efficient if we look at dk, the value of the decision variable at x = k dk+1= dk – 2△y, if dk > 0 dk+1= dk – 2(△y - △x), otherwise For each x, we need do only an integer addition and a test Single instruction on graphics chips multiply 2 is simple.

BSP display Type Tree Tree* front; Face face; Tree *back; End Algorithm DrawBSP(Tree T; point: w) //w 为视点 If T is null then return; endif If w is in front of T.face then DrawBSP(T.back,w); Draw(T.face,w); DrawBSP(T.front,w); Else // w is behind or on T.face DrawBSP(T. back,w); Endif end

Hidden Surface Removal Object-space approach: use pairwise testing between polygons (objects) Worst case complexity O(n2) for n polygons partially obscuring can draw independently

Image Space Approach Look at each projector (nm for an n x m frame buffer) and find closest of k polygons Complexity O(nmk) Ray tracing z-buffer

Painter’s Algorithm Render polygons a back to front order so that polygons behind others are simply painted over Fill B then A B behind A as seen by viewer

Depth Sort Requires ordering of polygons first O(n log n) calculation for ordering Not every polygon is either in front or behind all other polygons Order polygons and deal with easy cases first, harder later Polygons sorted by distance from COP

Easy Cases (1) A lies behind all other polygons Can render (2) Polygons overlap in z but not in either x or y Can render independently

Hard Cases cyclic overlap (4) (3) Overlap in all directions but can one is fully on one side of the other penetration

Back-Face Removal (Culling) face is visible iff 90    -90 equivalently cos   0 or v • n  0  plane of face has form ax + by +cz +d =0 but after normalization n = ( 0 0 1 0)T need only test the sign of c In OpenGL we can simply enable culling but may not work correctly if we have nonconvex objects

z-Buffer Algorithm Use a buffer called the z or depth buffer to store the depth of the closest object at each pixel found so far As we render each polygon, compare the depth of each pixel to depth in z buffer If less, place shade of pixel in color buffer and update z buffer

Efficiency If we work scan line by scan line as we move across a scan line, the depth changes satisfy ax+by+cz=0 Along scan line y = 0 z = - x In screen space x = 1

Scan-Line Algorithm Can combine shading and hsr through scan line algorithm scan line i: no need for depth information, can only be in no or one polygon scan line j: need depth information only when in more than one polygon

Implementation Need a data structure to store Flag for each polygon (inside/outside) Incremental structure for scan lines that stores which edges are encountered Parameters for planes

Visibility Testing In many realtime applications, such as games, we want to eliminate as many objects as possible within the application Reduce burden on pipeline Reduce traffic on bus Partition space with Binary Spatial Partition (BSP) Tree

Simple Example consider 6 parallel polygons top view The plane of A separates B and C from D, E and F

BSP Tree Can continue recursively Plane of C separates B from A Plane of D separates E and F Can put this information in a BSP tree Use for visibility and occlusion testing

Polygon Scan Conversion Scan Conversion = Fill How to tell inside from outside Convex easy Nonsimple difficult Odd even test Count edge crossings Winding number odd-even fill

Winding Number Count clockwise encirclements of point Alternate definition of inside: inside if winding number  0 winding number = 1 winding number = 2

Filling in the Frame Buffer Fill at end of pipeline Convex Polygons only Nonconvex polygons assumed to have been tessellated Shades (colors) have been computed for vertices (Gouraud shading) Combine with z-buffer algorithm March across scan lines interpolating shades Incremental work small

Using Interpolation C1 C2 C3 specified by glColor or by vertex shading C4 determined by interpolating between C1 and C2 C5 determined by interpolating between C2 and C3 interpolate between C4 and C5 along span C1 C4 C2 scan line C5 span C3

Flood Fill Fill can be done recursively if we know a seed point located inside (WHITE) Scan convert edges into buffer in edge/inside color (BLACK) flood_fill(int x, int y) { if(read_pixel(x,y)= = WHITE) { write_pixel(x,y,BLACK); flood_fill(x-1, y); flood_fill(x+1, y); flood_fill(x, y+1); flood_fill(x, y-1); } }

Scan Line Fill Can also fill by maintaining a data structure of all intersections of polygons with scan lines Sort by scan line Fill each span vertex order generated by vertex list desired order

Data Structure

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 antialiased original magnified

Polygon Aliasing Aliasing problems can be serious for polygons Jaggedness of edges Small polygons neglected Need compositing so color of one polygon does not totally determine color of pixel All three polygons should contribute to color