0% found this document useful (0 votes)
25 views

Lecture2 Continued

This document discusses different types of physical and logical input devices used for interacting with computer programs. It describes pointing devices like mice and trackballs, as well as keyboards. The document also covers different types of logical input like strings, locators, picks, choices, dials, and strokes. It provides examples of programming event-driven input using callbacks for mouse events, window resizing, and idle events to create animations.

Uploaded by

fajal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture2 Continued

This document discusses different types of physical and logical input devices used for interacting with computer programs. It describes pointing devices like mice and trackballs, as well as keyboards. The document also covers different types of logical input like strings, locators, picks, choices, dials, and strokes. It provides examples of programming event-driven input using callbacks for mouse events, window resizing, and idle events to create animations.

Uploaded by

fajal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 2 continued:

Input and Interaction


(Chapter 3 of Angel)

Physical Input Devices

There are two primary types:

pointing devices: allow a user to indicate a position on the screen, plus


one or more buttons for sending a signal,
keyboard devices: return character codes.

There are several kinds of pointing device:

mouse: output can be viewed either as (x, y) screen coordinates or as


velocities vx , vy which can be integrated to give relative positions on
the screen,
trackball: similar to mouse (e.g. for a laptop),
data tablet: provides absolute positioning,
lightpen: used in Sutherlands Sketchpad, provides direct positioning
on the screen,
joystick: has two orthogonal directions of movement, interpreted as
velocities, good for flight simulators and games.

1
Logical Input Devices

These are input devices as viewed from inside the application program.
There are six main types.

String: provides a string, e.g. ASCII (usually from the keyboard).

Locator: provides a position in world coordinates. In OpenGL we


must convert from screen to world coordinates.

Pick: picks and object, usually via a pointing device.

Choice: allows a user to select from a discrete number of options, often


provided by a widget, e.g., a menu, scrollbar, or graphical buttons.

Dial: provides floating point numbers, e.g. through a slidebar.

Stroke: returns an array of locations, e.g. when moving the mouse


(between pushing the button down and releasing it).

2
Programming Event-Driven Input
We base the programming of input and interaction on events. For example,
a mouse event occurs when one of the mouse buttons is either depressed
or released. In GLUT, we can associate a function called a callback with
each specific event. The display function we wrote earlier was an example
of a callback.

Example 1. Mouse event

void mouse_callback(int button, int state, int x, int y)


{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
drawSquare(x,y);
if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
exit();
}

A small square is drawn at the point (x, y) when the left mouse button
is depressed. The program exits when the right button is depressed. The
mouse callback must have the above form, and is specified, usually in main,
through the GLUT function

glutMouseFunc(mouse_callback)

3
Example 2. Reshape event

A reshape event occurs when the window is resized.

void myReshape(GLsizei w, GLsizei h)


{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glViewport(0,0,w,h);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}

This code resizes the clipping rectangle (in world coordinates!) to mimic
the new window size. We could instead keep the clipping rectangle fixed,
and adjust the viewport dimensions to keep a fixed aspect ratio.

4
Example 3. Idle callback

The idle callback is invoked when there are no other events. It can be
used to create animations. Consider a square rotating around the origin,
whose vertices lie on the unit circle:

(sin , cos )

(cos , sin )

( cos , sin )

(sin , cos )

5
The following code rotates the square:

void display()
{
glClear();
glBegin(GL_POLYGON);
thetar = theta/((2*3.14159)/360.0); /* radians */
glVertex2f(cos(thetar), sin(thetar));
glVertex2f(-sin(thetar), cos(thetar));
glVertex2f(-cos(thetar), -sin(thetar));
glVertex2f(sin(thetar), -cos(thetar));
glEnd();
}

void idle()
{
theta += 2.0; /* or some other amount */
if(theta >= 360.0) theta -= 360.0;
glutPostRedisplay();
}

In fact it doesnt work very well. Its better to use double buffering.

You might also like