Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world...

47
Computer Graphics 5 - View transformation and clipping Tom Thorne Slides courtesy of Taku Komura www.inf.ed.ac.uk/teaching/courses/cg

Transcript of Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world...

Page 1: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Computer Graphics 5 - View transformation andclipping

Tom Thorne

Slides courtesy of Taku Komurawww.inf.ed.ac.uk/teaching/courses/cg

Page 2: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Overview

View transformation

I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume

Clipping

I Line and polygon clipping

Page 3: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Outline of procedure

1. Transform from world to camera coordinates2. Project into view volume or screen coordinates

3. Clip geometry that falls outside of the view volume

vg

g

c

Mg<-c

Page 4: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

View projection

I Homogeneous transformationI Parallel projectionI Perspective projectionI Canonical view volume

Page 5: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Homogeneous transformations

v = Mproj

McΩg

MgΩl

vl

vg

g

c

Mg<-c vgv l

v l

Projection Global to camera Local to global

Page 6: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Homogeneous coordinates

I Allows us to represent translation, rotation and scaling bymatrix multiplication

I Coordinates (x , y , z , w) and (cx , cy , cz , cw) represent the samepoint

I Return to Cartesian coordinates using(x Õ, y

Õ, z

Õ) = (x/w , y/w , z/w)

Page 7: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Camera coordinate system

I As used by OpenGLI Camera is facing in the negative z direction

z

y

x

y

x

(1,1)(-1,1)

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

Page 8: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Parallel (orthographic) projection

I Specified by a direction of projectionI No change in size of objects

Page 9: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Parallel projection

Project coordinates onto plane at z = 0x

Õ = x , y

Õ = y , z

Õ = 0

M

orth

=

Q

ccca

1 0 0 00 1 0 00 0 0 00 0 0 1

R

dddb

Q

ccca

1 0 0 00 1 0 00 0 0 00 0 0 1

R

dddb

Q

ccca

x

y

z

1

R

dddb =

Q

ccca

x

y

01

R

dddb

Page 10: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Perspective projection

I Far away objects appear smallerI Specified by a center of projection and focal distance

Page 11: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Perspective projection

y

x

+z -z

d

(x,y,z)

(x ,y ,-d)p p

Page 12: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Perspective projection - simple case

From similar triangles:

I x

p

d

= x

≠z

, y

p

d

= y

≠z

Ix

p

= dx

≠z

= x

≠z/d

, y

p

= dy

≠z

= y

≠z/d

y

+z -z

d

(x,y,z)

(x ,y ,-d)p p

yp

y

Page 13: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Perspective projection

Q

ccca

x

Õ

y

Õ

≠d

1

R

dddb =

Q

ccca

≠d

x

z

≠d

y

z

≠d

1

R

dddb =

Q

ccca

x

y

z

≠z/d

R

dddb

Represented as a matrix multiplication:

M

proj

=

Q

ccca

1 0 0 00 1 0 00 0 1 00 0 ≠ 1

d

0

R

dddb

Page 14: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Perspective projection

V

p

= M

proj

V

c

=

Q

ccca

1 0 0 00 1 0 00 0 1 00 0 ≠ 1

d

0

R

dddb

Q

ccca

x

y

z

1

R

dddb =

Q

ccca

x

y

z

≠ z

d

R

dddb =

Q

ca

x

≠z/d

y

≠z/d

≠d

R

db

Page 15: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Alternative formulationCenter of projection at z = d , projection plane at z = 0

I x

p

d

= x

≠z+d

, y

p

d

= y

≠z+d

Ix

p

= dx

≠z+d

= x

(≠z/d)+1 , y

p

= dy

≠z

= y

(≠z/d)+1

y

+z -z

d

(x,y,z)

(x ,y ,0)p p

yp

y

This allows for d æ Œ

Page 16: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Alternative formulationy

+z -z

d

(x,y,z)

(x ,y ,0)p p

yp

y

Ix

p

= dx

≠z+d

= x

(≠z/d)+1 , y

p

= dy

≠z

= y

(≠z/d)+1

M

proj

=

Q

ccca

1 0 0 00 1 0 00 0 0 00 0 ≠1

d

1

R

dddb

Page 17: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Exercise

Where will the two points be projected?

(-1,-1,-1)

(-2,2,-2)

d=1

M

proj

=

Q

ccca

1 0 0 00 1 0 00 0 0 00 0 ≠ 1

d

1

R

dddb

Page 18: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Problems

I After projection depth information is lost (this is needed forhidden surface removal)

I Objects behind the camera are projected infront of the camera!

Page 19: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

3D view volumeDefine a volume in which objects are visible

I For parallel projection, a boxI For perspective projection, a frustumI Surfaces outside this volume are clipped or removed and not

drawn

Page 20: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Canonical view volume

It can be computationally expensive to check if a point is inside afrustum

I Instead transform the frustum into a normalised canonical viewvolume

I Uses the same ideas a perspective projectionI Makes clipping and hidden surface calculation much easier

Page 21: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Transforming the view frustumThe frustum is defined by a set of parameters, l , r , b, t, n, f :

l Left x coordinate ofnear plane

r Right x coordinateof near plane

b Bottom ycoordinate of nearplane

t Top y coordinate ofnear plane

n Minus z coordinateof near plane

f Minus z coordinateof far plane

With 0 < n < f .

z=-n

z=-f

(l,b,-n) (r,t,-n)

Page 22: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Transforming the view frustum

We can transform the perspective canonical view volume to theparallel view volume using:

If z œ [≠n, ≠f ] and 0 < n < f then

M

can

=

Q

ccca

2n

r≠l

0 r+l

r≠l

00 2n

t≠b

t+b

t≠b

00 0 ≠ f +n

f ≠n

≠ 2fn

f ≠n

0 0 ≠1 0

R

dddb

Page 23: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Final steps

I After transformation to canonical view volume, divide by w toproduce 3D Cartesian coordinates

I Perform clipping in the canonical view volume:

≠1 Æ x Æ 1,≠1 Æ y Æ 1,≠1 Æ z Æ 1

Page 24: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Example

What does ABC look like after projection?

M

can

=Q

ccca

2n

r≠l

0 r+l

r≠l

00 2n

t≠b

t+b

t≠b

00 0 ≠ f +n

f ≠n

≠ 2fn

f ≠n

0 0 ≠1 0

R

dddb

Page 25: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

ExampleM

can

=Q

ccca

2n

r≠l

0 r+l

r≠l

00 2n

t≠b

t+b

t≠b

00 0 ≠ f +n

f ≠n

≠ 2fn

f ≠n

0 0 ≠1 0

R

dddb =

Q

ccca

1 0 0 00 1 0 00 0 ≠2 ≠30 0 ≠1 0

R

dddb

n = 1,f = 3,r = 1,l = ≠1,t =1,b = ≠1

Q

ccca

1 0 0 00 1 0 00 0 ≠2 ≠30 0 ≠1 0

R

dddb

Q

ccca

30

≠21

R

dddb =

Q

ccca

3012

R

dddb =

Q

ccca

320121

R

dddb

Page 26: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Projection summaryI Parallel and perspective projectionI Projection matrices transform points to 2D coordinates on the

screenI Canonical view volumes can be used for clipping

z=-n

z=-f

(l,b,-n) (r,t,-n)

Page 27: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Overview

View transformation

I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume

Clipping

I Line and polygon clipping

Page 28: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Projection of polygons and lines

I Lines in 3D become lines in 2DI Polygons in 3D become polygons in 2D

Page 29: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Clipping

They may intersect the canonical view volume, then we need toperform clipping:

I Clipping lines (Cohen-Sutherland algorithm)I Clipping polygons (Sutherland-Hodgman algorithm)

Page 30: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

Input Screen and 2D line segmentOutput Clipped 2D line segment

Page 31: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

0001

101010001001

0010

011001000101

0000

Outcodes:

I Space is split into nine regionsI The center corresponds to the area visible on the screenI Each region is encoded by four bits

Page 32: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

0001

101010001001

0010

011001000101

0000

4th bit

1st bit

2nd bit

3rd bit

Outcodes:

1st bit Above top of screen (y > y

max

)2nd bit Below bottom of screen (y < y

min

)3rd bit Right of right edge of screen (x > x

max

)4th bit Left of left edge of screen (x < x

min

)

Page 33: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

Data: Line from coordinate A to BResult: Clipped linewhile true do

Calculate outcodes of endpoints;if trivial accept or trivial reject then

return accepted line or reject;else

Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;

end

end

Page 34: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Bitwise AND and OR

0 OR 0 = 00 OR 1 = 11 OR 0 = 11 OR 1 = 1

0 AND 0 = 00 AND 1 = 01 AND 0 = 01 AND 1 = 1

Page 35: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Trivial acceptAll line endpoints are inside the screen

I Apply an OR to outcodes of the line endpoints, test for equalto 0

Page 36: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

Data: Line from coordinate A to BResult: Clipped linewhile true do

Calculate outcodes of endpoints;if trivial accept or trivial reject then

return accepted line or reject;else

Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;

end

end

Page 37: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Trivial rejectBoth line endpoints are outside of the screen on the same side

I Apply AND operation to the two endpoints and reject if not 0

Page 38: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

0001

101010001001

0010

011001000101

0000

0001

0000

1000

0000

Logical AND between codes for two endpoints. If non-zero, trivialrejection.

Page 39: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

0001

101010001001

0010

011001000101

0000

Logical AND between codes for two endpoints. If non-zero, trivialrejection.

Page 40: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

Data: Line from coordinate A to BResult: Clipped linewhile true do

Calculate outcodes of endpoints;if trivial accept or trivial reject then

return accepted line or reject;else

Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;

end

end

Page 41: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Line intersectionI Need to clip a line to the edges of the rectangleI Find an endpoint which is outside the rectangleI Select edge to clip based on the outcode, split the line and

feedback into algorithm

A

B

A'A''

Page 42: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Cohen-Sutherland algorithm

Extension to 3D

I Clip lines to near and far planes of view volumeI Needs more bits for the outcode

Page 43: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Polygon clipping

Sutherland-Hodgman algorithm

Input 2D polygonOutput List of clipped vertices

Traverse the polygon and clip at each edge of the screen

Page 44: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Sutherland-Hodgman algorithm

For each edge of the clipping rectangle:

I For each polygon edge between v

i

and v

i+1

Edge of screenOutsideInside

Case 1

Output both vertices

Edge of screenOutsideInside

Case 2

Output intersection with screen

Edge of screenOutsideInside

Case 3

No output

Edge of screenOutsideInside

Case 4

Output intersection with screenand second vertex of edge

Page 45: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Example

Page 46: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

Summary

Projection

I Parallel and perspectiveI Canonical view volumes

Clipping

I Cohen-Sutherland algorithmI Sutherland-Hodgman algorithm

Page 47: Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world to camera coordinates 2. Project into view volume or screen coordinates 3. Clip geometry

References

View transformation

I Shirley, Chapter 7I Foley, Chapter 6

Clipping

I Foley Chapter 3.12, 3.14I http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/

Haowei.Hsieh/mm.html