Download presentation source

Download Report

Transcript Download presentation source

CS 551 / 645:
Introductory Computer Graphics
David Luebke
[email protected]
http://www.cs.virginia.edu/~cs551
David Luebke
7/27/2016
Recap: OpenGL


OpenGL provides an interface and
implementation for interactive rendering.
It has become a standard because:
– A standard was badly needed
– OpenGL is pretty good
– SGI promoted it and Microsoft (sorta) bought in

OpenGl is particularly tuned to hardwareaccelerated transformation, lighting, texturing,
and Z-buffering
David Luebke
7/27/2016
Recap: OpenGL Conventions

Functions in OpenGL start with gl (or glu)

Function names indicate argument type/#
– E.g., glColor3f() vs glColor3fv() vs
glColor4ub()

Geometry is specified as a list of vertices
between glBegin() and glEnd() calls:
glBegin(GL_POLYGON);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
glEnd();
David Luebke
7/27/2016
Recap: Miscellaneous OpenGL



The vertices of the front side of a polygon are
ordered counterclockwise
You can draw multiple triangles between
glBegin(GL_TRIANGLES) and glEnd()
The GL_TRIANGLE_STRIP primitive reduces
redundancy by sharing vertices:
v2
v0
v4
v5
v1
David Luebke
v3
7/27/2016
Miscellaneous OpenGL

The GL_TRIANGLE_FAN primitive is another
way to reduce vertex redundancy:
v4
v3
v5
v0
v2
v6
v1
David Luebke
7/27/2016
Recap: OpenGL Lighting



OpenGL binds our Phong lighting coefficients
(ka, kd, ks, nshiny) into materials
Calling glMaterialfv() sets a single
attribute of the current material
Example:
float green[] = {0, 1,
float white[] = {1, 1,
glMaterialfv(GL_FRONT,
glMaterialfv(GL_FRONT,
David Luebke
0, 1};
1, 1};
GL_DIFFUSE, green);
GL_SPECULAR, white);
7/27/2016
Recap: OpenGL Lighting

OpenGL supports at least 8 lights, with
parameters set by the glLight() call:
float l_amb [] = {.1, .1, .1, 1.0};
float l_diff[] = {1, 0, 0, 1};
float l_spec[] = {1, 1, 1, 1};
float l_pos[] = {10, 100, 30, 0};
glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l_diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);
glLightfv(GL_LIGHT0, GL_POSITION, l_pos);
(the 4th coordinate in l_pos is 0.0 for a directional
light, 1.0 for a point light)
David Luebke
7/27/2016
Errata: OpenGL Lighting

Recall the Phong lighting model:
I total  ka I ambient 

#lights


i 1
 

nshiny


ˆ
ˆ
ˆ
ˆ
I  kd N  L  k s V  R


i
OpenGL modifies this slightly:
I total 
#lights

i 1




nshiny
i
ˆ
ˆ
ˆ
ˆ
k I  I kd N  L  I s k s V  R
i
a a
i
d
– Each light contributes separately to ambient term
– Lights have a separate intensity for specular
reflection (Why might this be useful?)
OpenGL: Lighting

Don’t forget to enable lighting and each light:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

Can set the lighting model too:
– Intensity of the ambient light source
– Whether to treat eye point as infinitely far away
– Whether to perform lighting calculations for both
sides of polygons
– All these things have reasonable default values
– man glLightModel for details
David Luebke
7/27/2016
OpenGL: Display Lists

OpenGL can “compile” a series of rendering
commands into a display list...
glNewList(1, GL_COMPILE);
glBegin(GL_TRIANGLES);
glVertex3fv(v0);
/* draw lots of triangles… */
glVertex3fv(v2);
glEnd();
glEndList();

…which can be rendered with a single call:
glCallList(1);
David Luebke
7/27/2016
OpenGL: Display Lists

Display lists can contain:
–
–
–
–

Geometry
Color, material, and texture specifications
Matrix transforms (up shortly)
Other display lists!
Display lists are not only handy, they usually
increase performance
– Why might OpenGL be able to render a series of
commands faster if they have been compiled into
a display list?
David Luebke
7/27/2016
OpenGL: Matrix Manipulation

OpenGL keeps two matrices that vertices are
multiplied by upon calling glVertex()
– The modelview matrix combines all modeling
transforms and the viewing transform
– The projection matrix performs the projection
(usually a perspective projection)
– Various commands affect the current matrix
– You need to specify which matrix is current:

E.g., glMatrixMode(GL_MODELVIEW) or
glMatrixMode(GL_PROJECTION)
– glLoadIdentity() replaces the contents of the
current matrix with the identity matrix
David Luebke
7/27/2016
OpenGL: Modeling Transforms

Some OpenGL commands generate
transformation matrices:
glTranslatef(Tx, Ty, Tz);
glRotatef(angleDegrees, Ax, Ay, Az);
glScalef(Sx, Sy, Sz);

The resulting matrix concatenates to the right
of the current matrix
David Luebke
7/27/2016
OpenGL: Modeling Transforms

Example:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(…);
glRotatef(…);

Result: the modelview matrix is set to:
I • T • R == T • R
which then multiplies all following vertices
– Thus transformations appearing last in the
program have the first effect on the geometry
David Luebke
7/27/2016
OpenGL: Viewing Transforms


Viewing transforms are treated the same way
Ex: gluLookAt() computes a lookat matrix
and concatenates it with the current matrix:
gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
upX, upY, upZ);
– Again, this matrix postmultiplies the current matrix
– Should gluLookAt() be called first or last?
David Luebke
7/27/2016
OpenGL: Projection Transforms

The projection matrix is generally used for
the perspective projection matrix
– Why do you suppose OpenGL separates the
modelview and projection matrices?

gluPerspective() creates a projection
matrix similarly to the call in assignment 6
gluPerspective(double FOVy, double aspect,
double near, double far);
– FOVy: field of view (°) in the y vertical (y) direction
aspect: viewport width (y) divided by height (x)
David Luebke
7/27/2016
The Scene Graph

Recall the concept of instancing, or using the
same geometry for multiple objects
– Example: 4 wheels on car
– How might we use display lists for instancing?





Compile geometry (say a car tire, centered about the
origin) into a display list
Set up matrices: viewing transform + modeling
transform(s) to put origin at front left corner of car
Call display list for tire
Set up matrices, this time putting origin at front right of car
Call display list for tire [Etc…]
– Why is this inefficient?
David Luebke
7/27/2016
The Scene Graph


Answer: because many objects in a scene
typically share multiple transformations
The scene graph captures transformations
and object-object relationships in a DAG:
World
Objects
Robot
Head
Mouth
David Luebke
Instancing
(i.e, a matrix)
Body
Eye
Leg
Trunk
Arm
Legend
7/27/2016
The Scene Graph

Traverse the scene graph in depth-first order,
concatenating and undoing transforms:
– For example, to render the robot:
 Apply robot head matrix
 Apply head mouth matrix
– Render mouth
 Un-apply head mouth matrix
 Apply head left eye matrix
– Render eye
 Un-apply head left eye matrix
 Apply head right eye matrix
– Render eye
 Un-apply head right eye matrix
 Un-apply robot head matrix
 Apply robot body matrix
David Luebke
How should we
implement this
“un-apply” business?
7/27/2016
The Scene Graph in OpenGL

OpenGL maintains a matrix stack of
modeling and viewing transformations:
Robot
Visited
Head
Body
Unvisited
Active
Matrix
Stack
Mouth
Eye
Leg
Trunk
Arm
Foot
David Luebke
7/27/2016
OpenGL: The Matrix Stack

The user can save the current transformation
matrix by pushing it onto the stack with
glPushMatrix()

The user can later restore the most recently
pushed matrix with glPopMatrix()

These commands really only make sense
when in GL_MODELVIEW matrix mode
David Luebke
7/27/2016
OpenGL: Matrix Stack Example
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(…);
// save translation matrix:
glPushMatrix();
glRotatef(…);
// render something translated & rotated:
glCallList(foo);
// restore pushed matrix, undoing rotation:
glPopMatrix();
// render something else, no rotation:
glCallList(bar);
David Luebke
7/27/2016
Coming Up:


Animation: smooth (flicker-free) motion using
double buffering
More OpenGL tricks
– Backface culling
– Gouraud shading
– Computing vertex normals
David Luebke
7/27/2016