3d viewing and projection 5 - villanova university · 2010-10-06 · 3d viewing and projection...

27
3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates Describe not just the objects themselves, but specify also where in the world they are and how they appear (translated, rotated, scaled). z world x world y world

Upload: others

Post on 08-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

3D Viewing and ProjectionReading: Chapter 5

Review: Model to World Coordinates

• Describe not just the objects themselves, but specify also where in the world they are and how they appear (translated, rotated, scaled).

zworld

xworld

yworld

Page 2: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Today: World to Viewing Coordinates

World coordinates Viewing coordinates:Viewers (Camera) position and viewing plane.

zworld

xworld

yworld xview

yview

zview

Transformation Pipeline

ModelingCoordinates

ModelingTransformation

WorldCoordinates

ViewingTransformation

ViewingCoordinates

ProjectionTransformation

ProjectionCoordinates

Page 3: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

The Viewing System

Three aspects of the viewing process:

1. Positioning the camera– Setting the MODELVIEW matrix

2. Selecting a lens– Setting the PROJECTION matrix

3. Clipping– Setting the view volume

Start with a discussion of OpenGL defaults.

OpenGL Viewing Defaults

Page 4: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

The OpenGL Camera

• In OpenGL, initially the world and camera frames are the same

• The camera is located at origin and points in the ‐z direction

• OpenGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin:

glOrtho(‐1, 1, ‐1, 1, ‐1,1);

The OpenGL (default) Projection

• Default projection is orthogonal (orthographic)

• Default projection matrix is identity

clipped out

(z coordinates dropped)

Page 5: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Orthographic Projection in OpenGLglOrtho(xmin, xmax, ymin, ymax, zmin, zmax);

• We always view in ‐z direction  • zmin and zmax are specified as positive distances along –z,relative to the camera

Hands‐on Session

The OpenGL tutor programs

Go to the class website, click on the Links section

Download and compile the OpenGL tutors

Run the projection tutor

Use the menu to select orthographic projection

Play with the parameters of glOrtho

1. Double the viewing volume

2. Halve the viewing volume

Page 6: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Positioning the Camera

• OpenGL viewing defaults have limitations:– fixed origin and fixed projection direction

• How to obtain arbitrary camera orientations and positions? 

Positioning the Camera

• Suppose that we wish to position the camera at (0, 0, 2) w.r.t. the world. Two (equivalent) possibilities:

– Transform the world prior to creation of objects: glTranslatef(0, 0, ‐2);

– Position the camera with respect to the world: gluLookAt(0, 0, 2, … );

Page 7: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Positioning the Camera – OpenGL Code

glMatrixMode(GL_MODELVIEW)glLoadIdentity();glTranslatef(0.0, 0.0,-2.0);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0. 0.0);

Moving the objects:

Moving the camera:

Positioning the Camera – View Parameters

• A minimal view  is described in terms of:

– Camera location: position in the world ‐coordinates representing distance from the origin

– Viewing direction: which direction are we aiming the camera – a direction vector

– Camera orientation: usually defined by an up vector

DIRECTION VECTOR

{ 0, 0, 0}

UP VECTOR

Page 8: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

OpenGL gluLookAt

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

equivalent to: glTranslatef(‐eyex, ‐eyey, ‐eyez);glRotatef(α, 1.0, 0.0, 0.0);glRotatef(β, 0.0, 1.0, 0.0);

α

at

β

Understanding gluLookAt

• 2‐unit cube centered at (0,0,0)

z

x

y

gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0)

• Change the eye position to get these:

gluLookAt(___,___,___,0,0,0,0,1,0)

Page 9: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

gluLookAt Up‐vector Effects

Size 2 cube centered at the origin, viewed by:

glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-4.0,4.0, -4.0, 4.0, -4.0, 4.0);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(1,1,1,0,0,0, 0, 1, 0);

Viewed by:

gluLookAt(1,1,1,0,0,0, __, __, __);

z

x

y

Hands‐on Session

1. Run again the projection tutor

Use the menu to select orthographic projection

Double the viewing volume

Play with the parameters of gluLookAt

1. Modify the eye position

2. Modify the LookAt point (center) position

3. Modify the up vector. (Turn the image upside‐down.)

2. Study the SpecialKey function in the robotSkeleton code

How is translation implemented?

What about the zoom in /zoom out operations?

Page 10: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Perspective Projection

• We have determined how objects are placed relative to camera.

• But how are the objects projected to the image?

(Converting from 3D to 2D)

Projection

Projection Plane

Viewer

Scene

Image

• The main types of projection in computer graphics are:

• Parallel (orthographic) projection

• Perspective projection 

Page 11: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Recall: Orthographic Projection

• Rays travel parallel to the z‐axis (orthogonal to the image)• World point (xw, yw, zw) projects to image point (xw, yw, 0) 

(xw, yw, zw)

(xw, yw, 0)

Orthographic Projection Matrix

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎜⎜⎜⎜⎜

=

⎟⎟⎟⎟⎟

1________

________

________

________

1

0 w

w

w

w

w

z

y

x

y

x

Page 12: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Properties of Parallel Projections

• Parallel projections preserve parallelism (parallel lines remain parallel even after being flattened to 2D)

• Useful for tech‐drawing, computer aided design architecture, schematics etc.– This is because you can infer the original dimensions of 3D objects from their 2D images.

• Do not model what our eyes do:

The Visual Cone

Perspective Projection

Page 13: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Perspective Projection

Pietro Perugino (1481‐82)

Perspective Projection• Creates more realistic images:

– Note how parallel lines in 3D space may appear to converge to a single point when viewed in perspective

• Also called central projection:– projection lines passing through the center (eye point)

Page 14: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Perspective Projection

Projection plane

Extend lines from each point on the scene to the center of projection (camera position). Where these lines intersect with the projection plane is where we draw the object.

Center of projection

Orthographic vs. Perspective

• Object appears same size, no matter how far from the camera

• Farther objects appear smaller

• Parallel lines in the world scene are parallel lines in the image

• Parallel lines in the world scene are not generally parallel lines in the image

Page 15: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Outline:Pinhole Camera

Mathematics of PerspectiveOpenGL Implementations

Perspective Projection

Pinhole Camera Model

• Pinhole camera ‐ box with a tiny front hole and film at the back 

• Image is upside down ‐‐models what our eyes do

Page 16: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Human Eye

• Images are inverted on the retina

Reflected Light

• The colours that we perceive are determined by the nature of the light reflected from an object

• For example, if white light is shone onto a green object most wavelengths are absorbed, while green light is reflected from the object

White Light

Colours Absorbed

Green Light

Page 17: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Pinhole Camera Principle

(xw, yw, zw)

• Determine point I = (?,?,?) where projection ray intersects the image.

I = (?, ?, ?) lens

f  (focal length)

y

x

z

Mathematics of Perspective

• Determine point I = (?,?,?) where the projection ray intersects the image.

• One coordinate is easy to determine:

I = (?, ?, ___)

(xw, yw, zw)

I = (?, ?, ?)

f

lens

y

x

z

Page 18: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

• Consider the line passing through origin and parallel to vector v.

• Line through origin parallel to v is the set of all points       with: 

Mathematics Warmup:Parametric Equations for Lines

⎟⎟⎠

⎞⎜⎜⎝

⎛y

x

⎟⎟⎠

⎞⎜⎜⎝

⎛=

2

1v

x

y

⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

⎛y

x

Exercise:

• Give parametric equation for line parallel to       passing

through       : ⎟⎟⎠

⎞⎜⎜⎝

⎛1

1

⎟⎟⎠

⎞⎜⎜⎝

⎛5

0y

x

Page 19: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Parametric Equations in 3D

x

y

z

⎟⎟⎟

⎜⎜⎜

⎛=

1

1

1

v

x

y

⎟⎟⎟

⎜⎜⎜

⎛=

0

7

2

A

⎟⎟⎟

⎜⎜⎜

⎛=

4

2

2

B

• Line through A in the direction of B is:

• Line through (0,0,0) parallel to (1,1,1) is:

Back to Camera

• Determine point I = (?,?,f) where the projection ray intersects the image.

(xw, yw, zw)

I = (?, ?, f)

f

lens

y

x

z

Page 20: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Projection Ray

• Parametric equation for projection ray for world point (xw, yw, zw):

• By varying t, we can “travel” along the line.

• What value of t puts us on the image (makes z = f)?

⎟⎟⎟

⎜⎜⎜

⎛=

⎟⎟⎟

⎜⎜⎜

w

w

w

z

y

x

t

z

y

x

Projection Ray

• Now substitute for x and y:

)⎜⎝⎛=

=⋅=

=⋅=

fyz

fx

z

fI

yz

fyty

xz

fxtx

ww

ww

ww

w

ww

w

,,

Page 21: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Virtual Image in Front of Camera

• To simplify things, we form the image in front of the pinhole:

f

(xw, yw, zw)

lens

y

xz

f

)⎜⎝⎛= fy

z

fx

z

fI w

ww

w,,

Image point = (?, ?, ?)

• How are x and y coordinates affected by values of zw?

Projection Matrix

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎜⎜⎜⎜⎜⎜⎜

=

⎟⎟⎟⎟⎟⎟

1????

????

????

????

1

w

w

w

ww

ww

z

y

x

f

yz

f

xz

f

)( www zyx ,, )⎜⎝⎛ −−− fy

z

fx

z

fw

ww

w,,maps to

• What is the 4x4 transformation matrix?

Mperspective

Page 22: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Projection Matrix

⎟⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜⎜

=

01

00

0100

0010

0001

M eperspectiv

f

• Does this matrix work?

Projection Matrix

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜⎜

−⎜⎜⎜⎜⎜⎜

=

⎟⎟⎟⎟⎟⎟

− 101

00

0100

0010

0001

w

w

w

w

w

w

w

z

y

x

ff

zz

y

x

• Example:

• Or in 3D coordinates (divide by the 4th coordinate):

⎜⎜⎝

⎛⎟⎠⎞−=−−− fz

z

fy

z

fx

z

fw

ww

ww

w,,

Page 23: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Perspective in OpenGL

Perspective in OpenGL

• Specifying a perspective view can be done in many ways

• OpenGL supports two methods:

– glFrustrum and gluPerspective

Page 24: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

OpenGL glFrustumglMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(xmin, xmax, ymin, ymax, zmin, zmax);

zmin and zmax are specified as positive distances along ‐z

Why near/far clipping planes?

• Discard things too close to the camera• would block view of rest of scene

• Discard things too far away from camera• distant objects may appear too small to be visually significant,but still take long time to render

• by discarding them we lose a small amount of detail but reclaim a lot of rendering time

Page 25: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

OpenGL gluPerspective

gluPerspective(fov, aspect, near, far);

Only allows the creation of symmetric frustrums.

(field of view ∈[0…180])

2tan2

2tan

2 θθnearh

near

h=⇒=

gluPerspective Parameters

Page 26: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Zoom• Field of view: Smaller angle means more zoom

Hands‐on Session

Run again the projection tutor

Play with projection parameters 

Play with camera orientation

Switch between 

glFrustum, gluPerspective, and glOrtho

Page 27: 3D Viewing and Projection 5 - Villanova University · 2010-10-06 · 3D Viewing and Projection Reading: Chapter 5 Review: Model to World Coordinates • Describe not just the objects

Summary

• 3D Viewing – Camera Positioning– Projection 3D  2D

• Orthographic vs. Perspective Projection

• Projection Transformation Matrices

• OpenGL Viewing Functions– gluLookAt, glOrtho, glFrustrum, gluPerspective