Viewing in 3D
In 2D, we specify
• a window on the 2D world and
• a viewport on the 2D view surface.
Objects in the world are clipped against the window and
are then transformed into the viewport for display.
In 3D, we specify
• a view volume in the world,
• a projection onto a projection plane, and
• a viewport on the view surface.
Objects in the 3D world are clipped against the 3D view
volume and are then projected. The contents of the
projection of the view volume onto the projection plan,
called the window, are then transformed into the 1
viewport for display.
Viewing in 3D
• Viewing requires three basic elements
One or more objects
A viewer with a projection surface
Projectors that go from the object(s) to the projection
surface
• Classical views are based on the relationship among
these elements
The viewer picks up the object and orients it how she
would like to see it
• Each object is assumed to constructed from flat
principal faces
Buildings, polyhedra, manufactured objects
2
Clipped 2D device
world coordinates
3D world-coordinate
coordinates Transform into
output primitives
Project onto
Clip against viewport in 2D
projection
view volume device coordinates
plane
for display
3
Planar Geometric Projections
• Standard projections project onto a plane
• Projectors are lines that either
converge at a center of projection
are parallel
• Such projections preserve lines
but not necessarily angles
• Nonplanar projections are needed for applications
such as map construction
4
Perspective vs Parallel
• Computer graphics treats all projections the same and
implements them with a single pipeline
• Classical viewing developed different techniques for
drawing each type of projection
• Fundamental distinction is between parallel and
perspective viewing even though mathematically
parallel viewing is the limit of perspective viewing
5
Perspective Projection
Projectors coverage at center of projection
6
Perspective Projection
7
Parallel Projection
8
Computer Viewing
• There are three aspects of the viewing process, all of
which are implemented in the pipeline,
Positioning the camera
• Setting the model-view matrix
Selecting a lens
• Setting the projection matrix
Clipping
• Setting the view volume
9
Computer Viewing
3D viewing involves not just a projection, but also a view
volume against which the 3D world is clipped.
The projection and view volume together provide all the
information needed to clip and project into 2D space.
view angle
10
The windows have a certain aspect ratio
OpenGL clips off any parts of the scene that lie outside the view
volume.
Points lying inside the view volume are projected onto the viewplane.
view angle
11
Setting the view volume
The shape of the camera’s view volume is encoded in the
projection matrix.
glMatrixMode(GL_PROJECTION);//make the projection matrix current
glLoadIdentity(); // start with a unit matrix
gluPerspective(viewAngle, aspectRatio, N,F); //load the appropriate
value
12
The OpenGL Camera
• In OpenGL, initially the world and camera frames are
the same
Default model-view matrix is an identity
• The camera is located at origin and points in the
negative z direction
• OpenGL also specifies a default view volume that is a
cube with sides of length 2 centered at the origin
Default projection matrix is an identity
13
Default Projection
Default projection is orthogonal
clipped out
z=0
14
Moving the Camera Frame
• If we want to visualize object with both positive and
negative z values we can either
Move the camera in the positive z direction
• Translate the camera frame
Move the objects in the negative z direction
• Translate the world frame
• Both of these views are equivalent and are determined by
the model-view matrix
Want a translation (glTranslatef(0.0,0.0,-d);)
d > 0
15
Moving Camera back from Origin
frames after translation by –d
d>0
default frames
16
Moving the Camera
• We can move the camera to any desired position by a
sequence of rotations and translations
• Example: side view
Rotate the camera
Move it away from origin
Model-view matrix C = TR
17
OpenGL code
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
glTranslatef(0.0, 0.0, -d);
glRotatef(90.0, 0.0, 1.0, 0.0);
18
The LookAt Function
• The GLU library contains the function glLookAt to from
the required modelview matrix through a simple
interface
• Note the need for setting an up direction
• Still need to initialize
Can concatenate with modeling transformations
• Example: isometric view of cube aligned with axes
glMatrixMode(GL_MODELVIEW):
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0. 0.0);
19
gluLookAt(eye.x, eye.y, eye.z, look.x, look.y, look.z,
up.x, up.y, up.z)
20
It is useful to attach a coordinate system to the camera.
This coordinate system has its origin at the eye, and has three axes, u,
v, and n
21
Other Viewing APIs
• The LookAt function is only one possible API for
positioning the camera
• Others include
View reference point, view plane normal, view up (PHIGS,
GKS-3D)
Yaw, pitch, roll
Elevation, azimuth, twist
Direction angles
22
23
What are the directions of u,v, and n when we execute gluLookAt()?
n = eye – look
u = up x n
v=n xu
24
The transformation that gluLookAt() sets up
The gluLookAt() sets up the transformation matrix V that converts
world coordinates to camera coordinates and the matrix M that
embodies all of the modeling all of the modeling transformations
applied to points.
25
Projections and Normalization
• The default projection in the eye (camera) frame is
orthogonal
• For points within the default view volume
xp = x
yp = y
zp = 0
• Most graphics systems use view normalization
All other views are converted to the default view by transformations
that determine the projection matrix
Allows use of the same pipeline for all views
26
Perspective Projections of 3D objects
27
Perspective projection of a point
x* N
P x P z
x , y N PP
* * x
,N
Py
P z
z
Note that the denominator term is larger for more remote points .
This introduces perspective forshortening and makes remote parts of an
object appear smaller than nearer parts.
28
Straight lines project to straight lines.
29
Example:
30
View 1: Set the camera’s eye at (0,0,2) and have it look down the
negative z-axis, with u = (1,0,0), n = (-1,0,0)
Set the near plane at a distance of unity from the eye.
All points on the front wall have Pz = -1
All points on the back wall have Pz = -2
Any point (Px,Py,Pz) on the front wall projects to P’= (Px,Py)
Any point on the back wall projects to P’= (Px/2,Py /2)
31
View 2: Set the camera’s eye at (0.5,0,2) , u and n are the same.
Set the near plane at a distance of unity from the eye.
32
View 3: Set the camera’s eye at (2,5,2) , look = (0,0,0).
33
Perspective Projection of a line
Suppose a line in 3D passes through the point A = (Ax, Ay, Az) and
the direction vector c = (cx,cy,cz).
P(t) = A + ct.
A x c x t A y c y t
p(t ) N ,N
A z c z t A z c z t
Suppose the line A + ct is parallel to the viewplane. Then cz = 0
p (t )
N
A c x t , A y c y t
Az
This is the parametric form of a line with slope cy/cx
34
If two lines in 3D are parallel to each other and to the viewplane, they
project to two parallel lines.
All parallel lines share the same vanishing point. In particular, they
project to lines that are not parallel.
35
A grid of lines
36
Adding Pseudodepth
Taking a projection discards information on depth; we lose information
about how far a point is from the eye.
37
Adding Pseudodepth
The actual distance of a point P from the eye, is Px2 Py2 Pz2
Cumbersome and slow to compute
38
What is a good choice for the pseudodepth function?
P Py aP b
( x* , y * , z * ) N x , N ,N z
Pz Pz Pz
We will choose a and b s.t. the pseudodepth varies between -1 and 1.
FN 2 FN
a ,b
FN FN
39
Px Py aPz b
( x , y , z ) N
* * *
,N ,N
Pz Pz Pz
40
Homogeneous Coordinate Form
Recall that a point P=(Px,Py,Pz) has the representation
(Px,Py,Pz,1) in homogeneous coordinates and
a vector v=(vx,vy,vz) has the representation (vx,vy,vz,0)
Now we extend the idea and say that a point P=(Px,Py,Pz)
has a whole family of homogeneous representations
(wPx,wPy,wPz,w) for any value of w except 0.
e.g. the point (1,2,3) has representations (1,2,3,1), (2,4,6,2), (.3,.6,.9,.3)
41
Homogeneous Coordinate Form
Consider and affine transform 2 1 3 1
q = Mp where 6 .5
M= 1 4
0 4 2 3
0 0 0 1
2 1 3 1 wPx wQx
6 .5 1 4 wPy wQ y
0 4 2 3 wPz wQz
0 0 0 1 w w
42
What if we deviate from a fourth row of (0,0,0,1)?
N 0 0 0
0 N 0 0
consider
0 0 a b
0 0 1 0
N 0 0 0 wPx wNPx
0
N 0 0 wPy wNPy
0 0 a b wPz w aPz b
0 0 1 0 w wPz
43
N 0 0 0 wPx wNPx
0
N 0 0 wPy wNPy
0 0 a b wPz w aPz b
0 0 1 0 w wPz
Which point is this?
Dividing through the fourth component
Px Py aPz b
N ,N , Perspective division!!
Pz Pz Pz
Perspective transformation!!
44
Px Py aPz b Px Py
N ,N , N ,N ,0
Pz Pz Pz Pz Pz
Perspective projection
Perspective projection = perspective transformation + orthographic
projection
45
Transforming the view volume
The perspective transformation “warps” objects so
that, when viewed with an orthographic projection,
they appear the same as the original objects do when
viewed with a perspective projection 46
Transforming the view volume
47
OpenGL composes the perspective transformation with another
mapping that scales and shifts this parallelepiped into the canonical
view volume, a cube that extends from -1 to 1 in each dimension.
48
• Two steps
Move center to origin
T(-(left+right)/2, -(bottom+top)/2)
Scale to have sides of length 2
S(2/(left-right),2/(top-bottom),2/(near-far))
2N right left
right left 0 0
right left
2N top bottom
0 0
R= top bottom top bottom
F N 2 FN
0 0
FN FN
0 0 1 0
This is the projection matrix, and it performs the perspective
transformation plus scaling and shifting to transform the camera’s view
volume into the canonical view volume.
49
2N right left
right left 0 0
right left
2N top bottom
R= 0 0
top bottom top bottom
F N 2 FN
0 0
FN FN
0 0 1 0
This is the matrix, that OpenGL creates (and by which it multiplies the
current matrix) when glFrustum(left,right,bott,top,N,F)
is executed.
50
The view volume bounds the portion of the scene that is to be clipped
out and projected onto the view plane. However, the view volume
depends on the type of projection.
51
52
Canonical View Volume
The canonical view volumes are a prism for parallel projections
and the truncated right regular pyramid for perspective projections.
It is much easier to clip to these volumes than to an arbitrary view
volume, so view volumes are often transformed to one of these before
clipping.
53
Clipping Faces against View Volume
A point lies inside the camera’s view volume if its transformed version
lies inside the canonical view volume.
54
Back-face culling can be performed to eliminate the polygons which
are facing away from the viewer.
Culling is a process where any polygon facing away from the view
direction is labeled as 'invisible' and ignored through the rest of the
pipeline.
55
56
The test for visibility is quite straightforward and is best carried out in
view space.
We calculate the outward normal for a polygon and apply it to the
following equation:
If Np . N > 0 then the polygon is visible, otherwise invisible where:
Np is the polygon normal N is the vector from the polygon to the
viewpoint ('line of sight' vector)
57
The Cohen-Sutherland algorithm (3D Version)
The Cohen-Sutherland algorithm extends to 3D very simply.
The outcode for 3D clipping is a 6-bit code. For a parallel canonical
view volume the bits represent:
bit 1 - point above view volume (Y>1)
bit 2 - point below view volume (Y<-1)
bit 3 - point to the right of view volume (X>1)
bit 4 - point to the left of view volume (X<-1)
bit 5 - point behind view volume (Z<-1)
bit 6 - point in front of view volume (Z>0)
58
As in the 2D case, the line may be trivially accepted
(both outcodes=000000), or trivially rejected
((code 1 AND code 2) not 000000).
Otherwise line subdivision is used again. The points of intersection
with the clipping edges are found using a parametric representation of
the line being clipped.
59
Suppose we want to clip the line segment AC against the CVV.
View the CVV as six infinite planes and consider where the given
edge lies relative to each plane in turn.
60
A +(C-A)t
If A and C lie on the same side of a wall, no need to compute the
intersection of the edge with the wall.
If A and C lie on opposite sides, locate the intersection point and clip
off the part of the edge that lies outside the wall.
61
A ( a x , a y , a z aw ), and C ( c x , c y , c z cw )
We need to test whether a point is on the “outside” or “inside” of a plane.
Consider the plane x = -1. The point A lies to the right of it
(on the “inside) if
ax
1, or a x aw , or a x aw 0
aw
62
63
Following the clipping operation, perspective division is finally done.
64
Pipeline View
65
OpenGL Orthogonal Viewing
glOrtho(xmin,xmax,ymin,ymax,near,far)
glOrtho(left,right,bottom,top,near,far)
near and far measured from camera
66
OpenGL Perspective
glFrustum(xmin,xmax,ymin,ymax,near,far)
67
Using Field of View
• With glFrustum it is often difficult to get the desired view
•gluPerpective(fovy, aspect, near, far) often
provides a better interface
front plane
aspect = w/h
68
Notes
• We stay in four-dimensional homogeneous coordinates
through both the modelview and projection
transformations
Both these transformations are nonsingular
Default to identity matrices (orthogonal view)
• Normalization lets us clip against simple cube regardless
of type of projection
• Delay final projection until end
Important for hidden-surface removal to retain depth information
as long as possible
69
Final Projection
• Set z =0
• Equivalent to the homogeneous coordinate
transformation
1 0 0 0
0 1 0 0
Morth =
0 0 0 0
0 0 0 1
• Hence, general orthogonal projection in 4D is
P = MorthST
70
Producing Stereo Views
To make a stereo view, two pictures – a “left eye” and a
“right eye” picture – are made using slightly different
cameras.
The cameras have the same LookAt point, but different eye positions.
71
Where do we put the left and right eye?
72
Taxonomy of Planar Geometric
Projections
planar geometric projections
parallel perspective
1 point 2 point 3 point
multiview axonometric oblique
orthographic
isometric dimetric trimetric
73
Vanishing Points
• Parallel lines (not parallel to the projection plan) on the
object converge at a single point in the projection (the
vanishing point)
• Drawing simple perspectives by hand uses these
vanishing point(s)
vanishing point
74
Perspective projections are categorized by their number of principal
vanishing points and therefore by the number of axes the projection
plane cuts.
One point perspective views
75
One-Point Perspective
• One principal face parallel to projection plane
• One vanishing point for cube
76
Two-Point Perspective
• On principal direction parallel to projection plane
• Two vanishing points for cube
77
The infinite grid in two-point perspective
78
Three-Point Perspective
• No principal face parallel to projection plane
• Three vanishing points for cube
79
Escher’s Ascending and Descending: a three-point perspective.
80
Advantages and Disadvantages
• Objects further from viewer are projected smaller than
the same sized objects closer to the viewer
(diminuition)
Looks realistic
• Equal distances along a line are not projected into
equal distances (non-uniform foreshortening)
• Angles preserved only in planes parallel to the
projection plane
• More difficult to construct by hand than parallel
projections (but not more difficult by computer)
81
Parallel Projections
To what point p does P project?
n.( B P)
p Pd
n.d
82
n.( B P)
p Pd
n.d
Using n=(0,0,1), and project onto the xy plane for which B=(0,0,0)
P P
p Px d x z , Py d y z ,0
dz dz
83
Parallel Projections
Oblique Orthographic
Oblique = the projection direction d is not parallel to n
Orthographic = the projection direction d is parallel to n
84
Orthographic Projection
Projectors are orthogonal to projection surface
In orthographic projections, dx and dy are zero. p Px , Py ,0
85
Orthogonal Normalization
glOrtho(left,right,bottom,top,near,far)
normalization find transformation to convert
specified clipping volume to default
86
Orthogonal Matrix
• Two steps
Move center to origin
T(-(left+right)/2, -(bottom+top)/2,(near+far)/2))
Scale to have sides of length 2
S(2/(left-right),2/(top-bottom),2/(near-far))
2 right left
right left 0 0
right left
2 top bottom
0 0
top bottom top bottom
P = ST = 2 far near
0 0
near far far near
0 0 0 1
87
Multiview Orthographic Projection
• Projection plane parallel to principal face
• Usually form front, top, side views
isometric (not multiview
orthographic view)
front
in CAD and architecture,
we often display three
multiviews plus isometric
top side
88
89
Advantages and Disadvantages
• Preserves both distances and angles
Shapes preserved
Can be used for measurements
• Building plans
• Manuals
• Cannot see what object really looks like because
many surfaces hidden from view
Often we add the isometric
90
Axonometric Orthographic
Projections
Allow projection planes that are not normal to a principle
axis and therefore show several faces of an object at
once.
classify by how many angles of
a corner of a projected cube are
the same
none: trimetric
1
two: dimetric 2 3
three: isometric
91
Axonometric Orthographic
Projections
foreshortening
92
Types of Axonometric Projections
2 principle axes foreshortened equally.
All 3 principle axes foreshortened unequally.
All 3 principle axes foreshortened equally.
93
Advantages and Disadvantages
• Lines are scaled (foreshortened) but can find scaling
factors (all three principal axes are equally foreshortened
• Lines preserved but angles are not
Projection of a circle in a plane not parallel to the projection
plane is an ellipse
• Can see three principal faces of a box-like object
• Some optical illusions possible
Parallel lines appear to diverge
• Does not look real because far objects are scaled the
same as near objects
• Used in CAD applications
94
Oblique Projection
Orthographic projections preserve the exact shape of one
face of an object, but do not reveal its 3D nature very
well.
Axonometric projections show a 3D quality, but do not yield the
exact shape of any of the faces of the object.
Oblique projections are an attempt to combine the uesful properties of
both orthographic and axonometric projections.
They usually present the exact shape of one face of an object (the most
important face), while simultaneously revealing the general 3D
appearance of the object.
95
Oblique Projections
Oblique Projection = Shear + Orthogonal Projection
dx
1 0 0
px dz Px
p dy
P
y 0 1 0 y
pz dz Pz
0 0 1 0 1
1
0 0 0 1
96
Oblique Projection
Arbitrary relationship between projectors and projection
plane
97
dx
1 0 0
px dz
py dy
p 0 1 0
dz
z
1 0 0 0 0
0 0 0 1
98
Advantages and Disadvantages
• Can pick the angles to emphasize a particular face
Architecture: plan oblique, elevation oblique
• Angles in faces parallel to projection plane are
preserved while we can still see “around” side
• In physical world, cannot create with simple camera;
possible with bellows camera or special lens
(architectural)
99
Classical Projections
100
OpenGL Perspective
•glFrustum allows for an unsymmetric viewing frustum
(although gluPerspective does not)
101
OpenGL Perspective Matrix
• The normalization in glFrustum requires an initial
shear to form a right viewing pyramid, followed by a
scaling to get the normalized perspective volume.
Finally, the perspective matrix results in needing only
a final orthogonal transformation
P = NSH
our previously defined shear and scale
perspective matrix
102
Why do we do it this way?
• Normalization allows for a single pipeline for both
perspective and orthogonal viewing
• We keep in four dimensional homogeneous
coordinates as long as possible to retain three-
dimensional information needed for hidden-surface
removal and shading
• We simplify clipping
103