Computer Graphics OpenGL
What is OpenGL? OpenGL (Open Graphics Library) is a library for computer graphics It consists of several procedures and functions that allow a programmer to specify the objects and operations involved in producing high-quality graphical images, specifically colour images of three-dimensional objects.
OpenGL Features Used to draw graphics primitives in 2D & 3D Points Lines Polygons Also used in Lighting. Shading. Texture mapping. Animation. Other special effects.
Installation Install Microsoft Visual C++ You need to get glut.h glut32.dll glut32.lib Copy (glut.h) file into C:\Program Files\Microsoft Visual Studio\VC98\include\gl Put the DLL file (glut32.dll) in C:\WINDOWS\SYSTEM Put the library file (glut32.lib) into C:\Program Files\Microsoft Visual Studio\VC98\lib
How it works Start MS Visual Studio Select FILE NEW to create a new project Select Win32 Console Application, choose a project name, directory location, make sure create new workspace is checked then click OK. When asked what type of application to build select An Empty Project, then click Finish.
How it works Some settings should be changed so the project is linked to the OpenGL library files (Done only once in your PC) Select PROJECT SETTINGS from the main menu bar Change the Settings For option to All Configurations. Select the LINK tab Make sure category is General In the Object/library modules textbox add: glut32.lib as the first entry with a space separating it from the next entry. Then click OK Select FILE NEW from the main menu bar Select C++ Source File
Sample code What is the output? #include <GL/glut.h> // (or others, depending on the system in use) void init (void) { glclearcolor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glmatrixmode (GL_PROJECTION); // Set projection parameters. gluortho2d (0.0, 200.0, 0.0, 150.0); } void linesegment (void) { glclear (GL_COLOR_BUFFER_BIT); // Clear display window. glcolor3f (0.0, 0.0, 1.0); // Set line segment color to blue. glbegin (GL_LINES); glvertex2i (180, 15); // Specify line-segment geometry. glvertex2i (10, 145); glend ( ); glflush ( ); // Process all OpenGL routines as quickly as possible. } void main (int argc, char** argv) { glutinit (&argc, argv); // Initialize GLUT. glutinitdisplaymode (GLUT_SINGLE GLUT_RGB); // Set display mode. glutinitwindowposition (50, 100); // Set top-left display-window position. glutinitwindowsize (400, 300); // Set display-window width and height. glutcreatewindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutdisplayfunc (linesegment); // Send graphics to display window. glutmainloop ( ); // Display everything and wait. }
Syntax Header Files: Describes all function calls # include <GL/gl.h> (the core Library) # include <GL/glu.h> (the utility Library) # include <GL/glut.h> ( win. Toolkit Lib) Libraries You can call other libraries. Data Types variables for your program. float GLfloat int GLint
Syntax Function names glxxxyy() XXX - root command YY arguments type: a number and a data type For example: glcolor3f - used to specify a color with 3 float values glvertex2d - used to specify a vertex with 2 double values
glvertex To send vertices to the GL, call glvertex() glvertex3f( float x, float y, float z ); The number will represent how many dimensions you are drawing in, so you will use 2 or 3 The type of data you send is simply the format. You can send floats, (f), doubles (d), ints (i), shorts (s)
General Program structure void display(){ } void main(){ }
main function structure void main( ) { glutinitwindowsize(int width, int height); glutinitwindowposition(int x, int y); glutcreatewindow("opengl First Example"); glutdisplayfunc(display); glutmainloop(); }
glutinitwindowsize (int width, int height) width, height - the size of the window in pixels glutinitwindowposition (int x, int y) defines the position of the window on the screen X,Y - the top left corner. (0, 0) is the upper left corner of the display.
glutcreatewindow( Window Name ) Creates a window with the specified title. Note: the window is NOT displayed until glutmainloop() is called. Example: glutcreatewindow( Sphere");
Callback function glutdisplayfunc(displayfunc_name) The argument is a pointer to a callback function that takes no argument. Whenever the window needs refreshing this displayfunc is called. Therefore, you should put all the routines you need to redraw the scene in this function. Example: glutdisplayfunc(display);
glutmainloop( ) is the starting point of GLUT, windows are displayed and the event processing is started. glutdisplayfunc and glutmainloop are used to display a scene.
#include <GL/glut.h> void draw(void) { glclear(gl_color_buffer_bit); glbegin(gl_triangles); glcolor3f(1, 0, 0); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glend(); glflush(); } void main(){ glutinitwindowsize(400,400); glutinitdisplaymode(glut_rgb); glutcreatewindow("opengl Set Up Test"); glutdisplayfunc(draw); glclearcolor(0.0, 0.0, 0.0, 0.0); gluortho2d(0.0, 400.0, 0.0, 400.0); glutmainloop(); } Sample Program
Graphics primitives
Points and Lines GL_POINTS GL_LINES Line segments GL_LINE_STRIP A series of one or more connected line segments the first vertex specifies the first segment s start point while the second vertex specifies the first segment s endpoint and the second segment s start point. GL_LINE_LOOP Same as GL_LINE_STRIP, in addition to connecting the last vertex with the first one.
What is the output? glbegin( GL_POINTS ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINES ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINE_STRIP ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINE_LOOP ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend();
Graphics primitives Cont. GL_POLYGON GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP
GL_POLYGON To display a single polygon fill area At least 3 vertices Example: glbegin(gl_polygon); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); p6 p5 (125, 375) (275, 375) p1 p4 (25, 200) (375, 200) p2 p3 (125, 25) (275, 25)
GL_TRIANGLES To display Triangles Example: glbegin(gl_triangles); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); Output?
GL_TRIANGLE_STRIP A triangle strip is a series of triangles connected along shared edges. In this case, the first three vertices define the first triangle. Each subsequent vertex defines a new triangle using that point along with two vertices from the previous triangle. What is the necessary code to output the following shape using GL_TRIANGLE_STRIP? How many triangles do you need? (125, 375) p6 p5 (275, 375) p1 (25, 200) p4 (375, 200) p2 p3 (125, 25) (275, 25)
GL_TRIANGLE_FAN A triangle fan is the same as a triangle strip with one exception: each vertex after the first triangle always replaces the 2 nd vertex of the previous triangle. All triangles share a common vertex. What is the necessary code to output the following shape using GL_TRIANGLE_FAN? (125, 375) p6 p5 (275, 375) p1 (25, 200) p4 (375, 200) p2 p3 (125, 25) (275, 25)
GL_QUADS To display four-sided polygons At least four vertices Example glbegin(gl_quads); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); GL_QUAD_STRIP To display set of connected quads For n vertices (n>=4), we obtain n/2 1 quads
2D Viewing Viewport glviewport(xvmin, yvmin, vpwidth, vpheight); Clipping gluortho2d(xwmin, xwmax, ywmin, ywmax); Or glortho
Attributes: color, width and size Color: glcolor3f(r, G, B); Point size: glpointsize(size); Line width: gllinewidth(width);
Geometric Transformations For 2D transformations, we set t z =0, (v x, v y, v z )=(0,0,1), and s z =1 Suffix: f (float), d (double) Translation gltranslate(t x, t y, t z ); specifies a translation vector Example: gltranslatef(10.0, 20.0, 0.0); Rotation glrotate(theta, v x, v y, v z ); specifies a rotation angle about a rotation axis vector (v x, v y, v z ), with the axis passing through the origin. Example glrotatef(90.0, 0.0, 0.0, 1.0);
Geometric Transformations Scaling glscale(s x, s y, s z ); specifies multiplicative factors relative to the origin. Example glscalef(2.0, 1.0, 1.0); Reflection operation can be generated by setting scaling factors to negative Example glscalef(-1.0, 1.0, 1.0);
GLUT menus OpenGL supports menus so that the user can select multiple options. To use a menu in your program, you have to do: Define the entries in the menu. Link the menu to a particular mouse button. Define the callback function corresponding to each menu.
Menus To create a pop-up menu glutcreatemenu(menufcn); Where menufcn is the name of the procedure to be invoked when a menu entry is selected, This procedure has one argument, which is the integer value corresponding to the position of a selected menu void menufcn (GLint menuitemnumber);
Menus To add a menu entry, use glutaddmenuentry(charstring, menuitemnumber); charstring is the text to be displayed in the menu entry, and menuitemnumber gives the location of that entry, the following statements create a menu with two options: glutcreatemenu(menufcn); glutaddmenuentry( New, 1); glutaddmenuentry( Open, 2); We must specify a mouse button to be used to select a menu option: glutattachmenu(button);
Submenus submenuid = glutcreatemenu(submenufcn); glutaddmenuentry( First submenu item, 1);... glutcreatemenu(menufcn); glutaddmenuentry( First menu item, 1);... glutaddsubmenu( Submenu option, submenuid);
Example Let us define the following pop-up menu:
Example Define Right Button Menu glutcreatemenu(right_menu); glutaddmenuentry( Quit, 1); glutaddmenuentry( Clear, 2); glutattachmenu(glut_right_button); Define Color Menu C_menu = glutcreatemenu(color_menu); glutaddmenuentry( RED, 1); glutaddmenuentry( BLUE, 2); glutaddmenuentry( BLACK, 3); glutaddmenuentry( GREEN, 4); glutaddmenuentry( WHITE, 5);
Example Define Pixel Size Menu P_menu = glutcreatemenu(pixel_menu); glutaddmenuentry( Increase Size, 1); glutaddmenuentry( Decrease Size, 2); Define Left Menu glutcreatemenu(left_menu); glutaddsubmenu( COLOR, C_menu); glutaddsubmenu ( PIXEL SIZE, P_menu); glutattachmenu(glut_left_button);
Example Define all functions to a menu response: void Color_menu (GLint id) { if (id==1) glcolor3f(1.0,0.0,0.0); if (id==2) glcolor3f(0.0,0.0,1.0); if (id==3) glcolor3f(0.0,0.0,0.0); if (id==4) glcolor3f(0.0,1.0,0.0); if (id==5) glcolor3f(1.0,1.0,1.0); }
OpenGL Interactive Input-Device Functions GLUT Mouse Functions When a mouse button is pressed or released. glutmousefunc(mousefcn); void mousefcn(glint button, GLint action, GLint xmouse, GLint ymouse) button: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON action: GLUT_DOWN GLUT_UP //press //release (xmouse, ymouse): The coordinate position, This location is relative to the top-left corner of the display window
Mouse Functions When a mouse is moved within the display window with one or more buttons activated. glutmotionfunc(mousefcn); void mousefcn(glint xmouse,glint ymouse) When a mouse is moved within the display window without pressing a button. glutpassivemotionfunc(mousefcn);
GLUT Keyboard Functions When a key is pressed glutkeyboardfunc(keyfcn); void keyfcn(glubyte key, Glint xmouse, Glint ymouse) key is assigned a character value or the corresponding ASCII code.
Keyboard Functions For function keys and other special-purpose keys. glutspecialfunc(keyfcn); void keyfcn(glint specialkey, Glint xmouse, Glint ymouse) specialkey: GLUT_KEY_F1... GLUT_KEY_F12 GLUT_KEY_UP... GLUT_KEY_RIGHT GLUT_KEY_PAGE_DOWN GLUT_KEY_HOME Backspace, delete, and escape keys can be designated with glutkeyboardfunc using their ASCII code 8, 127 and 27 respectively.
OpenGL Codes Find the output of the attached C++ programs