CSC 706 Computer Graphics / Dr. N. Gueorguieva 1 OpenGL Libraries Drawing Primitives OpenGL basics OpenGL core library OpenGL32 on Windows GL on most unix/linux systems (libgl.a) OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code Links with window system GLX for X window systems WGL for Windows AGL for Macintosh CSC 706 Computer Graphics / Dr. N. Gueorguieva 2 GLUT OpenGL Utility Toolkit (GLUT) Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars Primitives Points Line Segments Polygons Attributes Transformations Viewing Modeling Control (GLUT) Input (GLUT) Query OpenGL Functions CSC 706 Computer Graphics / Dr. N. Gueorguieva 3 CSC 706 Computer Graphics / Dr. N. Gueorguieva 4 Lack of Object Orientation Example OpenGL is not object oriented so that there are multiple functions for a given logical function glvertex3f glvertex2i glvertex3dv Underlying storage mode is the same Easy to create overloaded functions in C++ but issue is efficiency type of object location of vertex glbegin(gl_polygon) glvertex3f(0.0, 0.0, 0.0); glvertex3f(0.0, 1.0, 0.0); glvertex3f(0.0, 0.0, 1.0); glend( ); end of object definition CSC 706 Computer Graphics / Dr. N. Gueorguieva 5 CSC 706 Computer Graphics / Dr. N. Gueorguieva 6
CSC 706 Computer Graphics / Dr. N. Gueorguieva 7 OpenGL function format OpenGL #defines function name dimensions glvertex3f(x,y,z) x,y,z are floats belongs to GL library glvertex3fv(p) p is a pointer to an array Most constants are defined in the include files gl.h, glu.h and glut.h Note #include <GL/glut.h> should automatically include the others Examples glbegin(gl_polygon) glclear(gl_color_buffer_bit) include files also define OpenGL data types: GLfloat, GLdouble,. CSC 706 Computer Graphics / Dr. N. Gueorguieva 8 Event driven programming Event driven programming Almost all window based programs follow the event driven paradigm program waits for events to occur takes appropriate action Events are typically stored in an event queue Application programs specify the types of events that are of interest The window system passes events to apps. Appropriate action is implemented by the application calling a unique, user defined function for each event of interest these functions are referred to as callback functions Associating callback functions with events is not a rendering issue, OpenGL does not provide for this GLUT (OpenGL Utility Toolkit) does CSC 706 Computer Graphics / Dr. N. Gueorguieva 9 CSC 706 Computer Graphics / Dr. N. Gueorguieva 10 Registering Events The function call: glutmousefunc(mouse) associates the callback function mouse with all mouse events button press/release When a mouse button is pressed or released in the graphics application window, the user defined function mouse is called Application ignores all mouse events without this function call Callback Functions (Re)draws screen when window opened or another window moved off it. Reports new window width and height for reshaped window. (Moving a window does not produce a reshape event.) glutidlefunc(myidle); when nothing else is going on, simply redraws display using void myidle() glutpostredisplay(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 11 CSC 706 Computer Graphics / Dr. N. Gueorguieva 12
CSC 706 Computer Graphics / Dr. N. Gueorguieva 13 Callback Functions (2) Handles mouse button presses. Knows mouse location and nature of button (up or down and which button). glutmotionfunc(mymotionfunc); Handles case when the mouse is moved with one or more mouse buttons pressed. Callback Functions (3) glutpassivemotionfunc(mypassivemotionfunc) Handles case where mouse enters the window with no buttons pressed. glutkeyboardfunc(mykeyboardfunc); Handles key presses and releases. Knows which key was pressed and mouse location. glutmainloop() Runs forever waiting for an event. When one occurs, it is handled by the appropriate callback function. CSC 706 Computer Graphics / Dr. N. Gueorguieva 14 Event driven programming main() Opening a window for drawing main() glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow("my first attempt"); Establishing coordinate system void myinit(void) glclearcolor(0.0,1.0,1.0,0.0); glcolor3f(1.0,0.0f,1.0f); glpointsize(4.0); glmatrixmode(gl_projec TION); glloadidentity(); gluortho2d(0.0,440.0,0.0,4 80.0); void myinit(void) glclearcolor(0.0,1.0,1.0,0.0); glcolor3f(1.0,0.0f,1.0f); glpointsize(9.0); glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0.0,440.0,0.0,480.0); void mydisplay(void) glclear(gl_color_buffer_bit); glbegin(gl_points); glvertex2i(100,50); glvertex2i(100,130); glvertex2i(150, 130); glvertex2i(230, 100); glend(); glflush(); void main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_sing LE GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow("my first attempt"); // // // myinit(); glutmainloop(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 15 CSC 706 Computer Graphics / Dr. N. Gueorguieva 16 A GL Program to Open a Window void main(int argc, char** argv) glutinit(&argc, argv); // initialize the Open-GL toolkit glutinitdisplaymode(glut_single GLUT_RGB); // set the display mode: single display buffer and colors glutinitwindowsize(640,480); // set window size glutinitwindowposition(100, 150); // set window upper left corner position on screen glutcreatewindow("my first attempt"); // open the screen window (Title: my first attempt) Window Program cont d // register the callback functions myinit(); // additional initializations as necessary glutmainloop(); // go into a perpetual loop CSC 706 Computer Graphics / Dr. N. Gueorguieva 17 CSC 706 Computer Graphics / Dr. N. Gueorguieva 18
CSC 706 Computer Graphics / Dr. N. Gueorguieva 19 Effect of Program First program - putting it together Vertices - instead of points we work with vertex and create objects from vertices. glbegin(gl_points); glvertex2i(100,50); glvertex2i(100,130); glvertex2i(150, 130); glvertex2i(230, 100); glend(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 20 void mykeyboard(unsigned char key, int mousex, int mousey) Runs whenever a keyboard event occurs (e.g. user presses a key) void mymouse(int button, int state, int x, int y) Runs whenever a mouse event occurs (e.g. user presses a mouse button, mouse moves) void mydisplay(void) Runs whenever the system determines that the window must be redrawn (e.g. window comes to the front, window has been moved) void myreshape(void) Runs whenever a window changes size void mykeyboard(unsigned char key, int mousex, int mousey) if (key == f key == F ) glutfullscreen(); else if(key == w key == W ) glutreshapewindow(640,480); CSC 706 Computer Graphics / Dr. N. Gueorguieva 21 CSC 706 Computer Graphics / Dr. N. Gueorguieva 22 int main() //initialise things //create a screen window //initialise other things glutmainloop(); Opening a Window int main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT RGB); glutinitwindowsize(640,480); glutinitwindowposition(100,100); glutcreatewindow( My OpenGL Window ); myinit(); glutmainloop(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 23 CSC 706 Computer Graphics / Dr. N. Gueorguieva 24
CSC 706 Computer Graphics / Dr. N. Gueorguieva 25 Drawing Dots in OpenGL Display for Dots glbegin (GL_POINTS); glvertex2i (100, 50); glvertex2i (100, 130); glvertex2i (150, 130); glend(); GL_POINTS is constant built-into Open-GL (also GL_LINES, GL_POLYGON, ) CSC 706 Computer Graphics / Dr. N. Gueorguieva 26 Setting Drawing Colors in GL glcolor3f(red, green, blue); // set drawing color glcolor3f(1.0, 0.0, 0.0); // red glcolor3f(0.0, 1.0, 0.0); // green glcolor3f(0.0, 0.0, 1.0); // blue glcolor3f(0.0, 0.0, 0.0); // black glcolor3f(1.0, 1.0, 1.0); // bright white glcolor3f(1.0, 1.0, 0.0); // bright yellow glcolor3f(1.0, 0.0, 1.0); // magenta Setting Background Color in GL glclearcolor (red, green, blue, alpha); Sets background color. Alpha is degree of transparency; use 0.0 for now. glclear(gl_color_buffer_bit); clears window to background color Setting the Background Colour glclearcolour(r, G, B, α); glclearcolour(1,0,0,0); glclearcolour(0,1,0,0); glclearcolour(0,0,1,0); Setting Up a Coordinate System void myinit(void) glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0, 640.0, 0, 480.0); // sets up coordinate system for window from (0,0) to (679, 479)
Drawing Lines glbegin (GL_LINES); //draws one line glvertex2i (40, 100); // between 2 vertices glvertex2i (202, 96); glend (); glflush(); If more than two vertices are specified between glbegin(gl_lines) and glend() they are taken in pairs, and a separate line is drawn between each pair. Line Attributes Color, thickness, stippling. glcolor3f() sets color. gllinewidth(4.0) sets thickness. The default thickness is 1.0. a). thin lines b). thick lines c). stippled lines Setting Line Parameters Polylines and Polygons: lists of vertices. Polygons are closed (right); polylines need not be closed (left). Rectangles A special type of polygon with four sides and with aligned with the coordinate axis is called an aligned rectangle. Aligned Rectangles glrecti(glint x1, GLint y1, GLint x2, GLint y2); (x1,y1) (x2,y2) Drawing rectangles void mydisplay(void) glclear(gl_color_buffer_bit); glclearcolor(1.0,1.0,1.0,0.0); // white background glcolor3f(0.6,0.6,0.6); // bright gray glrecti(20,20,100,70); glcolor3f(0.2,0.2,0.2); // dark gray glrecti(70, 50, 150, 130); glflush(); by the opposite corners by the center point, height and width by the upper left corner, width and aspect ratio
Aligned Rectangles Aligned Rectangles glrecti(glint x1, GLint y1, GLint x2, GLint y2); The rectangle will be drawn, filled with the current foreground colour. Aspect Ratio The shape of a rectangle can be described as it s aspect ratio. The aspect ratio is calculated thus: AR = Width/Height Landscape 29.5/21.2 Portrait 21.2/29.5 Golden Rectangle ɸ = 1.61803399