Transcript PowerPoint

More OpenGL
CS 551/645 Introduction to
Computer Graphics
What’s New?


Everyone getting class email?
SGI accounts at Small Hall working
–


2
sgi-5.unixlab is misconfigured
Program 1 deadline extended until Monday,
September 18th, 2000 at midnight
TA Office Hours: Tues (3:30 - 5:00) W (3:00 5:00) in Small Hall
Where are we in the book?

3
Read OpenGL Guide, Chapters 1 - 3
 Read Foley (CGP&P) Chapter 3
Unix Commands - Basics




4
mkdir foo - make a directory
rmdir foo - remove a directory
cp foo bar - create a new file named bar
that is a copy of foo
mv foo bar - create a new file named bar
that is a copy of foo and delete foo
Unix Commands - Basics





5
ls - display the contents of the directory
ls -l - display the ‘l’ong version of the
directory
cd foo - move from the current directory to
the directory named foo
cd .. - move up a directory
cd ../foo - move up and over to the foo
directory
Unix Commands - Basics



6
foo | bar - called ‘pipe’ a way to sequence
commands. The output from foo is used as
input by bar
foo > bar - called a redirect. The output
from foo is redirected to a file named bar.
Useful for capturing output streams to a file
foo < bar - the contents of file bar are used
as input to foo
Unix Commands - Basics




7
man foo - print out the manual pages for the
command, foo
tar - a way to convert a directory structure into
a flat file
tar - a way to unpack a flat file into a directory
structure
tar | tar - a way to copy a directory
structure from one place to another
Unix Commands - Basics


8
gzip - a way to compress foo. A new file
named foo.gz will be created and foo will be
deleted
gunzip foo.gz - a way to uncompress foo.
Setup Hints

Things complicated because of unification
between blue.unix and sgi.unixlab
–
–
–
9
.xinitrc, .profile, .login, .kshrc may all be incorrectly
configured for another kind of computer
Unfortunately there isn’t a quick fix
One solution is to create a subdir and move all the
dot files to the subdir (so they won’t be executed
when you log on the SGI. This will screw up other
logins, though.
Makefile



10
-I refers to the include path. Compiler looks in
all these directories for .h files
-L refers to the library path. Compiler looks in
all these directories for the required libraries
-l refers to the required libraries
Creating Geometry

All geometry composed of vertices (points)
‘Primitive type’ defines the shape they describe

Example vertices: glVertex (x, y, z, w)

–
–
–
11
glVertex2d - z coordinate is set to 0.0
glVertex3d - w coordinate is set to 1.0
glVertex4d - all coordinates specified (rarely done)
Primitive Types





12
GL_POINTS
GL_LINE{S | _STRIP | _LOOP}
GL_TRIANGLE{S | _STRIP | _FAN}
GL_QUAD{S | _STRIP}
GL_POLYGON
OpenGL Programming Guide, page 45
GL_POLYGON


13
List of vertices defines polygon edges
Polygon must be convex
Non-planar Polygons



14
Imagine polygon with non-planar vertices
Some perspectives will be rendered as
concave polygons
These concave polygons may not rasterize
correctly
Generating Primitives



Primitive defined within glBegin() and glEnd()
Very few GL commands can be executed
within these two GL calls
Any amount of computation can be performed
glBegin (GL_LINE_LOOP);
for (j=0; j<10; j++) {
angle = 2*M_PI*j/10;
glVertex2f (cos(angle), sin(angle);
}
glEnd();
15
glColor()


Color is a GL state variable
Vertices are colored according to current value
glBegin (GL_POINTS)
glColor3f (0.0,
glColor3f (1.0,
glVertex ();
glColor3f (1.0,
glColor3f (0.0,
glVertex ();
glVertex ();
glEnd();
16
1.0, 0.0);
0.0, 0.0);
1.0, 0.0);
0.0, 1.0);
Polygon Rendering Options





17
Rendered as points, lines, or filled
Front and back faces can be rendered
separately using glPolygonMode( )
glPolygonStipple( ) overlays a MacPaint-style
overlay on the polygon
glEdgeFlag specifies polygon edges that can
be drawn in line mode
Normal vectors: normalized is better, but
glEnable (GL_NORMALIZE) will guarantee it
Polygonalization Hints





Keep orientations (windings) consistent
Best to use triangles (guaranteed planar)
Keep polygon number to minimum
Put more polygons on silhouettes
Avoid T-intersections to avoid cracks
BAD
B
A
C

18
OK
B
A
C
Use exact say coordinates for closing loops
Viewing with GL

Camera Analogy
–
Set up your tripod and point the camera

–
Move objects from world into camera scene

–
Projection Transformation
Determine size of photograph

19
Modeling Transformation
Choose a camera lens and adjust zoom

–
Viewing Transformation
Viewport Transformation
Useful Transformation Commands

glMatrixMode (GL_MODELVIEW,
GL_PROJECTION, or GL_TEXTURE)
–

glLoadIdentity ( )
–
20
Specify which matrix subsequent transformation
commands will effect
Sets the currently modified matrix to identity
Useful Transformation Commands

glLoadMatrix ( )
–

glMultMatrix ( )
–

21
explicitly load the parameter into the currently
modified matrix
explicitly multiply the currently modified matrix by
the parameter matrix and store the results in the
modified matrix
Argument is vector of 16 values. Vector
specifies matrix column by column, left to right.
Useful Transformation Commands

Warning about declaring matrices in C
–
–
22
matrix m[4][4] can be accesses as m[ i ] [ j ], but this
accesses the ith column and the jth row of the
OpenGL transformation matrix
Because C convention is opposite the OpenGL
convention, m[16] is recommended declaration
Modeling Transformations

glTranslate (x, y, z)
–

glRotate (theta, x, y, z)
–
23
Multiplies the current matrix by a matrix that moves
the object by the given x-, y-, and z-values
Multiplies the current matrix by a matrix that rotates
the object in a counterclockwise direction about the
ray from the origin through the point (x, y, z)
Modeling Transformations

glScale (x, y, z)
–
24
Multiplies the current matrix by a matrix that
stretches, shrinks, or reflects an object along the
axes.
Viewing Transformations



25
Default camera: y is up, look down -z axis
glTranslate and glRotate change view
gluLookAt ( ) uses these two commands to
change the view
Projection Transformations

First, execute
–
–

glFrustum (…)
–
–

defines position of viewing frustum and near and far
clipping planes
note, frustum need not be symmetric
gluPerspective (…)
–
26
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
specifies frustum, but limited to symmetric ones
Viewing Transformations

First, execute
–
–

glOrtho (…)
–

defines position of viewing frustum and near and far
clipping planes
gluOrtho2D (…)
–
27
glLoadMatrixMode (GL_ORTHO);
glLoadIdentity( );
projects a 2D image to a 2D screen
Viewport Transformation

glViewport (x, y, width, height)
–

glDepthRange ( )
–
–
28
defines the lower-left corner of the viewport and the
size of the viewport
depth, distance from camera, is stored with the (x,
y) position of each rendered vertex
this function clamps the range of depth values
stored in the depth buffer
Troubleshooting the ‘Black Screen’


Object color different from background?
Object within the (near, far) clipping planes?
–

Don’t make near clipping plane too small
–


29
Try (0.001, 100000.0) to check
depth buffer accuracy will degrade
Camera pointing towards the object?
Double-check origins for rotations
Manipulating Matrix Stacks



Observation: Certain model transformations
are shared among many models
We want to avoid continuously reloading the
same sequence of transformations
glPushMatrix ( )
–

glPopMatrix ( )
–
30
push all matrices in current stack down one level
and copy topmost matrix of stack
pop the top matrix off the stack
Matrix Manipulation - Example

Drawing a car with wheels and lugnuts
draw_wheel( );
for (j=0; j<5; j++) {
glPushMatrix ();
glRotatef(72.0*j, 0.0, 0.0, 1.0);
glTranslatef (3.0, 0.0, 0.0);
draw_bolt ( );
glPopMatrix ( );
31
Add Matrices to Projection Stack

Additional Clipping Planes
–
glClipPlane (ID, eqn)



–
glEnable ( GL_CLIP_PLANEi )

–
Enables clipping plane
glDisable ( GL_CLIP_PLANEi)

32
Six additional clipping planes can be added
ID (0…5) corresponds to each plane
eqn points to 4 parameters of plane: Ax + By + Cz + D = 0
Disables clipping plane
Reversing Transformations

Where in 3-space is the user’s mouse point?
–
–

gluUnProject (winx, winy, winz, modelMatrix,
projMatrix, viewport, worldx, worldy, worldz);
–
33
We’ve used transformations to transform 3D to 2D
How about 2D to 3D?
You provide everything but world coordinates
gluUnproject ( )



34
Computes the line from center of projection to
mouse point
Then computes the x-, y-, z-value of the line at
some ‘depth’ in the world
If depth is set to near clipping plane, the
returned (x, y, z) is a point on the clipping plane