cs 470 introduction to computer graphics

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

Upload: sofia

Post on 17-Jan-2016

26 views

Category:

Documents


2 download

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 Presentation

TRANSCRIPT

Page 1: CS 470 Introduction to Computer Graphics

CS 470 Introduction to CS 470 Introduction to Computer GraphicsComputer Graphics

Using GLUT and a few other Using GLUT and a few other thingsthings

Page 2: CS 470 Introduction to Computer Graphics

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

Page 3: CS 470 Introduction to Computer Graphics

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

Page 4: CS 470 Introduction to Computer Graphics

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

Page 5: CS 470 Introduction to Computer Graphics

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

Page 6: CS 470 Introduction to Computer Graphics

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

Page 7: CS 470 Introduction to Computer Graphics

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

Page 8: CS 470 Introduction to Computer Graphics

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

Page 9: CS 470 Introduction to Computer Graphics

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

Page 10: CS 470 Introduction to Computer Graphics

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

Page 11: CS 470 Introduction to Computer Graphics

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

Page 12: CS 470 Introduction to Computer Graphics

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

Page 13: CS 470 Introduction to Computer Graphics

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

Page 14: CS 470 Introduction to Computer Graphics

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

Page 15: CS 470 Introduction to Computer Graphics

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

}}

Page 16: CS 470 Introduction to Computer Graphics

GLUTGLUT

Using Text…Using Text…

Page 17: CS 470 Introduction to Computer Graphics

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

Page 18: CS 470 Introduction to Computer Graphics

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

Page 19: CS 470 Introduction to Computer Graphics

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.

Page 20: CS 470 Introduction to Computer Graphics

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

Page 21: CS 470 Introduction to Computer Graphics

GLUTGLUT

Keyboard InteractionKeyboard Interaction

Page 22: CS 470 Introduction to Computer Graphics

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)

Page 23: CS 470 Introduction to Computer Graphics

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

Page 24: CS 470 Introduction to Computer Graphics

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

Page 25: CS 470 Introduction to Computer Graphics

GLUTGLUT

Mouse InteractionMouse Interaction

Page 26: CS 470 Introduction to Computer Graphics

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

Page 27: CS 470 Introduction to Computer Graphics

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();

}}

Page 28: CS 470 Introduction to Computer Graphics

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

Page 29: CS 470 Introduction to Computer Graphics

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.

Page 30: CS 470 Introduction to Computer Graphics

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

Page 31: CS 470 Introduction to Computer Graphics

GLUTGLUT

Creating and Using MenusCreating and Using Menus

Page 32: CS 470 Introduction to Computer Graphics

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

Page 33: CS 470 Introduction to Computer Graphics

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

Page 34: CS 470 Introduction to Computer Graphics

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

Page 35: CS 470 Introduction to Computer Graphics

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.

Page 36: CS 470 Introduction to Computer Graphics

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

Page 37: CS 470 Introduction to Computer Graphics

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();

}}

Page 38: CS 470 Introduction to Computer Graphics

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

Page 39: CS 470 Introduction to Computer Graphics

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

Page 40: CS 470 Introduction to Computer Graphics

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.

Page 41: CS 470 Introduction to Computer Graphics

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.

Page 42: CS 470 Introduction to Computer Graphics

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

Page 43: CS 470 Introduction to Computer Graphics

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

Page 44: CS 470 Introduction to Computer Graphics