1 / 44

CS 470 Introduction to Computer Graphics

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

sofia
Download Presentation

CS 470 Introduction to Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 470 Introduction to Computer Graphics Using GLUT and a few other things

  2. 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

  3. …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

  4. … 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.

  5. 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).

  6. 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).

  7. 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

  8. 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

  9. 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()

  10. GLUT int glutCreateWindow(char *name); creates (but does not display) a window according current glutInitWindowSize() and glutInitWindowPosition() window not actually displayed until glutMainLoop()

  11. 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.)

  12. 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()

  13. 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

  14. 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

  15. 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() }

  16. GLUT Using Text…

  17. 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”);

  18. 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

  19. 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.

  20. GLUT void glutSwapBuffers(); swaps front and back buffers. assuming that we requested double buffering – glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

  21. GLUT Keyboard Interaction

  22. 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)

  23. 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);

  24. 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

  25. GLUT Mouse Interaction

  26. 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

  27. 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(); }

  28. 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

  29. 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.

  30. 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

  31. GLUT Creating and Using Menus

  32. 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

  33. 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

  34. GLUT - menus void glutSetMenu(int id); sets menu id to be the current menu

  35. 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.

  36. 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

  37. 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(); }

  38. 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

  39. 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

  40. GLUT – more on windows void glutDestroyWindow(int id); Destroys with window with the id value of id.

  41. GLUT – more on windows void glutSetWindow(int id); Sets the window with id = id to be the current window.

  42. 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

  43. 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);

More Related