from vertices to fragments ii

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

Upload: simon-mcintosh

Post on 02-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

From Vertices to Fragments II. Software College, Shandong University Instructor: Zhou Yuanfeng E-mail: [email protected]. Review. Cyrus-Beck clipping algorithm; Liang-Barsky clipping algorithm; Sutherland-Hodgman polygon clipping; DDA line algorithm; Bresenham’s line algorithm;. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: From Vertices to Fragments II

From Vertices to Fragments II

Software College, Shandong University

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

Page 2: From Vertices to Fragments II

Review

•Cyrus-Beck clipping algorithm;•Liang-Barsky clipping algorithm;•Sutherland-Hodgman polygon clipping;•DDA line algorithm;•Bresenham’s line algorithm;

Page 3: From Vertices to Fragments II

3

Objectives

•Rasterization: Polygon scan conversion algorithm;

•Hidden surface removal•Aliasing

Page 4: From Vertices to Fragments II

4

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

Page 5: From Vertices to Fragments II

5

Winding Number

•Count clockwise encirclements of point

•Alternate definition of inside: inside if winding number 0

winding number = 2

winding number = 1

Page 6: From Vertices to Fragments II

OpenGL & Concave Polygon

•OpenGL can only fill convex polygon correctly

6

Page 7: From Vertices to Fragments II

OpenGL & Concave Polygon

// create tessellator

GLUtesselator *tess = gluNewTess();

// describe non-convex polygon

gluTessBeginPolygon(tess, user_data);

// first contour

gluTessBeginContour(tess);

gluTessVertex(tess, coords[0], vertex_data);

...

gluTessEndContour(tess);

...

gluTessEndPolygon(tess);

// delete tessellator after processing

gluDeleteTess(tess); 7

Page 8: From Vertices to Fragments II

Constrained Delaunay Triangulation

8

Page 9: From Vertices to Fragments II

9

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 10: From Vertices to Fragments II

10

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 11: From Vertices to Fragments II

11

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); } } //4 directions

Page 12: From Vertices to Fragments II

Flood Fill

12

//8 directions

Page 13: From Vertices to Fragments II

Flood Fill with Scan Line

13

1

2

1

2

10

2

2

2

2

3

32

2

2

2

2Stack

Page 14: From Vertices to Fragments II

14

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 15: From Vertices to Fragments II

Singularities

15

Page 16: From Vertices to Fragments II

16

Data Structure

Page 17: From Vertices to Fragments II

Scan line filling

• For each scan line:

1.Find the intersections of the scan line with all edges of the polygon;

2.Sort the intersections by increasing x-coordinate;

3.Fill in all pixels between pairs of intersections.

Problem:

Calculating intersections is slow.

Solution:

Incremental computation / coherence

17

Page 18: From Vertices to Fragments II

Coherence of Region

18

,,,1,0),,( niyxP iii niii yyy

10

876543210 854726013 ,,,,,,,, yyyyyyyyy

Trapezoid: inside and outside

Page 19: From Vertices to Fragments II

Coherence of Scan Line

• e is an integer; ≥e ≥

• Intersections number is even;• are inside; 19

654321 467320 ,,,,, eeeeee xxxxxx

insideinsideinside

0iy

niy

leieieiei xxxx ,,,,321

1,,5,3,1),(1

lkxxkk eiei

Page 20: From Vertices to Fragments II

Coherence of Edge

• Intersection points

of d is le and ld

The number of le = ld and are on the same edge

20

ed

y63

y241,, , kiki ydey

),( dxrdi),( ex

rei

rdrer mxx /1

Page 21: From Vertices to Fragments II

Coherence of Edge

• Observation: Not all edges intersect each scanline.

• Many edges intersected by scanline i will also be intersected by scanline i+1

• Formula for scanline s is y = s, for an edge is y = mx + b

• Their intersection is s = mxs + b --> xs = (s-b)/m

• For scanline s + 1, xs+1 = (s+1 - b)/m = xs + 1/m

• Incremental calculation: xs+1 = xs + 1/m

21

Page 22: From Vertices to Fragments II

Data Structure

22

Edge index table ET

Active Edge List AEL

Node:

ymax The max coord y;

x The bottom x coord of edge

in ET, the intersection x of edge

and scan line in AEL.

Δx 1/m

next the next node

Where is the end of one edge

while scanning

Initial x

Current scan line and edge intersection

When y=y+1, x=x+1/m

Next edge

Page 23: From Vertices to Fragments II

Data Structure ET

23

y

12

56

Page 24: From Vertices to Fragments II

Data Structure AEL

24

AEL is the edges list which intersect with

current scan line, the next intersection

can be computed by:

rdrer mxx /1

Active

Edge

List

-5/35 7

AEL

e0 e5

4 212

AEL at y=2 scan line

-5/35 5

AEL

e0 e5

4 214

AEL at y=3 scan line

2

ymax

x1/m

Edge list is ordered

according x increasing

Page 25: From Vertices to Fragments II

Algorithm

Construct the Edge Table (ET);

Active Edge List (AEL) = null;

for y = Ymin to Ymax

Merge-sort ET[y] into AEL by x value

Fill between pairs of x in AEL

for each edge in AEL

if edge.ymax = y

remove edge from AEL

else

edge.x = edge.x + dx/dy

sort AEL by x value

end scan_fill25

Page 26: From Vertices to Fragments II

26

Hidden Surface Removal

•Object-space approach: use pairwise testing between polygons (objects space)

•Worst case complexity O(n2) for n polygons

partially obscuring can draw independently

Page 27: From Vertices to Fragments II

27

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

•Fast but with low paint quality

Page 28: From Vertices to Fragments II

28

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 29: From Vertices to Fragments II

29

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 30: From Vertices to Fragments II

30

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 31: From Vertices to Fragments II

31

Hard Cases

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

cyclic overlap

penetration

(4)

Page 32: From Vertices to Fragments II

32

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 33: From Vertices to Fragments II

33

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 34: From Vertices to Fragments II

z-Buffer Algorithm

34

Page 35: From Vertices to Fragments II

z-Buffer Algorithm

35

Page 36: From Vertices to Fragments II

Example

36

Page 37: From Vertices to Fragments II

Example

37

Page 38: From Vertices to Fragments II

Example

38

Page 39: From Vertices to Fragments II

39

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 40: From Vertices to Fragments II

40

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 41: From Vertices to Fragments II

z-Buffer Scan-Line

41

Page 42: From Vertices to Fragments II

42

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 for intersections

Page 43: From Vertices to Fragments II

43

Visibility Testing

• In many real-time 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 44: From Vertices to Fragments II

44

Simple Example

consider 6 parallel polygons

top view

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

Page 45: From Vertices to Fragments II

45

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 46: From Vertices to Fragments II

BSP Tree

46

Page 47: From Vertices to Fragments II

BSP Algorithm

Choose a polygon P from the list.

Make a node N in the BSP tree, and add P to the list of polygons at that node.

For each other polygon in the list:

If that polygon is wholly in front of the plane containing P, move that polygon to the list of nodes in front of P.

If that polygon is wholly behind the plane containing P, move that polygon to the list of nodes behind P.

If that polygon is intersected by the plane containing P, split it into two polygons and move them to the respective lists of polygons behind and in front of P.

If that polygon lies in the plane containing P, add it to the list of polygons at node N.

Apply this algorithm to the list of polygons in front of P.

Apply this algorithm to the list of polygons behind P.47

Page 48: From Vertices to Fragments II

48

BSP display

Type TreeTree* front;Face face;Tree *back;

EndAlgorithm DrawBSP(Tree T; point: w)

//w 为视点If T is null then return; endifIf 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);

Endifend

Page 49: From Vertices to Fragments II

49

Aliasing

• Ideal rasterized line should be 1 pixel wide

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

Page 50: From Vertices to Fragments II

50

Antialiasing by Area Averaging

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

original antialiased

magnified

Page 51: From Vertices to Fragments II

51

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

Page 52: From Vertices to Fragments II

Time-domain aliasing

•Adding ray tracing line from one pixel;

52