David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
David Luebke1/5/2016 Recap: OpenGL l OpenGL provides an interface and implementation for interactive rendering. l It has become a standard because: –A standard was badly needed –OpenGL is pretty good –SGI promoted it and Microsoft (sorta) bought in l OpenGl is particularly tuned to hardware- accelerated transformation, lighting, texturing, and Z-buffering
David Luebke1/5/2016 Recap: OpenGL Conventions Functions in OpenGL start with gl (or glu ) l 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 Luebke1/5/2016 Recap: Miscellaneous OpenGL l 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: v0v0 v2v2 v1v1 v3v3 v4v4 v5v5
David Luebke1/5/2016 Miscellaneous OpenGL The GL_TRIANGLE_FAN primitive is another way to reduce vertex redundancy: v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6
David Luebke1/5/2016 Recap: OpenGL Lighting OpenGL binds our Phong lighting coefficients ( k a, k d, k s, n shiny ) into materials Calling glMaterialfv() sets a single attribute of the current material l Example: float green[] = {0, 1, 0, 1}; float white[] = {1, 1, 1, 1}; glMaterialfv(GL_FRONT, GL_DIFFUSE, green); glMaterialfv(GL_FRONT, GL_SPECULAR, white);
David Luebke1/5/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 4 th coordinate in l_pos is 0.0 for a directional light, 1.0 for a point light)
Errata: OpenGL Lighting l Recall the Phong lighting model: l OpenGL modifies this slightly: –Each light contributes separately to ambient term –Lights have a separate intensity for specular reflection (Why might this be useful?)
David Luebke1/5/2016 OpenGL: Lighting l Don’t forget to enable lighting and each light: glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); l 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 Luebke1/5/2016 OpenGL: Display Lists l 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(); l …which can be rendered with a single call: glCallList(1);
David Luebke1/5/2016 OpenGL: Display Lists l Display lists can contain: –Geometry –Color, material, and texture specifications –Matrix transforms (up shortly) –Other display lists! l 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 Luebke1/5/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 Luebke1/5/2016 OpenGL: Modeling Transforms l Some OpenGL commands generate transformation matrices: glTranslatef(Tx, Ty, Tz); glRotatef(angleDegrees, Ax, Ay, Az); glScalef(Sx, Sy, Sz); l The resulting matrix concatenates to the right of the current matrix
David Luebke1/5/2016 OpenGL: Modeling Transforms l Example: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(…); glRotatef(…); l 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 Luebke1/5/2016 OpenGL: Viewing Transforms l 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 Luebke1/5/2016 OpenGL: Projection Transforms l 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 Luebke1/5/2016 The Scene Graph l 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? n Compile geometry (say a car tire, centered about the origin) into a display list n Set up matrices: viewing transform + modeling transform(s) to put origin at front left corner of car n Call display list for tire n Set up matrices, this time putting origin at front right of car n Call display list for tire [Etc…] –Why is this inefficient?
David Luebke1/5/2016 The Scene Graph l Answer: because many objects in a scene typically share multiple transformations l The scene graph captures transformations and object-object relationships in a DAG: Robot BodyHead ArmTrunkLegEyeMouth Objects Instancing (i.e, a matrix) Legend World
David Luebke1/5/2016 The Scene Graph l Traverse the scene graph in depth-first order, concatenating and undoing transforms: –For example, to render the robot: n Apply robot head matrix u Apply head mouth matrix –Render mouth u Un-apply head mouth matrix u Apply head left eye matrix –Render eye u Un-apply head left eye matrix u Apply head right eye matrix –Render eye u Un-apply head right eye matrix n Un-apply robot head matrix n Apply robot body matrix How should we implement this “un-apply” business?
David Luebke1/5/2016 The Scene Graph in OpenGL l OpenGL maintains a matrix stack of modeling and viewing transformations: ArmTrunk Leg EyeMouth HeadBody Robot Foot Matrix Stack Visited Unvisited Active
David Luebke1/5/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 Luebke1/5/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 Luebke1/5/2016 Coming Up: l Animation: smooth (flicker-free) motion using double buffering l More OpenGL tricks –Backface culling –Gouraud shading –Computing vertex normals