Chapter 2 Using Open GL ChihKuo Yeh Textbook
Chapter 2 Using Open. GL Chih-Kuo Yeh
Textbook Addison Wesley Open. GL Super. Bible 4 th Edition Jun 2007 Author: Richard S. Wright, Jr. Benjamin Lipchak Nicholas Haemel
Open. GL and GLUT Overview What is Open. GL & what can it do for me? Open. GL in windowing systems Why GLUT A GLUT program template 3
What Is Open. GL? Graphics rendering API high-quality color images composed of geometric and image primitives window system independent operating system independent 4
SGI and GL Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) To access the system, application programmers used a library called GL With GL, it was relatively simple to program three dimensional interactive applications 1. 0 (1992) 5 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
Open. GL The success of GL lead to Open. GL (1992), a platform-independent API that was Easy to use Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies 6 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
Open. GL Libraries �Open. GL core library Open. GL 32 on Windows GL on most unix/linux systems (lib. GL. a) �Open. GL Utility Library (GLU) Provides functionality in Open. GL core but avoids having to rewrite code �Links with window system GLX for X window systems WGL for Windows AGL for Macintosh 7 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
Open. GL as a Renderer Geometric primitives points, lines and polygons Image Primitives images and bitmaps separate pipeline for images and geometry ▪ linked through texture mapping Rendering depends on state colors, materials, light sources, etc. 8
Related APIs AGL, GLX, WGL glue between Open. GL and windowing systems GLU (Open. GL Utility Library) part of Open. GL NURBS, tessellators, quadric shapes, etc. GLUT (Open. GL Utility Toolkit) portable windowing API not officially part of Open. GL 9
Open. GL and Related APIs application program Open. GL Motif widget or similar GLX, AGL or WGL GLUT GLU X, Win 32, Mac O/S software and/or hardware 10 GL
Open. GL Architecture Polynomial Evaluator CPU Display List Per Vertex Operations & Primitive Assembly Rasterization Texture Memory Pixel Operations 11 Per Fragment Operations Frame Buffer
Preliminaries Headers Files #ifdef WIN 32 #include <windows. h> #include "glee. h" #include <glgl. h> #include <glglu. h> #include "glut. h" #endif // Must have for Windows platform builds // Open. GL Extension "autoloader" // Microsoft Open. GL headers (version 1. 1 by themselves) // Open. GL Utilities // Glut (Free-Glut on Windows) Libraries Enumerated Types Open. GL defines numerous types for compatibility ▪ GLfloat, GLint, GLenum, etc. 12
Notes on compilation See website and ftp for examples 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 Check web for latest versions of Mesa and glut 13 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
Compilation on Windows Visual C++ Get glut. h, glut 32. lib and glut 32. dll from web Create a console application Add opengl 32. lib, glut 32. lib to project settings (under link tab) Borland C similar Cygwin (linux under Windows) Can use gcc and similar makefile to linux Use –lopengl 32 –lglut 32 flags 14 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
GLUT Basics Application Structure Configure and open window Initialize Open. GL state Register input callback functions ▪ render ▪ resize ▪ input: keyboard, mouse, etc. Enter event processing loop 15
Your First Program
simple. cpp 1. #include “. . /shared/gltools. h” // Open. GL toolkit 2. void Render. Scene(void) 3. { 4. gl. Clear(GL_COLOR_BUFFER_BIT); 5. gl. Flush(); 6. } 7. void Setup. RC(void) 8. { 9. gl. Clear. Color(0. 0 f, 1. 0 f); 10. } 11. int main(int argc, char* argv[]) 12. { 13. glut. Init(&argc, argv); 14. glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGBA); 15. glut. Create. Window(“Simple”); 16. glut. Display. Func(Render. Scene); 17. Setup. RC(); 18. glut. Main. Loop(); 19. return 0; 17 20. }
VC 2005 Compiler
VC 2005 Compiler
Sample Program
GLUT Callback Functions Routine to call when something happens window resize or redraw user input animation “Register” callbacks with GLUT glut. Display. Func( display ); glut. Idle. Func( idle ); glut. Keyboard. Func( keyboard ); 21
void gl. Clear. Color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
Some Common Composite Colors
Clearing the Color Buffer gl. Clear(GL_COLOR_BUFFER_BIT); gl. Flush();
Drawing Shapes with Open. GL
glrect. cpp int main(int argc, char* argv[]) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB); glut. Create. Window("GLRect"); glut. Display. Func(Render. Scene); glut. Reshape. Func(Change. Size); Setup. RC(); glut. Main. Loop(); return 0; }
Rendering Callback Do all of your drawing here void Render. Scene(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Rectf(-25. 0 f, -25. 0 f) gl. Flush(); }
Reshape Callback void Change. Size(int w, int h) { GLfloat aspect. Ratio; if(h == 0) h = 1; gl. Viewport(0, 0, w, h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); aspect. Ratio = (GLfloat)w / (GLfloat)h; if (w <= h) gl. Ortho (-100. 0, -100 / aspect. Ratio, 100. 0 / aspect. Ratio, 1. 0, -1. 0); else gl. Ortho (-100. 0 * aspect. Ratio, -100. 0, 1. 0, -1. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); }
Drawing a Rectangle void gl. Color 3 f(GLfloat red, GLfloat green, GLfloat blue); void gl. Rectf(GLfloat x 1, GLfloat y 1, GLfloat x 2, GLfloat y 2);
Open. GL Command Formats gl. Vertex 3 fv( v ) Number of components 2 3 4 31 - (x, y) (x, y, z, w) Data Type b ub s us i ui f d - byte unsigned byte short unsigned short int unsigned int float double Vector omit “v” for scalar form gl. Vertex 2 f( x, y )
Open. GL Variable Types
Setting the Viewport and Clipping Volume
Defining the Viewport void gl. Viewport(GLint x, GLint y, GLsizei width, GLsizei height);
Defining the Clipped Viewing Volume void gl. Ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );
Keeping a Square
Keeping a Square aspect. Ratio = (GLfloat)w / (GLfloat)h; if (w <= h) gl. Ortho (-100. 0, -100 / aspect. Ratio, 100. 0 / aspect. Ratio, 1. 0, -1. 0); else gl. Ortho (-100. 0 * aspect. Ratio, -100. 0, 1. 0, -1. 0);
Animation with Open. GL and GLUT
bounce. cpp int main(int argc, char* argv[]) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGBA); glut. Init. Window. Size(800, 600); glut. Create. Window("Bounce"); glut. Display. Func(Render. Scene); glut. Reshape. Func(Change. Size); glut. Timer. Func(33, Timer. Function, 1); Setup. RC(); glut. Main. Loop(); return 0; }
Rendering Callback GLfloat x = 0. 0 f; GLfloat y = 0. 0 f; GLfloat rsize = 25; GLfloat xstep = 1. 0 f; GLfloat ystep = 1. 0 f; GLfloat window. Width; GLfloat window. Height; void Render. Scene(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Rectf(x, y, x + rsize, y - rsize); glut. Swap. Buffers(); }
Timer Callback void Timer. Function(int value) { if(x > window. Width-rsize || x < -window. Width) xstep = -xstep; if(y > window. Height || y < -window. Height + rsize) ystep = -ystep; x += xstep; y += ystep; if(x > (window. Width-rsize + xstep)) x = window. Width-rsize-1; else if(x < -(window. Width + xstep)) x = -window. Width -1; } if(y > (window. Height + ystep)) y = window. Height-1; else if(y < -(window. Height - rsize + ystep)) y = -window. Height + rsize - 1; glut. Post. Redisplay(); glut. Timer. Func(33, Timer. Function, 1);
Time function glut. Timer. Func(unsigned int millis, void (GLUTCALLBACK *func)(int value), int value); Registers a timer callback to be triggered in a specified number of milliseconds.
Double Buffering glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGBA); glut. Swap. Buffers();
User Input Callbacks �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; } }
The mouse callback glut. Mouse. Func(mymouse) � void mymouse(GLint button, GLint state, GLint x, GLint y) Returns which button (GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON) caused event state of that button (GLUT_UP, GLUT_DOWN) Position in window 45 Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009
- Slides: 45