Computer graphics MN1 http://www.opengl.org Todays lecture What is OpenGL? How do I use it? Rendering pipeline Points, vertices, lines,, polygons Matrices and transformations Lighting and shading Code examples Open OpenGL Open Graphics raphics Libraryibrary Graphics API similar to DirectX,, Java3D OpenGL 2.0 released September 2004 Delivered with UNIX, Win9x/2000/Me/Nt/Xp Xp, Mac OS Utilizes the window system and event handling of the OS Often hardware supported in graphics cards Language bindings to C, C++, Java, Fortran, Python, Perl, Toolkits GLU (OpenGL( Utility Library) Support for NURBS, Quadrics,, etc. Delivered with OpenGL GLUT (OpenGL( Utility Toolkit) Simplifies window handling User interface functions Can be downloaded from opengl.org OpenGL Extensions allows new hardware innovations to be accessible through OpenGL MESA http://www.mesa3d.org/ Mesa is a 3-D 3 D graphics library with an API which is very similar to that of OpenGL. Mesa is not a licensed implementation of OpenGL and has not been tested by the OpenGL conformance tests. You can consider Mesa to be OpenGL ; you can use OpenGL documentation, for example. Goals for OpenGL Industry-wide acceptance Consistent implementations Innovative implementations Long life High quality 1
Why/Why not OpenGL + Supporting OS + Open (kind of) + Free versions (Mesa) + Widely used - Can be hard to use in structured programming. State machine. ( OpenGL is the assembler of computer graphics ) Application program Pipeline Transform Project Clip Pixel operations Frame buffer State machine OpenGL is designed as a state machine Input and outputs (geometric( primitives, bitmaps) The state machine converts the inputs to an output image The resulting image depends on the current state Example: colors, shading, texture,, Primitives in OpenGL OpenGL is using a right hand system Vertices are defined using glvertex*() where * can be 2f, 2d, 3f, 3d,... Ex.: void glvertex2f( GLfloat vx, GLfloat vy ); GLfloat typedefined as float Primitive defining statements all starts with glbegin(<primitive_type>) and ends with glend() Colors, normals, texture coordinates can be specified for each vertex Primitives in OpenGL Some primitive types GL_POINTS GL_LINES GL_LINESTRIP GL_TRIANGLES GL_QUADS GL_POLYGON 2
Example 1 glbegin(gl_line_loop); glvertex2f(-0.5,-0.5); // 1 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5, 0.5); // 3 glvertex2f(-0.5, 0.5); // 4 4 1 3 2 Example 2 Example 3 4 5 glbegin(gl_line_strip); glvertex2f(-0.5,-0.5); // 1 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5, 0.5); // 3 glvertex2f(-0.5, 0.5); // 4 4 1 3 glbegin(gl_triangles); glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(-0.5, 1.25, 0.0); // 4 glvertex3f(0.5, 1.25, 0.0); // 5 2 glvertex3f(0.25, 0.75, 0.0); // 6 6 3 1 2 3
Example 3 Color glbegin(gl_triangle_strip); glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(0.5, 0.75, 0.0); // 4 3 4 glcolor3f(1.0,0.0,0.0); // red glbegin(gl_triangles); 1 2 glbegin(gl_triangles); glcolor3f(); glcolor3f(); Color per vertex Normals glbegin(gl_triangles); glcolor3f(); glnormal3f(); glcolor3f(); glnormal3f(); Transformations OpenGL provides matrix stacks Two stacks: GL_MODELVIEW GL_PROJECTION Have to select which transformation matrix to modify: glmatrixmode( ( mode ) The current transformation matrix is the product of the above The matrices are 4x4 Transformations To set the matrix we can use or glloadmatrixf( GLfloat *ptr ); Rotation, translation, and scaling glrotatef(angle, vx,, vy, vz); gltranslatef(dx,, dy, dz); glscalef(sx,, sy, sz); 4
Example: : Rotation about a line glmatrixmode(gl_modelview); // identity matrix gltranslatef(4.0, 5.0, 6.0); glrotatef(45.0, 1.0, 2.0, 3.0); gltranslatef(-4.0, -5.0, -6.0); C=T(4.0,5.0,6.0) R(45,1.0,2.0,3.0) T(-4.0,-5.0,-6.0) q=cp The transformation specified last is the one applied first Camera model Position Direction (look at) View up vector glulookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); Projections Orthographic projection glortho(left, right, bottom, top, near, far); Projections Perspective projection glfrustum(xmin, xmax, ymin, ymax, near, far); gluperspective(fovy,aspect,near,far); Example glmatrixmode( GL_PROJECTION ); gluperspective(65.0, 1.0, 1.0, 20.0 ); glmatrixmode (GL_MODELVIEW ); glulookat(1,-4,-4,0,0,0,0,1,0); Callback functions Used for input and interaction The user submits a pointer to a function that OpenGL should call when the corresponding event occurs GLUT provides an easy-to-use interface 5
Callback functions glutmousefunc(function); // click glutmotionfunc(function); // drag glutreshapefunc(function); // window resize glutkeyboardfunc(function); // keyboard glutidlefunc(function); // animation glutdisplayfunc(function); // draw primitives A very simple program (program1.cpp) #ifdef WIN32 #include <GL/glaux.h> #endif #include <GL/gl.h> #include <GL/glut.h> using namespace std; // Title. const char* TITLE = "Program 1"; void initglut( int &argc, char **argv) glutinit( &argc,argv ); glutinitdisplaymode( GLUT_SINGLE GLUT_RGB ); glutinitwindowsize( 700, 700 ); glutinitwindowposition( 100, 100 ); glutcreatewindow( TITLE ); void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glmatrixmode( GL_PROJECTION ); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0 ); glmatrixmode (GL_MODELVIEW ); void draw() float point2[2] = 0.5, 0.75; glclear( GL_COLOR_BUFFER_BIT ); glcolor3f( 1.0, 1.0, 1.0 ); glbegin( GL_POLYGON ); glvertex2f( 0.25, 0.25 ); glvertex2fv( point2 ); glvertex2f( 0.75, 0.5 ); glvertex2f( 0.5, 0.5 ); glflush(); int main(int argc, char **argv) initglut(argc, argv); initgl(); // Set the display callback function to draw(). glutdisplayfunc( draw ); //Start the GLUT main Loop. glutmainloop(); return 0; 6
Z-Buffering void initglut( int &argc, char **argv) glutinit( &argc,argv ); glutinitdisplaymode( GLUT_SINGLE GLUT_RGB GLUT_DEPTH); // z-buffer glutinitwindowsize( 700, 700 ); glutinitwindowposition( 100, 100 ); glutcreatewindow( TITLE ); Z-Buffering Z-Buffering void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glmatrixmode( GL_PROJECTION ); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0 ); glmatrixmode (GL_MODELVIEW ); // Enable z-buffering glenable(gl_depth_test); void draw() glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); glflush(); program6.cpp void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glmatrixmode( GL_PROJECTION ); gluperspective(65.0, 1.0, 1.0, 20.0 ); glmatrixmode (GL_MODELVIEW ); glulookat(1,-4,-4,0,0,0,0,1,0); glenable(gl_depth_test); glshademodel( GL_SMOOTH ); glenable(gl_texture_2d); // Load the textures LoadGLTextures("textures/metal3.bmp", &ourtexture); cubefaces[0] = ourtexture; cubefaces[1] = ourtexture; cubefaces[2] = ourtexture; cubefaces[3] = ourtexture; cubefaces[4] = ourtexture; cubefaces[5] = ourtexture; // Set up lighting glenable(gl_lighting); glenable(gl_light1); gllightfv(gl_light1, GL_AMBIENT, light1ambient); gllightfv(gl_light1, GL_DIFFUSE, light1diffuse); gllightfv(gl_light1, GL_SPECULAR, light1specular); gllightfv(gl_light1, GL_POSITION, light1position); 7
void polygon(int a, int b, int c, int d, GLuint texture) glbindtexture(gl_texture_2d, texture); glbegin(gl_polygon); gltexcoord2f(0.0, 0.0); glnormal3fv(normals[a]); glvertex3fv(vertices[a]); void texturecube(gluint *textures) // No error checking of the array... // Draw the different sides. polygon(0,3,2,1, textures[0]); polygon(2,3,7,6, textures[1]); polygon(0,4,7,3, textures[2]); polygon(1,2,6,5, textures[3]); polygon(4,5,6,7, textures[4]); polygon(0,1,5,4, textures[5]); void draw() glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); glcolor3f(1.0, 1.0, 1.0); glrotatef(anglex,0,1,0); glrotatef(-angley,1,0,0); texturecube(cubefaces); glflush(); glutswapbuffers(); void mouse(int btn, int state, int x, int y) grab_pos_x = x; grab_pos_y = y; void mousemotion(int x, int y) anglex += (x-grab_pos_x)/2.0; if( anglex > 360.0 ) anglex -= 360.0; else if( anglex < -360.0 ) anglex += 360.0; angley += (y-grab_pos_y)/2.0; if( angley > 360.0 ) angley -= 360.0; else if( angley < -360.0 ) angley += 360.0; grab_pos_x = x; grab_pos_y = y; glutpostredisplay(); int main(int argc, char **argv) initglut(argc, argv); initgl(); // Set the callback functions. glutdisplayfunc( draw ); glutmousefunc( mouse ); glutmotionfunc( mousemotion ); //Start the GLUT main Loop. glutmainloop(); return 0; 8
Links The main page for reference, documentation,, news, downloads, tutorials,, etc. www.opengl.org Nice tutorials (beginner to expert) http://nehe.gamedev.net/ Books The standard reference books OpenGL Programming Guide `Red Book OpenGL Rererence Manual `Blue Book Available online (HTML,PDF) Course book Interactive Computer Graphics: A Top-Down Approach with OpenGL - 3rd Edition http://www.cs.unm.edu www.cs.unm.edu/~angel/ Hierarchical modeling Hierarchical modeling 9