cs 470 introduction to computer graphics
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
CS 470 Introduction to CS 470 Introduction to Computer GraphicsComputer Graphics
Using GLUT and a few other Using GLUT and a few other thingsthings
A few of comments…A few of comments…
• gl, glu and glut must handle events – gl, glu and glut must handle events – drawing events, mouse events,…drawing events, mouse events,…
• Events are handled by callback Events are handled by callback functionsfunctions
• Callback functions are functions Callback functions are functions coded to handle eventscoded to handle events
• You must register a function to serve You must register a function to serve as a callback for a specific eventas a callback for a specific event
……andand
• gl tends to deal with things in a world gl tends to deal with things in a world coordinate systemcoordinate system
• glut tends to deal with things in a glut tends to deal with things in a screen coordinate systemscreen coordinate system
• ……they are differentthey are different
… … coordinatescoordinates
• conceptually world coordinates have the conceptually world coordinates have the origin (0,0) in the lower left corner with origin (0,0) in the lower left corner with increasing y and x going up and to the right, increasing y and x going up and to the right, repectively.repectively.
• however, screen coordinates (window however, screen coordinates (window coordinates) have the origin in the upper left coordinates) have the origin in the upper left corner with increasing y and x values going corner with increasing y and x values going down and to the right, respectively.down and to the right, respectively.
• this can be a source of confusion/errors, if not this can be a source of confusion/errors, if not managed.managed.
GLUTGLUT
• GLUT is a user interface APIGLUT is a user interface API• it is generic across windowing systems (X it is generic across windowing systems (X
windows, MS Window, Apple Macintosh…)windows, MS Window, Apple Macintosh…)• Gives program control to manage the user Gives program control to manage the user
interface.interface.• Provides the “canvas” for OpenGL to draw Provides the “canvas” for OpenGL to draw
on.on.• Creates, manages windows (canvas)Creates, manages windows (canvas)• Provides user input functions (keyboard, Provides user input functions (keyboard,
mouse).mouse).
GLUTGLUT
• first…first…void glutInit(int argc, char **argv);void glutInit(int argc, char **argv);
Initialized the glut library. Initialized the glut library.
Must be called before any other glut routineMust be called before any other glut routine
argc and argv are command line parameters argc and argv are command line parameters (same as is used by main(argc, argv)(same as is used by main(argc, argv)
argc and argv all you to pass parameters to glut argc and argv all you to pass parameters to glut from the command line (system dependent).from the command line (system dependent).
GLUTGLUT
void glutInitDisplayMode(unsigned int mode);void glutInitDisplayMode(unsigned int mode);
specifies how opengl items will be displayed.specifies how opengl items will be displayed.mode is a bit mask – OR parametersmode is a bit mask – OR parametersmodes can be:modes can be:
GLUT_RGBGLUT_RGB GLUT_SINGLEGLUT_SINGLEGLUT_RGBAGLUT_RGBA GLUT_DOUBLEGLUT_DOUBLEGLUT_INDEXGLUT_INDEX GLUT_DEPTHGLUT_DEPTH……moremore
GLUTGLUT
void glutInitWindowSize(int width, int void glutInitWindowSize(int width, int height);height);
defines the size of the graphics defines the size of the graphics window (not yet created.window (not yet created.
width and height are in pixelswidth and height are in pixelsthis call maybe overridden by other this call maybe overridden by other
functionsfunctions
GLUTGLUT
voidvoid glutInitWindowPosition(int x, int y);glutInitWindowPosition(int x, int y);
defines the initial position of the graphics defines the initial position of the graphics window (not yet created)window (not yet created)x and y refer to the upper left corner of the x and y refer to the upper left corner of the displaydisplayx and y are in pixelsx and y are in pixelswindow boundaries are defined by x, y and window boundaries are defined by x, y and width, height (from glutInitWindowSize()width, height (from glutInitWindowSize()
GLUTGLUT
int glutCreateWindow(char *name);int glutCreateWindow(char *name);
creates (but does not display) a windowcreates (but does not display) a window
according current glutInitWindowSize()according current glutInitWindowSize()
and glutInitWindowPosition()and glutInitWindowPosition()
window not actually displayed until window not actually displayed until glutMainLoop()glutMainLoop()
GLUTGLUT
void glutDisplayFunc(void (*func)void));void glutDisplayFunc(void (*func)void));
registers a display functionregisters a display function
the display function includes the the display function includes the instructions to draw the object when instructions to draw the object when needed.needed.
display function is evoke when the image display function is evoke when the image needs to be redrawn (window changes, needs to be redrawn (window changes, state changes, etc.)state changes, etc.)
GLUTGLUT
void glutMainLoop();void glutMainLoop();
creates an event processing loopcreates an event processing loop
one event is to draw the image one event is to draw the image defined by the display functiondefined by the display function
this should be the last function in this should be the last function in main()main()
GLUTGLUT
void glutReshapeFunction(void (*f) (int void glutReshapeFunction(void (*f) (int width, int height);width, int height);
defines a callback function to respond defines a callback function to respond to change in the size of the current to change in the size of the current window.window.when a window is resized function f is when a window is resized function f is called and the width and height of the called and the width and height of the new window is passed to fnew window is passed to f
GLUTGLUT
void glutPostRedisplay()void glutPostRedisplay()
calls the display callback functioncalls the display callback function
similar to executing the display callback similar to executing the display callback directly, is a little smart about redrawing directly, is a little smart about redrawing the image.the image.
generally, it is preferable to call generally, it is preferable to call glutPostRedisplay() rather than the display glutPostRedisplay() rather than the display functionfunction
GLUTGLUT
void glutIdleFunc(void (*f) (void));void glutIdleFunc(void (*f) (void));
defines a function to call/execute when defines a function to call/execute when there are no events in the event queue.there are no events in the event queue.
glutIdleFunc(idler);glutIdleFunc(idler);
void idler() {void idler() {
glutPostRedisplay()glutPostRedisplay()
}}
GLUTGLUT
Using Text…Using Text…
GLUT – using textGLUT – using text
void glutBitMapCharacter(void *font, int void glutBitMapCharacter(void *font, int char)char)
renders the character char specified by its renders the character char specified by its ASCII code using font fontASCII code using font fontrenders at the current raster_position (state renders at the current raster_position (state variable)variable)
glutBitMapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, “A”);glutBitMapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, “A”);
not GLUTnot GLUT
void glRasterPos{2|3|4}{s|i|f|d}void glRasterPos{2|3|4}{s|i|f|d}(TYPE x, TYPE y, TYPE z, TYPE w);(TYPE x, TYPE y, TYPE z, TYPE w);
set the current raster position. the set the current raster position. the next character will be drawn there.next character will be drawn there.coordinates are world coordinatescoordinates are world coordinatescharacter drawing functions character drawing functions automatically update raster_positionautomatically update raster_position
not GLUTnot GLUT
void glutStrokeCharacter(void *font, int void glutStrokeCharacter(void *font, int char);char);
renders a vector (stroke) character in font renders a vector (stroke) character in font font.font.
predefined size about 100x100 in world predefined size about 100x100 in world coordinatescoordinates
can manipulate with primitive can manipulate with primitive transformation functions.transformation functions.
GLUTGLUT
void glutSwapBuffers();void glutSwapBuffers();
swaps front and back buffers.swaps front and back buffers.
assuming that we requested double assuming that we requested double buffering –buffering –
glutInitDisplayMode(GLUT_DOUBLE | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);GLUT_RGB);
GLUTGLUT
Keyboard InteractionKeyboard Interaction
GLUTGLUT
void glutKeyboardFunc(void *f(unsigned void glutKeyboardFunc(void *f(unsigned char key, int x, int y);char key, int x, int y);
defines a callback function to handle defines a callback function to handle keyboard key presses.keyboard key presses.the key (key) value is returned to the the key (key) value is returned to the function ffunction fthe position of the mouse (x,y) when the the position of the mouse (x,y) when the key was clicked is also returned to fkey was clicked is also returned to fx, y are in pixels (screen coordinates)x, y are in pixels (screen coordinates)
GLUTGLUT
void glutSpecialFunction(void (*f) (int key, void glutSpecialFunction(void (*f) (int key, int x, int y);int x, int y);
defines a callback function for handling defines a callback function for handling keypresses of special keyskeypresses of special keysreturns to f the special key pressed and returns to f the special key pressed and location of the mouse in pixels (screen location of the mouse in pixels (screen coordinates)coordinates)
if (key == GLUT_KEY_UP) exit(0);if (key == GLUT_KEY_UP) exit(0);
GLUTGLUT
int glutGetModifers();int glutGetModifers();
returns the state of modifer keys (alt, returns the state of modifer keys (alt, shift, control)shift, control)
values are GLUT_ACTIVE_SHIFT, values are GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALTGLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT
use inside keyboard event callbacksuse inside keyboard event callbacks
GLUTGLUT
Mouse InteractionMouse Interaction
GLUT - mouseGLUT - mousevoid glutMouseFunc(void (*f)(int button, void glutMouseFunc(void (*f)(int button, int int state, int x, int y);state, int x, int y);
Defines/registers a callback function f to Defines/registers a callback function f to handle mouse eventhandle mouse eventMouse event occurs when a mouse button is Mouse event occurs when a mouse button is clickedclickedstate can be GLUT_UP | GLUT_DOWNstate can be GLUT_UP | GLUT_DOWNbutton can be GLUT_LEFT_BUTTON, button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTONGLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTONx & y are in screen coordinatesx & y are in screen coordinates
GLUT - mouseGLUT - mouse
glutMouseFunc(mouse_catcher);glutMouseFunc(mouse_catcher);
void mouse_catcher(int x, int y, int button, int state) void mouse_catcher(int x, int y, int button, int state)
{{
if (state == GLUT_DOWN && if (state == GLUT_DOWN &&
button == GLUT_MIDDLE_BUTTON ) button == GLUT_MIDDLE_BUTTON ) exit();exit();
}}
GLUT - mouseGLUT - mouse
void glutMotionFunc(void (*f) (int x, int void glutMotionFunc(void (*f) (int x, int y));y));
defines/registers a callback to handle defines/registers a callback to handle mouse movement event…mouse movement event…
……when a button is held downwhen a button is held down
x & y are in screen coordinatesx & y are in screen coordinates
GLUT - mouseGLUT - mouse
void glutPassiveMotionFunc(void (*f) void glutPassiveMotionFunc(void (*f) (int (int x, int y));x, int y));
defines/registers a callback function to defines/registers a callback function to handle passive mouse movement handle passive mouse movement events…events…
……when a button is not held down.when a button is not held down.
x & y are in screen coordinates.x & y are in screen coordinates.
GLUT – mouseGLUT – mouse
void glutEntryFunc(void (*f) (int void glutEntryFunc(void (*f) (int state));state));
defines/registers a callback function f defines/registers a callback function f to handle entering/leaving eventsto handle entering/leaving eventsentering and leaving refers to entering entering and leaving refers to entering or leaving an opengl windowor leaving an opengl windowstate can be GLUT_ENTERED or state can be GLUT_ENTERED or GLUT_LEFTGLUT_LEFT
GLUTGLUT
Creating and Using MenusCreating and Using Menus
GLUT - menusGLUT - menus
GLUT has a limited ability to provide GLUT has a limited ability to provide interactive tools or widgets.interactive tools or widgets.
One type is does provide in the menuOne type is does provide in the menu
To use menus –To use menus –
define the menu entries (what do define the menu entries (what do say)say)
attach actions to each menu entryattach actions to each menu entry
attach a menu to a mouse buttonattach a menu to a mouse button
GLUT - menusGLUT - menus
int glutCreateMenu(void (*f) (int value));int glutCreateMenu(void (*f) (int value));
Define a top level menu that uses callback Define a top level menu that uses callback f.f.
f is passed the int value for the menu f is passed the int value for the menu entry.entry.
This call returns an int identifier for the This call returns an int identifier for the menumenu
The created menu becomes the current menuThe created menu becomes the current menu
GLUT - menusGLUT - menus
void glutSetMenu(int id);void glutSetMenu(int id);
sets menu id to be the current menusets menu id to be the current menu
GLUT - menusGLUT - menus
void glutAddMenuEntry(char *name, int void glutAddMenuEntry(char *name, int value);value);
Menus are initially created emptyMenus are initially created emptyAdd entries/options to menu with Add entries/options to menu with glutAddMenuEntry()glutAddMenuEntry()name is the displayed entry label.name is the displayed entry label.value is the int value returns to the menu value is the int value returns to the menu callback when this entry is selected.callback when this entry is selected.
GLUT - MenusGLUT - Menus
void glutAttachMenu(int button);void glutAttachMenu(int button);
Associates the current menu with a Associates the current menu with a specific mouse buttonspecific mouse buttonbutton can be button can be
GLUT_RIGHT_BUTTON GLUT_RIGHT_BUTTON GLUT_MIDDLE_BUTTON GLUT_MIDDLE_BUTTON GLUT_LEFT_BUTTONGLUT_LEFT_BUTTON
GLUT -menusGLUT -menus
glutCreateMenu(menu_handler);glutCreateMenu(menu_handler);glutAddMenuEntry(“Move Froggy Left”, 1);glutAddMenuEntry(“Move Froggy Left”, 1);glutAddMenuEntry(“Move Froggy Right”, 2);glutAddMenuEntry(“Move Froggy Right”, 2);glutAttachMenu(GLUT_MIDDLE_BUTTON);glutAttachMenu(GLUT_MIDDLE_BUTTON);
void menu_handler(int value);void menu_handler(int value);{{
if(value == 1) move_left();if(value == 1) move_left();if(value == 2) move_right();if(value == 2) move_right();
}}
GLUT - menusGLUT - menus
void glutAddSubMenu(char *name, void glutAddSubMenu(char *name, int menu);int menu);
This will add a submenu a the next This will add a submenu a the next entry in a menu.entry in a menu.It will be called nameIt will be called nameThe int value menu is the menu The int value menu is the menu value passed to the menu callbackvalue passed to the menu callback
GLUT – more on windowsGLUT – more on windows
Recall –Recall –
int glutCreateWindow(char *name);int glutCreateWindow(char *name);
creates a window called “name” and creates a window called “name” and returns a window int id as a handle returns a window int id as a handle for this windowfor this window
GLUT – more on windowsGLUT – more on windows
void glutDestroyWindow(int id);void glutDestroyWindow(int id);
Destroys with window with the id Destroys with window with the id value of id.value of id.
GLUT – more on windowsGLUT – more on windows
void glutSetWindow(int id);void glutSetWindow(int id);
Sets the window with id = id to be Sets the window with id = id to be the current window.the current window.
GLUT – more on windowGLUT – more on window
int glutCreateSubWindows(int parent, int int glutCreateSubWindows(int parent, int x, int y, int width, int height);x, int y, int width, int height);
You can also create subwindows to a window.You can also create subwindows to a window.This function creates a window that is a This function creates a window that is a subwindow to parent.subwindow to parent.The subwindow will have an origin a x, yThe subwindow will have an origin a x, yand will width x height in size.and will width x height in size.x, y, width and height are in screen x, y, width and height are in screen coordinatescoordinates
GLUT – more on windowsGLUT – more on windows
Some other GLUT windows functions of Some other GLUT windows functions of note-note-
glutReshapeWindow(int width, int glutReshapeWindow(int width, int height);height);
glutPositionWindow(int x, int y);glutPositionWindow(int x, int y);
glutHideWindow(void);glutHideWindow(void);
glutShowWindow(void);glutShowWindow(void);