Unit 2
Unit 2
Input and Interaction:Input devices, clients and servers, Display Lists, Display Lists and Modelling,
Programming Event Driven Input, Menus Picking, Building Interactive Models,Animating Interactive programs,
Design of Interactive programs, Logic operations .Curved surfaces, quadric surfaces, OpenGL Quadric-Surface
and Cubic-Surface Functions, Bezier Spline Curves, Bezier surfaces, OpenGL curve functions. Corresponding
openGL functions.
INTERACTION
In the field of computer graphics, interaction refers to the manner in which
the application program communicates with input and output devices of
the system.
For e.g. Image varying in response to the input from the user.
OpenGL doesn‟t directly support interaction in order to maintain
portability. However, OpenGL provides the GLUT library. This library
supports interaction with the keyboard, mouse etc and hence enables
interaction. The GLUT library is compatible with many operating systems
such as X windows, Current Windows, Mac OS etc and hence indirectly
ensures the portability of OpenGL.
INPUT DEVICES
Input devices are the devices which provide input to the computer graphics
application program. Input devices can be categorized in two ways:
1
1. Physical input devices
2. Logical input devices
2. MOUSE AND TRACKBALL: These are pointing devices used to specify the
position. Mouse and trackball interacts with the application program by passing
the position of the clicked button. Both these devices are similar in use and
construction. In these devices, the motion of the ball is converted to signal sent
back to the computer by pair of encoders inside the device. These encoders
measure motion in 2-orthogonal directions.
2
The values passed by the pointing devices can be considered as positions and
converted to a 2-D location in either screen or world co-ordinates. Thus, as a
mouse moves across a surface, the integrals of the velocities yield x,y values that
can be converted to indicate the position for a cursor on the screen as shown
below:
These devices are relative positioning devices because changes in the position
of the ball yield a position in the user program.
3
4. LIGHT PEN: It consists of light-sensing device such as “photocell”. The
light pen is held at the front of the CRT. When the electron beam strikes the
phosphor, the light is emitted from the CRT. If it exceeds the threshold then light
sensing device of the light pen sends a signal to the computer specifying the
position.
The major disadvantage is that it has the difficulty in obtaining a position that
corresponds to a dark area of the screen
4
6. SPACE BALL: It is a 3-Dimensional input device which looks like a
joystick with a ball on the end of the stick.
Stick doesn‟t move rather pressure sensors in the ball measure the forces
applied by the user. The space ball can measure not only three direct forces (up-
down, front-back, left-right) but also three independent twists. So totally device
measures six independent values and thus has six degree of freedom.
5
These are logical functions that are defined by how they handle input or
output character strings from the perspective of C program.
From logical devices perspective inputs are from inside the application
program. The two major characteristics describe the logical behavior of input
devices are as follows:
The measurements that the device returns to the user program
The time when the device returns those measurements
API defines six classes of logical input devices which are given below:
1. STRING: A string device is a logical device that provides the ASCII values
of input characters to the user program. This logical device is usually
implemented by means of physical keyboard.
2. LOCATOR: A locator device provides a position in world coordinates to
the user program. It is usually implemented by means of pointing devices such
as mouse or track ball.
3. PICK: A pick device returns the identifier of an object on the display to
the user program. It is usually implemented with the same physical device as
the locator but has a separate software interface to the user program. In OpenGL,
we can use a process of selection to accomplish picking.
4. CHOICE: A choice device allows the user to select one of a discrete number
of options. In OpenGL, we can use various widgets provided by the window
system. A widget is a graphical interactive component provided by the window
system or a toolkit. The Widgets include menus, scrollbars and graphical
buttons. For example, a menu with n selections acts as a choice device, allowing
user to select one of „n‟ alternatives.
5. VALUATORS: They provide analog input to the user program on some
graphical systems; there are boxes or dials to provide value.
6. STROKE: A stroke device returns array of locations. Example, pushing down
a mouse button starts the transfer of data into specified array and releasing of
button ends this transfer.
6
INPUT MODES
Input devices can provide input to an application program in terms of two
entities:
1. Measure of a device is what the device returns to the application
program.
2. Trigger of a device is a physical input on the device with which the user
can send signal to the computer
Example 1: The measure of a keyboard is a single character or array of
characters where as the trigger is the enter key.
Example 2: The measure of a mouse is the position of the cursor whereas the
trigger is when the mouse button is pressed.
The application program can obtain the measure and trigger in three distinct
modes:
1. REQUEST MODE: In this mode, measure of the device is not returned to
the program until the device is triggered.
For example, consider a typical C program which reads a character input
using scanf(). When the program needs the input, it halts when it
encounters the scanf() statement and waits while user type characters at
the terminal. The data is placed in a keyboard buffer (measure) whose
contents are returned to the program only after enter key (trigger) is
pressed.
Another example, consider a logical device such as locator, we can move
out pointing device to the desired location and then trigger the device with
its button, the trigger will cause the location to be returned to the
application program.
7
2. SAMPLE MODE: In this mode, input is immediate. As soon as the function
call in the user program is executed, the measure is returned. Hence no trigger
is needed.
Both request and sample modes are useful for the situation if and only if there is
a single input device from which the input is to be taken. However, in case of
flight simulators or computer games variety of input devices are used and these
mode cannot be used. Thus, event mode is used.
8
5.1.3 CLIENT AND SERVER
The computer graphics architecture is based on the client-server model.
I.e., if computer graphics is to be useful for variety of real applications, it must
function well in a world of distributed computing and network.
In this architecture the building blocks are entities called as “servers”
perform the tasks requested by the “client”
Servers and clients can be distributed over a network or can be present within
a single system. Today most of the computing is done in the form of
distributed based and network based as shown below:
Most popular examples of servers are print servers – which allow using
high speed printer devices among multiple users. File servers – allow
users to share files and programs.
Users or clients will make use of these services with the help of user programs
or client programs. The OpenGL application programs are the client programs that
use the services provided by the graphics server.
Even if we have single user isolated system, the interaction would be
configured as client-server model.
9
5.1.4 DISPLAY LISTS
The original architecture of a graphical system was based on a general-purpose
computer connected to a display. The architecture is shown in the next page.
At that time, the disadvantage is that system was slow and expensive. Therefore,
a special purpose computer is build which is known as “display processor”.
The user program is processed by the host computer which results a compiled
list of instruction that was then sent to the display processor, where the
instruction are stored in a display memory called as “display file” or “display
list”. Display processor executes its display list contents repeatedly at a sufficient
high rate to produce flicker-free image.
There are two modes in which objects can be drawn on the screen:
1. IMMEDIATE MODE: This mode sends the complete description of the object
which needs to be drawn to the graphics server and no data can be retained. i.e.,
to redisplay the same object, the program must re-send the information. The
information includes vertices, attributes, primitive types, viewing details.
10
2. RETAINED MODE: This mode is offered by the display lists. The object is defined
once and its description is stored in a display list which is at the server side and
redisplay of the object can be done by a simple function call issued by the client
to the server.
NOTE: The main disadvantage of using display list is it requires memory at the
server architecture and server efficiency decreases if the data is changing
regularly.
Display lists are defined similarly to the geometric primitives. i.e., glNewList()
at the beginning and glEndList() at the end is used to define a display list.
Each display list must have a unique identifier – an integer that is usually
a macro defined in the C program by means of #define directive to an
appropriate name for the object in the list. For example, the following code
defines red box:
The flag GL_COMPILE indicates the system to send the list to the server but
not to display its contents. If we want an immediate display of the contents
while the list is being constructed then GL_COMPILE_AND_EXECUTE flag
is set.
11
Each time if the client wishes to redraw the box on the display, it need not
resend the entire description. Rather, it can call the following function:
glCallList(Box)
The Box can be made to appear at different places on the monitor by changing
the projection matrix as shown below:
We can retrieve these values by popping them from the stack, usually the
below function calls are placed at the end of the displaylist,
glPopAttrib();
glPopMatrix();
We can create multiple lists with consecutive identifiers more easily
using:
glGenLists (number)
We can display multiple display lists by using single funciton call:
glCallLists()
12
TEXT AND DISPLAY LISTS
There are two types of text i.e., raster text and stroke text which can be
generated.
For example, let us consider a raster text character is to be drawn of size
8x13 pattern of bits. It takes 13 bytes to store each character.
If we define a stroke font using only line segments, each character
requires a different number of lines.
From the above figure we can observe to draw letter „I‟ is fairly simple,
whereas drawing „O‟ requires many line segments to get sufficiently smooth.
So, on the average we need more than 13 bytes per character to represent
stroke font. The performance of the graphics system will be degraded for the
applications that require large quantity of text.
A more efficient strategy is to define the font once, using a display list for
each char and then store in the server. We define a function OurFont()
which will draw any ASCII character stored in variable „c‟.
The function may have the form
For the character „O‟ the code sequence is of the form as shown below:
13
The above code approximates the circle with 12 quadrilaterals.
When we want to generate a 256-character set, the required code using
OurFont() is as follows
base = glGenLists(256);
for(i=0;i<256;i++) {
glNewList(base+i, GL_COMPILE);
OurFont(i);
glEndList();
}
To display char from the list, offset is set by using glListBase(base)
function. The drawing of a string is accomplished in the server by the
following function, char *text_string;
glCallLists((GLint) strlen (text_string), GL_BYTE, text_string);
FONTS IN GLUT
GLUT provides raster and stroke fonts; they do not make use of display
lists.
14
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, int character);
provides proportionally space characters. Position of a character is done
by using a translation before the character function is called.
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, int character);
produces the bitmap characters of size 8x13.
Each face has two identical eyes, two identical ears, one nose, one mouth
& an outline. We can specify these parts through display lists which is
given below:
15
PROGRAMMING EVENT DRIVEN INPUT
The various events can be recognized by the window system and call back
function can be called for each of these events.
16
The above code ensures whenever the left mouse button is pressed down,
execution of the program gets terminated.
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
int wh=500, ww=500;
float siz=3;
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glViewPort(0,0,w,h)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,(GLdouble) ww, 0, (GLdouble) wh);
glMatrixMode(GL_MODELVIEW);
glColor3f(1,0,0);
}
17
glVertex2f(x-siz, y+siz);
glVertex2f(x-siz, y-siz);
glVertex2f(x+siz, y-siz);
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
}
18
KEYBOARD EVENTS
Keyboard devices are input devices which return the ASCII value to the
user program. Keyboard events are generated when the mouse is in the window
and one of the keys is pressed or released.
GLUT supports following two functions:
glutKeyboardFunc() is the callback for events generated by pressing a key
glutKeyboardUpFunc() is the callback for events generated by releasing a
key.
The information returned to the program includes ASCII value of the key
pressed and the position (x,y) of the cursor when the key was pressed.
Programming keyboard event involves two steps:
The above code ensures when „Q‟ or „q‟ key is pressed, the execution of the
program gets terminated.
19
WINDOW EVENTS
A window event is occurred when the corner of the window is dragged to
new position or size of window is minimized or maximized by using
mouse.
The information returned to the program includes the height and width of
newly resized window. Programming the window event involves two steps:
20
THE DISPLAY AND IDLE CALLBACKS
Display callback is specified by GLUT using
glutDisplayFunc(myDisplay). It is invoked when GLUT determines that
window should be redisplayed. Re-execution of the display function can
be achieved by using glutPostRedisplay().
The idle callback is invoked when there are no other events. It is specified
by GLUT using glutIdleFunc(myIdle).
WINDOW MANAGEMENT
GLUT also supports multiple windows of a given window. We can create a
second top-level window as follows:
id = glutCreateWindow(“second window”);
The returned integer value allows us to select this window as the current
window.
i.e., glutSetWindow(id);
NOTE: The second window can have different properties from other window by
invoking the glutInitDisplayMode before glutCreateWindow.
MENUS
Menus are an important feature of any application program. OpenGL
provides a feature called “Pop-up-menus” using which sophisticated
interactive applications can be created.
Menu creation involves the following steps:
1. Define the actions corresponding to each entry in the menu.
2. Link the menu to a corresponding mouse button.
3. Register a callback function for each entry in the menu.
21
The glutCreateMenu() registers the callback function demo_menu. The
function glutAddMenuEntry() adds the entry in the menu whose name is
pased in first argument and the second argument is the identifier passed
to the callback when the entry is selected.
PICKING
Picking is the logical input operation that allows the user to identify an
object on the display.
The action of picking uses pointing device but the information returned to
the application program is the identifier of an object not a position.
22
It is difficult to implement picking in modern system because of graphics
pipeline architecture. Therefore, converting from location on the display to
the corresponding primitive is not direct calculation.
There are at least three ways to deal with this difficulty:
O Selection:
It involves adjusting the clipping region and viewport such
that we can keep track of which primitives lies in a small clipping
region and are rendered into region near the cursor.
These primitives are sent into a hit list that can be examined
later by the user program.
O Bounding boxes or extents:
Picking can be performed in four steps that are initiated by user defined
pick function in the application:
23
O We draw the objects into back buffer with the pick colors.
O We get the position of the mouse using the mouse callback.
O Use glReadPixels() to find the color at the position in the frame
buffer corresponding to the mouse position.
O We search table of colors to find the object corresponds to the color
read.
24
(a) There is a normal window and image on the display. We also see the
cursor with small box around it indicating the area in which primitive is
rendered.
(b) It shows window and display after the window has been changed by
gluPickMatrix.
#include<glut.h>
25
26
void myReshape()
{
glViewPort(0,0,w,h)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,(GLdouble) w, 0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
}
27
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(“picking”);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(Mouse);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
28
BUILDING INTERACTIVE MODELS
Using OpenGL, we can develop a program where we can do insertion,
manipulation, deletion etc and we can also build a program which is quite
interactive by using the concept of instancing and display lists.
Consider an interior design application which has items like chairs,
tables and other house hold items. These items are called the basic
building blocks of the application. Each occurrence of these basic items is
referred to as “instance”.
Whenever the instances of building blocks are created by the user using
the application program, the object (instance) is stored into an array
called as “instance table”. We reserve the type 0 to specify that the
object no longer exists (i.e., for deletion purpose)
Now suppose that the user has indicated through a menu that he wishes
to eliminate an object and use the mouse to locate the object.
O The program can now search the instance table till it finds the object
as specified in the bounding box and then set its type to 0.
O Hence, next time when the display process goes through the instance
table, the object would not be displayed and thereby it appears that
object has been deleted.
Although the above strategy works fine, a better data structure to implement
the instance table is using linked lists instead of arrays.
29
Consider a 2D point p(x,y) such that x = cos , y= sin . This point would
lie on a unit circle regardless of the value of . Thus, if we connect the
above given four points we get a square which has its center as the origin.
The above square can be displayed as shown below:
The above idle callback function must be registered in the main function:
glutIdleFunc(idle);
Suppose that we want to turn off and turn on the rotation feature then
we can write a mouse callback function as shown below:
30
The above mouse callback function starts the rotation of the cube when
the left mouse button and when the middle button is pressed it will halt
the rotation.
The above mouse callback function must be registered in the main
function as follow:
glutMouseFunc(mouse);
However, when the above program is executed using single buffering scheme
then flickering effect would be noticed on the display. This problem can
be overcome using the concept of double buffering.
DOUBLE BUFFERING:
Double buffering is a must in such animations where the primitives,
attributes and viewing conditions are changing continuously.
Double buffer consists of two buffers: front buffers and back buffers.
Double buffering mode can be initialized:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
31
USING TIMER:
To understand the usage of timer, consider cube rotation program and its
execution is done by using fast GPU (modern GPUs can render tens of
millions of primitives per second) then cube will be rendered thousands
of time per second and we will see the blur on the display.
Therefore, GLUT provides the following timer function:
Execution of this function starts timer in the event loop that delays for
delay milliseconds. When timer has counted down, timer_func is executed
the value parameter allow user to pass variable into the timer call back.
32
2. Rubberbanding: It is a technique used to define the elastic nature of pointing
device to draw primitives.
33
The pixel that we want to write is called as “source pixel”.
The pixel in the drawing buffer which gets replaced by source pixel is
called as “destination pixel”.
In Exclusive-OR or (XOR) mode, corresponding bits in each pixel are
combing using XOR logical operation.
If s and d are corresponding bits in the source and destination pixels, we
can denote the new destination bit as d‟. d‟ = d s
One special property of XOR operation is if we apply it twice, it returns to the
original state, it returns to the original state. So, if we draw some thing in XOR
mode, we can erase it by drawing it again.
d = ( d s ) s
OpenGL supports all 16 logic modes, copy mode (GL_COPY) is the default.
To change mode, we must enable logic operation,
glEnable(GL_COLOR_LOGIC_OP) and then it can change to XOR mode
glLogicOp(GL_XOR)
Again mouse is used to get second point and draw a line segment in XOR
mode.
34
Here in the above code, copy mode is used to switch back in order to
draw other objects in normal mode.
If we enter another point with mouse, we first draw line in XOR mode
from 1st point to 2nd point and draw second line using 1st point to current
point is as follows:
In this example, we draw rectangle using same concept and the code for
callback function are given below:
35
36
For the first time, we draw a single rectangle in XOR mode.
After that each time that we get vertex, we first erase the existing
rectangle by redrawing new rectangle using new vertex.
Finally, when mouse button is released the mouse callback is executed
again which performs final erase and draw and go to replacement mode.
37
Alternative is to provide hardware support by providing extra bits in the
color buffer by adding “overlay planes”.
Therefore, typical color buffer may have 8 bits for each Red, green and blue and one
red, one green and one blue overlay plane. i.e., each color will be having its own
overlay plane then those values will be updated to color buffer.