1
CS 543 - Computer Graphics:Projection
byRobert W. Lindeman
[email protected](with help from Emmanuel Agu ;-)
R.W. Lindeman - WPI Dept. of Computer Science 2
3D Viewing and View VolumeRecall: 3D viewing set up
2
R.W. Lindeman - WPI Dept. of Computer Science 3
Projection TransformationView volume can have different shapes
Parallel, perspective, isometric
Different types of projection Parallel (orthographic), perspective, etc.
Important to control Projection type: perspective or orthographic,
etc. Field of view and image aspect ratio Near and far clipping planes
R.W. Lindeman - WPI Dept. of Computer Science 4
Perspective ProjectionSimilar to real worldCharacterized by object foreshortening
Objects appear larger if they are closer tocamera
Need to define Center of projection (COP) Projection (view) plane
Projection Connecting the object to the center of
projection
projection plane
camera
3
R.W. Lindeman - WPI Dept. of Computer Science 5
Why is it Called Projection?
View plane
R.W. Lindeman - WPI Dept. of Computer Science 6
Orthographic (Parallel) ProjectionNo foreshortening effect
Distance from camera does not matter
The center of projection is at infinityProjection calculation
Just choose equal z coordinates
4
R.W. Lindeman - WPI Dept. of Computer Science 7
Field of ViewDetermine how much of the world is
taken into the pictureLarger field of view = smaller object-
projection size
x
y
z
y
z θ
field of view(view angle)
center of projection
R.W. Lindeman - WPI Dept. of Computer Science 8
Near and Far Clipping PlanesOnly objects between near and far planes
are drawnNear plane + far plane + field of view =
View Frustum
x
y
z
Near plane Far plane
5
R.W. Lindeman - WPI Dept. of Computer Science 9
View Frustum3D counterpart of 2D-world clip windowObjects outside the frustum are clipped
x
y
z
Near plane Far plane
View Frustum
R.W. Lindeman - WPI Dept. of Computer Science 10
Projection TransformationIn OpenGL
Set the matrix mode to GL_PROJECTION For perspective projection, use
gluPerspective( fovy, aspect, near, far );
orglFrustum(left, right, bottom, top,
near, far );
For orthographic projection, useglOrtho( left, right, bottom, top,
near, far );
6
R.W. Lindeman - WPI Dept. of Computer Science 11
gluPerspective( fovy, aspect, near, far )
Aspect ratio is used to calculatethe window width
x
y
z
y
z
fovy
eye
near farAspect = w / h
w
h
θ
R.W. Lindeman - WPI Dept. of Computer Science 12
glFrustum( left, right, bottom, top,near, far )
Can use this function in place ofgluPerspective( )
x
y
z
left
rightbottom
top
near far
7
R.W. Lindeman - WPI Dept. of Computer Science 13
glOrtho( left, right, bottom, top,near, far )
For orthographic projection
x
y
z
left
rightbottom
top
nearfar
R.W. Lindeman - WPI Dept. of Computer Science 14
Example: ProjectionTransformation
void display( ) { glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluPerspective( FovY, Aspect, Near, Far ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); gluLookAt( 0, 0, 1, 0, 0, 0, 0, 1, 0 ); myDisplay( ); // your display routine}
8
R.W. Lindeman - WPI Dept. of Computer Science 15
Projection TransformationProjection
Map the object from 3D space to 2D screen
x
y
z
x
y
z
Perspective: gluPerspective( ) Parallel: glOrtho( )
R.W. Lindeman - WPI Dept. of Computer Science 16
Parallel Projection (The Math) After transforming the object to eye space,After transforming the object to eye space,
parallel projection is relatively easy: we couldparallel projection is relatively easy: we couldjust set all Z to the same valuejust set all Z to the same value Xp = x Yp = y Zp = -d
We actually want to remember Z – why?
x
y
z(x,y,z)
(Xp, Yp)
9
R.W. Lindeman - WPI Dept. of Computer Science 17
Parallel ProjectionOpenGL maps (projects) everything in
the visible volume into a canonical viewvolume (CVV)
(-1, -1, 1)
(1, 1, -1)
Canonical View VolumeglOrtho( xmin, xmax, ymin, ymax, near, far )
(xmin, ymin, near)
(xmax, ymax, far)
Projection: Need to build 4x4 matrix to do mapping from actual view volume to CVV
R.W. Lindeman - WPI Dept. of Computer Science 18
Parallel Projection: glOrthoParallel projection can be broken down
into two parts Translation, which centers view volume at
origin Scaling, which reduces cuboid of arbitrary
dimensions to canonical cubeDimension 2, centered at origin
10
R.W. Lindeman - WPI Dept. of Computer Science 19
Parallel Projection: glOrtho(cont.) Translation sequence moves midpoint of view
volume to coincide with origin e.g., midpoint of x = (xmax + xmin)/2
Thus, translation factors are-(xmax+xmin)/2, -(ymax+ymin)/2, -(far+near)/2
So, translation matrix M1:
!
1 0 0 "(xmax+ xmin) /2
0 1 0 "(ymax+ ymin) /2
0 0 1 "(zmax+ zmin) /2
0 0 0 1
#
$
% % % %
&
'
( ( ( (
R.W. Lindeman - WPI Dept. of Computer Science 20
Parallel Projection: glOrtho(cont.)Scaling factor is ratio of cube dimension
to Ortho view volume dimensionScaling factors
2/(xmax-xmin), 2/(ymax-ymin), 2/(zmax-zmin)
So, scaling matrix M2:
!
2
xmax" xmin0 0 0
02
ymax" ymin0 0
0 02
zmax" zmin0
0 0 0 1
#
$
% % % % % % %
&
'
( ( ( ( ( ( (
11
R.W. Lindeman - WPI Dept. of Computer Science 21
Parallel Projection: glOrtho()(cont.)Concatenating M1xM2, we get transform
matrix used by glOrtho
!
M2 "M1=
2 /(xmax# xmin) 0 0 #(xmax+ xmin) /(xmax# xmin)
0 2 /(ymax# ymin) 0 #(ymax+ ymin) /(ymax# ymin)
0 0 2 /(zmax# zmin) #(zmax+ zmin) /(zmax# zmin)
0 0 0 1
$
%
& & & &
'
(
) ) ) )
Refer to: Hill, 7.6.2
!
2
xmax" xmin0 0 0
02
ymax" ymin0 0
0 02
zmax" zmin0
0 0 0 1
#
$
% % % % % % %
&
'
( ( ( ( ( ( (
X
!
1 0 0 "(xmax+ xmin) /2
0 1 0 "(ymax+ ymin) /2
0 0 1 "(zmax+ zmin) /2
0 0 0 1
#
$
% % % %
&
'
( ( ( (
R.W. Lindeman - WPI Dept. of Computer Science 22
Perspective Projection: ClassicalSide view
x
yz
(0,0,0)
d
Projection plane
Eye (center of projection )
(x,y,z)
(x’,y’,z’)
-z
z
yBased on similar triangles:
y -z y’ d
d y’ = y * -z
=
12
R.W. Lindeman - WPI Dept. of Computer Science 23
Perspective Projection:Classical (cont.) So (x*, y*), the projection of point, (x, y, z)
onto the near plane N, is given as
Similar triangles
Numerical exampleQ: Where on the viewplane does P = (1, 0.5, -
1.5) lie for a near plane at N = 1?
(x*, y*) = (1 x 1/1.5, 1 x 0.5/1.5) = (0.666, 0.333)
!
x*,y *( ) = NPx
"Pz,N
Py
"Pz
#
$ %
&
' (
R.W. Lindeman - WPI Dept. of Computer Science 24
Pseudo Depth Checking Classical perspective projection drops z coordinates But we need z to find closest object (depth testing) Keeping actual distance of P from eye is
cumbersome and slow
Introduce pseudodepth: all we need is a measureof which objects are further if two points project tothe same (x, y)
Choose a, b so that pseudodepth varies from –1 to1 (canonical cube)
!
distance = Px2
+ Py2
+ Pz2( )
!
x*,y*,z *( ) = NPx
"Pz,N
Py
"Pz,aPz + b
"Pz
#
$ %
&
' (
13
R.W. Lindeman - WPI Dept. of Computer Science 25
Pseudo Depth Checking (cont.)Solving:
For two conditions, z* = -1 when Pz = -Nand z* = 1 when Pz = -F, we can set uptwo simultaneous equations
Solving for a and b, we get
!
z* =aP
z+ b
"Pz
!
a ="(F + N)
F " N
!
b ="2FN
F " N
R.W. Lindeman - WPI Dept. of Computer Science 26
Homogenous Coordinates Would like to express projection as 4x4 transform
matrix Previously, homogeneous coordinates for the point P =
(Px, Py, Pz) was (Px, Py, Pz, 1) Introduce arbitrary scaling factor, w, so that P = (wPx,
wPy, wPz, w) (Note: w is non-zero) For example, the point P = (2, 4, 6) can be expressed
as (2, 4, 6, 1) or (4, 8, 12, 2) where w=2 or (6, 12, 18, 3) where w = 3
So, to convert from homogeneous back to ordinarycoordinates, divide all four terms by last component anddiscard 4th term
14
R.W. Lindeman - WPI Dept. of Computer Science 27
Perspective ProjectionSame for x, so we have x’ = x * d / -z y’ = y * d / -z z’ = -d
Put in a matrix form
OpenGL assumes d = 1, i.e., the image plane is at z = -1!
1 0 0 0
0 1 0 0
0 0 1 0
0 0 1"d( ) 1
#
$
% % % %
&
'
( ( ( (
x
y
z
1
#
$
% % % %
&
'
( ( ( (
=
x '
y '
z'
w
#
$
% % % %
&
'
( ( ( (
)
"d xz( )
"d yz
# $ % &
' (
"d
1
#
$
% % % % %
&
'
( ( ( ( (
R.W. Lindeman - WPI Dept. of Computer Science 28
Perspective Projection (cont.)We are not done yet!
Need to modify the projection matrix toinclude a and b
x’ 1 0 0 0 x y’ = 0 1 0 0 y z’ 0 0 a b z w 0 0 (1/-d) 0 1
We have already solved a and b
x
yz
Z = 1 z = -1
15
R.W. Lindeman - WPI Dept. of Computer Science 29
Perspective Projection (cont.)Not done yet! OpenGL also normalizes
the x and y ranges of the view frustumto [-1, 1] (translate and scale)
So, as in ortho, to arrive at finalprojection matrix We translate by
–(xmax + xmin)/2 in x -(ymax + ymin)/2 in y
And scale by2/(xmax – xmin) in x2/(ymax – ymin) in y
R.W. Lindeman - WPI Dept. of Computer Science 30
Perspective Projection (cont.)Final projection matrixglFrustum( xmin, xmax, ymin, ymax, N, F )
N = near plane, F = far plane
!
2N
xmax" xmin0
xmax+ xmin
xmax" xmin0
02N
ymax" ymin
ymax+ ymin
ymax" ymin0
0 0"(F + N)
F " N
"2FN
F " N0 0 "1 0
#
$
% % % % % % %
&
'
( ( ( ( ( ( (
16
R.W. Lindeman - WPI Dept. of Computer Science 31
Perspective Projection (cont.)After perspective projection, viewing
frustum is also projected into a canonicalview volume (like in parallel projection)
(-1, -1, 1)
(1, 1, -1)
Canonical View Volume
x
y
z
R.W. Lindeman - WPI Dept. of Computer Science 32
ReferencesHill, Chapter 7