Jinxiang Chai CSCE 441 Computer Graphics 3-D Viewing 0.

Post on 21-Jan-2016

245 views 0 download

Tags:

Transcript of Jinxiang Chai CSCE 441 Computer Graphics 3-D Viewing 0.

Jinxiang Chai

CSCE 441 Computer Graphics3-D Viewing

1

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

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

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

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

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)

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)

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)

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

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

3D Geometry Pipeline

11

Normalized project

space

View spaceWorld spaceObject space

Image space

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

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

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

Image space

3D Geometry Pipeline

15

Screen spaceNormalized project

space

View spaceWorld spaceObject space

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

3D Geometry Pipeline

View spaceWorld space

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

3D Geometry Pipeline

View spaceWorld space

Rotate&translate

Camera Coordinate

18

Canonical coordinate system

- usually right handed (looking down –z axis)

- convenient for project and clipping

Camera Coordinate

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

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

to camera

Viewing Transformation

wcw

c pTp ,

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, )(

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

,

?

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

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

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

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

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)

Viewing Trans: gluLookAt

30

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

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

,,

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

,,

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)

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)

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)

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?

3D Geometry Pipeline

View spaceWorld space

3D-3D viewing

transformation

Projection

General definition

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

In computer graphics

map 3D coordinates to 2D screen coordinates

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?

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?

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?

Camera Obscura

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

Perspective Projection

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

43

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?

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

3D->2D

45

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

3D->2D

46

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

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

'

'

Homogeneous Coordinates

Is this a linear transformation?

Homogeneous Coordinates

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

Homogeneous Coordinates

Is this a linear transformation?

Trick: add one more coordinate:

homogeneous image

coordinates

homogeneous scene

coordinates

– no—division by z is nonlinear

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”

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

'

'

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!

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

Perspective Effects

Distant object becomes small

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

Perspective Effects

Distant object becomes small

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

Perspective Effects

Distant object becomes small

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

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.

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.

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

Vanishing Points

61

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

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

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

'

'

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

'

'

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

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

'

'

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!

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

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

Parallel Projection

Center of projection is at infinity

Direction of projection (DOP) same for all points

Orthographic Projection

Direction of projection (DOP) perpendicular to view plane

Orthographic Projection

Direction of projection (DOP) perpendicular to view plane

1000

0000

0010

0001

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

Perspective projection from 3D to 2D

3D->2D

75

10100

0010

0001

'

'

'

z

y

x

dw

y

x

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

Image space

3D Geometry Pipeline

77

Screen spaceNormalized project

space

View spaceWorld spaceObject space

z = -1z = 1

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

OpenGL Perspective-Projection

79

Normalized project

space

View space

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

gluPerspective(fovy,aspect,dnear, dfar)

z = -1z = 1

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

General Perspective-Projection

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

Left-vertical clipping plane

General Perspective-Projection

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

right-vertical clipping plane

General Perspective-Projection

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

bottom-horizontal clipping plane

General Perspective-Projection

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

top-horizontal clipping plane

General Perspective-Projection

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

near clipping plane

nearnear zd

General Perspective-Projection

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

far clipping plane

farfar zd

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

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

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

89

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

Image space

3D Geometry Pipeline

90

Normalized project

space

View spaceWorld spaceObject space

Image space-.>Image space

Viewport Transformation

91

glViewport(xmin, ymin, width, height)

(xmin,ymin)

Display window

viewport

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

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

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

Image space

Summary: 3D Geometry Pipeline

95

Normalized project

space

View spaceWorld spaceObject space

Taking Steps Together

96

11

z

y

x

MCPVd

v

u

Image spaceNormalized project

space

View spaceWorld spaceObject space

OpenGL Codes

97