1 of 53

Nima Kalantari

CSCE 441 - Computer Graphics

Viewing Transformations

Some slides from Ren Ng and Ravi Ramamoorthi

2 of 53

Model Transformation

Model from Pradeep Sen

World Coordinate

y

x

z

Local Coordinate

Z

Y

X

3 of 53

Outline

  • View transformation
  • Projection transformation

4 of 53

Camera Space

  • Camera pose: origin (eye), look at (center), up

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

w

v

u

Z

Y

X

5 of 53

View Transformation

  • Construct camera coordinate frame
  • Represent vertices in camera coordinate

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

6 of 53

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

7 of 53

Coordinate Frames

  • Any set of 3 vectors (in 3D) so that

8 of 53

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

9 of 53

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

10 of 53

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

11 of 53

View Transformation

  • Construct camera coordinate frame
  • Represent vertices in camera coordinate

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

12 of 53

Coordinate Change: 2D Example

World

13 of 53

Coordinate transformation

world

camera

camera to world

world to camera

14 of 53

Camera Control – Assignment 2

  • Turntable camera
    • Eye is floating around the surface of the sphere
    • Camera is looking at the center of the sphere

center

eye

15 of 53

Camera Control – Assignment 2

  • 3 operations
    • Scroll: zoom in and out
    • Right click: translate
    • Left click: rotate

center

eye

16 of 53

Camera Control – Assignment 2

  • Zoom – change length of a
  • Translate – look at and eye
    • Y along the local up vector
    • X along the right vector
  • Rotate – change direction of a & v
    • rotation along the right
      • first
    • rotation along the global up
      • second
  • glm::lookat(eye, center, v)

center

eye

local up (v)

a

right (u)

global up (up)

17 of 53

Outline

  • View transformation
  • Projection transformation

18 of 53

Motivation

  • So far transformations in 3D
  • Goal: make a 2D picture
  • Projection: transformation from 3D to NDC

x

z

y

+1

-1

+1

-1

-1

+1

Normalized Device

Coordinate (NDC)

x

z

y

Camera coordinate

19 of 53

Projection Transformation

  • Orthographic projection
  • Perspective projection

20 of 53

Orthographic Projection

  • Basic idea: simply project onto xy plane, drop z coordinate

21 of 53

22 of 53

Orthographic Projection

  • Parallel lines remain parallel
  • Application in technical drawing

23 of 53

Orthographic Projection

  • Basic idea: simply project onto xy plane, drop z coordinate

left

right

top

buttom

24 of 53

Orthographic Projections

25 of 53

In general

  • We have a cuboid that we want to map to the canonical cube from [-1, +1] in all axes
  • First center cuboid by translating
  • Then scale into unit cube

x

z

y

l

r

t

b

n

f

x

z

Translate

y

x

z

y

Scale

+1

-1

+1

-1

-1

+1

Normalized Device

Coordinates (NDC)

26 of 53

Transformation Matrix

Scale

Translation (centering)

27 of 53

Caveats

  • Looking down –z, f and n are negative (n > f)
  • OpenGL convention: positive n, f, negate internally

x

z

y

l

r

t

b

n

f

x

z

Translate

y

x

z

y

Scale

+1

-1

+1

-1

-1

+1

Normalized Device

Coordinates (NDC)

28 of 53

Final Result

29 of 53

Final Result

  • glm::ortho(left, right, bottom, top, near, far)

30 of 53

Projection Transformation

  • Orthographic projection
  • Perspective projection

31 of 53

Perspective in Photographs

32 of 53

Pinhole Camera

33 of 53

Perspective view volume to NDC

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

x

z

y

+1

-1

+1

-1

-1

+1

Normalized Device

Coordinate (NDC)

34 of 53

Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

35 of 53

Specifying Perspective Projection

Field of view

(fovy)

width

height

Aspect ratio = width / height

36 of 53

Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

37 of 53

Summary

  • Fovy: vertical angular field of view
  • Aspect ratio: width / height
  • Near: depth of near clipping plane
  • Far: depth of far clipping plane

38 of 53

Perspective view volume to NDC

  • Project all the points to view plane

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

39 of 53

Side View of Our Screen

40 of 53

In Matrices

  • Last row affected (no longer 0 0 0 1)
  • w coord is no longer = 1
  • Must divide at the end by w (perspective division)

41 of 53

Perspective view volume to NDC

  • Project all the points to a normalized plane

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

+1

-1

-1

+1

42 of 53

Side View of Our Screen

fovy

+1

-1

43 of 53

In Matrices

44 of 53

Taking aspect into account

  • If height = 2, width = 2 * aspect

Field of view

(fovy)

width

height

45 of 53

Perspective view volume to NDC

  • Project all the points to a normalized plane

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

+1

-1

-1

+1

+1

-aspect

-1

+aspect

46 of 53

In Matrices

  • Aspect ratio taken into account
  • Must map z values based on near and far planes

47 of 53

Perspective Projection Transform

Normalized Device Coordinates

(1, 1, 1)

(1, 1, –1)

Near Plane

Far Plane

Camera Coordinates

48 of 53

In Matrices

  • Homogeneous, simpler to multiply through by d

  • Third row responsible for z
  • x and y should not contribute to z

49 of 53

In Matrices

  • Homogeneous, simpler to multiply through by d

  • Third row responsible for z
  • x and y should not contribute to z
  • A and B selected to map n and f to -1, +1 respectively

50 of 53

In Matrices

  • Homogeneous, simpler to multiply through by d

  • Third row responsible for z
  • x and y should not contribute to z
  • A and B selected to map n and f to -1, +1 respectively

51 of 53

Z mapping derivation

  • Two equations to equate z = -n with -1 and z = -f with +1

52 of 53

Perspective Matrix

53 of 53

Perspective Matrix

  • glm::perspective(fovy, aspect, near, far)