
UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide
Intro to OpenGL • OpenGL operates as an infinite loop • Put things in the scene (points, colored lines, textured polys) • Describe the camera (location, orientation, field of view) • Listen for keyboard/mouse events • Render – draw the scene
Intro to OpenGL • OpenGL has a “state” • There are a lot of ways your OpenGL program can be configured • The current configuration is stored in OpenGL’s state • Be aware that OpenGL commands affect the program’s state rather than redirect its logical execution
The Graphics Pipeline and State Machines • Graphic system as a state machine, a black box that contains a finite-state machine. • Machine may change the state, • Or cause to produce a visible output. • An example state : • We set a color, • That remains the present coloruntil it is changed.
The OpenGL Interface MS windows extension MS windowssystem GLX X window system GLUT :Graphics utility toolkit (glut~) - interface with the windowsystem- #include <GL/glut.h> OpenGL application program Frame buffer (video card) GLU :Graphics utility library (glu~) - Contain code for common objects such as spheres, teapot, etc.- #include <GL/glu.h> GL :OpenGL functions(gl~)- #include <GL/gl.h>
Software • Hide the details • User should not need to worry about how graphics are displayed on monitor • User doesn’t need to know about how a line is converted into pixels and drawn on screen (hardware dependent) • User doesn’t need to rebuild the basic tools of a 3D scene • Virtual camera, light sources, polygon drawing • OpenGL does this for you…
Software • Hide the details • User doesn’t need to know how to read the data coming from the mouse • User doesn’t need to know how to read the keystrokes • OpenGL Utility Toolkit (GLUT) does this for you…
OpenGL Design Goals • SGI’s design goals for OpenGL: • High-performance (hardware-accelerated) graphics API • Some hardware independence • Natural, terse API with some built-in extensibility • OpenGL has become a standard (competing with DirectX) because: • It doesn’t try to do too much • Only renders the image, doesn’t manage windows, etc. • No high-level animation, modeling, sound (!), etc. • It does enough • Useful rendering effects + high performance • Open source and promoted by SGI (& Microsoft, half-heartedly)
The Big Picture • Who gets control of the main control loop? • GLUT – the code that controls the window and refresh • Must be responsive to windowing system and OS • OpenGL – the code that controls what is drawn • Must be responsive to the program that specifies where objects are located. If something moves, I want to see it.
Intro to OpenGL • OpenGL uses matrices • Matrix describes camera type • Matrix describes current configuration of the 3D space • Explanation…
Intro to OpenGL • OpenGL coordinate system • right-handed • Hold out your right hand and hold your thumb, index, and middle fingers orthogonal to one another • Call your thumb the x-axis, index = y-axis, and middle = axis • This is the OpenGL coordinate system • The camera defaults to look down negative z-axis
Intro to OpenGL • So… • X-axis = thumb = 1, 0, 0 • Y-axis = index = 0, 1, 0 • Z-axis = middle = 0, 0, 1 • Camera defaults to look down negative z-axis • Let’s say we want it to look down x-axis
Intro to OpenGL • Coordinate system transformation so camera looks down x-axis • If x-axis negative z-axis • x -z • y y • z x
Intro to OpenGL • The a i matrix defines the transformation • Why store the transformation matrix and not the final desired matrix? =
Intro to OpenGL • The transformation will be applied to many points • If the following transformation moves the axes • The same transformation moves vertices • Example: (1, 1, -1) (-1, 1, -1) =
Intro to OpenGL • This important matrix is stored as the MODELVIEW matrix • The MODELVIEW matrix is so important OpenGL maintains a stack of these matrices • You have control of this stack with the glPushMatrix and glPopMatrix commands • (The matrix is actually 4x4, but we will study the details to understand why in the future) • Note OpenGL preserves a similar matrix to describe the camera type and this is called the PROJECTION_MATRIX
Introduction to OpenGL • What’s hard about this assignment? • Managing the MODELVIEW stack • We’ll use: • glLoadIdentity(); • glTranslatef(GLfloat x, y, z); • glRotatef(GLfloat degrees, x, y, z); • glScalef (GLfloat x, y, z); • glPushMatrix(), glPopMatrix();
Modeling Transformations • glTranslate (x, y, z) • Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values • glRotate (theta, x, y, z) • Post-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) • Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes
Modeling Transformations • It is important that you understand the order in which OpenGL concatenates matrices • glMatrixMode (MODELVIEW); • glLoadIdentity(); • glMultMatrix(N); • glMultMatrix(M); • glMultMatrix(L); • glBegin(POINTS); • glVertex3f(v); • glEnd(); Modelview matrix successively contains: I(dentity), N, NM, NML The transformed vertex is: NMLv = N(M(Lv))
Manipulating Matrix Stacks • Observation: Certain model transformations are shared among many models • We want to avoid continuously reloading the same sequence of transformations • glPushMatrix ( ) • push all matrices in current stack down one level and copy topmost matrix of stack • glPopMatrix ( ) • 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 ( ); }
Matrix Manipulation – Example Grand, fixed coordinate system 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 ( ); R RT RTv Global – Bottom Up Start Rot Trans
Matrix Manipulation – Example Local coordinate system 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 ( ); R RT RTv Local – Top Down Start Rot Trans
OpenGL: Conventions • Functions in OpenGL start with gl • Most functions just gl (e.g., glColor()) • Functions starting with glu are utility functions (e.g., gluLookAt()) • Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)
OpenGL: Conventions • Function names indicate argument type and number • Functions ending with f take floats • Functions ending with i take ints • Functions ending with b take bytes • Functions ending with ub take unsigned bytes • Functions that end with v take an array. • Examples • glColor3f() takes 3 floats • glColor4fv() takes an array of 4 floats
OpenGL: Conventions • Variables written in CAPITAL letters • Example: GLUT_SINGLE, GLUT_RGB • usually constants • use the bitwise or command (x | y) to combine constants
OpenGL: Simple Use • Open a window and attach OpenGL to it • Set projection parameters (e.g., field of view) • Setup lighting, if any • Main rendering loop • Set camera pose with gluLookAt() • Camera position specified in world coordinates • Render polygons of model • Simplest case: vertices of polygons in world coordinates
OpenGL: Simple Use • Open a window and attach OpenGL to it • glutCreateWindow()
OpenGL: Perspective Projection • Set projection parameters (e.g., field of view) • Typically, we use a perspective projection • Distant objects appear smaller than near objects • Vanishing point at center of screen • Defined by a view frustum (draw it) • Other projections: orthographic, isometric
Setting up Camera • glMatrixMode(GL_MODELVIEW); • glLoadIdentity(); • gluLookAt( eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); • eye[XYZ]: camera position in world coordinates • look[XYZ]: a point centered in camera’s view • up[XYZ]: a vector defining the camera’s vertical • Creates a matrix that transforms points in world coordinates to camera coordinates • Camera at origin • Looking down -Z axis • Up vector aligned with Y axis
OpenGL: Perspective Projection • In OpenGL: • Projections implemented by projection matrix • gluPerspective() creates a perspective projection matrix: glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); • Parameters to gluPerspective(): • vfov: vertical field of view • aspect: window width/height • near, far: distance to near & far clipping planes
OpenGL: Lighting • Setup lighting, if any • Simplest option: change the current color between polygons or vertices • glColor() sets the current color • Or OpenGL provides a simple lighting model: • Set parameters for light(s) • Intensity, position, direction & falloff (if applicable) • Set material parameters to describe how light reflects from the surface • Won’t go into details now; check the red book if interested
OpenGL: Specifying Geometry • Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() • A simple example: telling GL to render a triangle glBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); • Usage: glBegin(geomtype) where geomtype is: • Points, lines, polygons, triangles, quadrilaterals, etc...
Primitive Types • GL_POINTS • GL_LINE • {S | _STRIP | _LOOP} • GL_TRIANGLE • {S | _STRIP | _FAN} • GL_QUAD • {S | _STRIP} • GL_POLYGON
p0 p7 p1 p6 p2 p5 p3 p4 Points in OpenGL glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Lines in OpenGL (1/3) • Line Segments glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Lines in OpenGL (2/3) • Polylines – Line Strip glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Lines in OpenGL (3/3) • Polylines – Line Loop glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
Polygon Basics (1/2) • Definition • Object that is closed as a line loop, but that has an interior • Simple Polygon • No pair of edges of a polygon cross each other Simple Nonsimple
Polygon Basics (2/2) • Convexity • If all points on the line segment between any two points inside the object, or on its boundary, are inside the object p1 p2 convex concave Convex Objects
Polygon Rendering Options • Rendered as points, lines, or filled • Front and back faces can be rendered separately using glPolygonMode(face, mode)face : GL_FRONT, GL_BACK or GL_FRONT_AND_BACKmode : GL_POINT, GL_LINE or GL_FILL. • glPolygonStipple( ) overlays a MacPaint-style overlayonthe polygon • glEdgeFlag(bool) specifies polygon edges that can be drawn in line modebool : GL_FALSE orGL_TRUE • Normal vectors: normalized is better, but glEnable(GL_NORMALIZE) will guarantee it
OpenGL: Specifying Normals • Calling glNormal() sets the normal vectorfor the following vertices, till next glNormal() • So flat-shaded lighting requires:glShadeModel(GL_FLAT);glNormal3f(Nx, Ny, Nz);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2); • While smooth-shaded requires: • (Of course, lighting requires additional setup…) • glShadeModel(GL_SMOOTH);glNormal3f(N0x, N0y, N0z); glVertex3fv(v0); • glNormal3f(N1x, N1y, N1z); glVertex3fv(v1); • glNormal3f(N2x, N2y, N2z); glVertex3fv(v2);
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 • Use exact coordinates for closing loops E BAD OK B B D A A C C
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (1/6) • Polygon glBegin(GL_POLYGON); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (2/6) • Quadrilaterals glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (3/6) • Quadstrip glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p6); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (4/6) • Triangles glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (5/6) • Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p4); glEnd();
p0 p1 p7 p6 p2 p5 p3 p4 Polygons in OpenGL (6/6) • Triangle Fan glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd();