Viewing & Projection
Lecture: 6
Fall 2016
Computer Graphics (CS3388) Department of Computer Science
University of Western Ontario
Viewing & Projection
Outline
Rendering pipeline
Camera geometry World-View transformation Perspective projection Orthographic projection Pseudo depth
(materials from Tamara Munzner (UBC), R. Rao (Washington univ.), R.W. Lindeman (Worcester Polytechnic), A. Hausner (UofT), C.H. Chuan (NTU))
1
Viewing & Projection
From 3D world to 2D image Projection: Geometric abstraction
what eyes or cameras do Viewing transformation:
where is the camera? where is it pointing? Perspective transformation: 3D to 2D
flatten to image
2
Projective Rendering Pipeline
3
Projective Rendering Pipeline
4
Viewing
Remember OpenGL function:
gluLookAt(eye, at(or gaze point), up vector);
Camera at world origin
y-axis is up
Camera at world origin looking down negative z-axis
5
Placing Camera in World Coordinates
Let’s derive Modelview matrix M -world to view (W2V): (We will do the reverse first: V2W)
treat camera as if it’s just an object
1 translate from origin to eye
2 rotate view vector (lookat − eye) to w axis
3 rotate around w to bring up vector into vw-plane
6
V2W Transformation
1. Translate the origin to eye:
1 0 0 ex T=0 1 0 ey 0 0 1 e z
0001
7
V2W Transformation
2. rotate view vector (lookat − eye) to w axis:
w: normalized opposite of view/gaze vector g
w = − gˆ = − g ||g||
8
V2W Transformation
3. rotate around w to bring up into vw-plane:
u should be perpendicular to vw-plane, thus perpendicular to w and
up vector t
v should be perpendicular to u and w
u= t×w ,v=w×u ||t × w||
9
V2W Transformation
Rotate from WCS xyz into uvw coordinate system with matrix that has columns u, v, w
u= t×w ,v=w×u,w=−gˆ=− g ||t × w|| ||g||
1 0 0 ex ux vx wx 0 T=0 1 0 ey,R=uy vy wy 0 0 0 1 e z u z v z w z 0
0001 0001 MV2W =TR
10
V2W Transformation
we derived position of camera as object in world. invert for gluLookAt: go from world to camera!
MW2V =(MV2W)−1 =R−1T−1
ux uy uz 01 0 0 −ex MW2V =vx vy vz 00 1 0 −ey w z w y w z 0 0 0 1 − e z
00010001
ux = vx
uy uz vy vz wy wz
−e.u −e.v −e.w
wz 0001
11
V2W Transformation
ux MWorld2View =vx
uy uz vy vz wy wz
−ex ∗ux +−ey ∗uy +−ez ∗uz −ex ∗vx +−ey ∗vy +−ez ∗vz −ex ∗wx +−ey ∗wy +−ez ∗wz
wz
000 1
12
World vs. Camera Coordinates Example
13
Projections
14
The World is spherical…
15
Wait!!! … The World is flat
The brain constructs a 3D interpretation consistent with the 2D projection of the scene on your retina
16
Projection
Let’s design a camera
Idea 1: put a piece of film in front of an object Do we get a reasonable image?
17
Projection
Add a barrier to block off most of the rays This reduces blurring
The opening is known as the aperture How does this transform the image?
18
Projection
Theoretical perfect pinhole
light shining through tiny hole into dark space yields upside-down picture
19
Projection
Actual pinhole
non-zero sized hole: rays hit multiple points on film plane – blur effect
20
First Camera
How does the aperture size affect the image?
21
First Camera
Why not make the aperture as small as possible? Less light gets through
Diffraction effects…
22
Shrinking the aperture
23
Solution: Lens
A lens focuses light onto the film, BUT…
There is a specific distance at which objects are “in focus” Changing the shape of the lens changes this distance
24
Solution: Lens
real pinhole camera – image inverted
computer graphics camera:
25
Perspective Projection
26
Modeling Projection
use the pin-hole camera as an approximation optical center (Center Of Projection) at the origin image plane (Projection Plane) in front of the COP
avoids inverted image
camera looks down the negative z-axis
right-handed-coordinates
27
Basic Perspective Projection
From similar triangles,
y′ y ′ d.y −d = z , =⇒ y = − z
x′ x ′ d.x −d = z , =⇒ x = − z
(x,y,z) → (−dx,−dy,−d) zz
28
Matrix form
− x x z/d
− y is homogenized version of y , where w = −z/d
z/d z d −z/d
Projection matrix:
x 1 0 0 0x y =0 1 0 0y
z 0 0 1 0z −z/d 0 0 −1/d 0 1
29
Matrix form
Scaling by c:
cx c 0 0 0x cy=0c 0 0y
cz 0 0 c 0z
−cz/d 0 0 −c/d 0 1
Same result if (x, y, z) is scaled by c
In the image, a larger object further away (scaled (x,y,z)) can have the same size as a smaller object that is closer
30
Basic Perspective Projection
Perspective Projection is irreversible
many 3D points can be mapped to same (x, y, d) on the projection plane
no way to retrieve the unique z values
31
Orthographic Projection
Suppose d → ∞
Centre of Projection (eye point) is at infinity
(x,y,z) → (x,y) (throw away z-values)
This is called Orthographic or Parallel Projection
xp 1 0 0 0x yp = 0 1 0 0y
zp 0 0 0 0z
100011
32
Orthographic Projection
Viewing Volume for Orthographic Projection Camera positioned infinitely far away at z = ∞
33
Perspective to Orthographic
transformation of space
center of projection moves to infinity
from frustum (truncated pyramid) to parallelepiped (box)
34
Viewing volumes
35
Vanishing points
Vanishing point: mathematical representation
Suppose a 3D line passes through point A = (Ax , Ay , Az ) and has direction vector c = (cx , cy , cz ).
The parametric form of the line is:
p(t) = A + ct
36
Vanishing points
We already have, Hence,
(x,y,z) → (−dx,−dy,−d) zz
p(t)=(d Ax +cxt ,d Ay +cyt ) −Az −czt −Az −czt
If the direction c of the line is not parallel to the viewplane, say cz < 0, so that as t increases,
This is called the Vanishing point for the line.
p(∞)=(d cx ,d cy ) −cz −cz
37
Pseudo Depth
Classical perspective projection drops z-coordinates
we need z to find closest object (depth testing)
introduce pseudodepth: a measure of which objects are further if two points project to the same (x, y)
38
Pseudo Depth
What is a good choice of pseudodepth function? Remember (x,y,z) → (d x ,d y ,−d)
−z −z
We have to change only the z-value. Let’s choose a function with the
same denominator (−z) and a numerator that is linear in z:
(x∗,y∗,z∗)=(d x ,d y ,az+b) −z −z −z
for some choice of constants a and b.
Choose a, b so that pseudodepth varies from −1 to 1 (canonical cube)
39
Pseudo Depth
We have, z∗ = az+b −z
We choose constants such that, −1 ≤ az+b ≤ 1 −z
we keep the pseudo-depth values comprised between -1 and 1 for z between -d and -F
when z = −d, depth is -1,
az+b=−1,=⇒ a(−d)+b=−1
−z −(−d ) when z = −F, depth is 1,
az+b=1,=⇒ a(−F)+b=−1 −z −(−F )
40
Pseudo Depth
Solving the equations for a & b, we get
a = − F + d , b = −2Fd
F−d F−d So, the final expression for pseudodepth point is,
(x∗,y∗,z∗)=(d x ,d y ,az+b) −z −z −z
=⇒
x y −F+dz+−2Fd
(−d ,−d , F−d F−d ) z z −z
41
Orthographic Derivation
From view coordinate system(VCS) to normalized device coordinate system(NDCS): scale, translate, reflect for new coordinate system:
y ′ = ay + b (a,b are transformation parameters)
42
Orthographic Derivation
y ′ = ay + b Now, y=top means y′ = 1, which gives
1 = a.top + b y=bot means y′ = −1, which gives
−1 = a.bot + b Substituting values, we obtain:
a = 2 , b = −top − bot top + bot top − bot
43
Orthographic Derivation
a = 2 , b = −top + bot top + bot top − bot
use the same idea for right/left, far/near
44
Orthographic Derivation
The resultant transformation matrix is:
2 0 0 −right+left
′
top−bot 0 0 −2
−far+nearP far−near
right −left 0
2
right −left top+bot −top−bot
P= 0
(notice the scale, translate, reflect factors)
far−near 0001
45
Perspective Projection Matrix
Let’s now look at the final matrix of Perspective Projection. Remember the simple form:
x 1 0 0 0x y =0 1 0 0y
z 0 0 1 0z −z/d 0 0 −1/d 0 1
Now, complete this by incorporating shear, scale, projection-normalization: x E 0 A 0x
A,B: shear
E, F, C: scale
C, D, -1: projection-normalization
y=0 F B 0y
z 0 0 C Dz w′ 00−101
46
Perspective Projection Matrix
x = left =⇒ x′/w′ = −1
x = right =⇒ x′/w′ = 1
y = top =⇒ y′/w′ = 1
y = bottom =⇒ y′/w′ = −1 z = −near =⇒ z′/w′ = −1 z = −far =⇒ z′/w′ = 1
47
Perspective Projection Matrix
After solving, final projection matrix becomes:
2d 0 right −left
0 2d top−bot
right+left right −left top+bot top−bot −(F+d)
0 0
0 0
0 0 −1 0
−2Fd F−d
F−d
48