Input with GLUT GLUT functions glut Init allows

  • Slides: 11
Download presentation
Input with GLUT

Input with GLUT

GLUT functions �glut. Init allows application to get command line arguments and initializes system

GLUT functions �glut. Init allows application to get command line arguments and initializes system �glu. Init. Display. Mode requests properties for the window (the rendering context) � RGB color � Single buffering � Properties logically ORed together �glut. Window. Size in pixels �glut. Window. Position from top-left corner of display �glut. Create. Window create window with title “simple” �glut. Display. Func display callback �glut. Main. Loop enter infinite event loop 2

main. c (in book) includes gl. h #include <GL/glut. h> int main(int argc, char**

main. c (in book) includes gl. h #include <GL/glut. h> int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE|GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(0, 0); define window properties glut. Create. Window("simple"); glut. Display. Func(mydisplay); display callback init(); set Open. GL state glut. Main. Loop(); } enter event loop Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 3

Event Types �Window: resize, expose, iconify �Mouse: click one or more buttons �Motion: move

Event Types �Window: resize, expose, iconify �Mouse: click one or more buttons �Motion: move mouse �Keyboard: press or release a key �Idle: nonevent �Define what should be done if no other event is in queue Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 4

Callbacks �Programming interface for event-driven input �Define a callback function for each type of

Callbacks �Programming interface for event-driven input �Define a callback function for each type of event the graphics system recognizes �This user-supplied function is executed when the event occurs �GLUT example: glut. Mouse. Func(mymouse) mouse callback function Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 5

GLUT callbacks GLUT recognizes a subset of the events recognized by any particular window

GLUT callbacks GLUT recognizes a subset of the events recognized by any particular window system (Windows, X, Macintosh) �glut. Display. Func �glut. Mouse. Func �glut. Reshape. Func �glut. Keyboard. Func �glut. Idle. Func �glut. Motion. Func, glut. Passive. Motion. Func Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 6

GLUT Event Loop �Recall that the last line in main. c for a program

GLUT Event Loop �Recall that the last line in main. c for a program using GLUT must be glut. Main. Loop(); which puts the program in an infinite event loop �In each pass through the event loop, GLUT �looks at the events in the queue �for each event in the queue, GLUT executes the appropriate callback function if one is defined �if no callback is defined for the event, the event is ignored Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 7

The display callback �The display callback is executed whenever GLUT determines that the window

The display callback �The display callback is executed whenever GLUT determines that the window should be refreshed, for example �When the window is first opened �When the window is reshaped �When a window is exposed �When the user program decides it wants to change the display �In main. c �glut. Display. Func(mydisplay) identifies the function to be executed �Every GLUT program must have a display callback Angel: Interactive Computer Graphics 5 E © Addison-Wesley 2009 8

Double Buffering �Instead of one color buffer, we use two �Front Buffer: one that

Double Buffering �Instead of one color buffer, we use two �Front Buffer: one that is displayed but not written to �Back Buffer: one that is written to but not displayed �Program then requests a double buffer in main. c �glut. Init. Display. Mode(GL_RGB | GL_DOUBLE) �At the end of the display callback buffers are swapped void mydisplay() { gl. Clear(GL_COLOR_BUFFER_BIT|…. ). /* draw graphics here */. glut. Swap. Buffers() } 9

Using the idle callback �The idle callback is executed whenever there are no events

Using the idle callback �The idle callback is executed whenever there are no events in the event queue �glut. Idle. Func(myidle) �Useful for animations void myidle() { /* change something */ t += dt glut. Post. Redisplay(); } Void mydisplay() { gl. Clear(); /* draw something that depends on t */ glut. Swap. Buffers(); } 10

Using globals �The form of all GLUT callbacks is fixed �void mydisplay() �void mymouse(GLint

Using globals �The form of all GLUT callbacks is fixed �void mydisplay() �void mymouse(GLint button, GLint state, GLint x, GLint y) �Must use globals to pass information to callbacks float t; /*global */ void mydisplay() { /* draw something that depends on t } 11