3.1 Viewing and Projection Hao Li http://cs420.hao-li.com 1 - - PowerPoint PPT Presentation

3 1 viewing and projection
SMART_READER_LITE
LIVE PREVIEW

3.1 Viewing and Projection Hao Li http://cs420.hao-li.com 1 - - PowerPoint PPT Presentation

Fall 2014 CSCI 420: Computer Graphics 3.1 Viewing and Projection Hao Li http://cs420.hao-li.com 1 Recall: Affine Transformations [ x y z ] > Given a point [ x y z 1] > form homogeneous coordinates [ x 0 y 0 z 0 ] > The


slide-1
SLIDE 1

CSCI 420: Computer Graphics

Hao Li

http://cs420.hao-li.com

Fall 2014

3.1 Viewing and Projection

1

slide-2
SLIDE 2

Recall: Affine Transformations

  • Given a point
  • form homogeneous coordinates
  • The transformed point is

[x y z]> [x y z 1]> [x0 y0 z0]>

2

slide-3
SLIDE 3

Transformation Matrices in OpenGL

  • Transformation matrices in OpenGL are vectors
  • f 16 values (column-major matrices)
  • In glLoadMatrixf(GLfloat *m);
  • Some books transpose all matrices!

represents

3

m> = [m1, m2, . . . , m16]>

slide-4
SLIDE 4

Shear Transformations

  • x-shear scales proportional to
  • Leaves and values fixed

4

x y y z

slide-5
SLIDE 5

Specification via Shear Angle

= shear angle

5

cot(θ) = (x0 − x)/y x0 = x + y cot(θ) y0 = y z0 = z θ [x, y] [x0, y0] x0 − x x y y θ θ

slide-6
SLIDE 6

Specification via Ratios

  • For example, shear in both and direction
  • Leave fixed
  • Slope for -shear, for -shear
  • Solve
  • Yields

6

y x z α γ x z

slide-7
SLIDE 7

Composing Transformations

  • Let , and
  • Then


 matrix multiplication

7

p = Aq q = Bs p = (AB)s AB A B s q p

slide-8
SLIDE 8

Composing Transformations

  • Every affine transformation is a composition of

rotations, scalings, and translations

  • So, how do we compose these to 


form an x-shear?

  • Exercise!

8

slide-9
SLIDE 9

Outline

  • Shear Transformation
  • Camera Positioning
  • Simple Parallel Projections
  • Simple Perspective Projections

9

slide-10
SLIDE 10

Transform Camera = Transform Scene

  • Camera position is identified with a frame
  • Either move and rotate the objects
  • Or move and rotate the camera
  • Initially, camera at origin, pointing in 


negative z-direction

10

slide-11
SLIDE 11

The Look-At Function

  • Convenient way to position camera
  • gluLookAt(ex, ey, ez, fx, fy, fz, ux, uy, uz);
  • e = eye point
  • f = focus point
  • u = up vector

u e u e f f view plane

11

slide-12
SLIDE 12

OpenGL code

  • void display()

{ glClear (GL_COLOR_BUFFER_BIT |
 GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_MODELVIEW); glLoadIdentity();

  • gluLookAt (ex, ey, ez, fx, fy, fz, ux, uy, uz);
  • glTranslatef(x, y, z);

... renderBunny();

  • glutSwapBuffers();

}

12

slide-13
SLIDE 13

Implementing the Look-At Function

  • 1. Transform world frame to camera frame
  • Compose a rotation with translation
  • 2. Invert to obtain viewing transformation
  • Derive , then , then

13

R T W = TR W V V = W−1 = (TR)−1 = R−1T−1 R T R−1T−1

slide-14
SLIDE 14

World Frame to Camera Frame I

  • Camera points in negative direction
  • is unit normal to view plane
  • Therefore, maps to

view plane

14

z n = (f e)/kf ek R [0 0 − 1]> [nx ny nz]> e n u f

slide-15
SLIDE 15

World Frame to Camera Frame II

  • maps to projection of u onto view plane
  • This projection equals:
  • view plane

15

e n u f α v0 R [0 1 0]> α = u>n/knk = u>n v0 = u − αn v = v0/kv0k v

slide-16
SLIDE 16

World Frame to Camera Frame III

  • Set to be orthogonal to and ,
  • ,
  • is right-handed

view plane

16

e n f v w w v n w = n × v [w v − n]>

slide-17
SLIDE 17

Summary of Rotation

  • gluLookAt(ex, ey, ez, fx, fy, fz, ux, uy, uz);
  • ,
  • ,
  • .
  • Rotation must map:
  • to
  • to
  • to

17

n = (f e)/kf ek v = (u (u>n)n)/ku (u>n)nk w = n × v [1 0 0] [0 1 0] [0 0 − 1] w v n

slide-18
SLIDE 18

World Frame to Camera Frame IV

  • Translation of origin to

18

e> = [ex ey ez 1]>

slide-19
SLIDE 19

Camera Frame to Rendering Frame

  • ,
  • is rotation, so
  • is translation, so negates displacement

19

V = W−1 = (TR)−1 = R−1T−1 R R1 = R> T T−1

slide-20
SLIDE 20

Putting it Together

  • Calculate
  • This is different from book [Angel, Ch. 5.3.2]
  • There, are right-handed (here: )

20

V = R−1T−1 u, v, n u, v, −n

slide-21
SLIDE 21

Other Viewing Functions

  • Roll (about z), pitch (about x), yaw (about y)
  • Assignment 2 poses a related problem

21

slide-22
SLIDE 22

Outline

  • Shear Transformation
  • Camera Positioning
  • Simple Parallel Projections
  • Simple Perspective Projections

22

slide-23
SLIDE 23

Projection Matrices

  • Recall geometric pipeline
  • Projection takes 3D to 2D
  • Projections are not invertible
  • Projections are described by a 4x4 matrix
  • Homogenous coordinates crucial
  • Parallel and perspective projections

23

slide-24
SLIDE 24

Parallel Projection

  • Project 3D object to 2D via parallel lines
  • The lines are not necessarily orthogonal


to projection plane

source:Wikipedia

24

slide-25
SLIDE 25

Parallel Projection

  • Problem: objects far away do not appear smaller
  • Can lead to “impossible objects” :

Penrose stairs

source:Wikipedia

25

slide-26
SLIDE 26

Orthographic Projection

  • A special kind of parallel projection:


projectors perpendicular to projection plane

  • Simple, but not realistic
  • Used in blueprints (multiview projections)

26

slide-27
SLIDE 27

Orthographic Projection Matrix

  • Project onto
  • , ,
  • In homogenous coordinates

27

z = 0 xp = x yp = y zp = 0

slide-28
SLIDE 28

Perspective

  • Perspective characterized by foreshortening
  • More distant objects appear smaller
  • Parallel lines appear to converge
  • Rudimentary perspective in cave drawings:

Lascaux, France


source: Wikipedia

28

slide-29
SLIDE 29

Discovery of Perspective

  • Foundation in geometry (Euclid)

Mural from Pompeii, Italy

29

slide-30
SLIDE 30

Middle Ages

  • Art in the service of religion
  • Perspective abandoned or forgotten

Ottonian manuscript,

  • ca. 1000

30

slide-31
SLIDE 31

Renaissance

  • Rediscovery, systematic study of perspective

Filippo Brunelleschi Florence, 1415

31

slide-32
SLIDE 32

Projection (Viewing) in OpenGL

  • Remember: camera is pointing in the

negative z direction

32

slide-33
SLIDE 33

Orthographic Viewing in OpenGL

  • glOrtho(xmin, xmax, ymin, ymax, near, far)

33

zmin = near, zmax = far

slide-34
SLIDE 34

Perspective Viewing in OpenGL

  • Two interfaces: glFrustum and gluPerspective
  • glFrustum(xmin, xmax, ymin, ymax, near, far);

34

zmin = near, zmax = far

slide-35
SLIDE 35

Field of View Interface

  • gluPerspective(fovy, aspectRatio, near, far);
  • and as before
  • aspectRatio =
  • Fovy specifies field 

  • f view as 


height ( ) angle

35

w/h y near far

slide-36
SLIDE 36

OpenGL code

  • void reshape(int x, int y)

{ glViewport(0, 0, x, y);

  • glMatrixMode(GL_PROJECTION);

glLoadIdentity();

  • gluPerspective(60.0, 1.0 * x / y, 0.01, 10.0);
  • glMatrixMode(GL_MODELVIEW);

}

36

slide-37
SLIDE 37

Perspective Viewing Mathematically

  • = focal length
  • so
  • Note that is non-linear in the depth !

37

d y/z = yp/d yp = y/(z/d) = yd/z yp z

slide-38
SLIDE 38

Exploiting the 4th Dimension

Perspective projection is not affine:

  • Idea: exploit homogeneous coordinates
  • has no solution for

for arbitrary

38

M w 6= 0

slide-39
SLIDE 39

Perspective Projection Matrix

  • Use multiple of point
  • Solve

with

39

slide-40
SLIDE 40

Projection Algorithm

  • Input: 3D point to project
  • Form
  • Multiply with ; obtaining
  • Perform perspective division:


, ,

  • Output:
  • (last coordinate will be )

40

[x y z]> [x y z 1]> [x y z 1]> [X Y Z W]> M X/W Y/W Z/W [X/W, Y/W, Z/W]> d

slide-41
SLIDE 41

Perspective Division

  • Normalize to
  • Perform perspective division after projection
  • Projection in OpenGL is more complex


(includes clipping)

41

[X Y Z W]> [X/W, Y/W, Z/W, 1]>

slide-42
SLIDE 42

http://cs420.hao-li.com

Thanks!

42