Input and GLUTs Callbacks - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Input and GLUTs Callbacks

Description:

All the GLUT supported I/O routines use callbacks to return information to the program. ... the y position of the mouse when the key was pressed. Mouse/Locator Input ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 27
Provided by: geraldr6
Category:

less

Transcript and Presenter's Notes

Title: Input and GLUTs Callbacks


1
Input and GLUTs Callbacks
  • EECS 4530 Computer Graphics ISeptember 9, 2002

2
Characteristics
  • All the GLUT supported I/O routines use callbacks
    to return information to the program.
  • This type of input would be known as event mode
    input.
  • Request mode wait at a given point in a program
    for input
  • Sample mode Go out and get an input state at an
    instant in time dont wait for input.

3
Keyboard
  • Register Callback withglutKeyboardFunc ( void
    (func) (unsigned char key, int x, int y))
  • Routine receives
  • key code (unsigned character) ASCII
  • the x position of the mouse when the key was
    pressed
  • the y position of the mouse when the key was
    pressed

4
Mouse/Locator Input
  • glutMouseFunc attaches a callback to button
    press events.
  • glutMotionFunc returns location information
    when a mouse is moved while the mouse button is
    down.
  • glutPassiveMotionFunc returns location
    information when the mouse is moved with no
    buttons down.

5
Special keys
  • glutSpecialFunc (void (func) (int key, int x,
    int y))
  • Returns key information for special keys.
  • GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3,
    GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,GLUT_KEY_F7
    , GLUT_KEY_F8, GLUT_KEY_F9,GLUT_KEY_F10,
    GLUT_KEY_F11, GLUT_KEY_F12
  • GLUT_KEY_UP, GLUT_KEY_DOWN, GLUT_KEY_RIGHT,
    GLUT_KEY_LEFT
  • GLUT_KEY_HOME, GLUT_KEY_END, GLUT_KEY_PAGE_UP,
    GLUT_KEY_PAGE_DOWN

6
A Simple Example
  • include "glut.h"
  • include ltcmathgt
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • void getKey( unsigned char, int, int)
  • void getButtonPress ( int, int, int, int)
  • void getSpecialKey(int, int, int)
  • void init (void)
  • void display (void)

7
main
  • int main (int argCount, char argValues)
  • /
  • Initialize the package (takes arguments from
    the command line
  • /
  • glutInit (argCount, argValues)
  • /
  • Initialize the display, and the window
    position
  • /
  • glutInitDisplayMode ( GLUT_SINGLE GLUT_RGB)
  • glutInitWindowSize (400, 400)
  • glutInitWindowPosition (100, 100)

8
main (contd)
  • /
  • Create the actual window
  • /
  • glutCreateWindow (argValues0)
  • init()
  • /
  • hook up the display function to the GLUT
    package. You have
  • one function you can hook up this way.
  • /
  • glutDisplayFunc (display)

9
main (contd)
  • / Hook up the two functions for keyboard input
  • /
  • glutKeyboardFunc (getKey)
  • glutSpecialFunc (getSpecialKey)
  • /
  • Add the mouse routine...
  • /
  • glutMouseFunc (getButtonPress)
  • /
  • Start responding to events.
  • /
  • glutMainLoop ( )
  • return EXIT_SUCCESS

10
init
  • void init()
  • glClearColor(0.0, 0.0, 0.0, 0.0)/ background
    color/
  • / Simple projection 6 by 6 by 20 cube
  • /
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(-3.0, 3.0, -3.0, 3.0, -10.0, 10.0)
  • / Return to drawing mode (modelview)
  • /
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()

11
getKey
  • void getKey(unsigned char inChar, int xposition,
    int yposition)
  • / Get the key press from the user. If you need
    to get a ctrl or alt key you will need
  • glutGetModifiers to find the state of the
    keys. /
  • if ( inChar 'q')
  • cout ltlt "Exitting..." ltlt endl
  • exit(0)
  • else
  • cout ltlt "Key " ltlt inChar ltlt " pressed " ltlt
    endl
  • cout ltlt "\tThis occurred at position (" ltlt
    xposition ltlt " ," ltlt yposition ltlt ")" ltlt
    endl

12
getSpecialKey
  • void getSpecialKey (int inKey, int xposition, int
    yposition)
  • / Grab the special keys. If you need the
    modifiers
  • you need to call glutGetModifiers()
  • /
  • if (inKey GLUT_KEY_F1)
  • cout ltlt "F1 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F2)
  • cout ltlt "F2 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F3)
  • cout ltlt "F3 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F4)
  • cout ltlt "F4 Key Pressed" ltlt endl

13
getSpecialKey (contd)
  • else if (inKey GLUT_KEY_F5)
  • cout ltlt "F5 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F6)
  • cout ltlt "F6 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F7)
  • cout ltlt "F7 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F8)
  • cout ltlt "F8 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F9)
  • cout ltlt "F9 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F10)
  • cout ltlt "F10 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_F11)
  • cout ltlt "F11 Key Pressed" ltlt endl

14
getSpecialKey (contd)
  • else if (inKey GLUT_KEY_F12)
  • cout ltlt "F12 Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_LEFT)
  • cout ltlt "Left arrow Key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_UP)
  • cout ltlt "Up arrow key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_DOWN)
  • cout ltlt "Down arrow key Pressed" ltlt endl
  • else if (inKey GLUT_KEY_RIGHT)
  • cout ltlt "Right arrow key pressed" ltlt endl
  • cout ltlt "\tThis occurred at position (" ltlt
    xposition ltlt " ," ltlt yposition ltlt ")" ltlt endl

15
getButtonPress
  • void getButtonPress (int buttonPressed, int
    state, int xposition, int yposition)
  • / Handle a button press from the mouse...
  • This routine doesn't do anything useful --
    just
  • reports info. /
  • string buttonState, buttonName
  • if (buttonPressed GLUT_MIDDLE_BUTTON)
  • buttonName "middle"
  • else if (buttonPressed GLUT_LEFT_BUTTON)
  • buttonName "left"
  • else if (buttonPressed GLUT_RIGHT_BUTTON)
  • buttonName "right"

16
getButtonPress (contd)
  • if (state GLUT_UP)
  • buttonState "released"
  • else if (state GLUT_DOWN)
  • buttonState "pressed"
  • cout ltlt "The " ltlt buttonName ltlt " mouse button
    was " ltlt buttonState ltlt endl
  • cout ltlt "\tThis occurred at position (" ltlt
    xposition ltlt " ," ltlt yposition ltlt ") ltlt
    endl

17
display
  • void display ()
  • /
  • Simple display -- just output the teapot...
  • /
  • glClear(GL_COLOR_BUFFER_BIT)
  • glutWireTeapot(1.0)
  • glFlush()

18
Creating a Menu
  • glutCreateMenu(void (func)(int value))
  • glutAddMenuEntry ( char name, int value)
  • glutAddSubMenu()
  • glutAttachMenu(int button)
  • glutDetachMenu(int button)
  • glutRemoveMenuItem(int entry)

19
Menu Example 1
  • include "glut.h"
  • include ltcmathgt
  • include ltiostreamgt
  • using namespace std
  • void display()
  • void init()
  • void menuHandler(int)

20
main
  • int main (int argCount, char argValues)
  • int menuID
  • / Initialize the package /
  • glutInit (argCount, argValues)
  • / Initialize the display, and the window
    position /
  • glutInitDisplayMode ( GLUT_SINGLE GLUT_RGB)
  • glutInitWindowSize (400, 400)
  • glutInitWindowPosition (100, 100)

21
main
  • / Create the actual window
  • /
  • glutCreateWindow (argValues0)
  • init()
  • / Hook up the display function to the GLUT
  • package.
  • /
  • glutDisplayFunc (display)

22
main (contd)
  • / Build a simple menu /
  • menuID glutCreateMenu(menuHandler)
  • glutAddMenuEntry("Red", 1)
  • glutAddMenuEntry("Green", 2)
  • glutAddMenuEntry("Blue", 3)
  • glutAddMenuEntry("White", 4)
  • glutAddMenuEntry("Black", 5)
  • glutAttachMenu (GLUT_RIGHT_BUTTON)
  • / Start responding to events.
  • /
  • glutMainLoop ( )
  • return EXIT_SUCCESS

23
init
  • void init()
  • glClearColor(0.0, 0.0, 0.0, 0.0)/ background
    color/
  • / Simple projection -- 6 by 6 by 20 cube
  • /
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(-3.0, 3.0, -3.0, 3.0, -10.0, 10.0)
  • /
  • Return to drawing mode (modelview)
  • /
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()

24
display
  • void display ()
  • /
  • Simple display -- just output the teapot...
  • /
  • glClear(GL_COLOR_BUFFER_BIT)
  • glutWireTeapot(1.0)
  • glFlush()

25
menuHandler
  • void menuHandler(int itemPicked)
  • / Simple handler that changes color drawn
    with.
  • /
  • switch (itemPicked)
  • case 1 / red /
  • glColor3f(1.0, 0.0, 0.0)
  • break
  • case 2 / green /
  • glColor3f(0.0, 1.0, 0.0)
  • break
  • case 3 / blue /
  • glColor3f(0.0, 0.0, 1.0)
  • break

26
menuHandler (contd)
  • case 4 / white /
  • glColor3f(1.0, 1.0, 1.0)
  • break
  • case 5 / black /
  • glColor3f(0.0, 0.0, 0.0)
  • break
  • default
  • cout ltlt "Unexpected Choice " ltlt itemPicked ltlt
    endl
  • break
  • glutPostRedisplay() // force a redraw
Write a Comment
User Comments (0)
About PowerShow.com