csce 441 computer graphics 3-d viewing

96
Jinxiang Chai CSCE 441 Computer Graphics 3-D Viewing 1

Upload: dunn

Post on 05-Feb-2016

68 views

Category:

Documents


0 download

DESCRIPTION

CSCE 441 Computer Graphics 3-D Viewing. Jinxiang Chai. Outline. 3D Viewing Required readings: HB 10-1 to 10-10 Compile and run the codes in page 388 - opengl three-dimensional viewing program example. 1. Taking Pictures Using A Real Camera. Steps: - Identify interesting objects - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSCE 441 Computer Graphics 3-D Viewing

Jinxiang Chai

CSCE 441 Computer Graphics3-D Viewing

1

Page 2: CSCE 441 Computer Graphics 3-D Viewing

Outline

3D Viewing

Required readings: HB 10-1 to 10-10

Compile and run the codes in page 388

- opengl three-dimensional viewing program example

2

Page 3: CSCE 441 Computer Graphics 3-D Viewing

Taking Pictures Using A Real Camera

Steps:

- Identify interesting objects

- Rotate and translate the camera to a desired camera viewpoint

- Adjust camera settings such as focal length

- Choose desired resolution and aspect ratio, etc.

- Take a snapshot

Page 4: CSCE 441 Computer Graphics 3-D Viewing

Taking Pictures Using A Real Camera

Steps:

- Identify interesting objects

- Rotate and translate the camera to a desired camera viewpoint

- Adjust camera settings such as focal length

- Choose desired resolution and aspect ratio, etc.

- Take a snapshot

Graphics does the same thing for rendering an image for 3D geometric objects

Page 5: CSCE 441 Computer Graphics 3-D Viewing

Image space

3D Geometry Pipeline

5

Normalized

projection space

View spaceWorld spaceObject space

Aspect ratio &

resolution

Focal length

Rotate and translate the

camera

11

z

y

x

MCPVd

v

u

Page 6: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations...

6

Model space

(Object space)

Page 7: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations...

7

World space

(Object space)

Page 8: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations...

8

Eye space

(View space)

Page 9: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations...

9

Normalized projection

space

Page 10: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations...

10

Image space, window space, raster

space, screen space, device space

Page 11: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

11

Normalized project

space

View spaceWorld spaceObject space

Image space

Page 12: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

World spaceObject space

glTranslate*(tx,ty,tz)

11000

100

010

001

1

z

y

x

t

t

t

z

y

x

x

y

x

Translate, scale &rotate

Page 13: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Object space

11000

000

000

000

1

z

y

x

s

s

s

z

y

x

z

y

x

glScale*(sx,sy,sz)

World space

Translate, scale &rotate

Page 14: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

Object space

glRotate*),,,( zyx vvv

xy

z),( , zyx vvvr

TRRRRRT zyzyz )()()()()(1

Rotate about r by the angle

World space

Translate, scale &rotate

Page 15: CSCE 441 Computer Graphics 3-D Viewing

Image space

3D Geometry Pipeline

15

Screen spaceNormalized project

space

View spaceWorld spaceObject space

Page 16: CSCE 441 Computer Graphics 3-D Viewing

Now look at how we would compute the world->eye transformation

3D Geometry Pipeline

View spaceWorld space

Page 17: CSCE 441 Computer Graphics 3-D Viewing

Now look at how we would compute the world->eye transformation

3D Geometry Pipeline

View spaceWorld space

Rotate&translate

Page 18: CSCE 441 Computer Graphics 3-D Viewing

Camera Coordinate

18

Page 19: CSCE 441 Computer Graphics 3-D Viewing

Canonical coordinate system

- usually right handed (looking down –z axis)

- convenient for project and clipping

Camera Coordinate

Page 20: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

- eye position maps to origin

- right vector maps to x axis

- up vector maps to y axis

- back vector maps to z axis

Camera Coordinate

Page 21: CSCE 441 Computer Graphics 3-D Viewing

We have the camera in world coordinates We want to transformation T which takes object from world

to camera

Viewing Transformation

wcw

c pTp ,

Page 22: CSCE 441 Computer Graphics 3-D Viewing

We have the camera in world coordinates We want to transformation T which takes object from world

to camera

Trick: find T-1 taking object from camera to world

Viewing Transformation

wcw

c pTp ,

ccw

w pTp 1, )(

Page 23: CSCE 441 Computer Graphics 3-D Viewing

We have the camera in world coordinates We want to transformation T which takes object from world

to camera

Trick: find T-1 taking object from camera to world

Viewing Transformation

wcw

c pTp ,

ccw

w pTp 1, )(

plhd

okgc

njfb

miea

TT wccw ,1

,

?

Page 24: CSCE 441 Computer Graphics 3-D Viewing

Review: 3D Coordinate Trans.

Transform object description from to

pj

i

i

j

o

o

'kji ijk

1

'

'

'

1000

'

'

'

10

0

0

z

y

x

zkkjkik

ykjjjij

xkijiii

z

y

x

TTT

TTT

TTT

),,( 000 zyx

k

'k

poooop

25

Page 25: CSCE 441 Computer Graphics 3-D Viewing

Review: 3D Coordinate Trans.

Transform object description from to

pj

i

0

0

1

i

0

1

0

j

o

o

'kji ijk

1

'

'

'

1000

'''

'''

'''

10

0

0

z

y

x

zkji

ykji

xkji

z

y

x

zzz

yyy

xxx),,( 000 zyx

1

0

0

k

'k

poooop

26

Page 26: CSCE 441 Computer Graphics 3-D Viewing

Review: 3D Coordinate Trans.

Transform object description from camera to world

27

),,( zyx uuu),,( zyx nnn),,( zyx vvv

10000

0

0

,1

, znvu

ynvu

xnvu

TTzzz

yyy

xxx

wccw

Page 27: CSCE 441 Computer Graphics 3-D Viewing

Trick: find T-1 taking object from camera to world

- eye position maps to origin

- back vector maps to z axis

- up vector maps to y axis

- right vector maps to x axis

Viewing Transformation

10000

0

0

,1

, znvu

ynvu

xnvu

TTzzz

yyy

xxx

wccw

Page 28: CSCE 441 Computer Graphics 3-D Viewing

Trick: find T-1 taking object from camera to world

Viewing Transformation

1

0

1

0

0

0

, 1000

1000

pnvu

znvu

ynvu

xnvu

Tzzz

yyy

xxx

cw

100

0

0

, pn

pv

pu

n

v

u

TT

T

T

cw

H&B equation (10-4)

Page 29: CSCE 441 Computer Graphics 3-D Viewing

Viewing Trans: gluLookAt

30

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

Page 30: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

Viewing Trans: gluLookAt

),,( zyxref atatatp

),,(0 zyx eyeeyeeyep

),,( zyx upupupV

How to determine ?

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

nvu

,,

Page 31: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

Viewing Trans: gluLookAt

),,( zyxref atatatp

),,(0 zyx eyeeyeeyep

),,( zyx upupupV

How to determine ?

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

nvu

,,

Page 32: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

Viewing Trans: gluLookAt

),,( zyxref atatatp

),,(0 zyx eyeeyeeyep

refppN

0

),,( zyx upupupV

N

Nn

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

Page 33: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

Viewing Trans: gluLookAt

),,( zyxref atatatp

),,(0 zyx eyeeyeeyep

refppN

0

),,( zyx upupupV

N

Nn

V

nVu

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

Page 34: CSCE 441 Computer Graphics 3-D Viewing

Mapping from world to eye coordinates

Viewing Trans: gluLookAt

),,( zyxref atatatp

),,(0 zyx eyeeyeeyep

refppN

0

),,( zyx upupupV

N

Nn

V

nVu

unv

H&B equation (10-1)

gluLookAt (eyex,eyey,eyez,atx,aty,atz,upx, upy,upz)

Page 35: CSCE 441 Computer Graphics 3-D Viewing

Viewing Trans: gluLookAt

Test: gluLookAt( 4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 0, 1.0, 0 )

- What’s the transformation matrix from the world space to the camera reference system?

Page 36: CSCE 441 Computer Graphics 3-D Viewing

3D Geometry Pipeline

View spaceWorld space

3D-3D viewing

transformation

Page 37: CSCE 441 Computer Graphics 3-D Viewing

Projection

General definition

transform points in n-space to m-space (m<n)

In computer graphics

map 3D coordinates to 2D screen coordinates

Page 38: CSCE 441 Computer Graphics 3-D Viewing

Projection

General definition

transform points in n-space to m-space (m<n)

In computer graphics

map 3D coordinates to 2D screen coordinates

How can we project 3d objects to 2d screen space?

Page 39: CSCE 441 Computer Graphics 3-D Viewing

How Do We See the World?

Let’s design a camera:

idea 1: put a piece of film in front of camera

Do we get a reasonable picture?

Page 40: CSCE 441 Computer Graphics 3-D Viewing

Pin-hole Camera

Add a barrier to block off most of the raysThis reduces blurringThe opening known as the apertureHow does this transform the image?

Page 41: CSCE 441 Computer Graphics 3-D Viewing

Camera Obscura

The first camera Known to Aristotle Depth of the room is the focal length Pencil of rays – all rays through a point

Page 42: CSCE 441 Computer Graphics 3-D Viewing

Perspective Projection

Maps points onto “view plane” along projectors emanating from “center of projection” (COP)

43

Page 43: CSCE 441 Computer Graphics 3-D Viewing

Perspective Projection

Maps points onto “view plane” along projectors emanating from “center of projection” (COP)

44

What’s relationship between 3D points and

projected 2D points?

Page 44: CSCE 441 Computer Graphics 3-D Viewing

Consider the projection of a 3D point on the camera plane

3D->2D

45

Page 45: CSCE 441 Computer Graphics 3-D Viewing

Consider the projection of a 3D point on the camera plane

3D->2D

46

Page 46: CSCE 441 Computer Graphics 3-D Viewing

Consider the projection of a 3D point on the camera plane

3D->2D

47

By similar triangles, we can compute how much the x and y coordinates are scaled

Page 47: CSCE 441 Computer Graphics 3-D Viewing

Consider the projection of a 3D point on the camera plane

3D->2D

48

By similar triangles, we can compute how much the x and y coordinates are scaled

z

dxx

d

x

z

x

'

z

dyy

d

y

z

y

'

'

Page 48: CSCE 441 Computer Graphics 3-D Viewing

Homogeneous Coordinates

Is this a linear transformation?

Page 49: CSCE 441 Computer Graphics 3-D Viewing

Homogeneous Coordinates

Is this a linear transformation?– no—division by z is nonlinear

Page 50: CSCE 441 Computer Graphics 3-D Viewing

Homogeneous Coordinates

Is this a linear transformation?

Trick: add one more coordinate:

homogeneous image

coordinates

homogeneous scene

coordinates

– no—division by z is nonlinear

Page 51: CSCE 441 Computer Graphics 3-D Viewing

Remember how we said 2D/3D geometric transformations work with the last coordinate always set to one

What happens if the coordinate is not one We divide all coordinates by w:

Homogeneous Point Revisited

52

If w=1, nothing happens

Sometimes, we call this division step the “perspective divide”

Page 52: CSCE 441 Computer Graphics 3-D Viewing

Now we can rewrite the perspective projection equation as matrix-vector multiplications

The Perspective Matrix

53

z

dxx

d

x

z

x

'

z

dyy

d

y

z

y

'

'

Page 53: CSCE 441 Computer Graphics 3-D Viewing

Now we can rewrite the perspective projection equation as matrix-vector multiplications

The Perspective Matrix

54

z

dxx

d

x

z

x

'

z

dyy

d

y

z

y

'

'

10100

0010

0001

'

'

'

z

y

x

dw

y

x

This becomes a linear transformation!

Page 54: CSCE 441 Computer Graphics 3-D Viewing

Now we can rewrite the perspective projection equation as matrix-vector multiplications

The Perspective Matrix

55

After the division by w, we have

z

dxx

d

x

z

x

'

z

dyy

d

y

z

y

'

'

10100

0010

0001

'

'

'

z

y

x

dw

y

x

11

'

'

zdy

zdx

y

x

Page 55: CSCE 441 Computer Graphics 3-D Viewing

Perspective Effects

Distant object becomes small

The distortion of items when viewed at an angle (spatial foreshortening)

Page 56: CSCE 441 Computer Graphics 3-D Viewing

Perspective Effects

Distant object becomes small

The distortion of items when viewed at an angle (spatial foreshortening)

Page 57: CSCE 441 Computer Graphics 3-D Viewing

Perspective Effects

Distant object becomes small

The distortion of items when viewed at an angle (spatial foreshortening)

Page 58: CSCE 441 Computer Graphics 3-D Viewing

Perspective projection is an example of projective transformation

- lines maps to lines

- parallel lines do not necessary remain parallel

- ratios are not preserved

Properties of Perspective Proj.

Page 59: CSCE 441 Computer Graphics 3-D Viewing

Perspective projection is an example of projective transformation

- lines maps to lines

- parallel lines do not necessary remain parallel

- ratios are not preserved One of advantages of perspective projection is that

size varies inversely proportional to the distance-looks realistic

Properties of Perspective Proj.

Page 60: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

Vanishing Points

61

Page 61: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

Vanishing Points

62

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

p v

Page 62: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

After perspective transformation, we have

Vanishing Points

63

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

11

'

'

zdy

zdx

y

x

d

tvp

tvp

dtvp

tvp

y

x

zz

yy

zz

xx

11

'

'

p v

Page 63: CSCE 441 Computer Graphics 3-D Viewing

Letting t go to infinity:

Vanishing Points (cont.)

64

d

tvp

tvp

dtvp

tvp

y

x

zz

yy

zz

xx

11

'

'

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

Page 64: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

Vanishing Points

65

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

Page 65: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

How about the line

Vanishing Points

66

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

01z

y

x

z

y

x

v

v

v

tq

q

q

vtql

Page 66: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

How about the line

Vanishing Points

67

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

01z

y

x

z

y

x

v

v

v

tq

q

q

vtql

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

Page 67: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

The equation of the line:

How about the line

Vanishing Points

68

01z

y

x

z

y

x

v

v

v

tp

p

p

vtpl

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

01z

y

x

z

y

x

v

v

v

tq

q

q

vtql

d

v

v

dv

v

y

x

z

y

z

x

11

'

'

Same vanishing point!

Page 68: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

Each set of parallel lines intersect at a vanishing point on the PP

Vanishing Points

Page 69: CSCE 441 Computer Graphics 3-D Viewing

What happens to parallel lines they are not parallel to the projection plane?

Each set of parallel lines intersect at a vanishing point on the PP

Vanishing Points

Page 70: CSCE 441 Computer Graphics 3-D Viewing

Parallel Projection

Center of projection is at infinity

Direction of projection (DOP) same for all points

Page 71: CSCE 441 Computer Graphics 3-D Viewing

Orthographic Projection

Direction of projection (DOP) perpendicular to view plane

Page 72: CSCE 441 Computer Graphics 3-D Viewing

Orthographic Projection

Direction of projection (DOP) perpendicular to view plane

1000

0000

0010

0001

Page 73: CSCE 441 Computer Graphics 3-D Viewing

Properties of Parallel Projection

Not realistic looking Good for exact measurement Are actually affine transformation

- parallel lines remain parallel

- ratios are preserved

- angles are often not preserved Most often used in CAD, architectural drawings,

etc. where taking exact measurement is important

Page 74: CSCE 441 Computer Graphics 3-D Viewing

Perspective projection from 3D to 2D

3D->2D

75

10100

0010

0001

'

'

'

z

y

x

dw

y

x

Page 75: CSCE 441 Computer Graphics 3-D Viewing

Perspective projection from 3D to 2D

3D->2D

76

10100

0010

0001

'

'

'

z

y

x

dw

y

x

But so far, we have not considered the size of film

plane!

We have also not considered visibility problem

Page 76: CSCE 441 Computer Graphics 3-D Viewing

Image space

3D Geometry Pipeline

77

Screen spaceNormalized project

space

View spaceWorld spaceObject space

z = -1z = 1

Page 77: CSCE 441 Computer Graphics 3-D Viewing

The center of projection and the portion of projection plane that map to the final image form an infinite pyramid. The sides of pyramid called clipping planes

Additional clipping planes are inserted to restrict the range of depths

Perspective Projection Volume

Page 78: CSCE 441 Computer Graphics 3-D Viewing

OpenGL Perspective-Projection

79

Normalized project

space

View space

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

gluPerspective(fovy,aspect,dnear, dfar)

z = -1z = 1

Page 79: CSCE 441 Computer Graphics 3-D Viewing

View space

(xwmin,ywmin,Znear)(xwmax,ywmax,znear)

General Perspective-Projection

zfar

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

Six parameters define six clipping planes!

nearnear zd

Page 80: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

Left-vertical clipping plane

Page 81: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

right-vertical clipping plane

Page 82: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

bottom-horizontal clipping plane

Page 83: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

top-horizontal clipping plane

Page 84: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

near clipping plane

nearnear zd

Page 85: CSCE 441 Computer Graphics 3-D Viewing

General Perspective-Projection

glFrustum(xwmin, xwmax,ywmin, ywmax ,dnear,dfar)

far clipping plane

farfar zd

Page 86: CSCE 441 Computer Graphics 3-D Viewing

Normalized project space (left-

handed)

View space (right-

handed)

B’=(1,1,1)

A’=(-1,-1,-1)

General Perspective-Projection

A maps to A’, B maps to B’Keep the directions of x and y axes!

A

B

Page 87: CSCE 441 Computer Graphics 3-D Viewing

B’=(1,1,1)

A’=(-1,-1,-1)

General Perspective-Projection

0100

200

02

0

002

minmax

minmax

minmax

minmax

minmax

minmax

farnear

farnear

farnear

farnear

near

near

zz

zz

zz

zzywyw

ywyw

ywyw

zxwxw

xwxw

xwxw

z

T

H&B equation (10-40)

A

B

Page 88: CSCE 441 Computer Graphics 3-D Viewing

OpenGL Symmetric Perspective-Projection Function gluPerspective(fovy,aspect,dnear, dfar)

89

Assume z-axis is the centerline of 3D view frustum!

Page 89: CSCE 441 Computer Graphics 3-D Viewing

Image space

3D Geometry Pipeline

90

Normalized project

space

View spaceWorld spaceObject space

Page 90: CSCE 441 Computer Graphics 3-D Viewing

Image space-.>Image space

Viewport Transformation

91

glViewport(xmin, ymin, width, height)

(xmin,ymin)

Display window

viewport

Page 91: CSCE 441 Computer Graphics 3-D Viewing

Image space-.>Image space

Viewport Transformation

92

glViewport(xmin, ymin, width, height)

(xmin,ymin)

- Besides x and y, each pixel has a depth value z, which is stored in depth buffer.

-Depth values will be used for visibility testing

-The color of the pixel is stored in color buffer

Page 92: CSCE 441 Computer Graphics 3-D Viewing

93

Image space

Normalized project

space

B’=(1,1,1)

A’=(-1,-1,-1)

Normalized project space

A’’=(xvmin,yvmin,0)

B’’=(xvmax,yvmax,1)

Viewport Transformation

-1<=z<=1

0<=z<=1

Page 93: CSCE 441 Computer Graphics 3-D Viewing

Viewport Transformation

94

Image space

Normalized project

space

B’=(1,1,1)

A’=(-1,-1,-1)

Normalized project space

A’’=(xvmin,yvmin,0)

B’’=(xvmax,yvmax,1)

10002

1

2

100

20

20

200

2minmaxminmax

minmaxminmax

yvyvyvyv

xvxvxvxv

T

H&B equation (10-42)

-1<=z<=1

0<=z<=1

Page 94: CSCE 441 Computer Graphics 3-D Viewing

Image space

Summary: 3D Geometry Pipeline

95

Normalized project

space

View spaceWorld spaceObject space

Page 95: CSCE 441 Computer Graphics 3-D Viewing

Taking Steps Together

96

11

z

y

x

MCPVd

v

u

Image spaceNormalized project

space

View spaceWorld spaceObject space

Page 96: CSCE 441 Computer Graphics 3-D Viewing

OpenGL Codes

97