polygon scan conversion

85
Polygon Scan Conversion Instructor: Paul Merrell

Upload: dinah

Post on 02-Feb-2016

45 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Polygon Scan  Conversion

Polygon Scan Conversion

Instructor: Paul Merrell

Page 2: Polygon Scan  Conversion

2

Rasterization

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

Page 3: Polygon Scan  Conversion

3

Filling Polygons

First approach:

1. Polygon Scan-Conversion• Rasterize a polygon scan line by scan line,

determining which pixels to fill on each line.

Page 4: Polygon Scan  Conversion

4

Filling Polygons

Second Approach:

2. Polygon Fill• Select a pixel inside the polygon. Grow outward

until the whole polygon is filled.

Page 5: Polygon Scan  Conversion

5

Why Polygons?

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

• Graphics hardware is optimized for polygons (and especially triangles)

Page 6: Polygon Scan  Conversion

6

Coherence

Scan-line

Edge

Span

Page 7: Polygon Scan  Conversion

7

Polygon Scan Conversion

Intersection Points

Other points in the span

Page 8: Polygon Scan  Conversion

8

Polygon Scan Conversion

• Process each scan line1. Find the intersections of the scan line with all

polygon edges.

2. Sort the intersections by x coordinate.

3. 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.

Page 9: Polygon Scan  Conversion

9

Polygon Scan-Conversion

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

Page 10: Polygon Scan  Conversion

10

Computing Intersections

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

Page 11: Polygon Scan  Conversion

11

Scan Line Intersection

Currentscan line

yi+1

Polygon Edges

Intersection points from

previous scan line

Intersection pointsneeded

Previousscan line

yi

Page 12: Polygon Scan  Conversion

12

Scan Line IntersectionUse 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,

min@max@

minmax ,yy xx

yymbmxy

11 ii yy

m

byx

(x@ymax,ymax)

(x@ymin,ymin)

Page 13: Polygon Scan  Conversion

13

Scan Line Intersection

So,

and

then

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

m

byx

m

byx i

ii

i

11 ,

11 ii yy

mx

mm

by

m

byx i

iii

1111

Page 14: Polygon Scan  Conversion

14

Edge Tables

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

Page 15: Polygon Scan  Conversion

15

Active Edge Table

• 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.• Add any new edges intersected.• Remove any edges no longer intersected.

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

mxx ii

11

Page 16: Polygon Scan  Conversion

16

Global Edge Table Example

GET

0

1

2

3

4

5

6

7

8

Indexed by

scan line

AB EA

(ymax, x@ymin, 1/m)

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

8

A

B

C

D

E

Place entries into the GET

based on the ymin values.

8, 6, -3/2

6, 3, 3

8, 0, ¾

5, 2, ¼ 4, 2, -2/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)

DE

CD

BC

Page 17: Polygon Scan  Conversion

17

Active Edge Table Example

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

Page 18: Polygon Scan  Conversion

18

Active Edge Table Example

AETScan Line 3

1. The ymax value for that edge2. The x value where the scan line intersects

the edge.3. The x increment value (1/m)

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

8

A

B

C

D

E

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)

AB

4, 2/3, -2/3 5, 5/2, 1/4

EA

(ymax, x, 1/m)

Page 19: Polygon Scan  Conversion

19

Active Edge Table Example

AETScan Line 4

1. The ymax value for that edge2. The x value where the scan line intersects

the edge.3. The x increment value (1/m)

Scan Line 4

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

New x value for AB is 5/2 + 1/4 = 11/4

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

so it gets added to the AET.

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

8

A

B

C

D

E

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)

AB

4, 2/3, -2/3 5, 5/2, 1/4

EA

(ymax, x, 1/m)

8, 0, 3/4

DE

5, 11/4, 1/4

Page 20: Polygon Scan  Conversion

20

Active Edge Table Example

AETScan Line 5

1. The ymax value for that edge2. The x value where the scan line intersects

the edge.3. The x increment value (1/m)

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

8

A

B

C

D

E

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)

ABDE

(ymax, x, 1/m)

8, 0, 3/4 5, 11/4, 1/48, 3/4, 3/4

Increment x = 0 + 3/4 Remove AB,since ymax = 5.

Add BC since it is in the GET at ymin = 5.

6, 3, 3

BC

Page 21: Polygon Scan  Conversion

21

Active Edge Table Example

Scan Line 6

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

0 1 2 3 4 5 6 7 8

0

1

2

3

4

5

6

7

8

A

B

C

E

Page 22: Polygon Scan  Conversion

Clipping

Page 23: Polygon Scan  Conversion

23

Line Clipping

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

DrawingArea

Page 24: Polygon Scan  Conversion

24

Line Clipping

• Strategies for clipping:a) Check (in inner loop) if each point is inside

Works, but slow

b) 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 tointersection

Page 25: Polygon Scan  Conversion

25

Line Clipping: Possible Configurations

1. Both endpoints are inside the region (line AB)• No clipping necessary

2. One endpoint in, oneout (line CD)

• Clip at intersection point

3. Both endpoints outsidethe region:

a. No intersection (lines EF, GH)

b. Line intersects the region (line IJ)• Clip line at both intersection points

A

B

C

D

F

E

I

J

G

H

Page 26: Polygon Scan  Conversion

26

Line Clipping: Cohen-Sutherland

• Basic algorithm:• Accept lines that have both

endpoints inside the region.

F

E

Trivially reject

A

BTrivially accept

H

C

D

I

J

G

Clip andretest

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

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

Page 27: Polygon Scan  Conversion

27

Cohen-Sutherland: Accept/Reject Tests

• Assign a 4-bit code to each endpoint c0, c1 based 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 functionsif c0 | c1 = 0000

accept (draw)else if c0 & c1 0000

reject (don’t draw)else clip and retest

01000101 0110

10001001 1010

0001 00100000

Page 28: Polygon Scan  Conversion

28

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

Cohen-Sutherland Accept/Reject

01000101 0110

10001001 1010

0001 00100000

Page 29: Polygon Scan  Conversion

29

Polygon Clipping

What about polygons?

Page 30: Polygon Scan  Conversion

30

Polygon Clipping: ExampleClip first to ymin and ymax

ymax

ymin

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v0

Inside region: No

Add p0 to output list

Output vertex list:

p0

Line intersect boundary: Yes

p0

Page 31: Polygon Scan  Conversion

31

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v1

inside region: yes

add v1 to output list

Output vertex list:

p0

line intersect boundary: no

, v1

ymax

ymin

p0

Page 32: Polygon Scan  Conversion

32

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v2

inside region: yes

add v2, p1 to output list

Output vertex list:

p0

, v2, p1

line intersect boundary: yes

p1

ymax

ymin

p0 , v1

Page 33: Polygon Scan  Conversion

33

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v3

inside region: no

Output vertex list:

p0

p0, v1, v2, p1

line intersect boundary: no

p1

ymax

ymin

Page 34: Polygon Scan  Conversion

34

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v4

inside region: no

add p2 to output list

Output vertex list:

p0

, p2

p1

line intersect boundary: yes

p2

ymax

ymin

p0, v1, v2, p1

Page 35: Polygon Scan  Conversion

35

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v5

inside region: yes

add v5, p3 to output list

Output vertex list:

p0

p0, v1, v2, p1, p2

p1

p2

line intersect boundary: yes

p3

ymax

ymin

, v5, p3

Page 36: Polygon Scan  Conversion

36

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v6

inside region: no

Output vertex list:

p0

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

p1

p2

line intersect boundary: no

p3

ymax

ymin

Page 37: Polygon Scan  Conversion

37

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v7

inside region: no

Output vertex list:

p0

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

p1

p2

line intersect boundary: no

p3

ymax

ymin

Page 38: Polygon Scan  Conversion

38

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v8

inside region: no

add p4 to output list

Output vertex list:

p0

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

p1

p2

p3

line intersect boundary: yes

p4

ymax

ymin

, p4

Page 39: Polygon Scan  Conversion

39

Polygon Clipping: ExampleClip first to ymin and ymax

v0

v1v2

v3v4

v5

v6v7

v8

v9

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

vertex: v9

inside region: yes

add v9, p5 to output list

Output vertex list:

p0

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

p1

p2

p3p4

line intersect boundary: yes

p5

ymax

ymin

, v9, p5

Page 40: Polygon Scan  Conversion

40

Polygon Clipping: ExampleThis gives us a new polygon

v1v2

v5

v9

with vertices:

p0

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

p1

p2

p3p4 p5

ymax

ymin

Page 41: Polygon Scan  Conversion

41

Polygon Clipping: Example (cont.)Now clip to xmin and xmax

xmin xmax

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)

v1v2

v5

v9

p0

p1

p2

p3p4 p5

p6

p7

p8

p9

Page 42: Polygon Scan  Conversion

42

Polygon Clipping: Example (cont.)Now post-process

xmin xmax

v9

v3

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)

p8

p6

v2

p7

p0

p5 p3p4

p9

p1

Page 43: Polygon Scan  Conversion

Transformations

Page 44: Polygon Scan  Conversion

44

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

y

x

y

x

f

f

y

xpp

p

pp and where

)(

)(

Page 45: Polygon Scan  Conversion

45

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:

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

322

14

22

2

xyy

xyx

x

Page 46: Polygon Scan  Conversion

46

Linear Transformations

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

• Advantages:• Lines transform to lines• Only need to transform vertices.• Computationally efficient• Problem: would like to simplify further get rid of T

TMppp

y

x

yy

xx

c

c

ba

ba

Page 47: Polygon Scan  Conversion

47

Translation

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

x

t

tT

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

3

4

T

Page 48: Polygon Scan  Conversion

48

Scale

Change an object’s size:• Forward transform:

x´ = sx x

y´ = sy y

or p´ = Sp where

• Inverse transform: p = S-1p´

where

y

x

s

s

0

0 S

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

20

02 S

y

x

s

s

1

11

0

0 S

Why do we get anapparent translation?

Page 49: Polygon Scan  Conversion

49

Scale

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

Page 50: Polygon Scan  Conversion

50

Scale can accomplish the following transforms:• Uniform scale: sx = sy

• Preserves angles, but not lengths• Non-uniform scale: sx sy

• Doesn’t preserve angles or lengths

• Reflection about the

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

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

20

02 S

20

05.0 S

y = –x

10

01 S

10

01 S

10

01 S

Page 51: Polygon Scan  Conversion

51

Rotation

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

y

xp

22 yxr

x

yarctan

)sin(

)cos(

r

r

y

xp

r

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

p = (x, y)x = r cos()

y = r sin()

)sin(

)cos(

ry

rx

1. Convert point p to polar coordinates:

Page 52: Polygon Scan  Conversion

52

Rotation (cont)

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

)sin(

)cos(

r

r

y

xp

r

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

p = (x, y)

)cos()sin()cos()sin(

)sin()sin()cos()cos(

rr

rr

y

xp

y

x

yx

yx

)cos()sin(

)sin()cos(

)cos()sin(

)sin()cos(p

p´= (x’, y’)

r

4. Substitute x and y back in:

3. Apply the sum of angles formula

2. To rotate degrees, simply add to :

)sin(

)cos(

ry

rx

Page 53: Polygon Scan  Conversion

53

Rotation (cont)

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

)cos()sin(

)sin()cos(

Page 54: Polygon Scan  Conversion

54

Apply the rotation to each vertex of the object• Forward transform:

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

or p´ = Rp where

• Inverse rotation: p = R-1p´

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

Rotating an Object

)θcos()θsin(

)θsin()θcos( R

T

1

)θcos()θsin(

)θsin()θcos(

)θcos()θsin(

)θsin()θcos(

R

R

= 45°

Page 55: Polygon Scan  Conversion

55

Rotations• Right-hand rule: Stick your thumb

towards the z-axis. Your fingers rotate in the positive direction as they curl.

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

r

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

p = (x, y)

p´= (x’, y’)

r

r

x

y

0 1 2 3 4 5 6 701

2

3

4

5

6

7

p = (x, y)

p´= (x’, y’)

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

Page 56: Polygon Scan  Conversion

56

• How can we do more complex transformations?• 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° ( = – / 6): p´ = RTp• Scale in x by ½: p´´ = Sp´• Rotate counter-clockwise 30° ( = / 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.

Page 57: Polygon Scan  Conversion

57

Composition: Scale at Arbitrary Angle

Scale object at an arbitrary angle :

1. Rotate clockwise by : p´ = RTp

2. Scale about origin: p´ = SRTp

3. Rotate back (counter-clockwiseby ): p´ = RSRTp

10

0 xs

S

)θcos()θsin(

)θsin()θcos( TR

)θcos()θsin(

)θsin()θcos( R

x

y

0 1 2 3 401

2

3

4

5

2/31/2-

1/22/3

)6/cos()6/sin(

)6/sin()6/cos( TR

= 30°

x

y

0 1 2 3 401

2

3

4

5

x

y

0 1 2 3 401

2

3

4

5

= 30°

10

05.0 S

Page 58: Polygon Scan  Conversion

58

• 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 = M p

• Much more efficient

Composition and Efficiency

Page 59: Polygon Scan  Conversion

59

Rotation about an Arbitrary Point

• 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

Page 60: Polygon Scan  Conversion

60

Composition: Rotation About a Point

Rotate object about an arbitrary point T:

1. Translate T to origin: p´ = p – T

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

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

)θcos()θsin(

)θsin()θcos( R

= 45°

y

x

t

tT x

y

0 1 2 3 401

2

3

4

5

x

y

0 1 2 3 401

2

3

4

5

2

2

T

x

y

0 1 2 3 401

2

3

4

5

Page 61: Polygon Scan  Conversion

61

Rotation about an Arbitrary Point

• Standard format for rotating about an arbitrary point:

• Translate point of rotation to the origin• Rotate about 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.

Page 62: Polygon Scan  Conversion

62

Homogeneous Coordinates

• 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) specifythe same 2-D Cartesian point if:

p1 = cp2 for some real-valued scalar number c

w

y

x

p

Page 63: Polygon Scan  Conversion

63

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’tinclude the origin

• For simplicity, choose the planew = 1 [x y 1]T = [X Y 1]T

x

y

w

0

0

0

1

0

0

w

wy

wx

y

x

y

x

2

2

2

1

11

/

/

Y

X

wy

wx

w

y

x

Homogeneous point

2-D Cartesiancoordinates

Page 64: Polygon Scan  Conversion

64

Homogeneous Coordinates

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

• And the transformation matrices are 3x3 matrices

w

y

x

p

333231

232221

131211

ttt

ttt

ttt

T

Page 65: Polygon Scan  Conversion

65

Homogeneous Coordinates: Translation

• What does the translation matrix look like?• To translate a point p to point p’, we need

with

and

100

10

01

,

1

y

x

t

t

Ty

x

p

' Tpp

11

'

'

' y

x

ty

tx

y

x

p

Page 66: Polygon Scan  Conversion

66

Homogeneous Coordinates: Scale

• What does the scale matrix look like?• To scale a point p to point p’, we need

with

and

,

1

y

x

p

' Spp

11

'

'

' yS

xS

y

x

p y

x

100

00

00

y

x

S

S

S

Page 67: Polygon Scan  Conversion

67

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

100

0cossin

0sincos

R

θcosθsin

θsinθcos R

Page 68: Polygon Scan  Conversion

68

Composition: Rotation About a Point

Now let’s try that rotation about an arbitrary point T:1. Translate to origin: p´ = T1p

2. Rotate about origin: p´ = RT1p

3. Translate back: p´ = T2RT1p

100

0)54cos()54sin(

0)54sin()54cos(

R

= 45°

100

210

201

T

x

y

0 1 2 3 401

2

3

4

5

x

y

0 1 2 3 401

2

3

4

5

x

y

0 1 2 3 401

2

3

4

5

100

210

201

T

100

210

201

T

Page 69: Polygon Scan  Conversion

69

Composition: Rotation About a Point

• So

• Where M is the composite transformation matrix

Mp

pRTTp

12

100

828.707.707.

2707.707.

100

210

201

100

2707.707.

2707.707.

100

210

201

100

0)54cos()54sin(

0)54sin()54cos(

100

210

201

12RTTM

Page 70: Polygon Scan  Conversion

70

3D Transformations

111000

100

010

001

z

y

x

z

y

x

tz

ty

tx

z

y

x

t

t

t

Tpp

111000

000

000

000

zs

ys

xs

z

y

x

s

s

s

z

y

x

z

y

x

Spp

Mpp

11000

z

y

x

dcba

dcba

dcba

zzzz

yyyy

xxxx

General Case:

Translation: Scale:

Page 71: Polygon Scan  Conversion

71

3D Rotation

Rotate about x-axis Rotate about y-axis

Rotate about z-axis

1000

0100

00cossin

00sincos

zR

1000

0cos0sin

0010

0sin0cos

yR

1000

0cossin0

0sincos0

0001

xR

These rotations can be combined to rotate about an arbitrary axis.

Page 72: Polygon Scan  Conversion

Viewing

Page 73: Polygon Scan  Conversion

73

Graphics PipelineModel Space

World Space

Eye/CameraSpace

Screen Space

Model Transformations

Viewing Transformation

Projection & WindowTransformation

Page 74: Polygon Scan  Conversion

74

Viewing Transformations• 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

Page 75: Polygon Scan  Conversion

75

Projection Environment

x

y

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

• The view direction is parallel to the z axis

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

• We will use a right-handed view system

d

Page 76: Polygon Scan  Conversion

76

Parallel Projection

x

z

(x, y, z)

• So z’ = 0, x’ = x

• Looking down the y axis:

(x’, y’, z’)

Page 77: Polygon Scan  Conversion

77

Parallel Projection• 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

Mpp '

1000

0000

0010

0001

M

What is M?i.e., we simply drop the z coordinate

Page 78: Polygon Scan  Conversion

78

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

• Projection lines converge

Perspective Projection

x

y

z

Virtual Image Plane

Page 79: Polygon Scan  Conversion

79

Perspective Projection Computation

zd

x

d

x

'

Looking down the y axis:

x

x’

d

d-z

By similar triangles:

zd

xdx

'

d

zdx

x

'

d

zx

x

1

'

view plane

eye

p = (x, y, z)

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

z

Page 80: Polygon Scan  Conversion

80

Perspective Projection Computation

zd

y

d

y

'

Looking down the x axis:

yy’

d

d-z

By similar triangles:

zd

ydy

'

d

zdy

y

'

d

zy

y

1

'

view planeeye

p = (x, y, z)

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

y

z

Page 81: Polygon Scan  Conversion

81

Perspective Projection Computation

• So, we have

• what is z’?

• can we put this into matrix form?

d

zy

y

1

'

d

zx

x

1

'

0,

1,

1)',','( have weso ,0'

d

zy

d

zx

zyxz

Page 82: Polygon Scan  Conversion

82

Perspective Projection Computation

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

1

1000

0000

001

10

0001

1

z

y

x

d

z

d

z

Mbad

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

Page 83: Polygon Scan  Conversion

83

Perspective Projection Computation• 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?

d

zyx 10

d

z

d

zy

d

zx

1

0,

1,

1

Page 84: Polygon Scan  Conversion

84

Perspective Projection Computation

• We want:

11

0 z

y

x

ponm

lkji

hgfe

dcba

y

x

dz

0,0,0,1 so, , dcbadczbyaxx

0,0,1,0 so, , hgfehgzfyexy

0,0,0,0 so, ,0 lkjilkzjyix

1,1

,0,0 so, ,1 pd

onmpoznymxd

z

so,

Page 85: Polygon Scan  Conversion

85

Perspective Projection Computation

• So, the new matrix we get is:

11

00

0000

0010

0001

d

M per

This gives us correct results, and is point independent