Viewing (Review of 2D)rsnapp/teaching/cs274/lectures/viewing2015.pdf · Robert R. Snapp ' 2015...

Preview:

Citation preview

Viewing (Review of 2D)

AA

AA

A A AA

AA

A A

A

A

AA

A A A

AA

AA

A A

1

1

1024

768

Local (Model)Coordinates

WorldCoordinates

ProjectionCoordinates

Device (Screen)Coordinates

NormalizedDevice

Coordinates(NDC)

Obje

ctTr

ansf

orm

Viewing Transform Normalization

Workstation

Transform

In 3-D we must clip with respect to a 3-D window, and must apply a projectionto a 2-D device.

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 1 / 22

Viewing in 3D

AAA

A

A A

AA

AA

A A AA

AA

A A

A

A

AA

A A A

AA

AA

A A

1

1

1024

768

Local (Model)Coordinates

WorldCoordinates

ProjectionCoordinates

ViewingCoordinates

Device (Screen)Coordinates

NormalizedDevice

Coordinates(NDC)

Obje

ctTr

ansf

orm

Normalization

Workstation

Transform

Viewing Transform

Projection Transform

From World to projection coordinates entails:1 Define a 3-D window, or view volume.2 Transform to the viewing coordinates.3 Clip graphical objects with respect to the view volume (e.g., 3D versions

of Cohen-Sutherland, Liang-Barsky, Sutherland-Hodgeman).4 Apply a projection transformation to obtain the projection coordinates.

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 2 / 22

Camera Model for Projections

x

y

z

n

v

u

World Coordinates Viewing Coordinates

p

OView ReferencePoint

View Plane

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 3 / 22

Transforming from World to Viewing Coordinates

The glm.hh command,

glm::mat4 glm::lookAt(glm::vec3 const &eye, // view reference pointglm::vec3 const &center, // point on camera axisglm::vec3 const &up); // vector having vertical projection

Yields a transformation to the coordinate frame, {u,v,n} where,

n =eye − center‖eye − center‖

u =up× n‖up × n‖

v = n× u

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 4 / 22

World to Viewing Coordinate Transformation

Let p = (x, y, z) denote a point in world coordinates. To find the coordinates ofthis point in the viewing coordinate system, apply the transformation

V = R(u,v,n)T(−eye)

=

ux uy uz 0vx vy vz 0nx ny nz 00 0 0 1

1 0 0 −eyeX0 1 0 −eyeY0 0 1 −eyeZ0 0 0 1

=

ux uy uz −u · eyevx vy vz −v · eyenx ny nz −n · eye0 0 0 1

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 5 / 22

Projections

Earliest evidence of projection transformations occur in cave paintings fromthe paleolithic era. These examples from Lascaux, France are approximately15,000 years old.

Painting portraits of the three-dimensional world on a two-dimensional canvas(or rock wall) is similar to the task of representing a three dimensional modelon a two-dimensional graphics device.

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 6 / 22

Visual Art in Ancient Egypt

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 7 / 22

Visual Art in the Italian Renaissance

Mathematics of point-line perspective was pioneered during the ItalianRenaissance by Filippo Brunelleschi (1377–1446), Leono Battista Alberti(1404–1472), and Piero della Francesca (c.1420–1492).

Ideal Piazza, Piero della Francesca

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 8 / 22

Projections

ParallelI Orthographic

F Top (plan)F Front ElevationF Side ElevationF Axonometric (e.g., Isometric)

I ObliqueF CabinetF CavalierF Other

PerspectiveI One-pointI Two-pointI Three-point

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 9 / 22

Parallel ProjectionGiven a constant vector vp , construct a line (parallel to vp) through everyvertex of interest in viewing coordinates. The intersections of these lines withthe view plane define the parallel projections.

n

u

v

(u, v, n)

(u, v, 0)

(up, vp, 0)

α

φ vp

L

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 10 / 22

Parallel Projection (cont.)

n

u

v

(u, v, n)

(u, v, 0)

(up, vp, 0)

α

φ vp

L

Alternatively, the projection can be defined using angles α and φ.

up = u + L cosφvp = v + L sinφ

where, L = n/ tanα. Letting L1 = 1/ tanα, we obtain

Mparallel =

1 0 L1 cosφ 00 1 L1 sinφ 00 0 0 00 0 0 1

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 11 / 22

Special Parallel Projections

Orthographic: α = π/2 or L1 = 0. Useful for architecture and CAD:elevations and plans. Axonometric (orthographic) projections reveal morethat one side of an object. An isometric (orthographic) projection treatsdistances along each coordinate axis equally. In general, lengths andangles are accurate. Normal lengths are invisible.Cavalier: α = π/4, thus L1 = 1. Lines perpendicular to the projectedplane appear with no change in length.Cabinet: α = tan−1 2, thus L1 = 1/2. More realistic than cavalier: normallengths are half as long.

Popular values for φ are π/4 and π/6.Theorem: The parallel projections of parallel lines are parallel.

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 12 / 22

Point-Line Perspective (Phigs)Each point (in viewing coordinates) is projected along a line that contains acommon projection reference point pprp . Intersections of these lines with theview plane defines the projections.

n

u

v

(u, v, n)

(up, vp, 0)

vp = (0, 0, np)

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 13 / 22

Point-Line Perspective (cont.)An orthographic view, illustrating two similar triangles:

u

up

O np

(u,n)

n n

up

np=

unp − n

=⇒ up =u

1 − nnp

A similar equation can be obtained for vp .N.B. The expression is not linear in n. However, we can exploit the rationalnature of homogeneous coordinates to express the above as a linearoperation.Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 14 / 22

Point-Line Perspective

Let,

Mpersective =

1 0 0 00 1 0 00 0 0 00 0 − 1

np1

Then

uhvhnhhh

= Mpersective

uvnh

whence,

up =uh

hh, vp =

vh

hh, np = 0

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 15 / 22

Point-Line Perspective (OpenGL)The projection reference point vp = (0,0,0) is a the origin of the camera (uvn)coordinate system. The view plane is described by the equation n = np forsome chosen np < 0. Each model vertex (u, v,n) is projected onto theviewplane (up , vp ,np) via a line segment that connects the model vertex withthe projection reference point.

n

v

u

(u, v, n)

(up, vp, np)

vp = (0, 0, 0)

n=np

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 16 / 22

Point-Line Perspective (cont.)An orthographic view, illustrating two similar triangles:

u

n

up

Onp

(u,n)

vie

wp

lan

en

up

−np=

u−n

=⇒ up =u

n/np

A similar equation can be obtained for vp .N.B. The expression is not linear in n. However, we can exploit the rationalnature of homogeneous coordinates to express the above as a linearoperation.

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 17 / 22

Point-Line Perspective

Let,

Mpersective =

1 0 0 00 1 0 00 0 1 00 0 1

np0

Then

uhvhnhhh

= Mpersective

uvnh

whence,

up =uh

hh=

unp

n, vp =

vh

hh=

vnp

n, np =

nh

hh= np

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 18 / 22

Defining a Perspective Projection with OpenGL

glm::mat4 glm::ortho(float left, float right,float bottom, float top,float near, float far);

glm::mat4 glm::perspective(float fovy,float aspect, // (width / height)float near,float far);

glm::mat4 glm::frustum(float left, float right,float bottom, float top,float near, float far);

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 19 / 22

Clipping a Perspective Projection

A perspective transformation defines a frustum of a rectangular cone.

u

v

n

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 20 / 22

View VolumesWe seek the projection matrix,

2r − l

0 0 −r + lr − l

02

t − b0 −

t + bt − b

0 02

f − nf + nf − n

0 0 0 1

where each parameter is represented by its initial letter. Vertices of the viewvolume then transform to

(l,b,−f)→ (−1,−1,−1), (l,b,−n)→ (−1,−1,+1)(l, t ,−f)→ (−1,+1,−1), (l, t ,−n)→ (−1,+1,+1)(r ,b,−f)→ (+1,−1,−1), (r ,b,−n)→ (+1,−1,+1)(r , t ,−f)→ (+1,+1,−1), (r , t ,−f)→ (+1,+1,+1).

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 21 / 22

View Volumes: Perspective ProjectionsFor perspective transformations, we use the projection matrix

2nr − l

0 −r + lr − l

0

02n

t − b−

t + bt − b

0

0 0 −f + nf − n

−2fn

f − n

0 0 −1 0

where each parameter is represented by its initial letter. Vertices of the viewvolume then transform to,

(l,b,−f)→ (−1,−1,−1), (l,b,−n)→ (−1,−1,+1)(l, t ,−f)→ (−1,+1,−1), (l, t ,−n)→ (−1,+1,+1)(r ,b,−f)→ (+1,−1,−1), (r ,b,−n)→ (+1,−1,+1)(r , t ,−f)→ (+1,+1,−1), (r , t ,−f)→ (+1,+1,+1).

Robert R. Snapp © 2015 Viewing Transfomations CS 274: Spring 2015 22 / 22

Recommended