1 / 85

# Polygon Scan Conversion - PowerPoint PPT Presentation

Polygon Scan Conversion. Instructor: Paul Merrell. Rasterization. Rasterization takes shapes like triangles and determines which pixels to fill. Filling Polygons. First approach: 1. Polygon Scan-Conversion

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Polygon Scan Conversion' - dinah

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Polygon Scan Conversion

Instructor: Paul Merrell

Rasterization takes shapes like triangles and determines which pixels to fill.

First approach:

1. Polygon Scan-Conversion

Rasterize a polygon scan line by scan line, determining which pixels to fill on each line.

Second Approach:

Polygon Fill

Select a pixel inside the polygon. Grow outward until the whole polygon is filled.

You can approximate practically any surface if you have enough polygons.

Graphics hardware is optimized for polygons (and especially triangles)

Scan-line

Edge

Span

Intersection Points

Other points in the span

Process each scan line

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

Sort the intersections by x coordinate.

Fill in pixels between pairs of intersections using an odd-parity rule.

Set parity even initially.

Each intersection flips the

parity.

Draw when parity is odd.

Process for scan converting polygons

Process one polygon at a time.

Store information about every polygon edge.

Compute spans for each scan line.

Draw the pixels between the spans.

This can be optimized using an table that stores information about each edge.

Perform scan conversion one scan line at a time.

For each scan line, we need to know if it intersects the polygon edges.

It is expensive to compute a complete line-line intersection computation for each scan line.

After computing the intersection between a scan line and an edge, we can use that information in the next scan line.

Polygon Edges

Intersection

points

needed

Previous

scan line

yi

Current

scan line

yi+1

Intersection

points from

previous scan line

Use edge coherence to incrementally update the x intersections.

We know

Each new scan line is 1 greater in y

We need to compute x for a given scan line,

([email protected],ymax)

([email protected],ymin)

So,

and

then

This is a more efficient way to compute xi+1.

• We will use two different edge tables:

• Active Edge Table (AET)

• Stores all edges that intersect the current scan line.

• Global Edge Table (GET)

• Stores all polygon edges.

• Used to update the AET.

Table contains one entry per edge intersected by the current scan line.

At each new scan line:

Compute new intersections for all edges using the formula.

Remove any edges no longer intersected.

To efficiently update the AET, we will can use a global edge table (GET)

D

8

7

C

6

5

B

E

4

3

2

1

A

0

0

1

2

3

4

5

6

7

8

Place entries into the GET

based on the ymin values.

GET

8

(ymax, [email protected], 1/m)

7

8, 6, -3/2

6

CD

Indexed

by

scan

line

6, 3, 3

5

BC

8, 0, ¾

4

DE

3

AB = (2, 1), (3, 5)

BC = (3, 5), (6, 6)

CD = (6, 6), (3, 8)

DE = (3, 8), (0, 4)

EA = (0, 4), (2, 1)

2

AB

EA

5, 2, ¼

4, 2, -2/3

1

0

The active edge table stores information about the edges that intersect the current scan line.

Entries are

The ymax value of the edge

The x value where the edge intersects the current scan line.

The x increment value (1/m)

Entries are sorted by x values.

D

8

7

C

6

5

B

E

4

3

2

1

A

0

0

1

2

3

4

5

6

7

8

The ymax value for that edge

The x value where the scan line intersects the edge.

The x increment value (1/m)

(ymax, x, 1/m)

AET

Scan Line 3

4, 2/3, -2/3

5, 5/2, 1/4

EA

AB

AB = (2, 1), (3, 5)

BC = (3, 5), (6, 6)

CD = (6, 6), (3, 8)

DE = (3, 8), (0, 4)

EA = (0, 4), (2, 1)

D

8

7

C

6

5

B

E

4

3

2

1

A

0

0

1

2

3

4

5

6

7

8

The ymax value for that edge

The x value where the scan line intersects the edge.

The x increment value (1/m)

(ymax, x, 1/m)

AET

Scan Line 4

4, 2/3, -2/3

8, 0, 3/4

5, 11/4, 1/4

5, 5/2, 1/4

EA

DE

AB

Scan Line 4

In the GET, edge DE is stored in bucket 4,

so it gets added to the AET.

ymax = 4 for EA, so it gets removed from the AET.

New x value for AB is

5/2 + 1/4 = 11/4

AB = (2, 1), (3, 5)

BC = (3, 5), (6, 6)

CD = (6, 6), (3, 8)

DE = (3, 8), (0, 4)

EA = (0, 4), (2, 1)

D

8

7

C

6

5

B

E

4

3

2

1

A

0

0

1

2

3

4

5

6

7

8

The ymax value for that edge

The x value where the scan line intersects the edge.

The x increment value (1/m)

Scan Line 5

(ymax, x, 1/m)

AET

8, 3/4, 3/4

8, 0, 3/4

5, 11/4, 1/4

6, 3, 3

DE

AB

BC

Increment x = 0 + 3/4

Remove AB,

since ymax = 5.

in the GET at ymin = 5.

AB = (2, 1), (3, 5)

BC = (3, 5), (6, 6)

CD = (6, 6), (3, 8)

DE = (3, 8), (0, 4)

EA = (0, 4), (2, 1)

D

8

7

C

6

Scan Line 6

5

B

E

4

3

2

1

A

0

0

1

2

3

4

5

6

7

8

AB = (2, 1), (3, 5)

BC = (3, 5), (6, 6)

CD = (6, 6), (3, 8)

DE = (3, 8), (0, 4)

EA = (0, 4), (2, 1)

### Clipping

What happens when one or both endpoints of a line segment are not inside the specified drawing area?

Drawing

Area

Strategies for clipping:

Check (in inner loop) if each point is inside  Works, but slow

Find intersection of line with boundary  Correct

if (x ≥ xmin and x ≤ xmax and y ≥ ymin and y ≤ ymax)

drawPoint(x,y,c);

Clip line to

intersection

Line Clipping: Possible Configurations

Both endpoints are inside the region (line AB)

No clipping necessary

One endpoint in, one

out (line CD)

Clip at intersection point

Both endpoints outside

the region:

No intersection (lines EF, GH)

Line intersects the region (line IJ)

Clip line at both intersection points

J

D

I

A

F

C

H

B

E

G

Line Clipping: Cohen-Sutherland

Basic algorithm:

Accept lines that have both endpoints inside the region.

J

D

I

Clip and

retest

A

F

C

H

B

Trivially accept

E

G

Trivially reject

• Reject lines that have both endpoints less than xmin or ymin or greater than xmax or ymax.

• Clip the remaining lines at a region boundary and repeat the previous steps on the clipped line segments.

Cohen-Sutherland: Accept/Reject Tests

Assign a 4-bit code to each

endpoint c0, c1based on its

position:

1st bit (1000): if y > ymax

2nd bit (0100): if y < ymin

3rd bit (0010): if x > xmax

4th bit (0001): if x < xmin

Test using bitwise functions

if c0 | c1 = 0000

accept (draw)

else if c0 & c1 0000

reject (don’t draw)

else clip and retest

1001

1000

1010

0001

0000

0010

0101

0100

0110

1001

1000

1010

0001

0000

0010

0101

0100

0110

Accept/reject/redo all based on bit-wise Boolean ops.

Polygon Clipping: Example

p0

Line intersect

boundary: Yes

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v0

ymax

Inside region: No

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0

Polygon Clipping: Example

p0

line intersect

boundary: no

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v1

ymax

inside region: yes

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0

, v1

Polygon Clipping: Example

p0

line intersect

boundary: yes

p1

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v2

ymax

inside region: yes

v9

v5

v1

v2

add v2, p1 to output list

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0 ,v1

, v2, p1

Polygon Clipping: Example

p0

line intersect

boundary: no

p1

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v3

ymax

inside region: no

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1

Polygon Clipping: Example

p0

line intersect

boundary: yes

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v4

ymax

inside region: no

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1

, p2

Polygon Clipping: Example

p3

p0

line intersect

boundary: yes

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v5

ymax

inside region: yes

v9

v5

v1

v2

add v5, p3 to output list

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1, p2

, v5, p3

Polygon Clipping: Example

p3

p0

line intersect

boundary: no

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v6

ymax

inside region: no

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1, p2, v5, p3

Polygon Clipping: Example

p3

p0

line intersect

boundary: no

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v7

ymax

inside region: no

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1, p2, v5, p3

Polygon Clipping: Example

p4

p3

p0

line intersect

boundary: yes

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v8

ymax

inside region: no

v9

v5

v1

v2

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1, p2, v5, p3

, p4

Polygon Clipping: Example

p4

p3

p5

p0

line intersect

boundary: yes

p1

p2

v6

v7

Clip first to ymin and ymax

v0

v8

vertex:v9

ymax

inside region: yes

v9

v5

v1

v2

add v9, p5 to output list

ymin

v3

v4

Input vertex list:(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)

Output vertex list:

p0, v1, v2, p1, p2, v5, p3, p4

, v9, p5

Polygon Clipping: Example

p4

p3

p5

p0

p1

p2

This gives us a new polygon

ymax

v9

v5

v1

v2

ymin

with vertices:

(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)

Polygon Clipping: Example (cont.)

p4

p3

p5

p0

p1

p2

Now clip to xmin and xmax

xmin

xmax

p9

v9

p6

v5

p7

v1

v2

p8

Input vertex list:= (p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)

Output vertex list:(p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)

Polygon Clipping: Example (cont.)

Now post-process

xmin

xmax

p4

p3

p5

p0

p9

v9

p6

p7

v2

p1

v3

p8

Output vertex list:(p0, p6, p7, v2, p1, p8, p9, p3, p4, v9, p5)

Post-process:(p0, p6, p9, p3,) and (p7, v2, p1, p8) and (v4, v9, p5)

### Transformations

Want to be able to manipulate graphical objects:

Translation: move an object

Rotation: change orientation

Scale: change size

Other transformations: reflection, shear, etc.

A general 2-D transformation has the form:

x´ = fx(x, y)

y´ = fy(x, y)

where x and y are the original coordinates and x´ and y´ are the transformed coordinates.

In vector form:

General Transformations (cont.)

General transforms may be non-linear:

Lines do not necessarily map to lines

Every point (along lines, inside shapes, etc.) needs to be transformed

Non-linear Transformations are harder to deal with.

Fortunately, many important transformations are linear.

Non-Linear Example:

y

y

7

7

6

6

5

5

4

4

3

3

2

2

1

1

x

x

0

0

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

We will use linear transformations:

x´ = fx(x, y) = axx + bxy + cx

y´ = fy(x, y) = ayx + byy + cy

Linear Transforms can be written using matrix operations:

Lines transform to lines

Only need to transform vertices.

Computationally efficient

Problem: would like to simplify further  get rid of T

Move object from one place to another:

Forward transform:

x´ = x + tx

y´ = y + ty

or p´ = p + T where

Inverse transform: p = p´ – T

y

7

6

5

4

3

2

1

x

0

0

1

2

3

4

5

6

7

Change an object’s size:

Forward transform:

x´ = sxx

y´ = syy

or p´ = Sp where

Inverse transform: p = S-1p´

where

y

7

6

5

4

3

2

1

x

0

0

1

2

3

4

5

6

7

Why do we get an

apparent translation?

Properties of the scale:

The scale is performed relative to the origin.

A scale factor greater than one enlarges the objects and moves it away from the origin.

A scale factor less than one shrinks the object and moves it towards the origin.

Usually this isn’t what we want

Generally we would like to scale about the object’s center - not the coordinate origin.

Uniform scale: sx = sy

Preserves angles, but not lengths

Non-uniform scale: sxsy

Doesn’t preserve angles or

lengths

x-axis:sx = 1, sy = –1

y-axis:sx = –1, sy = 1

line y = –x:sx = –1, sy = –1

Reflection preserves angles

and lengths

What is the inverse matrix?

A reflection matrix is its own

inverse.

Scale: Transforms

y

7

6

5

4

3

2

1

x

0

0

1

2

3

4

5

6

7

y = –x

Given a point p on the plane, how do we rotate that point about the origin?

y

x = r cos(f)

p = (x, y)

r

7

y = r sin(f)

x

f

6

5

4

3

2

1

0

0

1

2

3

4

5

6

7

1. Convert point p to polar coordinates:

Now, let’s rotate that point by q about the origin

y

p´= (x’, y’)

r

p = (x, y)

r

q

7

x

f

6

5

4

3

2

1

0

0

1

2

3

4

5

6

7

2. To rotate qdegrees, simply addqtof:

3. Apply the sum of angles formula

4. Substitute x and y back in:

So, given a point p that we wish to rotate about the origin, we simply multiply p by the rotation matrix

Forward transform:

x´ = x cos(q) – y sin(q)

y´ = x sin(q) + y cos(q)

or p´ = Rp where

Inverse rotation: p = R-1p´

Rotating an Object

y

7

6

5

4

3

2

1

x

0

0

1

2

3

4

5

6

7

q = 45°

q

Right-hand rule: Stick your thumb towards the z-axis. Your fingers rotate in the positive direction as they curl.

If q is positive, the rotation is counterclockwise about the origin.

y

p´= (x’, y’)

r

q

p = (x, y)

7

7

x

6

6

5

5

4

4

3

3

2

2

1

1

0

0

0

0

1

1

2

2

3

3

4

4

5

5

6

6

7

7

-q

p´= (x’, y’)

r

• If q is negative, the rotation is clockwise about the origin.

y

p = (x, y)

r

x

Scale an object in an arbitrary direction (other than in x and/or y)

Rotate an object about a point (other than the origin)

Etc.

Composition: combine basic transforms to achieve a more complex one

Example: Scale by ½ at a 30° angle

Rotate clockwise 30°(q = –p / 6): p´ = RTp

Scale in x by ½: p´´ = Sp´

Rotate counter-clockwise 30°(q = p / 6): p´´´ = Rp´´

Final result: p´´´ = Rp´´ = RSp´ = RSRTp

Composition

Recall that a negative rotation matrix is the transpose of its corresponding positive rotation matrix.

Composition: Scale at Arbitrary Angle

Scale object at an arbitrary angle q:

Rotate clockwise by q: p´ = RTp

Scale about origin: p´ = SRTp

Rotate back (counter-clockwise

by q):p´ = RSRTp

y

y

y

5

5

4

4

3

3

2

2

1

1

x

x

x

0

0

0

0

1

1

2

2

3

3

4

4

q = 30°

5

4

q = 30°

3

2

1

0

0

1

2

3

4

Multiple transformations: Multiply each point (i.e., vertex) in an object by each basic transformation

p´ = RSRTp

Requires multiple matrix multiplies per vertex

Lots of extra computation if transforming thousands (or millions) of points.

Composition: matrix multiply is associative

Multiply matrices first to create a single matrix

Multiply each point by a single matrix

p´ = RSRTp = Mp

Much more efficient

Composition and Efficiency

Our rotations (so far) have been about the origin

This rotation “moves” the entire object as it rotates

Usually when we want to rotate an object, we want to rotate it about its center (or some other fixed point)

How can we do this?

Answer: Apply a sequence of transformations

Rotate object about an arbitrary point T:

Translate Tto origin: p´ = p –T

Rotate about origin: p´ = R(p –T)

Translate back: p´ = R(p –T) + T

y

y

y

5

5

5

4

4

4

3

3

3

2

2

2

1

1

1

x

x

x

0

0

0

0

0

0

1

1

1

2

2

2

3

3

3

4

4

4

q = 45°

Standard format for rotating about an arbitrary point:

Translate point of rotation to the origin

Translate the point back to its original position

Compute the composite transformation that will accomplish this, then apply that transformation to all vertices in the object.

Problem:

Rotation, scale, and shear multiply a matrix with p: p´ = Mp

Translation adds a vector to p: p´ = p + T

Would like to treat all transformations the same

Optimize the hardware

Compose transformations

Solution: homogeneous coordinates

Increase point’s dimensionality  add a third coordinate w:

Two homogeneous points (p1 and p2) specify

the same 2-D Cartesian point if:

p1 = cp2for some real-valued scalar numberc

Homogeneous Coordinates (cont.)

With homogeneous coordinates, the x-y plane is a two-dimensional sub-space in 3-D

Although homogeneous points have three coordinates, they correlate to positions on a 2-D plane

Can use any 2-D plane that doesn’t

include the origin

For simplicity, choose the plane

w = 1  [xy 1]T = [XY 1]T

w

2-D Cartesian

coordinates

Homogeneous point

y

x

For 2D transformations, the points are now 3-vectors

And the transformation matrices are 3x3 matrices

Homogeneous Coordinates: Translation

What does the translation matrix look like?

To translate a point p to point p’, we need

with

and

What does the scale matrix look like?

To scale a point p to point p’, we need

with

and

Homogeneous Coordinates: Rotation

Rotation has a similar derivation. Remember the 2D rotation matrix:

When we move to homogeneous coordinates, we have

We apply this matrix to each vertex of a polygon to rotate the polygon as a whole

Now let’s try that rotation about an arbitrary point T:

Translateto origin: p´ = T1p

Rotate about origin: p´ = RT1p

Translate back: p´ = T2RT1p

y

y

y

5

5

5

4

4

4

3

3

3

2

2

2

1

1

1

x

x

x

0

0

0

0

0

0

1

1

1

2

2

2

3

3

3

4

4

4

q = 45°

So

Where M is the composite transformation matrix

General Case:

Translation:

Scale:

These rotations can be combined

to rotate about an arbitrary axis.

### Viewing

Model Space

Model Transformations

World Space

Viewing Transformation

Eye/Camera

Space

Projection & Window

Transformation

Screen Space

• Projection: take a point from m dimensions to n dimensions where n < m

• There are essentially two types of viewing transforms:

• Orthographic: parallel projection

• Points project directly onto the view plane

• In eye/camera space (after viewing

transformation): drop z

• Perspective: convergent projection

• Points project through the origin

onto the view plane

• In eye/camera space (after viewing

transformation): divide by z

Projection Environment

• We will use a right-handed view system

y

• The eye or camera position is on the +z axis, a distance d from the origin.

x

• The view direction is parallel to the z axis

z

• The view plane is in the xy plane and passes through the origin

• Looking down the y axis:

(x, y, z)

(x’, y’, z’)

x

z

• So z’ = 0, x’ = x

• Thus, for parallel, orthographic projections,

• x’ = x, y’ = y, z’ = 0

• So, to perform a parallel projection on an object, we can use matrix multiplication

What is M?

i.e., we simply

drop the z

coordinate

• Points project through the focal point (e.g., eyepoint) onto the view plane:

• Projection lines converge

y

Virtual Image

Plane

x

z

Looking down the y axis:

By similar triangles:

x

p = (x, y, z)

x’

x

view plane

p’ = (x’, y’, z’)

d-z

d

eye

z

Looking down the x axis:

By similar triangles:

y

p = (x, y, z)

p’ = (x’, y’, z’)

y

y’

z

view plane

eye

d

d-z

• So, we have

• what is z’?

• can we put this into matrix form?

• So, the matrix that will give us the correct perspective is:

This works - what is the problem with it?

Answer: The entries in the matrix are point dependent!

i.e., every point will have to have a different matrix

• Solution: use homogeneous points (remember them?)

• Our Cartesian point is:

• A homogeneous point that is equivalent to our desired Cartesian point is:

• can we come up with a matrix that gives us what we need, but is point independent?

• We want:

so,

• So, the new matrix we get is:

This gives us correct results, and is point independent