1 of 52

Nima Kalantari

CSCE 441 - Computer Graphics

​

Viewing Transformations

Some slides from Ren Ng and Ravi Ramamoorthi

2 of 52

Model Transformation

Model from Pradeep Sen

World Coordinate

y

x

z

Local Coordinate

Z

Y

X

3 of 52

Outline

  • View transformation
  • Projection transformation

4 of 52

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 52

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 52

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

7 of 52

Coordinate Frames

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

​

8 of 52

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

9 of 52

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

10 of 52

Construct Camera Coordinate Frame

Model from Pradeep Sen

Camera

Camera Coordinate

World Coordinate

Z

Y

X

w

v

u

up

a

11 of 52

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 52

Coordinate Change: 2D Example

​

​

​

​

World

13 of 52

Coordinate transformation

world

camera

camera to world

world to camera

14 of 52

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 52

Camera Control – Assignment 2

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

center

eye

16 of 52

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 52

Outline

  • View transformation
  • Projection transformation

18 of 52

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 52

Projection Transformation

  • Orthographic projection
  • Perspective projection

20 of 52

Orthographic Projection

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

21 of 52

Orthographic Projection

  • Parallel lines remain parallel
  • Application in technical drawing

22 of 52

Orthographic Projection

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

left

right

top

buttom

23 of 52

Orthographic Projections

24 of 52

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)

25 of 52

Transformation Matrix

​

​

​

​

Scale

Translation (centering)

26 of 52

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)

27 of 52

Final Result

​

​

​

​

​

​

​

​

28 of 52

Final Result

​

​

​

​

​

​

​

​

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

​

29 of 52

Projection Transformation

  • Orthographic projection
  • Perspective projection

30 of 52

Perspective in Photographs

31 of 52

Pinhole Camera

32 of 52

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)

33 of 52

Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

34 of 52

Specifying Perspective Projection

Field of view

(fovy)

width

height

Aspect ratio = width / height

35 of 52

Specifying Perspective Projection

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

36 of 52

Summary

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

​

​

​

37 of 52

Perspective view volume to NDC

  • Project all the points to view plane

From Angel and Shreiner, Interactive Computer Graphics

Near

Far

38 of 52

Side View of Our Screen

39 of 52

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)

​

​

40 of 52

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

41 of 52

Side View of Our Screen

fovy

+1

-1

42 of 52

In Matrices

​

​

​

​

​

43 of 52

Taking aspect into account

  • If height = 2, width = 2 * aspect

Field of view

(fovy)

width

height

44 of 52

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

45 of 52

In Matrices

​

​

​

​

​

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

46 of 52

Perspective Projection Transform

Normalized Device Coordinates

(1, 1, 1)

(1, 1, –1)

Near Plane

Far Plane

Camera Coordinates

47 of 52

In Matrices

  • Homogeneous, simpler to multiply through by d

​

​

​

​

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

48 of 52

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

49 of 52

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 52

Z mapping derivation

​

​

​

​

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

​

​

​

51 of 52

Perspective Matrix

52 of 52

Perspective Matrix

​

​

​

​

​

​

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