65
Viewing and Projections Dr. Amy Zhang

Viewing and Projections Dr. Amy Zhang. Reading 2 Hill, Chapter 5 and 7 Red Book, Chapter 3, “Viewing”

Embed Size (px)

Citation preview

Page 1: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Viewing and Projections

Dr. Amy Zhang

Page 2: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Reading

2

Hill, Chapter 5 and 7 Red Book, Chapter 3, “Viewing”

Page 3: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

3D Graphics Pipeline

3

The big picture…

Page 4: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Outline

4

Camera Models Viewing Transformation Projection Matrix OpenGL Transformation Pipeline

Page 5: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Cameras

5

Cameras have an optical system: Filters Lenses Aperture

The projection surface may be flat or curved, oriented at various angles with respect to the incoming light.

Examples: A camera or the eye.

Page 6: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

6

Page 7: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Camera Obscura

7

The first cameras – a dark box with a small hole in it

Page 8: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The Pinhole Camera

8

An abstract camera model Models the geometry of perspective projection Used in most of computer graphics

Page 9: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Pinhole Optics

9

Page 10: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective

10

Page 11: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective Derivation

11

Consider the projection of a point onto the projection plane:

Page 12: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

12

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

Looking down y axis:

Page 13: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

13

We get:

This is clearly a non-linear transformation BUT: We can split it into a linear part followed

by a nonlinear part

Page 14: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Homogeneous Coordinates

14

Remember homogeneous coordinates:

To get a homogeneous point we divide all the coordinates by w:

This is called the perspective divide

Page 15: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective Projections

15

We can now rewrite the perspective projection as a linear transformation:

After division by the 4th component we get:

Page 16: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The Reason for Lenses *

16

Page 17: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The Lens Model

17

Lens, aperture, and image plane

Page 18: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

18

Focal length f: the distance from lens to image plane

A point in focus: the image of a point is on the image plane

Page 19: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

19

An out-of-focus point The circle of confusion r

Page 20: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

20

The Gaussian / thin lens formula:

Page 21: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

21

A near point in out-of-focus

Page 22: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

22

The depth of focus dfocus and the depth of field (DOF) dfield

Page 23: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

23

Decreasing the aperture size reduces the size of the blur for points not in the focused plane, so that the blurring is imperceptible, and all points are within the dfield.

Page 24: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

24

Page 25: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Viewing and Projection

25

In OpenGL we distinguish between: Viewing: placing the camera Projection: describing the viewing frustum of the

camera (and thereby the projection transformation)

Perspective divide: computing homogeneous points

Page 26: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Outline

26

Camera Models Viewing Transformation Projection Matrix OpenGL Transformation Pipeline

Page 27: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

OpenGL Transformations

27

The viewing transformation V transforms a point from world space to eye space:

Page 28: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Placing the Camera

28

It is most natural to position the camera in world space as if it were a real camera

Identify the eye point where the camera is located

Identify the look-at point that we wish to appear in the center of our view

Identify an up-vector vector that we wish to be oriented upwards in our final image

Page 29: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Look-At Positioning

29

We specify the view frame using the look-at vector a and the camera up vector up

The vector a points in the negative viewing direction

In 3D, we need a third vector that is perpendicular to both up and a to specify the view frame

Page 30: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Where does it point to?

30

The result of the cross product is a vector, not a scalar, as for the dot product

Depending on the basis vectors i, j, and k, the new vector follows the right or left handed rule

In OpenGL, the cross product a x b yields a right hand side (RHS) vector perpendicular to a and b

Page 31: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Computing Cross Products

31

We can compute the cross product using yet another matrix-vector multiplication:

The matrix is sometimes called the skew-symmetric matrix of the vector (in this case a)

Cross products produce vectors for both vector and point inputs

Page 32: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Constructing a Frame

32

The cross product between the up and the look-at vector will get a vector that points to the right.

Finally, using the vector a and the vector r we can synthesize a new vector u in the up direction:

Page 33: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

World and Camera Frames

33

The relation between the world and the camera is expressed as:

We move the eye (camera) by updating E

Page 34: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Rotation

34

Rotation first:

Page 35: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Translation

35

Translation to the eye point:

Page 36: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Composing the Result

36

The final camera transformation is:

Why?

Page 37: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The Viewing Transformation

37

Expressing P in eye coordinates:

Page 38: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The Viewing Transformation

38

As a single 4x4 matrix:

Where these are normalized vectors:

Page 39: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

gluLookAt()

39

OpenGL provides a very helpful utility function that implements the look‐at viewing specification:

These parameters are expressed in world coordinates

Page 40: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Outline

40

Camera Models Viewing Transformation Projection Matrix OpenGL Transformation Pipeline

Page 41: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

OpenGL Transformations

41

The projection transformation P transforms a point from eye space to clip space:

Page 42: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Projection Transformations

42

Projections fall into two categories: Parallel projections: The camera is placed at an

infinite distance from the viewplane; lines of projection are parallel to each other

Perspective projections: Lines of projection converge at a point

Page 43: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Parallel Projections

43

The simplest form of parallel projection is simply along lines parallel to the z-axis onto the xy-plane

This form of projection is called orthographic

For other parallel projections see, e.g.:http://www.mtsu.edu/~csjudy/planeview3D/tutorialparallel.html

Page 44: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Orthographic Frustum

44

The user specifies the orthographic viewing frustum by specifying minimum and maximum x/y coordinates

It is necessary to indicate a range of distances along the z-axis by specifying near and far planes

Page 45: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Orthographic Projections to NDC

45

Normalized Device Coordinate (NDC) makes up a coordinate system that describes positions on a virtual plotting device

Here is the orthographic world-to-clip transformation: Move center to origin

T(-(left+right)/2, -(bottom+top)/2,(near+far)/2)) Scale to have sides of length 2

S(2/(right-left),2/(top-bottom),2/(far-near))

1000

200

02

0

002

nearfar

nearfar

nearfar

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

P = ST =

Page 46: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Orthographic Projection in OpenGL

46

This matrix is constructed with the following OpenGL call:

And the 2D version (another GL utility function):

Just a call to glOrtho() with near = -1 and far = +1

Page 47: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Properties of Parallel Projections

47

Not realistic looking Good for exact measurements A kind of affine transformation

Parallel lines remain parallel Ratios are preserved Angles (in general) not preserved

Most often used in CAD, architectural drawings, etc., where taking exact measurement is important

Page 48: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Isometric Games

48

A special kind of parallel projection called isometric projection is often used in games

It’s essentially a shear and an orthographic projection

Easier to compute than a full perspective transformation

SimCity

The Sims

Diablo

Page 49: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective Projections

49

Artists (Donatello, Brunelleschi, and Da Vinci) during the renaissance discovered the importance of perspective for making images appear realistic

Parallel lines intersect at a point

Page 50: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective Viewing Frustum

50

Just as in the orthographic case, we specify a perspective viewing frustum

Values for left, right, top, and bottom are specified at the near depth.

Page 51: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Perspective Viewing Frustum

51

OpenGL provides a function to set up this perspective transformation:

There is also a simpler OpenGL utility function:

fov = vertical field of view in degrees aspect = image width / height at near depth

Can only specify symmetric viewing frustums where the viewing window is centered around the –z axis.

Page 52: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

OpenGL Clip Space

52

In OpenGL clip space, the viewing frustum is mapped to a cube that extends from -1 to 1 in x, y, and z.

OpenGL also flips the z axis to create a left handed coordinate system during projection

Page 53: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

OpenGL Perspective Matrix

53

Mapping the perspective viewing frustum in OpenGL to clip space involves some affine transformations

OpenGL uses a clever composition of these transformations with the perspective projection matrix:

Page 54: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

What happens to the z value?

54

We get the following expression for z from our perspective matrix:

This mapping is non‐linear:

Page 55: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

55

Uniform differences in 3D (z) depth values do not correspond to uniform differences in z’ values:

The number of discernable depths is greater near the near plane than the far plane Tip: Choose the near plane as far away as

possible

Page 56: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Properties of Perspective Projections

56

The perspective projection is an example of a projective transformation

Here are some properties of projective transformations: Lines map to lines Parallel lines do not necessarily remain parallel Ratios are not preserved

One of the advantages of perspective projection is that size varies inversely with distance – looks realistic

A disadvantage is that we can't judge distances as exactly as we can with parallel projections

Page 57: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Outline

57

Camera Models Viewing Transformation Projection Matrix OpenGL Transformation Pipeline

Page 58: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

OpenGL Transformations

58

The rest of the OpenGL transformation pipeline:

3D:

2D:

Page 59: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Clipping & Perspective Division

59

The scene's objects are clipped against the clip space bounding box

This step eliminates any objects (and pieces of objects) that are not visible in the image

Hill describes an efficient clipping algorithm for homogeneous clip space

Perspective division divides all homogeneous coordinates through w

Clip space becomes Normalized Device Coordinate (NDC) space after the perspective division

Page 60: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Viewport Transformation

60

One last transformation from NDC space to the viewport on the screen

The viewport transformation also maps NDC depth values from the range [-1,1] to [0, 1]

The resulting screen space is still 3D with depth values between 0 and 1

Page 61: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Viewport Transformation

61

OpenGL provides a function to set up the viewport transformation:

Page 62: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

Screen Coordinate Systems

62

Page 63: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

3D Graphics Pipeline

63

The rasterization step scan converts the object into pixels

Page 64: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

3D Graphics Pipeline

64

A z-buffer depth test resolves visibility of the objects on a per-pixel basis and writes the pixels to the frame buffer

Page 65: Viewing and Projections Dr. Amy Zhang. Reading 2  Hill, Chapter 5 and 7  Red Book, Chapter 3, “Viewing”

The end

65

Questions and answers