Module 3 Part 1
Module 3 Part 1
From Chapter 20 of Computer Graphics with OpenGL®, Fourth Edition, Donald Hearn, M. Pauline Baker, Warren R. Carithers.
Copyright © 2011 by Pearson Education, Inc. Published by Pearson Prentice Hall. All rights reserved.
591
Interactive Input Methods and Graphical User Interfaces
Locator Devices
Interactive selection of a coordinate point is usually accomplished by positioning
the screen cursor at some location in a displayed scene, although other meth-
ods, such as menu options, could be used in certain applications. We can use
a mouse, touchpad, joystick, trackball, spaceball, thumbwheel, dial, hand cur-
sor, or digitizer stylus for screen-cursor positioning. In addition, various but-
tons, keys, or switches can be used to indicate processing options for the selected
location.
Keyboards are used for locator input in several ways. A general-purpose
keyboard usually has four cursor-control keys that move the screen cursor up,
down, left, and right. With an additional four keys, we can move the cursor
diagonally as well. Rapid cursor movement is accomplished by holding down
the selected cursor key. Sometimes a keyboard includes a touchpad, joystick,
trackball, or other device for positioning the screen cursor. For some applications,
it may also be convenient to use a keyboard to type in numerical values or other
codes to indicate coordinate positions.
Other devices, such as a light pen, have also been used for interactive
input of coordinate positions. But light pens record screen positions by detect-
ing light from the screen phosphors, and this requires special implementation
procedures.
592
Interactive Input Methods and Graphical User Interfaces
Stroke Devices
This class of logical devices is used to input a sequence of coordinate positions,
and the physical devices used for generating locator input are also used as stroke
devices. Continuous movement of a mouse, trackball, joystick, or hand cursor
is translated into a series of input coordinate values. The graphics tablet is one
of the more common stroke devices. Button activation can be used to place the
tablet into “continuous” mode. As the cursor is moved across the tablet surface,
a stream of coordinate values is generated. This procedure is used in paintbrush
systems to generate drawings using various brush strokes. Engineering systems
also use this process to trace and digitize layouts.
String Devices
The primary physical device used for string input is the keyboard. Character
strings in computer-graphics applications are typically used for picture or graph
labeling.
Other physical devices can be used for generating character patterns for spe-
cial applications. Individual characters can be sketched on the screen using a
stroke or locator-type device. A pattern recognition program then interprets the
characters using a stored dictionary of predefined patterns.
Valuator Devices
We can employ valuator input in a graphics program to set scalar values for
geometric transformations, viewing parameters, and illumination parameters. In
some applications, scalar input is also used for setting physical parameters such
as temperature, voltage, or stress-strain factors.
A typical physical device used to provide valuator input is a panel of control
dials. Dial settings are calibrated to produce numerical values within some pre-
defined range. Rotary potentiometers convert dial rotation into a corresponding
voltage, which is then translated into a number within a defined scalar range,
such as −10.5 to 25.5. Instead of dials, slide potentiometers are sometimes used
to convert linear movements into scalar values.
Any keyboard with a set of numeric keys can be used as a valuator device.
Although dials and slide potentiometers are more efficient for fast input.
Joysticks, trackballs, tablets, and other interactive devices can be adapted for
valuator input by interpreting pressure or movement of the device relative to a
scalar range. For one direction of movement, say left to right, increasing scalar
values can be input. Movement in the opposite direction decreases the scalar input
value. Selected values are usually echoed on the screen for verification.
Another technique for providing valuator input is to display graphical rep-
resentations of sliders, buttons, rotating scales, and menus on the video monitor.
Cursor positioning, using a mouse, joystick, spaceball, or other device, can be
used to select a value on one of these valuators. As a feedback mechanism for the
user, selected values are usually displayed in text or color fields elsewhere within
the graphical display belonging to the application.
Choice Devices
Menus are typically used in graphics programs to select processing options,
parameter values, and object shapes that are to be used in constructing a picture.
Commonly used choice devices for selecting a menu option are cursor-positioning
devices such as a mouse, trackball, keyboard, touch panel, or button box.
Keyboard function keys or separate button boxes are often used to enter
menu selections. Each button or function key is programmed to select a particular
593
Interactive Input Methods and Graphical User Interfaces
Pick Devices
We use a pick device to select a part of a scene that is to be transformed or edited
in some way. Several different methods can be used to select a component of a
displayed scene, and any input mechanism used for this purpose is classified as a
pick device. Most often, pick operations are performed by positioning the screen
cursor. Using a mouse, joystick, or keyboard, for example, we can perform picking
by positioning the screen cursor and pressing a button or key to record the pixel
coordinates. This screen position can then be used to select an entire object, a facet
of a tessellated surface, a polygon edge, or a vertex. Other pick methods include
highlighting schemes, selecting objects by name, or a combination of methods.
Using the cursor-positioning approach, a pick procedure could map a selected
screen position to a world-coordinate location using the inverse viewing and
geometric transformations that were specified for the scene. Then, the world-
coordinate position can be compared to the coordinate extents of objects. If the
pick position is within the coordinate extents of a single object, the pick object
has been identified. The object name, coordinates, or other information about the
object can then be used to apply the desired transformation or editing operations.
But if the pick position is within the coordinate extents of two or more objects,
further testing is necessary. Depending on the type of object to be selected and the
complexity of a scene, several levels of search may be required to identify the pick
object. For example, if we are attempting to pick a sphere whose coordinate extents
overlap the coordinate extents of some other three-dimensional object, the pick
position could be compared to the coordinate extents of the individual surface
facets of the two objects. If this test fails, the coordinate extents of individual line
d1 segments can be tested.
d2 When coordinate-extent tests do not uniquely identify a pick object, the
(x, y) distances from the pick position to individual line segments could be computed.
Figure 1 illustrates a pick position that is within the coordinate extents of
FIGURE 1 two line segments. For a two-dimensional line segment with pixel endpoint
Distances to line segments from a pick coordinates (x1 , y1 ) and (x2 , y2 ), the perpendicular distance squared from a pick
position. position (x, y) to the line is calculated as
[x(y − y1 ) − y(x − x1 )]2
d2 = (2)
x 2 + y2
594
Interactive Input Methods and Graphical User Interfaces
595
Interactive Input Methods and Graphical User Interfaces
Input Modes
Some input functions in an interactive graphics system are used to specify how
the program and input devices should interact. A program could request input
at a particular time in the processing (request mode), or an input device could
independently provide updated input (sample mode), or the device could inde-
pendently store all collected data (event mode).
In request mode, the application program initiates data entry. When
input values are requested, processing is suspended until the required values are
received. This input mode corresponds to the typical input operation in a general
programming language. The program and the input devices operate alternately.
Devices are put into a wait state until an input request is made; then the program
waits until the data are delivered.
In sample mode, the application program and input devices operate inde-
pendently. Input devices may be operating at the same time that the program is
processing other data. New values obtained from the input devices replace pre-
viously input data values. When the program requires new data, it samples the
current values that have been stored from the device input.
In event mode, the input devices initiate data input to the application pro-
gram. The program and the input devices again operate concurrently, but now
the input devices deliver data to an input queue, also called an event queue. All
input data is saved. When the program requires new data, it goes to the data
queue.
Typically, any number of devices can be operating at the same time in sample
and event modes. Some can be operating in sample mode, while others are operat-
ing in event mode. But only one device at a time can deliver input in request mode.
Other functions in the input library are used to specify physical devices
for the logical data classes. The input procedures in an interactive package can
involve complicated processing for some kinds of input. For instance, to obtain a
world-coordinate position, the input procedures must process an input screen
location back through the viewing and other transformations to the original
world-coordinate description of a scene. This processing also involves informa-
tion from the display-window routines.
Echo Feedback
Requests can usually be made in an interactive input program for an echo of input
data and associated parameters. When an echo of the input data is requested, it is
displayed within a specified screen area. Echo feedback can include, for example,
the size of the pick window, the minimum pick distance, the type and size of
a cursor, the type of highlighting to be employed during pick operations, the
range (mininum and maximum) for valuator input, and the resolution (scale) for
valuator input.
Callback Functions
For device-independent graphics packages, a limited set of input functions can be
provided in an auxiliary library. Input procedures can then be handled as callback
functions that interact with the system software. These functions specify what
actions are to be taken by a program when an input event occurs. Typical input
events are moving a mouse, pressing a mouse button, or pressing a key on the
keyboard.
596
Interactive Input Methods and Graphical User Interfaces
5 Virtual-Reality Environments
A typical virtual-reality environment is illustrated in Color Plate 24. Interac-
tive input is accomplished in this environment with a data glove, which is capable
of grasping and moving objects displayed in a virtual scene. The computer-gener-
ated scene is displayed through a head-mounted viewing system as a stereographic
projection. Tracking devices compute the position and orientation of the headset
and data glove relative to the object positions in the scene. With this system, a user
can move through the scene and rearrange object positions with the data glove.
Another method for generating virtual scenes is to display stereographic
projections on a raster monitor, with the two stereographic views displayed on
alternate refresh cycles. The scene is then viewed through stereographic glasses.
Interactive object manipulations can again be accomplished with a data glove
and a tracking device to monitor the glove position and orientation relative to the
position of objects in the scene.
600
Interactive Input Methods and Graphical User Interfaces
glutMouseFunc (mouseFcn);
This mouse callback procedure, which we named mouseFcn, has four arguments:
#include <GL/glut.h>
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
601
Interactive Input Methods and Graphical User Interfaces
void mousePtPlot (GLint button, GLint action, GLint xMouse, GLint yMouse)
{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
plotPoint (xMouse, winHeight - yMouse);
glFlush ( );
}
init ( );
glutDisplayFunc (displayFcn);
glutReshapeFunc (winReshapeFcn);
glutMouseFunc (mousePtPlot);
glutMainLoop ( );
}
602
Interactive Input Methods and Graphical User Interfaces
where (xMouse, yMouse) is the mouse location in the display window relative to
the top-left corner, when the mouse is moved with a button pressed.
Similarly, we can perform some action when we move the mouse within the
display window without pressing a button:
glutPassiveMotionFunc (fcnDoSomethingElse);
glutKeyboardFunc (keyFcn);
#include <GL/glut.h>
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
605
Interactive Input Methods and Graphical User Interfaces
init ( );
glutDisplayFunc (displayFcn);
glutReshapeFunc (winReshapeFcn);
glutKeyboardFunc (curveDrawing);
glutMainLoop ( );
}
606
Interactive Input Methods and Graphical User Interfaces
For function keys, arrow keys, and other special-purpose keys, we can use
the command
glutSpecialFunc (specialKeyFcn);
#include <GL/glut.h>
#inclue <stdlib.h>
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
607
Interactive Input Methods and Graphical User Interfaces
case GLUT_KEY_F3:
edgeLength /= 4;
break;
default:
break;
}
}
init ( );
glutDisplayFunc (displayFcn);
glutReshapeFunc (winReshapeFcn);
glutMouseFunc (fillSquare);
glutKeyboardFunc (enlargeSquare);
glutSpecialFunc (reduceSquare);
glutMainLoop ( );
}
glutTabletButtonFunc (tabletFcn);
and the arguments for the invoked function are similar to those for a mouse:
glutDeviceGet (GLUT_NUM_TABLET_BUTTONS);
glutTabletMotionFunc (tabletMotionFcn);
The returned values xTablet and yTablet give the coordinates on the tablet
surface.
609
Interactive Input Methods and Graphical User Interfaces
Spaceball buttons are identified with the same integer values as a tablet, and
parameter action is assigned either the value GLUT UP or the value
GLUT DOWN. We can determine the number of available spaceball buttons with a
call to glutDeviceGet using the argument GLUT NUM SPACEBALL BUTTONS.
Translational motion of a spaceball, when the mouse is in the display window,
is recorded with the function call
glutSpaceballMotionFunc (spaceballTranlFcn);
These translation distances are normalized within the range from −1000 to 1000.
Similarly, a spaceball rotation is recorded with
glutSpaceballRotateFunc (spaceballRotFcn);
The buttons are identified with integer values, and the button action is specified
as GLUT UP or GLUT DOWN.
In this case, we use the callback function to identify the dial and obtain the angular
amount of rotation:
void dialsFcn (GLint dial, GLint degreeValue);
Dials are designated with integer values, and the dial rotation is returned as an
integer degree value.
610
Interactive Input Methods and Graphical User Interfaces
glRenderMode (GL_SELECT);
611
Interactive Input Methods and Graphical User Interfaces
This places us in selection mode, which means that a scene is processed through
the viewing pipeline but not stored in the frame buffer. A record of information
for each object that would have been displayed in the normal rendering mode is
placed in the pick buffer. In addition, this command returns the number of picked
objects, which is equal to the number of information records in the pick buffer. To
return to the normal rendering mode (the default), we invoke the glRenderMode
routine using the argument GL RENDER. A third option is the argument
GL FEEDBACK, which stores object coordinates and other information in a feed-
back buffer without displaying the objects. Feedback mode is used to obtain
information about primitive types, attributes, and other parameters associated
with the objects in a scene.
We use the following statement to activate the integer-ID name stack for the
picking operations:
glInitNames ( );
The ID stack is initially empty, and this stack can be used only in selection mode.
To place an unsigned integer value on the stack, we can invoke the following
function:
glPushName (ID);
This places the value for parameter ID on the top of the stack and pushes the
previous top name down to the next position in the stack. We can also simply
replace the top of the stack using
glLoadName (ID);
but we cannot use this command to place a value on an empty stack. To eliminate
the top of the ID stack, we issue the command
glPopName ( );
612
Interactive Input Methods and Graphical User Interfaces
Stack position = 1
Min depth = 0, Max depth = 0
Stack IDs are:
30
Stack position = 3
Min depth = 0, Max depth = 0
Stack IDs are:
30 10 20
glutCreateMenu (menuFcn);
glutCreateMenu (menuFcn);
glutAddMenuEntry ("First Menu Item", 1);
glutAddMenuEntry ("Second Menu Item", 2);
glutAttachMenu (button);
616
Interactive Input Methods and Graphical User Interfaces
where parameter button is assigned one of the three GLUT symbolic constants
referencing the left, middle, or right mouse button.
To illustrate the creation and use of a GLUT menu, the following program
provides two options for displaying the interior fill of a triangle. Initially, the
triangle is defined with two white vertices, one red vertex, and a fill color
determined by an interpolation of the vertex colors. We use the glShadeModel
function to select a polygon fill that is either a solid color or an interpolation
(Gouraud rendering) of the vertex colors. A menu is created in this program that
allows us to choose between these two options using the right mouse button,
when the mouse cursor is inside the display window. This pop-up menu is dis-
played with the upper-left corner at the position of the mouse cursor. A menu
option is highlighted when we move the mouse cursor over that option. The
highlighted option is then selected by releasing the right button. If the
option “Solid-Color Fill” is selected, the triangle is filled with the color specified for
the last vertex (which is red). At the end of the menu-display procedure,
fillOption, we include a glutPostRedisplay command to indicate that
the triangle should be redrawn when the menu is displayed.
#include <GL/glut.h>
GLfloat red = 1.0, green = 1.0, blue = 1.0; // Initial triangle color: white.
GLenum fillMode = GL_SMOOTH; // Initial polygon fill: color interpolation.
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 300.0, 0.0, 300.0);
}
glBegin (GL_TRIANGLES);
glVertex2i (280, 20);
glVertex2i (160, 280);
617
Interactive Input Methods and Graphical User Interfaces
glFlush ( );
}
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (0.0, GLfloat (newWidth), 0.0, GLfloat (newHeight));
displayTriangle ( );
glFlush ( );
}
init ( );
glutDisplayFunc (displayTriangle);
glutReshapeFunc (reshapeFcn);
glutMainLoop ( );
}
618
Interactive Input Methods and Graphical User Interfaces
A newly created menu becomes the current menu for the current dis-
play window. To activate a menu for the current display window, we use the
statement
glutSetMenu (menuID);
This menu then becomes the current menu, which will pop up in the display
window when the mouse button that has been attached to that menu is pressed.
We eliminate a menu with the command
glutDestroyMenu (menuID);
If the designated menu is the current menu for a display window, then that
window has no menu assigned as the current menu, even though other menus
may exist.
The following function is used to obtain the identifier for the current menu
in the current display window:
currentMenuID = glutGetMenu ( );
A value of 0 is returned if no menus exist for this display window or if the previous
current menu was eliminated with the glutDestroyMenu function.
The glutAddSubMenu function can also be used to add the submenu to the
current menu.
In the following program, we illustrate the creation of a submenu. This
program, which is a modification of the previous menu program, displays
a submenu that provides three color choices (blue, green, and white) for
the first two vertices of the triangle. The main menu is now listed with three
options, and the third option is displayed with an arrow symbol to indicate
that a pop-up submenu will be displayed when that option is highlighted. A
glutPostRedisplay function is included at the end of both the main-menu
function and the submenu function.
619
Interactive Input Methods and Graphical User Interfaces
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (0.0, GLfloat (newWidth), 0.0, GLfloat (newHeight));
displayTriangle ( );
glFlush ( );
}
init ( );
glutDisplayFunc (displayTriangle);
glutReshapeFunc (reshapeFcn);
glutMainLoop ( );
}
glutDetachMenu (mouseButton);
621
Interactive Input Methods and Graphical User Interfaces
have detached the menu from the button, we can use glutAttachMenu to attach
it to a different button.
Options within an existing menu can also be changed. For example, we can
delete an option in the current menu with the function
glutRemoveMenuItem (itemNumber);
where parameter itemNumber is assigned the integer value of the menu option
that is to be deleted.
Other GLUT routines allow us to modify the names or status of items within
an existing menu. For example, we can use these routines to change the displayed
name of a menu option, to change the item number of the option, or to change an
option into a submenu.
622
Interactive Input Methods and Graphical User Interfaces
T A B L E 1
Summary of OpenGL Input Functions
Function Description
626
Interactive Input Methods and Graphical User Interfaces
T A B L E 1
(Continued)
Function Description
T A B L E 2
Summary of OpenGL Menu Functions
Function Description
627