Download - The Projection Matrix

Transcript
Page 1: The Projection Matrix

The Projection Matrix

Lecture 29Mon, Nov 5, 2007

Page 2: The Projection Matrix

The Graphics Pipeline

ObjectCoords

ModelTrans World

Coords

ViewTrans Eye

Coords

LightingCalculations

M V

ProjTrans Clip

Coords

Clipping

P

PerspectiveDivideNormal Dev

CoordsWindowCoords

Frame-buffer

Rasterization,Shading

Hidden-surfaceRemoval,

Texture Application

Page 3: The Projection Matrix

Homogeneous Coordinates

Points are stored in homogeneous coordinates (x, y, z, w).The true 3D coordinates are (x/w, y/w, z/w).Therefore, for example, the points (4, 3, 2, 1) and (8, 6, 4, 2) represent the same 3D point (4, 3, 2).This fact will play a crucial role in the projection matrix.

Page 4: The Projection Matrix

Coordinate Systems

Eye coordinates The camera is at the origin, looking

in the negative z-direction. View frustrum (right – left, bottom –

top, near – far).

Normalized device coordinates -1 x 1 -1 y 1 -1 z 1

Page 5: The Projection Matrix

The Transformation

Points in eye coordinates must be transformed into normalized device coordinates.But first they are transformed to clipping coordinates.

Page 6: The Projection Matrix

The Transformation

For example, The point (r, t, -n, 1) is first

transformed to the point (n, n, -n, n). The point (l(f/n), b(f/n), -f, 1) is first

transformed to the point (-f, -f, f, f).

Page 7: The Projection Matrix

The Transformation

Later, OpenGL divides by the w-coordinate to get normalized device coordinates. (n, n, -n, n) (1, 1, -1) (-f, -f, f, f) (-1, -1, 1)

This is called the homogeneous divide, or perspective division.It is a nonlinear transformation.It occurs at a later stage in the pipeline.

Page 8: The Projection Matrix

The Perspective Transformation – x, y

Points in eye coordinates are projected onto the near plane, sort of. The x- and y-coordinates are

projected onto the near plane. The z-coordinated is scaled to the

range [-n, f].

Page 9: The Projection Matrix

The Perspective Transformation – x, y

Consider the projection in the yz-plane.

-n -f

t

b

P(x, y, z)

near plane

Page 10: The Projection Matrix

The Perspective Transformation – x, y

Consider the projection in the yz-plane.

-f

t

b

P(x, y, z)

y

near plane

-z

Page 11: The Projection Matrix

The Perspective Transformation – x, y

Consider the projection in the yz-plane.

-f

t

b

P(x, y, z)

y

near plane

n

y

-z

Page 12: The Projection Matrix

The Perspective Transformation – x, y

By similar triangles, y/n = y/(-z),Therefore,

Similarly,

z

nyy

z

nxx

Page 13: The Projection Matrix

The Perspective Transformation – x, y

Notice that these transformation are not linear.That is, x and y are not linear combinations of x, y, and z.Therefore, this transformation cannot be carried out by matrix multiplication.We will perform the multiplication by n using a matrix, but dividing by -z, i.e., w, will have to be postponed.

Page 14: The Projection Matrix

The Perspective Transformation – z

The z-coordinate is handled differently because we must not lose the depth information yet.In the z-direction, we want to map the interval [-n, -f] to the interval [-n, f].Later, the perspective division will map [-n, f] into [-1, 1].

Page 15: The Projection Matrix

The Perspective Transformation – z

Since this is a linear transformation, we will have z = az + b, for some a and b, to be determined.We need

Therefore,

fbfa

nbna

)(

)(

nf

fnb

nf

nfa

2

Page 16: The Projection Matrix

The Perspective Transformation

That is, we will map (r, t, -n, 1) (nr, nt, -n, n), (l(f/n), b(f/n), -f, 1) (fl, fb, f,

f), etc. which is a linear transformation.These points are equivalent to (r, t, -1), (l, b, 1), etc.

Page 17: The Projection Matrix

The Perspective Transformation

The perspective matrix is

0100

200

000

000

1

nf

fn

nf

nfn

n

P

Representsdivision by –z(perspective division)

Page 18: The Projection Matrix

The Perspective Transformation

This transforms this frustum…

-n -f

t

b

P(x, y, z)

Page 19: The Projection Matrix

The Perspective Transformation

…into this frustum

-n f

nt

nb

P(x, y, z)

ft

fb

Page 20: The Projection Matrix

The Perspective Transformation

Later, the perspective division will transform this into a cube.

1

P(x, y, z)

-1

-1

1

Page 21: The Projection Matrix

The Perspective Transformation

Verify that P1 maps (r, t, -n, 1) (nr, nt, -n, n) (r, t, -1) (l, t, -n, 1) (nl, nt, -n, n) (l, t, -1) (r, b, -n, 1) (nr, nb, -n, n) (r, b, -1) (l, b, -n, 1) (nl, nb, -n, n) (l, b, -1) (r(f/n), t(f/n), -f, 1) (fr, ft, -f, f) (r, t, -1) (l(f/n), t(f/n), -f, 1) (fl, ft, -f, f) (l, t, -1) (r(f/n), b(f/n), -f, 1) (fr, fb, -f, f) (r, b, -1) (l(f/n), b(f/n), -f, 1) (fl, fb, -f, f) (l, b, -1)

Page 22: The Projection Matrix

The Projection Transformation

The second part of the projection maps

(nr, nt, -n, n) (n, n, -n, n) (1, 1, -1) (nl, nt, -n, n) (-n, n, -n, n) (-1, 1, -1) (nr, nb, -n, n) (n, -n, -n, n) (1, -1, -1) (nl, nb, -n, n) (-n, -n, -n, n) (-1, -1, -1) (fr, ft, -f, f) (f, f, f, f) (1, 1, 1) (fl, ft, -f, f) (-f, f, f, f) (-1, 1, 1) (fr, fb, -f, f) (f, -f, f, f) (1, -1, 1) (fl, fb, -f, f) (-f, -f, f, f) (-1, -1, 1)

Page 23: The Projection Matrix

The Projection Transformation

The matrix of this transformation is

1000

0100

02

0

002

2 bt

bt

bt

lr

lr

lr

P

Page 24: The Projection Matrix

The Projection Matrix

The product of the two transformations is the projection matrix.

0100

200

02

0

002

12

nf

fn

nf

nfbt

bt

bt

nlr

lr

lr

n

PPP

Page 25: The Projection Matrix

The Projection Matrix

The function glFrustum(l, r, b, t, n, f) creates this matrix and multiplies the projection matrix by it.

glMatrixMode(GL_PROJECTION);glLoadIdentity();glFrustum(l, r, b, t, n, f);

Page 26: The Projection Matrix

The Projection Matrix

The function gluPerspective(angle, ratio, near,

far)also creates the projection matrix by calculating r, l, t, and b.

glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(angle, ratio, n, f);

Page 27: The Projection Matrix

The Projection Matrix

The formulas are t = n tan(angle/2) b = -t r = t ratio l = -r

Page 28: The Projection Matrix

Question

When choosing the near and far planes in the gluPerspective() call, why not let n be very small, say 0.000001, and let f be very large, say 1000000.0?

Page 29: The Projection Matrix

Orthogonal Projections

The matrix for an orthogonal projection is much simpler.All it does is rescale the x-, y-, and z-coordinates to [-1, 1]. The positive direction of z is reversed.

It represents a linear transformation; the w-coordinate remains 1.

Page 30: The Projection Matrix

Orthogonal Projections

The matrix of an orthogonal projection is

1000

200

02

0

002

nf

nf

nf

bt

bt

bt

lr

lr

lr

P