CSCI 420: Computer Graphics Hao Li http://cs420.hao-li.com Fall 2018 3.1 Viewing and Projection 1
CSCI 420: Computer Graphics
Hao Lihttp://cs420.hao-li.com
Fall 2018
3.1 Viewing and Projection
1
Recall: Affine Transformations
• Given a point
• form homogeneous coordinates
• The transformed point is
[x y z]>
[x y z 1]>
[x0 y0 z0]>
2
Transformation Matrices in OpenGL
• Transformation matrices in OpenGL are vectors of 16 values (column-major matrices)
• In glLoadMatrixf(GLfloat *m);
• Some books transpose all matrices!
represents
3
m> = [m1,m2, . . . ,m16]>
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
✓
✓
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
Composing Transformations
• Let , and
• Then
matrix multiplication
7
p = Aq q = Bs
p = (AB)s
AB
AB
s q p
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
Outline
• Shear Transformation
• Camera Positioning
• Simple Parallel Projections
• Simple Perspective Projections
9
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
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
ue
f
f
view plane
11
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
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 VV = W�1 = (TR)�1 = R�1T�1
R T R�1T�1
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
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
World Frame to Camera Frame III
• Set to be orthogonal to and ,
• ,
• is right-handed
view plane
16
e n f
v
w
w vn
w = n⇥ v
[w v � n]>
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]
wv
n
Camera Frame to Rendering Frame
• ,
• is rotation, so
• is translation, so negates displacement
19
V = W�1 = (TR)�1 = R�1T�1
R R�1 = R>
T T�1
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
Other Viewing Functions
• Roll (about z), pitch (about x), yaw (about y)
• Assignment 2 poses a related problem
21
Outline
• Shear Transformation
• Camera Positioning
• Simple Parallel Projections
• Simple Perspective Projections
22
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
Parallel Projection
• Project 3D object to 2D via parallel lines
• The lines are not necessarily orthogonalto projection plane
source:Wikipedia
24
Parallel Projection
• Problem: objects far away do not appear smaller
• Can lead to “impossible objects” :
Penrose stairs source:Wikipedia
25
Orthographic Projection
• A special kind of parallel projection: projectors perpendicular to projection plane
• Simple, but not realistic
• Used in blueprints (multiview projections)
26
Orthographic Projection Matrix
• Project onto
• , ,
• In homogenous coordinates
27
z = 0
xp = x yp = y zp = 0
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
Middle Ages
• Art in the service of religion
• Perspective abandoned or forgotten
Ottonian manuscript, ca. 1000
30
Orthographic Viewing in OpenGL
• glOrtho(xmin, xmax, ymin, ymax, near, far)
33
zmin = near, zmax = far
Perspective Viewing in OpenGL
• Two interfaces: glFrustum and gluPerspective
• glFrustum(xmin, xmax, ymin, ymax, near, far);
34
zmin = near, zmax = far
Field of View Interface
• gluPerspective(fovy, aspectRatio, near, far);
• and as before
• aspectRatio =
• Fovy specifies field of view as height ( ) angle
35
w/h
y
near far
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
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
Exploiting the 4th Dimension
Perspective projection is not affine:
Idea: exploit homogeneous coordinates
has no solution for
for arbitrary
38
M
w 6= 0
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
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]>