Download presentation

Presentation is loading. Please wait.

Published byMarcella Coster Modified about 1 year ago

1

2
From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng

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

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

5
Cohen-Sutherland In case IV: o 1 & o 2 = 0 Intersection: Clipping Lines by Solving Simultaneous Equations 4

6
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, P 0 and P 1 P(t) = P 0 + (P 1 - P 0 ) t x(t) = x 0 + (x 1 - x 0 ) t y(t) = x 0 + (y 1 - y 0 ) t

7
6 Parametric Lines and Intersections For L 1 : x=x0 l1 + t(x1 l1 – x0 l1 ) y=y0 l1 + t(y1 l1 – y0 l1 ) For L 2 : x=x0 l2 + t(x1 l2 – x0 l2 ) y=y0 l2 + t(y1 l2 – y0 l2 ) The Intersection Point: x0 l1 + t 1 (x1 l1 – x0 l1 ) = x0 l2 + t 2 (x1 l2 – x0 l2 ) y0 l1 + t 1 (y1 l1 – y0 l1 ) = y0 l2 + t 2 (y1 l2 – y0 l2 )

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

9
Cyrus-Beck Algorithm Intersection: N L ● (P(t) – A) = 0 Substitute line equation for P(t) P(t) = P 0 + t(P 1 - P 0 ) Solve for t t = N L ● (P 0 – A) / -N L ● (P 1 - P 0 ) A NLNL P(t) Inside Outside P0P0 P1P1

10
Cyrus-Beck Algorithm Compute t for line intersection with all edges; Discard all (t 1); Classify each remaining intersection as Potentially Entering Point (P E ) Potentially Leaving Point (P L ) (How?) N L ● (P 1 - P 0 ) < 0 implies P L N L ● (P 1 - P 0 ) > 0 implies P E Note that we computed this term in when computing t

11
Cyrus-Beck Algorithm For each edge: 10 L1 L2 L3 L4 L5 P0P0 P1P1 t1 t2 t5 t3 t4 para ts para te Compute P E with largest t Compute P L with smallest t Clip to these two points

12
Cyrus-Beck Algorithm When ; then if 11 Then P 0 P 1 is invisible; Then go to next edge;

13
Programming: 12 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 ) else } Output: if ( ts > te ) return nil; else return P(ts) and P(te) as the true clip intersections; Input: If (P0 = P1 ) Line is degenerate so clip as a point;

14
Liang-Barsky Algorithm (1984) 13 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.

15
Because of horizontal and vertical clip lines: Many computations reduce Normals Pick constant points on edges solution for t: t L =-(x 0 - x left ) / (x 1 - x 0 ) t R =(x 0 - x right ) / -(x 1 - x 0 ) t B =-(y 0 - y bottom ) / (y 1 - y 0 ) t T =(y 0 - y top ) / -(y 1 - y 0 ) Liang-Barsky Algorithm (1984) PEPE PLPL P1P1 PLPL PEPE P0P0 (-1, 0) (1, 0) (0, -1) (0, 1)

16
15 Edge Inner normal AP1-AP1-A Left x=XL （1，0）（1，0）（ XL ， y ）（ x 1 -XL ， y 1 -y ） Right x=XR （ -1 ， 0 ）（ XR ， y ）（ x 1 -XR ， y 1 -y ） Bottom y=YB （0，1）（0，1）（ x ， YB ）（ x 1 -x ， y 1 -YB ） Top y=YT （ 0 ， -1 ）（ x ， YT ）（ x 1 -x ， y 1 -YT ） Liang-Barsky Algorithm (1984)

17
When r k 0, t k is leaving point. If r k =0 and s k <0, then the line is invisible; else process other edges 16 Liang-Barsky Algorithm (1984) Let ∆x=x2 － x1 ， ∆y=y2 － y1:

18
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

19
18 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

20
19 Pipeline Clipping of Line Segments Clipping against each side of window is independent of other sides Can use four independent clippers in a pipeline

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

22
21 Plane-Line Intersections

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

24
23 Normalized Form before normalizationafter 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 > x max top view

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

26
25 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

27
26 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

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

29
Sutherland-Hodgman Clipping Ivan Sutherland, Gary W. Hodgman: Reentrant Polygon Clipping. Communications of the ACM, vol. 17, pp , 1974 Basic idea: Consider each edge of the viewport individually Clip the polygon against the edge equation

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

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

39
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

40
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

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

42
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

43
Sutherland-Hodgman Clipping 42

44
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

45
44 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

46
45 Bounding Boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping Ellipsoid collision detection

47
46 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.

48
47 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

49
Scan Conversion of Line Segments 48 One pixel

50
49 DDA Algorithm 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 = y/ x = y 2 -y 1 /x 2 -x 1 Along scan line x = 1 For(x=x1; x<=x2, x++) { y += m; //note:m is float number write_pixel(x, round(y), line_color); }

51
50 Problem DDA = for each x plot pixel at closest y Problems for steep lines

52
51 Using Symmetry Use for 1 m 0 For m > 1, swap role of x and y For each y, plot closest x

53
52 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.

54
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 53

55
54 Candidate Pixels 1 m 0 last pixel candidates Note that line could have passed through any part of this pixel

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

57
Incremental Form 56 if ε(x i+1 ) ≥ 0, y i+1,r = y i,r +1, pick pixel D, the next pixel is ( x i+1, y i,r +1) y i,r y i,r +1 A B xixi x i+1 D C d1 d2 y i,r y i,r +1 A xixi x i+1 D C d1 d2 if ε(x i+1 ) < 0, y i+1,r = y i,r, pick pixel C, the next pixel is ( x i+1, y i,r )

58
Improvement a new = a last – m a new = a last – (m-1) b new = b last + m b new = b last + (m-1) d = △ x(a-b)

59
Improvement 58 More efficient if we look at d k, the value of the decision variable at x = k d k+1 = d k – 2 △ y, if d k > 0 d k+1 = d k – 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.

60
59 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.front,w); Draw(T.face,w); DrawBSP(T. back,w); Endif end

61
60 Hidden Surface Removal Object-space approach: use pairwise testing between polygons (objects) Worst case complexity O(n 2 ) for n polygons partially obscuringcan draw independently

62
61 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

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

64
63 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

65
64 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

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

67
66 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 = ( ) 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

68
67 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

69
68 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

70
69 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

71
70 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

72
71 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

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

74
73 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

75
74 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

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

77
76 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

78
77 Using Interpolation span C1C1 C3C3 C2C2 C5C5 C4C4 scan line C 1 C 2 C 3 specified by glColor or by vertex shading C 4 determined by interpolating between C 1 and C 2 C 5 determined by interpolating between C 2 and C 3 interpolate between C 4 and C 5 along span

79
78 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); }

80
79 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

81
80 Data Structure

82
81 Aliasing Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines

83
82 Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line original antialiased magnified

84
83 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google