Computer Graphics Implementation II

50
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 50 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 50 Computer Graphics Implementation II

description

Computer Graphics Implementation II. Polygon Filling Scan-line Conversion Approaches Area Filling Approaches Antialiasing Clipping. Polygon Filling. Scan-line Conversion Area Filling. P4. P0. P2. P3. P1. P6. P5. P7. Polygon Filling. - PowerPoint PPT Presentation

Transcript of Computer Graphics Implementation II

C O M P U T E R G R A P H I C S

Guoying Zhao 1 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 1 / 50

Computer Graphics

Implementation II

C O M P U T E R G R A P H I C S

Guoying Zhao 2 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 2 / 50

• Polygon Filling– Scan-line Conversion Approaches– Area Filling Approaches

• Antialiasing• Clipping

C O M P U T E R G R A P H I C S

Guoying Zhao 3 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 3 / 50

Polygon Filling

• Scan-line Conversion• Area Filling

C O M P U T E R G R A P H I C S

Guoying Zhao 4 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 4 / 50

Polygon Filling

• Vertex-edge representation Pixel set representation

P0

P1

P2

P3

P4

P5P6

P7

C O M P U T E R G R A P H I C S

Guoying Zhao 5 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 5 / 50

Scan-line Conversion

Check pixel by pixel

P0

P1

P2

P3

P4

P5P6

P7

C O M P U T E R G R A P H I C S

Guoying Zhao 6 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 6 / 50

How to judge a point inside or outside a polygon?

• Shoot a radial from the point to intersect with the polygon edges; if there are odd number of intersection points, the point is inside the polygon; if even number, outside.

• Odd point: the intersection point is polygon’s vertex (special case).

C O M P U T E R G R A P H I C S

Guoying Zhao 7 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 7 / 50

Scan-line Conversion

Improved (limit the pixel sets into the bounding box of the polygon for speeding up)

P0

P1

P2

P3

P4

P5P6

P7

C O M P U T E R G R A P H I C S

Guoying Zhao 8 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 8 / 50

Scan-line Conversion

• Scan-line polygon-fill algorithm– Taking full advantage of the

coherence properties of pixels– Three coherence properties

• Area coherence • Scan-line coherence • Edge coherence

C O M P U T E R G R A P H I C S

Guoying Zhao 9 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 9 / 50

Area Coherence

The screen region between two scan-lines is partitioned into some trapezoids by the polygon.

(1) Two types of trapezoid: the one inside the polygon and the one outside polygon.

(2) The two types of trapezoids are arranged alternately.

C O M P U T E R G R A P H I C S

Guoying Zhao 10 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 10 / 50

Scan-line Coherence

Suppose the intersection points of scan-line y=e and polygon edge ei

(Pi-1Pi) is xei.

Suppose the intersection points sequence arranged by the x-increase is xei1, xei2, xei3 … xein.According to area coherence, we can get:

(1) n is even number(2) On the scan-line, only the segments (xeik, xeik+1), k=1,3,5,…

n–1) are inside the polygon.Scan-line coherence is the reflection of area coherence on a

scan-line.

C O M P U T E R G R A P H I C S

Guoying Zhao 11 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 11 / 50

Edge Coherence

Suppose the intersection point sequence on y=e is xei1, xei2, …xein; point sequence on y=e-1 is xdi1, xdi2, … xdin.If edge er(Pr-1Pr) intersects with both y=e and y=e-1; the corresponding points xer and xdr have following relationship: xer = xdr + 1/mrThus, we can calculate the intersection points on y=e from the

points on y=e-1

Edge coherence is the reflection of area coherence on edges.

C O M P U T E R G R A P H I C S

Guoying Zhao 12 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 12 / 50

Polygon Filling

• Scan-line Conversion• Area Filling

C O M P U T E R G R A P H I C S

Guoying Zhao 13 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 13 / 50

Area Filling • Area Filling: To start from a given interior

position (seed) and paint outward from this point until we encounter the specified boundary conditions.

• The “area” should be identified with its interior color or boundary color.– Colorate all the interior pixels to a specified color– Colorate all the boundary pixels to the boundary color

C O M P U T E R G R A P H I C S

Guoying Zhao 14 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 14 / 50

种子填充

4-connected Neighbourhood

8-connected Neighbourhood

Two kinds of connectivity

C O M P U T E R G R A P H I C S

Guoying Zhao 15 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 15 / 50

4-connected area and 8-connected area

• 4-connected area: Giving any two interior points A and B, we can travel from A to B by the 4-directions moving: right, left, up and down moving.

• To filling the 4-connected area, we only need to test its 4-direction neighbors

4-connected area 8-connected area

C O M P U T E R G R A P H I C S

Guoying Zhao 16 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 16 / 50

4-connected area and 8-connected area

• The boundary of 8-connected area must be 4-connected

• The boundary of 4-connected area is 8-connected

C O M P U T E R G R A P H I C S

Guoying Zhao 17 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 17 / 50

Recursive Method for Filling a 4-connected Area

void AreaFill4(int x, int y, int fillCol, int boundaryCol)

{

int currentCol = getPixel(x,y);

if( (currentCol != boundaryCol)&& (currentCol != fillCol))

{ setPixel(x, y, fillCol);

AreaFill4(x, y+1, fillCol, boundaryCol);

AreaFill4(x, y-1, fillCol, boundaryCol);

AreaFill4(x-1, y, fillCol, boundaryCol);

AreaFill4(x+1, y, fillCol, boundaryCol);

}

}

The area is identified by the boundary color

C O M P U T E R G R A P H I C S

Guoying Zhao 18 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 18 / 50

Recursive Method for Filling a 4-connected Area

void FloodFill4(int x, int y, int fillCol, int interiorCol) { int currentCol = getPixel(x,y); if( currentCol == interiorCol )

{ setPixel(x, y, fillCol); FloodFill4(x, y+1, fillCol, interiorCol);

FloodFill4(x, y-1, fillCol, interiorCol);FloodFill4(x-1, y, fillCol, interiorCol);FloodFill4(x+1, y, fillCol, interiorCol);

} }

The area is identified by the interior color. Called flood-fill algorithm

C O M P U T E R G R A P H I C S

Guoying Zhao 19 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 19 / 50

Recursive Method for Filling a 4-connected Area

A

BC

DE

FG

H

I J

L

M N

O

PK

C O M P U T E R G R A P H I C S

Guoying Zhao 20 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 20 / 50

Recursive Method for Filling a 4-connected Area

Left pixel to 3 is the last one to be filled.

C O M P U T E R G R A P H I C S

Guoying Zhao 21 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 21 / 50

How to expand the algorithm for 4 –connected area to the algorithm for 8-connect area?

C O M P U T E R G R A P H I C S

Guoying Zhao 22 / 50

void AreaFill8(int x, int y, int fillCol, int boundaryCol) {

int currentCol = getPixel(x,y);

if((currentCol != boundaryCol) && (currentCol != fillCol))

{ setPixel(x, y, fillCol);

AreaFill8(x, y+1, fillCol, boundaryCol);

AreaFill8(x, y-1, fillCol, boundaryCol);

AreaFill8(x-1, y, fillCol, boundaryCol);

AreaFill8(x+1, y, fillCol, boundaryCol);

AreaFill8(x+1, y+1, fillCol, boundaryCol);

AreaFill8(x+1, y-1, fillCol, boundaryCol);

AreaFill8(x-1, y+1, fillCol, boundaryCol);

AreaFill8(x-1, y-1, fillCol, boundaryCol);

}

}

C O M P U T E R G R A P H I C S

Guoying Zhao 23 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 23 / 50

Comparison Between Scan-line Conversion (A) and Area Filling (B)

• Basic idea: A changes the edge list representation into lattice representation. It uses the coherences of polygons. B does not change the representation of the area, but the color. It uses the connectivity of area.

• The requirements: For area filling, a seed point inside the area is needed.

• Boundary: For A, the number of the intersection points of each scan line with edges should be even. For B, the boundary of 4-connected area is closed 8-connected area and the boundary of 8-connected area is closed 4-connected area.

C O M P U T E R G R A P H I C S

Guoying Zhao 24 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 24 / 50

Antialiasing

• Aliasing Problems of Raster Graphics

• Antialiasing Methods

C O M P U T E R G R A P H I C S

Guoying Zhao 25 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 25 / 50

Aliasing Problems of Raster Graphics

• What’s aliasing?

The distortion of information due to low-frequency sampling ( undersampling ) is called aliasing

C O M P U T E R G R A P H I C S

Guoying Zhao 26 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 26 / 50

Aliasing Problems of Raster Graphics --- Jagged Boundaries

8

8

7

6

5

4

3

2

0

1

9

7654320 1 9x

y

C O M P U T E R G R A P H I C S

Guoying Zhao 27 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 27 / 50

Aliasing Problems of Raster Graphics --- Shape Distortion

Slim Primitives are lost

C O M P U T E R G R A P H I C S

Guoying Zhao 28 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 28 / 50

Aliasing Problems of Raster Graphics --- Sparking

The slim primitive sparks when it is moved

C O M P U T E R G R A P H I C S

Guoying Zhao 29 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 29 / 50

Antialiasing

• Aliasing Problems of Raster Graphics

• Antialiasing Methods

C O M P U T E R G R A P H I C S

Guoying Zhao 30 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 30 / 50

Antialiasing Methods

• Adopting area-sampling instead of point-sampling

• Supersampling

C O M P U T E R G R A P H I C S

Guoying Zhao 31 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 31 / 50

Area-sampling

Using transitional color scales on the edges

C O M P U T E R G R A P H I C S

Guoying Zhao 32 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 32 / 50

Using Transitional Color Scales on the Edges

C O M P U T E R G R A P H I C S

Guoying Zhao 33 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 33 / 50

Area-sampling• Exact area-sampling is time-consuming• Some approximate algorithms are always used

– Wu’s algorithm for drawing antialiasing lines– Pitteway and Watkinson’s algorithm for drawing

antialiasing polygons

C O M P U T E R G R A P H I C S

Guoying Zhao 34 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 34 / 50

Supersampling for Antialiasing

• Hardware method: adopting high resolution raster display

• Software method: sampling objects at a high resolution and displaying the results at a lower resolution– High resolution sampling: partition each pixel

as several sub-pixels, such as the 3*3 partition. Then compute color for all the sub-pixels

– Low resolution display: compute the pixel’s color by adding up all its sub-pixels’ color with a weighting mask

C O M P U T E R G R A P H I C S

Guoying Zhao 35 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 35 / 50

Supersampling for Antialiasing

42

22211

11

Weight mask for the 3*3 partition

EB

HFDIG

CA

3*3 partition

Pixel color = (ColA + 2*ColB + ColC + 2*ColD + 4*ColE + 2*ColF + ColG + 2*ColH + ColI ) / 16

C O M P U T E R G R A P H I C S

Guoying Zhao 36 / 50

C O M P U T E R G R A P H I C S

Guoying Zhao 36 / 50

Supersampling for Antialiasing

32

646

23

4 62 6 48

269

31

68

93124

63

948

12

24

912

612816

2

1

34

2 31 3 24 1Weight mask for the 7*7 partition

C O M P U T E R G R A P H I C S

Guoying Zhao 37 / 50

Clipping

C O M P U T E R G R A P H I C S

Guoying Zhao 38 / 50

Clipping

C O M P U T E R G R A P H I C S

Guoying Zhao 39 / 50

Line Clipping• Clipping endpoints

xmin < x < xmax and ymin < y < ymax point inside

• Endpoint analysis for lines:

– if both endpoints in , do “trivial acceptance”– if one endpoint inside, one outside, must clip– if both endpoints out, don’t know

• Brute force clip: solve simultaneous equations using y = mx + b for line and four clip edges

– slope-intercept formula handles infinite lines only – doesn’t handle vertical lines

(Xmin , Ymin)

(Xmax , Ymax)

C O M P U T E R G R A P H I C S

Guoying Zhao 40 / 50

Parametric Line Formulation For Clipping

• Parametric form for line segmentX = x0 + t(x1 – x0) 0 < t < 1

Y = y0 + t(y1 – y0)

P(t) = P0 + t(P1 – P0)

• “true,” i.e., interior intersection, if sedge and tline in [0,1]

C O M P U T E R G R A P H I C S

Guoying Zhao 41 / 50

• Divide plane into 9 regions• Compute the sign bit of 4 comparisons between a vertex and an edge

– ymax – y; y – ymin; xmax – x; x – xmin (>0 “0”; otherwise “1”)– point lies inside only if all four sign bits are 0, otherwise exceeds edge

• 4 bit outcode records results of four bounds tests: “1”First bit: outside halfplane of top edge, above top edgeSecond bit: outside halfplane of bottom edge, below bottom edgeThird bit: outside halfplane of right edge, to right of right edgeFourth bit: outside halfplane of left edge, to left of left edgeo1=o2 =0, accept; AND(o1, o2) <>0 (in same side), discard.

Outcodes for Cohen-Sutherland Line Clipping in 2D

Clip Rectangle

C O M P U T E R G R A P H I C S

Guoying Zhao 42 / 50

Left plane

000001 (to left of)

000000 (to right of)

Outcodes for Cohen-Sutherland Line Clipping in 3D

• Very similar to 2D• Divide volume into 27 regions (Picture a Rubik’s cube)• 6-bit outcode records results of 6 bounds tests

First bit: outside back plane, behind back planeSecond bit: outside front plane, in front of front plane Third bit: outside top plane, above top planeFourth bit: outside bottom plane, below bottom planeFifth bit: outside right plane, to right of right planeSixth bit: outside left plane, to left of left plane

Top plane

001000 (above)

000000 (below)

Bottom plane

000000 (above)

000100 (below)

Front plane

010000 (in front)

000000 (behind)

Right plane

000000 (to left of)

000010 (to right of)

Back plane

000000 (in front)

100000 (behind)

C O M P U T E R G R A P H I C S

Guoying Zhao 43 / 50

• If we can neither trivially reject/accept, divide and conquer • subdivide line into two segments; then T/A or T/R one or both

segments:

– use a clip edge to cut line– use outcodes to choose edge that is crossed

• Edges where the two outcodes differ at that particular bit are crossed

– pick an order for checking edges • top – bottom – right – left

– compute the intersection point• the clip edge fixes either x or y • can substitute into the line equation

– iterate for the newly shortened line– “extra” clips may happen (e.g., E-I at H)

Clip rectangle

DC

BA

E

FG

HI

Cohen-Sutherland Algorithm

1010

0100

C O M P U T E R G R A P H I C S

Guoying Zhao 44 / 50

ComputeOutCode(x0, y0, outcode0)ComputeOutCode(x1, y1, outcode1)repeat

check for trivial reject or trivial acceptpick the point that is outside the clip rectangleif TOP then x = x0 + (x1 – x0) * (ymax – y0)/(y1 – y0); y = ymax;else if BOTTOM then x = x0 + (x1 – x0) * (ymin – y0)/(y1 – y0); y = ymin;else if RIGHT then y = y0 + (y1 – y0) * (xmax – x0)/(x1 – x0); x = xmax;else if LEFT then y = y0 + (y1 – y0) * (xmin – x0)/(x1 – x0); x = xmin;if (x0, y0 is the outer point) then x0 = x; y0 = y; ComputeOutCode(x0, y0, outcode0)else x1 = x; y1 = y; ComputeOutCode(x1, y1, outcode1)

until done

• y = y0 + slope*(x - x0) and x = x0 + (1/slope)*(y - y0)

Pseudocode for the Cohen- Sutherland Algorithm

C O M P U T E R G R A P H I C S

Guoying Zhao 45 / 50

Cyrus-Beck/Liang-Barsky Parametric Line Clipping-1

• Use parametric line formulation P(t) = P0 + (P1 – P0)t

• Determine where the line intersects the infinite line formed by each edge by solving for t 4 times. Decide which of these intersections actually occur on the rectangle

• For any point PEi on edge Ei

C O M P U T E R G R A P H I C S

Guoying Zhao 46 / 50

Now solve for the value of t at the intersection of P0 P1 with the edge Ei:

Ni • [P(t) – PEi] = 0

First, substitute for P(t):

Ni • [P0 + (P1 – P0)t – PEi] = 0

Next, group terms and distribute dot product:

Ni • [P0 – PEi] + Ni • [P1 – P0]t = 0

Let D be the vector from P0 to P1 = (P1 – P0), and solve for t:Note that this gives a valid value of t only if the denominator of the expression is nonzero.

For this to be true, it must be the case that:Ni 0 (that is, the normal should not be 0; this could occur only as a mistake)D 0 (that is, P1 P0)

Ni • D 0 (edge Ei and line D are not parallel; if they are, no intersection).

The algorithm checks these conditions.

Di

NiE

PPi

Nt

]0

[

C-B/L-B Param. Line Clipping-2

C O M P U T E R G R A P H I C S

Guoying Zhao 47 / 50

• Eliminate t’s outside [0,1] on the line (does not to be processed)

• Which remaining t’s produce interior intersections?

• Can’t just take the innermost t values! (decide PE and PL)

• Move from P0 to P1; for a given edge, just before crossing:

• if Ni • D < 0 Potentially Entering (PE), if Ni • D > 0 Potentially Leaving

(PL)

• Pick inner PE, PL pair: tE for PPE with max t, tL for PPL with min t, and tE > 0, tL < 1.

• If tL < tE, no intersection (Line 2)

C-B/L-B Param. Line Clipping-3

C O M P U T E R G R A P H I C S

Guoying Zhao 48 / 50

Pre-calculate Ni and select PEi for each edge;

for each line segment to be clipped

if P1 = P0 then

line is degenerate so clip as a point;

else

begin

tE = 0; tL = 1;

for each candidate intersection with a clip edge

if Ni • D 0 then {Ignore edges parallel to line}

begin

calculate t; {of line and clip edge intersection}

use sign of Ni • D to categorize as PE or PL;

if PE then tE = max(tE,t);

if PL then tL = min(tL,t);

end

if tE > tL then

return nil

else

return P(tE) and P(tL) as true clip intersections

end

Pseudocode for Cyrus-Beck/ Liang-Barsky Line Clipping Algorithm

C O M P U T E R G R A P H I C S

Guoying Zhao 49 / 50

• D = P1 – P0 = (x1 – x0, y1 – y0)

• Leave PEi as an arbitrary point on the clip edge; it’s a free variable and drops

out

Calculations for Parametric Line Clipping Algorithm

(x0-x,y0- ymax)(x, ymax)(0,1)top: y = ymax

(x0-x,y0- ymin)(x, ymin)(0,-1)bottom: y = ymin

(x0- xmax,y0-y)(xmax,y)(1,0)right: x = xmax

(x0- xmin,y0-y)(xmin, y)(-1,0)left: x = xmin

P0-PEiPEi

Normal NiClip Edgei

Di

NiE

PPi

Nt

)0

(

)01

(

)min0

(

xx

xx

)01

(

)max0

(

xx

xx

)01

(

)min0

(

yy

yy

)01

(

)max0

(

yy

yy

C O M P U T E R G R A P H I C S

Guoying Zhao 50 / 50

Sutherland-Hodgman Polygon Clipping