Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.

Similar presentations


Presentation on theme: "More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003."— Presentation transcript:

1 More on GLUT Programming Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003

2 15 Sep 2003CS 3812 Review: Introduction to GLUT [1/2] GLUT is a package, separate from OpenGL, that we use in this class to: Allow OS-independent CG programs. Simplify certain OpenGL operations. Introduce simple event-driven GUI programming. GLUT is not intended for professional quality, production code. However, the concepts we cover when learning GLUT (event-driven programming ideas) are relevant to programming with any GUI package.

3 15 Sep 2003CS 3813 Review: Introduction to GLUT [2/2] GLUT programs are organized differently from most programs you have seen before. GLUT handles overall flow of control. You write callback functions to handle events: Display needed. Keypress. Idle (nothing interesting has happened). Etc … When an event happens, GLUT calls the appropriate function. Thus, it is important that your function properly handles the event it is to be called for.

4 15 Sep 2003CS 3814 Review: intro2d.cpp We discussed intro2d.cpp, a simple OpenGL/GLUT program. Five functions: main, init, display, keyboard, idle. Functions main & init do initialization. Function display does drawing … … and only drawing, and nothing else does drawing. Remember THE RULES. Today, we look at keyboard & idle.

5 15 Sep 2003CS 3815 intro2d.cpp : Function keyboard void keyboard(unsigned char key, int x, int y) { switch (key) { case ESCKEY: // ESC: Quit exit(0); break; } GLUT calls the keyboard function whenever an ASCII keypress happens. Use the GLUT “special” function for non-ASCII (like arrow keys). The ASCII value of the keypress is in key. Mouse position is in x, y. Your keyboard (or special) function will usually be one big switch. Why did I put in the (unnecessary) break ?

6 15 Sep 2003CS 3816 intro2d.cpp : Function idle // Print OpenGL errors, if there are any (for debugging) if (GLenum err = glGetError()) { cerr << "OpenGL ERROR: " << gluErrorString(err) << endl; } GLUT calls the idle function whenever nothing else needs doing. It is useful for making time-dependent changes to the scene. Here, we check for OpenGL errors, and print a message if there is an error. This is a rather atypical use of the idle function. We will see a more “normal” use of idle in a few minutes.

7 15 Sep 2003CS 3817 Making a Changing Display: Overview All the programs you have seen so far in this class have had a static display. Now, we look at how to make the display change: Based on keyboard input. Using the keyboard (or special) function. Automatically. Using the idle function. We will add these new features to intro2d.cpp. We’re jumping ahead of the text a bit here.

8 15 Sep 2003CS 3818 Making a Changing Display: Keyboard Input [1/3] Suppose we wish to have the display change when a key is pressed. Keypress events are handled by the GLUT keyboard function. Actually, keyboard for ASCII keys, special for non-ASCII. So the keyboard function needs to do what is necessary to see that the display is updated. But, the keyboard function is not allowed to call the display function. That’s in THE RULES. Thus, the keyboard function needs to: Make sure that GLUT will call the display function. Make sure that, when the display function is called, it knows to re-draw the window in a different manner.

9 15 Sep 2003CS 3819 Making a Changing Display: Keyboard Input [2/3] The keyboard function needs to make sure that GLUT will call the display function. The keyboard function needs to post a redisplay event. This is done by calling glutPostRedisplay. Function glutPostRedisplay takes no parameters. The keyboard function needs to make sure that, when the display function is called, it knows to re-draw the window in a different manner. Since the display function takes no parameters, we can only communicate the change to the display function via a global variable.

10 15 Sep 2003CS 38110 Making a Changing Display: Keyboard Input [3/3] Now we put it all together. To add keypress-based display changes to a GLUT program: A global variable is needed to hold the current state of whatever part of the display is to change. Declare the variable. Initialize this variable to an appropriate value somewhere. In its declaration? In the init function? In the display function: Use the value of this variable. Draw whatever should be drawn, according to the current value of the variable. Do not change the variable in the display function. In the keyboard function, when the appropriate key is pressed: Change the value of the variable. Call “ glutPostRedisplay(); ”. Do not call the display function.

11 15 Sep 2003CS 38111 EXAMPLE 1 Modify intro2d.cpp to allow a keypress to change the display. Modified the program so that hitting the space bar toggled the color: red to blue and vice-versa.

12 15 Sep 2003CS 38112 Making a Changing Display: Automatic Changing Suppose we simply want the display to change automatically, regardless of any input. This is what the idle function is for. Recall: GLUT calls the idle function, repeatedly, when no other event is happening. Thus, this is handled just like keyboard input, except: We modify the idle function instead of the keyboard function. We do not need to worry about the ASCII value of a key.

13 15 Sep 2003CS 38113 EXAMPLE 2 Modify intro2d.cpp to change the display automatically. Modified the program so that the square moves to the right. Further modified it so that the square bounces off the sides of the window.


Download ppt "More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003."

Similar presentations


Ads by Google