8 3d Viewing
8 3d Viewing
Projection Viewing
Clip Space Eye Space
transformation transformation
Perspective Viewport
NDC space Screen space
divide mapping
Normalized Device Coordinates
Set up OpenGL Camera
n Set up a 3D scene is like taking a
photograph
Viewing
n Position and orient your camera
Viewing
n Important camera parameters to specify
n Camera (eye) position (Ex,Ey,Ez) in world coordinate
system
n Center of interest (coi) (cx, cy, cz)
n Orientation (which way is up?) View-up vector
(Up_x, Up_y, Up_z)
(0,1,0) (1,0,0)
v u
n (0,0,1)
y
(0,0,0)
coi
world x
z
v u
y I will explain the math behind for
n this matrix later
world
x
z
Use glMatrix to Construct the viewing Matrix
(1) (2) (3)
-Create a view matrix
var vMatrix = mat4.create();
vMatrix= mat4.lookAt([0.0, 0.0, 5.0], [0.0, 0.0, 0.0],[0.0, 1.0,
0.0], vMatrix);
mat4.identify(mMatrix);
mMatrix = mat4.rotate(mMatrix, angle, [0.0f, 0.0f, 1.0f]);
mMatrix = mat4.scale(mMatrix, [1,2,1]);
v u
n
y
Eye coordinate frame
coi
world x
z
Transformation Pipeline
Projection Viewing
Clip Space Eye Space
transformation transformation
Perspective Viewport
NDC space Screen space
divide mapping
Normalized Device Coordinates
Projection
n Control the “lens” of the camera
n Project the object from 3D world to 2D screen
Projection Transformation
z q z
x
n The larger is the field view, the smaller is the object
projection size
Near and Far Clipping Planes
n Only objects between near and far planes are
drawn Near plane
Far plane
y
Viewing Frustum
2/(xmax-xmin) 0 0 - (xmax+xmin)/(xmax-xmin)
M2 x M1 = 0 2/(ymax-ymin) 0 - (ymax+ymin)/(ymax-ymin)
0 0 -2/(zmax-zmin) - (zmax+zmin)/(zmax-zmin)
0 0 0 1
Projection Matrix
n Perspective Projection
x’ 2N/(xmax-xmin) 0 (xmax+xmin)/(xmax-xmin) 0 x
y’ = 0 2N/(ymax-ymin) (ymax+ymin)/(ymax-ymin) 0 y
z’ 0 0 -(F + N)/(F-N) -2F*N/(F-N) z
w’ 0 0 -1 0 1
Use GLM to set up the matrix
- Set up the perspective projection matrix
var pMatrix = mat4.create();
mat4.perspective(60.0,1.0,.1,100.0, pMatrix);
y y w
fovy
z
z
h
eye
x
Aspect = w / h near far
Use projection matrix
n The projection matrix is then combined with the
model view matrix and becomes modelviewprojection
(MVP) matrix
n MVP = projection * viewing * modeling
n Multiply MVP to your vertices will transform them
from local space to the canonical view volume, i.e., in
the range of [-1,1]3 if they are visible
n Remember to use 4D vectors [x,y,z,1] to multiply
Transformation Pipeline
Projection Viewing
Clip Space Eye Space
transformation transformation
Perspective Viewport
NDC space Screen space
divide mapping
Normalized Device Coordinates
3D viewing under the hood
Viewport
Transformation
Display
3D viewing under the hood
Topics of Interest:
n Viewing transformation
n Projection transformation
Viewing Transformation
world origin
Remember u,v,n should
be all unit vectors
(u,v,n should be orthogonal to each other)
Eye Coordinate Frame (3)
n How about u and v?
We can get u first -
v
V_up u u is a vector that is perpendicular
to the plane spanned by
COI N and view up vector (V_up)
eye
n
U = V_up x n
u = U / |U|
Eye Coordinate Frame (4)
n How about v? Knowing n and u, getting v is
easy
v
V_up u
v = n xu
COI
eye
n v is already normalized
Eye Coordinate Frame (5)
n Put it all together Eye space origin: (Eye.x , Eye.y, Eye.z)
Basis vectors:
v
V_up n = (eye – COI) / | eye – COI|
u
u = (V_up x n) / | V_up x n |
v = n x u
COI
eye
n
World to Eye Transformation
z
World to Eye Transformation
n Rotate the eye frame so that it will be “aligned” with
the world frame
n Translate (-ex, -ey, -ez)
Rotation: ux uy uz 0
v vx vy vz 0
u nx ny nz 0
y n 0 0 0 1
z
World to Eye Transformation (3)
v u
y Why -d ?
n
world When you rotate your head by d, it is like
x rotate the object by –d
z
Projection Transformation
y y
z z
x x
y
n We actually want to keep Z (x,y,z)
z
– why?
x
Parallel Projection (2)
2/(xmax-xmin) 0 0 - (xmax+xmin)/(xmax-xmin)
M2 x M1 = 0 2/(ymax-ymin) 0 - (ymax+ymin)/(ymax-ymin)
0 0 -2/(zmax-zmin) -(zmax+zmin)/(zmax-zmin)
0 0 0 1
Perspective Projection
y
n Side view: z
x
Projection plane
y (x,y,z)
Based on similar triangle:
(x’,y’,z’)
(0,0,0) y -z
=
z y’ d
d
d
-z Y’ = y x
-z
Eye (projection center)
Perspective Projection (2)
n Same for x. So we have:
x’ = x x d / -z
y’ = y x d / - z
z’ = -d
n Put in a matrix form:
x’ 1 0 0 0 x
y’ = 0 1 0 0 y
z’ 0 0 1 0 z
w 0 0 (1/-d) 0 1
n Use pseudo depth – OpenGL maps the near plane to -1, and far
plane to 1
y far
z near
x eye
(top view)
(-1, -1) Z = -1 z=1
n And takes care the case that eye is not at the center
of the view volume (shear)
Perspective Projection (6)
n Final Projection Matrix:
x’ 2N/(xmax-xmin) 0 (xmax+xmin)/(xmax-xmin) 0 x
y’ = 0 2N/(ymax-ymin) (ymax+ymin)/(ymax-ymin) 0 y
z’ 0 0 -(F + N)/(F-N) -2F*N/(F-N) z
w’ 0 0 -1 0 1
(1, 1, 1)
y