440 likes | 538 Views
CS 470 Introduction to Computer Graphics. Using GLUT and a few other things. A few of comments…. gl, glu and glut must handle events – drawing events, mouse events,… Events are handled by callback functions Callback functions are functions coded to handle events
E N D
CS 470 Introduction to Computer Graphics Using GLUT and a few other things
A few of comments… • gl, glu and glut must handle events – drawing events, mouse events,… • Events are handled by callback functions • Callback functions are functions coded to handle events • You must register a function to serve as a callback for a specific event
…and • gl tends to deal with things in a world coordinate system • glut tends to deal with things in a screen coordinate system • …they are different
… coordinates • conceptually world coordinates have the origin (0,0) in the lower left corner with increasing y and x going up and to the right, repectively. • however, screen coordinates (window coordinates) have the origin in the upper left corner with increasing y and x values going down and to the right, respectively. • this can be a source of confusion/errors, if not managed.
GLUT • GLUT is a user interface API • it is generic across windowing systems (X windows, MS Window, Apple Macintosh…) • Gives program control to manage the user interface. • Provides the “canvas” for OpenGL to draw on. • Creates, manages windows (canvas) • Provides user input functions (keyboard, mouse).
GLUT • first… void glutInit(int argc, char **argv); Initialized the glut library. Must be called before any other glut routine argc and argv are command line parameters (same as is used by main(argc, argv) argc and argv all you to pass parameters to glut from the command line (system dependent).
GLUT void glutInitDisplayMode(unsigned int mode); specifies how opengl items will be displayed. mode is a bit mask – OR parameters modes can be: GLUT_RGB GLUT_SINGLE GLUT_RGBA GLUT_DOUBLE GLUT_INDEX GLUT_DEPTH …more
GLUT void glutInitWindowSize(int width, int height); defines the size of the graphics window (not yet created. width and height are in pixels this call maybe overridden by other functions
GLUT void glutInitWindowPosition(int x, int y); defines the initial position of the graphics window (not yet created) x and y refer to the upper left corner of the display x and y are in pixels window boundaries are defined by x, y and width, height (from glutInitWindowSize()
GLUT int glutCreateWindow(char *name); creates (but does not display) a window according current glutInitWindowSize() and glutInitWindowPosition() window not actually displayed until glutMainLoop()
GLUT void glutDisplayFunc(void (*func)void)); registers a display function the display function includes the instructions to draw the object when needed. display function is evoke when the image needs to be redrawn (window changes, state changes, etc.)
GLUT void glutMainLoop(); creates an event processing loop one event is to draw the image defined by the display function this should be the last function in main()
GLUT void glutReshapeFunction(void (*f) (int width, int height); defines a callback function to respond to change in the size of the current window. when a window is resized function f is called and the width and height of the new window is passed to f
GLUT void glutPostRedisplay() calls the display callback function similar to executing the display callback directly, is a little smart about redrawing the image. generally, it is preferable to call glutPostRedisplay() rather than the display function
GLUT void glutIdleFunc(void (*f) (void)); defines a function to call/execute when there are no events in the event queue. glutIdleFunc(idler); void idler() { glutPostRedisplay() }
GLUT Using Text…
GLUT – using text void glutBitMapCharacter(void *font, int char) renders the character char specified by its ASCII code using font font renders at the current raster_position (state variable) glutBitMapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, “A”);
not GLUT void glRasterPos{2|3|4}{s|i|f|d}(TYPE x, TYPE y, TYPE z, TYPE w); set the current raster position. the next character will be drawn there. coordinates are world coordinates character drawing functions automatically update raster_position
not GLUT void glutStrokeCharacter(void *font, int char); renders a vector (stroke) character in font font. predefined size about 100x100 in world coordinates can manipulate with primitive transformation functions.
GLUT void glutSwapBuffers(); swaps front and back buffers. assuming that we requested double buffering – glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
GLUT Keyboard Interaction
GLUT void glutKeyboardFunc(void *f(unsigned char key, int x, int y); defines a callback function to handle keyboard key presses. the key (key) value is returned to the function f the position of the mouse (x,y) when the key was clicked is also returned to f x, y are in pixels (screen coordinates)
GLUT void glutSpecialFunction(void (*f) (int key, int x, int y); defines a callback function for handling keypresses of special keys returns to f the special key pressed and location of the mouse in pixels (screen coordinates) if (key == GLUT_KEY_UP) exit(0);
GLUT int glutGetModifers(); returns the state of modifer keys (alt, shift, control) values are GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT use inside keyboard event callbacks
GLUT Mouse Interaction
GLUT - mouse void glutMouseFunc(void (*f)(int button, int state, int x, int y); Defines/registers a callback function f to handle mouse event Mouse event occurs when a mouse button is clicked state can be GLUT_UP | GLUT_DOWN button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON x & y are in screen coordinates
GLUT - mouse glutMouseFunc(mouse_catcher); void mouse_catcher(int x, int y, int button, int state) { if (state == GLUT_DOWN && button == GLUT_MIDDLE_BUTTON ) exit(); }
GLUT - mouse void glutMotionFunc(void (*f) (int x, int y)); defines/registers a callback to handle mouse movement event… …when a button is held down x & y are in screen coordinates
GLUT - mouse void glutPassiveMotionFunc(void (*f) (int x, int y)); defines/registers a callback function to handle passive mouse movement events… …when a button is not held down. x & y are in screen coordinates.
GLUT – mouse void glutEntryFunc(void (*f) (int state)); defines/registers a callback function f to handle entering/leaving events entering and leaving refers to entering or leaving an opengl window state can be GLUT_ENTERED or GLUT_LEFT
GLUT Creating and Using Menus
GLUT - menus GLUT has a limited ability to provide interactive tools or widgets. One type is does provide in the menu To use menus – define the menu entries (what do say) attach actions to each menu entry attach a menu to a mouse button
GLUT - menus int glutCreateMenu(void (*f) (int value)); Define a top level menu that uses callback f. f is passed the int value for the menu entry. This call returns an int identifier for the menu The created menu becomes the current menu
GLUT - menus void glutSetMenu(int id); sets menu id to be the current menu
GLUT - menus void glutAddMenuEntry(char *name, int value); Menus are initially created empty Add entries/options to menu with glutAddMenuEntry() name is the displayed entry label. value is the int value returns to the menu callback when this entry is selected.
GLUT - Menus void glutAttachMenu(int button); Associates the current menu with a specific mouse button button can be GLUT_RIGHT_BUTTON GLUT_MIDDLE_BUTTON GLUT_LEFT_BUTTON
GLUT -menus glutCreateMenu(menu_handler); glutAddMenuEntry(“Move Froggy Left”, 1); glutAddMenuEntry(“Move Froggy Right”, 2); glutAttachMenu(GLUT_MIDDLE_BUTTON); void menu_handler(int value); { if(value == 1) move_left(); if(value == 2) move_right(); }
GLUT - menus void glutAddSubMenu(char *name, int menu); This will add a submenu a the next entry in a menu. It will be called name The int value menu is the menu value passed to the menu callback
GLUT – more on windows Recall – int glutCreateWindow(char *name); creates a window called “name” and returns a window int id as a handle for this window
GLUT – more on windows void glutDestroyWindow(int id); Destroys with window with the id value of id.
GLUT – more on windows void glutSetWindow(int id); Sets the window with id = id to be the current window.
GLUT – more on window int glutCreateSubWindows(int parent, int x, int y, int width, int height); You can also create subwindows to a window. This function creates a window that is a subwindow to parent. The subwindow will have an origin a x, y and will width x height in size. x, y, width and height are in screen coordinates
GLUT – more on windows Some other GLUT windows functions of note- glutReshapeWindow(int width, int height); glutPositionWindow(int x, int y); glutHideWindow(void); glutShowWindow(void);