Transcript Chapter 5
Chapter 5
Viewing
1
OpenGL Perspective Matrix
planar geometric projections
perspective
parallel
1 point
multiview axonometric oblique
orthographic
isometric
dimetric
2 point
3 point
trimetric
2
Perspective Projection
3
Parallel Projection
4
Classical Viewing
Viewing requires three basic elements
Classical views are based on the relationship
among these elements
One or more objects
A viewer with a projection surface
Projectors that go from the object(s) to the projection
surface
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
5
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
6
Classical Projections
7
Orthographic Projection
Projectors are orthogonal to projection surface
8
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
9
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
10
Axonometric Projections
Allow projection plane to move relative to object
classify by how many angles of
a corner of a projected cube are
the same
q1
none: trimetric
q2 q3
two: dimetric
three: isometric
11
Types of Axonometric Projections
12
Construction of Axonometric
Projections
Projectors are still orthogonal to the projection plane, but
Projection plane can have any orientation w.r.t. the object
13
Advantages and Disadvantages
Lines are scaled (foreshortened) but can find
scaling factors
Lines preserved but angles are not
Can see three principal faces of a box-like object
Some optical illusions possible
Projection of a circle in a plane not parallel to the
projection plane is an ellipse
Parallel lines appear to diverge
Does not look real because far objects are scaled
the same as near objects
Used in CAD applications
14
Oblique Projection
Arbitrary relationship between projectors and
projection plane
15
Advantages and Disadvantages
Can pick the angles to emphasize a particular face
In physical world, cannot create with simple
camera; possible with bellows camera or special
lens (architectural)
16
Perspective Projection
Projectors coverge at center of projection
17
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
18
Three-Point Perspective
No principal face parallel to projection plane
Three vanishing points for cube
19
Two-Point Perspective
On principal direction parallel to projection plane
Two vanishing points for cube
20
One-Point Perspective
One principal face parallel to projection plane
One vanishing point for cube
21
Advantages and Disadvantes
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 (nonuniform 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)
22
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
23
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
24
Default Projection
Default projection is orthogonal
clipped out
z=0
25
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
26
Moving Camera Back from Origin
frames after translation by –d
d>0
default frames
27
Moving the Camera (Object)
We can move the camera to any desired
position by a sequence of rotations and
translations
Example: positive x
(side view of an object)
Rotate the camera
Move it away from origin
Model-view matrix C = TR
28
OpenGL Code
Remember that last transformation
specified is first to be applied
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
glTranslatef(0.0, 0.0, -d);
glRotatef(90.0, 0.0, 1.0, 0.0);
We are changing the modelview matrix C. A vertex specified at q
through glVertex, after the change is still at q in the world frame,
but is at Cq in the camera frame known internally to OpenGL.
29
Manually Generating
an Isometric View – 1/3
Rotating about the y axis by 45o
Rotating about the x axis by 35.26o
Translating along the z direction by d
y
y
2 2
z
2 2
2
z
2
q
30
Manually Generating
an Isometric View – 2/3
1
0
M TRx Ry
0
0
1
0
0
0
0 2 / 2
1 0 0 6 / 6
0 1 d 3 / 3
0 0 1 0
0 0
0 2 / 2 0 2 / 2 0
6 / 3 3 / 3 0 0
1
0
0
3 /3
6 / 3 0 2 / 2 0 2 / 2 0
0
0
1 0
0
0
1
0
2 / 2 0 2 / 2
0
2/2 0
6 / 3 6 / 6 0 6 / 6
6 /3 6 /6 0
3 /3
3 / 3 0 3 / 3 3 / 3
3 /3 d
0
0
1 0
0
0
1
0 1
1 0 0 0
0 1 d 0
0 0 1 0
0 0
0
0
31
Manually Generating
an Isometric View – 3/3
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -d);
glRotatef(35.26, 1.0, 0.0, 0.0);
glRotate(45.0, 0.0, 1.0, 0.0);
32
The LookAt Function – 1/2
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.0, 1.0, 0.0);
33
The LookAt Function – 2/2
glLookAt(eyex, eyey, eyez, atx, aty, atz,
upx, upy, upz)
34
Other Viewing APIs – 1/2
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
35
Other Viewing APIs – 2/2
36
Simple Perspective
Center of projection at the origin
Projection plane z = d, d < 0
37
Perspective Equations
Consider top and side views
xp =
x
z/d
yp =
y
z/d
zp = d
38
Homogeneous Coordinate Form
consider q = Mp where
q=
x
y
z
1
1
0
M=
0
0
p=
0 0
1 0
0 1
0 1/ d
x
y
z
z / d
0
0
0
0
39
Perspective Division
However w 1, so we must divide by w to
return from homogeneous coordinates
This perspective division yields
xp =
x
z/d
yp =
y
z/d
zp = d
the desired perspective equations
We will consider the corresponding clipping
volume with the OpenGL functions
40
Orthogonal Projections
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
41
Homogeneous Coordinate
Representation
xp = x
yp = y
zp = 0
wp = 1
pp = Mp
1
0
M=
0
0
0
1
0
0
0
0
0
0
0
0
0
1
In practice, we can let M = I and set
the z term to zero later
42
Projections in OpenGL
43
OpenGL Perspective
glFrustum(xmin,xmax,ymin,ymax,near,far)
44
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
45
OpenGL Orthogonal Viewing
glOrtho(xmin,xmax,ymin,ymax,near,far)
glOrtho(left,right,bottom,top,near,far)
near and far measured from camera
46
Hidden-Surface Removal
47
Z-Buffer Algorithm
Keep track of the smallest depth or z value
for each pixel
Z value is initialized to the farthest distance
Worst-case time complexity is O(n), where
n is the number of polygons
glutInitDisplayMode(GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
48
Culling
Removing all the faces pointing away from
the viewer. For example, rendering n cubes
with culling can filter 3n polygons
glEnable(GL_CULL);
49
Walking Through a Scene – 1/3
void keys(unsigned char key, int x, int y)
{
if(key==‘x’) viewer[0]-=1.0;
if(key==‘X’) viewer[0]+=1.0;
if(key==‘y’) viewer[1]-=1.0;
if(key==‘Y’) viewer[1]+=1.0;
if(key==‘z’) viewer[2]-=1.0;
if(key==‘Z’) viewer[2]+=1.0;
glutPostRedisplay();
}
50
Walking Through a Scene – 2/3
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(theta[0],1.0,0.0,1.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
51
Walking Through a Scene – 3/3
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
52
Parallel-Projection Matrices
Perspective View
Orthographic projection of
distorted object
53
Normalization
Rather than derive a different projection
matrix for each type of projection, we can
convert all projections to orthogonal
projections with the default view volume
This strategy allows us to use standard
transformations in the pipeline and makes
for efficient clipping
54
Pipeline View
modelview
transformation
projection
transformation
perspective
division
4D 3D
nonsingular
clipping
against default cube
projection
3D 2D
55
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
56
Orthogonal Normalization
glOrtho(left,right,bottom,top,near,far)
-1
1
-1
1
-1
normalization find transformation to convert
specified clipping volume to default
Canonical view volume
1
57
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))
P = ST =
2
right left
0
0
0
0
0
2
top bottom
0
0
0
2
near far
0
right left
right left
top bottom
top bottom
far near
far near
1
58
Final Projection
Set z =0
Equivalent to the homogeneous coordinate
transformation
1
0
Morth =
0
0
0
1
0
0
0
0
0
0
0
0
0
1
Hence, general orthogonal projection in 4D is
P = MorthST
59
Oblique Projections
The OpenGL projection functions cannot
produce general parallel projections such as
However if we look at
the example of the cube it
appears that the cube has been sheared
Oblique Projection = Shear + Orthogonal
Projection
60
General Shear
z
tanq
x xp
x p x z cotq
y p y z cot
top view
side view
61
Shear Matrix
xy shear (z values unchanged)
1
0
H(q,) =
0
0
0 cot θ
1 cot φ
0
1
0
0
0
0
0
1
Projection matrix
P = Morth H(q,)
General case:
P = Morth STH(q,)
62
Equivalency
63
Effect on Clipping
The projection matrix P = STH transforms
the original clipping volume to the default
clipping volume
object
top view
DOP
z= 1
DOP
x = -1
far plane
clipping
volume
near plane
x=1
z = -1
distorted object
(projects correctly) 64
Simple Perspective
Consider a simple perspective with the COP at the
origin, the near clipping plane at z = -1, and a 90
degree field of view determined by the planes
x = z, y = z
65
Perspective Matrices
Simple projection matrix in homogeneous
coordinates (z=-1, and the center of
projection is at the origin)
1
0
M=
0
0
0 0
1 0
0 1
0 1
0
0
0
0
Note that this matrix is independent of the far
clipping plane
66
Generalization
1
0
Let N =
0
0
0 0
1 0
0 α
0 1
0
0
β
0
after persepective divison,
the point (x, y, z, 1) goes to
x’’ = -x/z
y’’ = -y/z
z’’ = -(a+b/z)
1
If we apply an orthographic projection
0
along the z-axis to N, we get:
MorthN=
0
And the projection of an arbitrary point p 0
0 0
1 0
0 0
0 1
0
0
0
0
is, p’=MorthNp =
x , after perspective division: xp = -x/z
y
yp = -y/z
0 We get the same result regardless
z of a and b!
67
Picking a and b
If we pick
z max z min
a
z max z min
2z maxz min
b
z max z min
the zmin plane is mapped to z = -1
the zmax plane is mapped to z =1
and the sides are mapped to x = 1, y = 1
Hence the new clipping volume is the default clipping volume
68
Normalization Transformation
distorted object
projects correctly
original
clipping
volume
original object
new clipping
volume
69
Normalization and
Hidden-Surface Removal
Although our selection of the form of the
perspective matrices may appear somewhat
arbitrary, it was chosen so that if z1 > z2 in the
original clipping volume then the for the
transformed points z1’ > z2’
Thus we hidden surface removal works if we
first apply the normalization transformation
However, the formula z’’ = -(a+b/z) implies that
the distances are distorted by the normalization
which can cause numerical problems especially
if the near distance is small
70
OpenGL Perspective
glFrustum allows for an unsymmetric
viewing frustum (although
gluPerspective does not)
71
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
72
Normalization Details
Shear the point of ((xmin+xmax)/2,(ymin+ymax)/2, zmax) to (0, 0, zmax)
z
1 xmin xmax
z=z
1 ymin ymax
H (q , ) H cot (
2 zmax
), cot (
2 zmax
)
max
So the resulting frustum is described
by the planes:
x
xmax xmin
z
2 zmax
y ymin
y max
z
2 zmax
z zmin
z zmax
z=zmin
q
x
Next, scale the volume so that
x=z, y= z, so the scaling matrix is:
S(2zmax/(xmax-xmin),2zmax/(ymax-ymin),1)
Finally, we make use of the previous N.
73
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
74
Projections and Shadows
Idea: put the projection
plane behind the object!
For example, in this figure,
assume (xl, yl, zl) is a light
source, then we can set the
projection plane on the
plane of y=0
75
Implementing Shadows – 1/2
Move the light source to the origin
by a translation:T(-xl, -yl, -zl), then we
could use a simple perspective projection
through the origin, the projection matrix is
1
0
M 0
0
0
1
0
1
yl
0 0
0 0
1 0
0 0
Finally we translate everything back.
76
Implementing Shadows – 2/2
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(xl, yl, zl);
glMultMatrixf(m);
glTranslate(-xl, -yl, -zl);
glColor3fv(shadow_color);
glBegin(GL_POLYGON);
. /* draw the polygon again */
.
glEnd();
glPopMatrix(); /* restore state */
This works if objects cannot cast shadows on other objects!
77
GLfloat m[16];
/* shadow projection matrix */
For(i=0;i<15;i++) m[i]=0.0;
m[0]=m[5]=m[10]=1.0;
m[7]=-1.0/yl;
glColor3fv(polygon_color);
glBegin(GL_POLYGON);
. /* draw polygon normally */
.
glEnd();
Summary and Notes
Classical Viewing
Parallel Projection versus Perspective
Projection
OpenGL Projections
Projection Normalization
Shadows
78