15
CS 450: COMPUTER GRAPHICS GLUT INPUT FUNCTIONS SPRING 2015 DR. MICHAEL J. REALE

CS 450: COMPUTER GRAPHICS GLUT INPUT FUNCTIONS SPRING 2015 DR. MICHAEL J. REALE

Embed Size (px)

Citation preview

CS 450: COMPUTER GRAPHICS

GLUT INPUT FUNCTIONSSPRING 2015

DR. MICHAEL J. REALE

INTRODUCTION

• Apart from creating the window, GLUT also allows accessing the mouse and keyboard as well as creating popup menus.

MOUSE COORDINATES: UP IS DOWN

• All mouse coordinates in GLUT are in SCREEN COORDINATES with Y INVERTED (so up is down)

• Relative to TOP-LEFT window corner!

• If storing, do something like this:

• currentYMouse = windowHeight – yMouse;

MOUSE BUTTONS

• glutMouseFunc(mouseFunction);

• void mouseFunction(GLint button, GLint action, GLint xMouse, GLint yMouse)

• Called when a mouse button is FIRST PRESSED or RELEASED

• Mouse click = two events (a press and release)

• GLint button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON

• GLint action either GLUT_DOWN or GLUT_UP

• GLint xMouse, yMouse mouse coordinates at time of event

MOUSE MOVING

• If the MOUSE is MOVING and ANY MOUSE BUTTON is DOWN:

• glutMotionFunc(mouseMovingButton);

• void mouseMovingButton(GLint xMouse, GLint yMouse)

• If the MOUSE is MOVING and NO MOUSE BUTTONS are DOWN:

• glutPassiveMotionFunc(mouseMovingNoButton);

• void mouseMovingNoButton(GLint xMouse, GLint yMouse)

• Usually means you have to keep track of what mouse buttons are down with your glutMouseFunc() callback

• E.g., have a global called “leftMouseDown” and set it in mouseFunction()

KEYBOARD: REGULAR KEYS

• glutKeyboardFunc(keyboard)

• void keyboard(GLubyte key, GLint xMouse, GLint yMouse)

• Function called when the user PRESSES a key.

• NOTE: It will CONTINUE to be called if the user keeps holding the key down!

• GLubyte key = character value or ASCII code for key pressed

• GLint xMouse, yMouse = mouse coordinates at time of key press

KEYBOARD: SPECIAL KEYS

• glutSpecialFunc(specialKeyboard)

• Void specialKeyboard(GLint specialKey, GLint xMouse, GLint yMouse)

• Uses for special-purpose keys like the function keys (F1, F2, etc.) and arrow keys

• GLint specialKey = integer-valued GLUT symbolic constant

• Examples: GLUT_KEY_F1, GLUT_KEY_UP, GLUT_KEY_HOME, etc.

• GLint xMouse, yMouse = mouse coordinates at time of key press

POPUP MENUS

• GLUT also allows you to create popup menus and submenus

• These are generally set up in the same place where you register the GLUT callback functions

• However, you can also add/remove items in other parts of your code

CREATING, SETTING, AND DESTROY A MENU• GLuint menuID = glutCreateMenu(menuFunction)

• void menuFunction(GLint menuItemNumber)

• Called when a menu item is clicked on

• GLint menuItemNumber = index of menu item

• In function, decide what to do for each menu item

• glutSetMenu(menuID)

• Sets menu as current menu for display window

• glutDestroyMenu(menuID)

• Destroys menu

• If menu happens to be the current menu for the display window window now has no menu assigned

GETTING CURRENT MENU ID

• currentMenuID = glutGetMenu()

• Returns current menu attached to display window (or 0 if no menu is attached)

ADDING MENU ENTRIES

• glutAddMenuEntry(charString, menuItemNumber)

• Add a menu item to current menu (determined by either last call to glutCreateMenu() or by glutSetMenu())

• charString = text for menu entry

• menuItemNumber = menu item number

• NOTE: Order you add them in determines order of display

• ALSO NOTE: ID does NOT have to follow order of display (e.g., first item could have an ID of 500)

• FURTHER NOTE: Two or more items can have the SAME ID

ATTACHING A MOUSE BUTTON

• We also need to determine which mouse button will be used to open the menu:

• glutAttachMenu(button)

• button = can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON

CREATING SUBMENUS

• Just create another menu:

• GLuint submenuID = glutCreateMenu(subMenuFunction);

• glutAddMenuEntry(“First submenu item”, 1);

• …

• Then, add menu to main menu:

• glutCreateMenu(menuFunction);

• …

• glutAddSubMenu(“Submenu Option”, submenuID);

CHANGING THE MENU MOUSE BUTTON

• To change the mouse button used to open menus, first detach it:

• glutDetachMenu(mouseButton);

• Then attach the button you want

DELETING MENU ITEMS

• glutRemoveMenuItem(itemNumber)

• Removes itemNumber from the current menu

• If multiple items have the same number, will delete one of them