From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng E-mail:...

83
From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: [email protected]

Transcript of From Vertices to Fragments Software College, Shandong University Instructor: Zhou Yuanfeng E-mail:...

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

From Vertices to Fragments

Software College, Shandong University

Instructor: Zhou Yuanfeng E-mail: [email protected]

Page 2: 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-Sutherland2

ProjectionFragmentsClippingShading

Black box Surface hiddenTextureTransparency

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

3

Objectives

•Geometric Processing:

Cyrus-Beck clipping algorithm

Liang-Barsky clipping algorithm• Introduce clipping algorithm for polygons•Rasterization: DDA & Bresenham

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

Cohen-Sutherland

• In case IV:

•o1 & o2 = 0

• Intersection: Clipping Lines by Solving Simultaneous Equations

4

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

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

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

6

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)

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

•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:

7

Cyrus-Beck Algorithm

P1

P2

A

N

R

para tspara te

112 )()( PtPPtP 0≤t≤1

0))(( AtPN )(tP,then is inside of R;

0))(( AtPN )(tP

0))(( AtPN )(tP

,then is on R or extension;

,then is outside of R.

cos)(

))((

AtPN

AtPN

0cos 90

0cos 90

0cos 90

How to get ts and te

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

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

OutsideP0

P1

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

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

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

Cyrus-Beck Algorithm

•For each edge:

10

L1

L2L3

L4

L5

P0

P1

t1

t2

t5

t3t4

para tspara te

0 1 0( ) ( ) 0, 0 1i i i i iP A P P t t N N

1 0

1 0

max{0,max{ | ( ) 0}}

min{1,min{ | ( ) 0}}s i i

e i i

t t P P

t t P P

N

NCompute PE with largest tCompute PL with smallest tClip to these two points

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

Cyrus-Beck Algorithm

•When ; then • if

• if

11

1 0( ) 0i P P N

0( ) 0i iP A N

Then P0P1 is invisible;

0( ) 0i iP A N

Then go to next edge;

1 0( )i P P N

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

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

} }

1 0min{1,min{ | ( ) 0}}e i it t P P N

1 0max{0,max{ | ( ) 0}}s i it t P P N

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;

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

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.

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

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

Liang-Barsky Algorithm (1984)

PE

PLP1

PL

PE

P0

(-1, 0)(1, 0)(0, -1)

(0, 1)

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

15

)(

)(

12

1

PP

APt

N

N

)(

)(

12

1

xx

XLx

12

1 )(

xx

XRx

)(

)(

12

1

yy

YBy

12

1 )(

yy

YTy

EdgeInner

normalA P1-A

Leftx=XL

( 1 ,0 )

( XL ,y )

( x1-XL , y1-

y )

Rightx=XR

( -1 ,0 )

( XR ,y ) ( x1-XR , y1-y )

Bottomy=YB

( 0 ,1 )

( x , YB ) ( x1-x , y1-YB )

Topy=YT

( 0 , -1 )

( x , YT ) ( x1-x , y1-YT )

Liang-Barsky Algorithm (1984)

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

• 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

16

Liang-Barsky Algorithm (1984)

Let ∆x=x2 - x1 ,∆ y=y2 - y1:

,

,

,

,

4

3

2

1

yr

yr

xr

xr

,

,

,

,

14

13

12

11

yys

yys

xxs

xxs

T

B

R

L

TBRLkrst kkk , , , , /

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

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

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

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

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

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

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

20

Clipping and Normalization

•General clipping in 3D requires intersection of line segments against arbitrary plane

•Example: oblique view

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

21

Plane-Line Intersections

0 1

2 1

( )

( )

n p pt

n p p

0 1 2 1(( ( ( ))) n 0P P t P P

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

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?

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

23

Normalized Form

before normalization after normalization

Normalization is part of viewing (pre clipping)but after normalization, we clip against sides ofright parallelepiped

Typical intersection calculation now requires onlya floating point subtraction, e.g. is x > xmax

top view

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

Clipping Polygons

•Clipping polygons is more complex than clipping the individual lines

Input: polygon

Output: polygon, or nothing

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

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

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

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

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

27

Pipeline Clipping of Polygons

• Three dimensions: add front and back clippers

• Strategy used in SGI Geometry Engine

• Small increase in latency

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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

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

no output

inside outside

sp

i output

inside outside

sp

i outputp output

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

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

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

Sutherland-Hodgman Clipping

42

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

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

np

q

P

np

q

P

np

q

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

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

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

45

Bounding Boxes

Can usually determine accept/reject based only on bounding box

reject

accept

requires detailed clipping

Ellipsoid collision detection

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

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.

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

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

x

ym

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

Scan Conversion of Line Segments

48

One pixel

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

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 = y2-y1/x2-x1

• 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);}

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

50

Problem

•DDA = for each x plot pixel at closest y Problems for steep lines

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

51

Using Symmetry

•Use for 1 m 0•For m > 1, swap role of x and y

For each y, plot closest x

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

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.

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

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

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

54

Candidate Pixels

1 m 0

last pixel

candidates

Note that line could havepassed through anypart of this pixel

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

55

Decision Variable

-

d = x(a-b)△

d is an integerd < 0 use upper pixeld > 0 use lower pixel

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

AB

C

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

Incremental Form

56

if ε(xi+1) ≥ 0, yi+1,r= yi,r+1, pick pixel D, the next pixel is

( xi+1, yi,r+1)

yi,r

yi,r+1

AB

xixi+1

D

C

d1

d2

yi,r

yi,r+1

A

xi xi+1

D

Cd1

d2

if ε(xi+1) < 0, yi+1,r= yi,r, pick pixel C, the next pixel is

( xi+1, yi,r)

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

Improvement

anew = alast – m anew = alast – (m-1)

bnew = blast + m bnew = blast + (m-1)

57

- -

- -

d = x(a-b)△

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

Improvement

58

•More efficient if we look at dk, the value of the decision variable at x = k

dk+1= dk – 2 y, if d△ k > 0dk+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.

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

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

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

60

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

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

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

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

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

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

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

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

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

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

65

Hard Cases

(3) Overlap in all directionsbut can one is fully on one side of the other

cyclic overlap

penetration

(4)

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

66

Back-Face Removal (Culling)

•face is visible iff 90 -90equivalently cos 0or v • n 0

•plane of face has form ax + by +cz +d =0but after normalization n = ( 0 0 1 0)T

•need only test the sign of c

•In OpenGL we can simply enable cullingbut may not work correctly if we have nonconvex objects

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

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

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

68

Efficiency

• If we work scan line by scan line as we move across a scan line, the depth changes satisfy ax+by+cz=0

Along scan line

y = 0z = - x

c

a

In screen space x = 1

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

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 noor one polygon

scan line j: need depth information only when inmore than one polygon

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

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

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

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

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

72

Simple Example

consider 6 parallel polygons

top view

The plane of A separates B and C from D, E and F

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

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

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

74

Polygon Scan Conversion

•Scan Conversion = Fill•How to tell inside from outside

Convex easy

Nonsimple difficult

Odd even test• Count edge crossings

Winding numberodd-even fill

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

75

Winding Number

•Count clockwise encirclements of point

•Alternate definition of inside: inside if winding number 0

winding number = 2

winding number = 1

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

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

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

77

Using Interpolation

span

C1

C3

C2

C5

C4scan line

C1 C2 C3 specified by glColor or by vertex shadingC4 determined by interpolating between C1 and C2

C5 determined by interpolating between C2 and C3

interpolate between C4 and C5 along span

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

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

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

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

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

80

Data Structure

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

81

Aliasing

• Ideal rasterized line should be 1 pixel wide

•Choosing best y for each x (or visa versa) produces aliased raster lines

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

82

Antialiasing by Area Averaging

• Color multiple pixels for each x depending on coverage by ideal line

original antialiased

magnified

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

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