48
Computer Graphics 6 - View Transformation and Clipping Tom Thorne Slides courtesy of Taku Komura www.inf.ed.ac.uk/teaching/courses/cg

Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Computer Graphics 6 - View Transformation andClipping

Tom Thorne

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

Page 2: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Overview

View transformation

I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume

Clipping

I Line and polygon clipping

Page 3: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Outline of procedure

1. Transform from world to camera coordinates2. Project into view volume or screen coordinates3. Clip geometry that falls outside of the view volume

Page 4: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

View projection

I Homogeneous transformationI Parallel projectionI Perspective projectionI Canonical view volume

Page 5: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Homogeneous transformations

v = MprojMc←gMg←lvl

Page 6: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 thesame point

I Return to Cartesian coordinates using(x ′, y ′, z ′) = (x/w , y/w , z/w)

Page 7: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Camera coordinate system

I As used by OpenGL

Page 8: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Parallel (orthographic) projection

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

Page 9: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Parallel projection

Project coordinates onto plane at z = 0

x ′ = x , y ′ = y , z ′ = 0

Morth =

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

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

xyz1

=

xy01

Page 10: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Perspective projection

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

Page 11: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Perspective projection

Page 12: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Perspective projection - simple case

Page 13: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Perspective projection

x ′y ′−d1

=

−d x

z−d y

z−d1

=

xyz−z/d

Represented as a matrix multiplication:

Mproj =

1 0 0 00 1 0 00 0 1 00 0 − 1

d 0

Page 14: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Perspective projection

Vp = MprojVc =

1 0 0 00 1 0 00 0 1 00 0 − 1

d 0

xyz1

=

xyz− z

d

=

x−z/d

y−z/d−d

Page 15: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Alternative formulation

Page 16: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Alternative formulation

$OWHUQDWLYH�IRUPXODWLRQ�

]

3�[�\�]�

G

[

[S

]

3�[�\�]�

G

\

\S

3URMHFWLRQ�SODQH�DW�]� ���&HQWUH�RI�SURMHFWLRQ�DW������������]�� �G

1RZ�ZH�FDQ�DOORZ�Gĺ�

Mproj =

1 0 0 00 1 0 00 0 0 00 0 −1

d 1

Page 17: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Exercise

Where will the two points be projected?

Mproj =

1 0 0 00 1 0 00 0 0 00 0 − 1

d 1

Page 18: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Problems

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

I Objects behind the camera are projected infront of thecamera!

Page 19: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Canonical view volume

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

I Instead transform the frustum into a normalised canonicalview volume

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

Page 21: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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

l Left x coordinate of near planer Right x coordinate of near planeb Bottom y coordinate of near planet Top y coordinate of near planen Minus z coordinate of near planef Minus z coordinate of far plane

With 0 < n < f .

Page 22: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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

Mcan =

2nr−l 0 r+l

r−l 00 2n

t−bt+bt−b 0

0 0 − f +nf−n − 2fn

f−n0 0 −1 0

Page 23: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Example

What does ABC look like after projection?

Mcan =2nr−l 0 r+l

r−l 00 2n

t−bt+bt−b 0

0 0 − f +nf−n − 2fn

f−n0 0 −1 0

Page 25: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

ExampleMcan =

2nr−l 0 r+l

r−l 00 2n

t−bt+bt−b 0

0 0 − f +nf−n − 2fn

f−n0 0 −1 0

=

1 0 0 00 1 0 00 0 −2 −30 0 −1 0

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

1 0 0 00 1 0 00 0 −2 −30 0 −1 0

30−21

=

3012

=

320121

Page 26: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Projection summary

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

screenI Canonical view volumes can be used for clipping

Page 27: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Overview

View transformation

I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume

Clipping

I Line and polygon clipping

Page 28: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Projection of polygons and lines

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

Page 29: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Cohen-Sutherland algorithm

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

Page 31: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Cohen-Sutherland algorithm

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Cohen-Sutherland algorithm

Outcodes:

1st bit Above top of screen (y > ymax )2nd bit Below bottom of screen (y < ymin)3rd bit Right of right edge of screen (x > xmax )4th bit Left of left edge of screen (x < xmin)

Page 33: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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;

endend

Page 34: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Trivial accept

All line endpoints are inside the screen

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

Page 36: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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;

endend

Page 37: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Trivial reject

Both 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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Cohen-Sutherland algorithm

Page 39: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Cohen-Sutherland algorithm

Page 40: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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;

endend

Page 41: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Sutherland-Hodgman algorithm

Traverse edges and divide into four types:

Page 45: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Sutherland-Hodgman algorithm

For each edge of the clipping rectangle:

I For each polygon edge between vi and vi+1

Page 46: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Example

Page 47: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

Summary

Projection

I Parallel and perspectiveI Canonical view volumes

Clipping

I Cohen-Sutherland algorithmI Sutherland-Hodgman algorithm

Page 48: Computer Graphics 6 - View Transformation and Clipping · Computer Graphics 6 - View Transformation and Clipping TomThorne ... Cohen-Sutherland algorithm Data:LinefromcoordinateAtoB

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