Computer Graphics BingYu Chen National Taiwan University Introduction
Computer Graphics Bing-Yu Chen National Taiwan University
Introduction to Open. GL o o o o o General Open. GL Introduction An Example Open. GL Program Drawing with Open. GL Transformations Animation and Depth Buffering Lighting Evaluation and NURBS Texture Mapping Advanced Open. GL Topics modified from Imaging Dave Shreiner, Ed Angel, and Vicki Shreiner. An Interactive Introduction to Open. GL Programming. ACM SIGGRAPH 2001 Conference Course Notes #54. & ACM SIGGRAPH 2004 Conference Course Notes #29.
What is Open. GL? & What can it do for me? o Open. GL is n a computer graphics rendering API o generate high-quality color images by rendering with geometric and image primitives o create interactive applications with 3 D graphics n window system independent n operating system independent
Open. GL as a Renderer o Geometric primitives n points, lines and polygons o Image Primitives n images and bitmaps n separate pipeline for images & geometry o linked through texture mapping o Rendering depends on state n colors, materials, light sources, etc.
Open. GL Libraries o Open. GL core library n Open. GL 32 on Windows n GL on most unix/linux systems o Open. GL Utility Library (GLU) n part of Open. GL n Provides functionality in Open. GL core but avoids having to rewrite code n NURBS, tessellators, quadric shapes, etc.
Open. GL Libraries o Links with window system n n GLX for X window systems WGL for Windows AGL for Macintosh glue between Open. GL & windowing systems
GLUT o Open. GL Utility Library (GLUT) n Provides functionality common to all window systems o Open a window o Get input from mouse and keyboard o Menus o Event-driven n Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform o Slide bars
Open. GL and Related APIs application program Open. GL Motif widget or similar GLX, AGL or WGL X, Win 32, Mac O/S GLUT GLU GL software and/or hardware
Open. GL Architecture Polynomial Evaluator CPU Display List Per Vertex Operations & Primitive Assembly Rasterization Texture Memory Pixel Operations Geometric pipeline Per Fragment Operations Frame buffer
Open. GL Functions o Primitives n Points n Line Segments n Polygons o Attributes o Transformations n Viewing n Modeling o Control o Input (GLUT)
What is Required for your Programs o Headers Files #include <GL/gl. h> <GL/glext. h> <GL/glut. h> o Libraries o Enumerated Types n Open. GL defines numerous types for compatibility n GLfloat, GLint, GLenum, etc.
Lack of Object Orientation o Open. GL is not object oriented so that there are multiple functions for a given logical function, n e. g. gl. Vertex 3 f, gl. Vertex 2 i, gl. Vertex 3 dv, …. . o Underlying storage mode is the same o Easy to create overloaded functions in C++ but issue is efficiency
Open. GL Command Formats gl. Vertex 3 fv( v ) Number of components 2 - (x, y) 3 - (x, y, z) 4 - (x, y, z, w) Data Type b ub s us i ui f d - byte unsigned short unsigned int unsigned float double byte short int Vector omit “v” for scalar form gl. Vertex 2 f( x, y )
Open. GL State o Open. GL is a state machine o Open. GL functions are of two types n Primitive generating o Can cause output if primitive is visible o How vertices are processes and appearance of primitive are controlled by the state n State changing o Transformation functions o Attribute functions
The Open. GL Pipeline Vertex Processing Fragment Processing Framebuffer o Processing is controlled by setting Open. GL’s state n colors, lights and object materials, texture maps n drawing styles, depth testing
GLUT Basics o Application Structure n Configure and open window n Initialize Open. GL state n Register input callback functions o render o resize o input: keyboard, mouse, etc. n Enter event processing loop
Sequence of Most Open. GL Programs configure and open a window initialize Open. GL’s state process user events update Open. GL’s state (if necessary) Draw an image
A Simple Program o Generate a square on a solid background
simple. c #include <GL/glut. h> void mydisplay(){ gl. Clear(GL_COLOR_BUFFER_BIT); gl. Begin(GL_POLYGON); gl. Vertex 2 f(-0. 5, -0. 5); gl. Vertex 2 f(-0. 5, 0. 5); gl. Vertex 2 f(0. 5, -0. 5); gl. End(); gl. Flush(); } int main(int argc, char** argv){ glut. Create. Window("simple"); glut. Display. Func(mydisplay); glut. Main. Loop(); }
Event Loop o Note that the program defines a display callback function named mydisplay n Every glut program must have a display callback n The display callback is executed whenever Open. GL decides the display must be refreshed, for example when the window is opened n The main function ends with the program entering an event loop
Notes on Compilation o Unix/linux Include files usually in …/include/GL Compile with –lglut –lglu –lgl loader flags May have to add –L flag for X libraries Mesa implementation included with most linux distributions n Check web for latest versions of Mesa and glut n n
Compilation on Windows o Visual C++ / Borland C n Get glut. h, glut 32. lib and glut 32. dll n Create a console application n Add opengl 32. lib, glut 32. lib to project settings (under link tab) o Cygwin (linux under Windows) n Can use gcc and similar makefile to linux n Use –lopengl 32 –lglut 32 flags
Another Sample Program void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glut. Init. Display. Mode( mode ); glut. Create. Window( argv[0] ); init(); glut. Display. Func( display ); glut. Reshape. Func( resize ); glut. Keyboard. Func( key ); glut. Idle. Func( idle ); glut. Main. Loop(); }
Open. GL Initialization o Set up whatever state you’re going to use void init( void ) { gl. Clear. Color( 0. 0, 1. 0 ); gl. Clear. Depth( 1. 0 ); gl. Enable( GL_LIGHTING ); gl. Enable( GL_DEPTH_TEST ); }
GLUT Callback Functions o Routine to call when something happens n window resize or redraw n user input n animation o “Register” callbacks with GLUT glut. Display. Func( display ); glut. Idle. Func( idle ); glut. Keyboard. Func( keyboard );
Rendering Callback o Do all of your drawing here glut. Display. Func( display ); void display( void ) { gl. Clear( GL_COLOR_BUFFER_BIT ); gl. Begin( GL_TRIANGLE_STRIP ); gl. Vertex 3 fv( v[0] ); gl. Vertex 3 fv( v[1] ); gl. Vertex 3 fv( v[2] ); gl. Vertex 3 fv( v[3] ); gl. End(); glut. Swap. Buffers(); }
Idle Callbacks o Use for animation & continuous update glut. Idle. Func( idle ); void idle( void ) { t += dt; glut. Post. Redisplay(); }
User Input Callbacks o Process user input glut. Keyboard. Func( keyboard ); void keyboard( unsigned char key, int x, int y ) { switch( key ) { case ‘q’ : case ‘Q’ : exit( EXIT_SUCCESS ); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; glut. Post. Redisplay(); break; } }
On-Line Resources o http: //www. opengl. org/ n start here; up to date specification and lots of sample code o news: comp. graphics. api. opengl o http: //www. sgi. com/software/opengl/ o http: //www. mesa 3 d. org/ n Brian Paul’s Mesa 3 D o http: //www. cs. utah. edu/~narobins/opengl. html n very special thanks to Nate Robins for the Open. GL Tutors n source code for tutors available here!
Books o D. Shreiner, M. Woo, J. Neider, T. Davis. Open. GL® Programming Guide: The Official Guide to Learning Open. GL, ver. 2, 5 th. ed. , Addison-Wesley, 2005.
Books o D. Shreiner. Open. GL® Reference Manual: The Official Reference Document to Open. GL, ver. 1. 4, 4 th. ed. , Addison-Wesley, 2004.
Books o R. Rost. Open. GL® Shading Language, 2 nd. ed. , Addison-Wesley, 2006.
Books o E. Angel. Interactive Computer Graphics: A Top-Down Approach with Open. GL™, 3 rd. ed. , Addison-Wesley, 2002.
- Slides: 33