More on GLUT Programming Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 381...

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

Transcript of More on GLUT Programming Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 381...

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

More on GLUT Programming

Glenn G. [email protected]

U. of Alaska Fairbanks

CS 381 Lecture NotesMonday, September 15, 2003

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

15 Sep 2003 CS 381 2

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.

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

15 Sep 2003 CS 381 3

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.

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

15 Sep 2003 CS 381 4

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.

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

15 Sep 2003 CS 381 5

intro2d.cpp:Function keyboardvoid 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?

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

15 Sep 2003 CS 381 6

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.

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

15 Sep 2003 CS 381 7

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.

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

15 Sep 2003 CS 381 8

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.

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

15 Sep 2003 CS 381 9

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.

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

15 Sep 2003 CS 381 10

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.

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

15 Sep 2003 CS 381 11

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.

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

15 Sep 2003 CS 381 12

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.

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

15 Sep 2003 CS 381 13

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.