0% found this document useful (0 votes)
21 views39 pages

Unit 2

This is the second unit of computer graphics

Uploaded by

Gayu Gayu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views39 pages

Unit 2

This is the second unit of computer graphics

Uploaded by

Gayu Gayu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Module 5

Input& interaction, Curves and Computer


Animation
Syllabus Contents

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

PHYSICAL INPUT DEVICES


 Physical input devices are the input devices which has the particular
hardware architecture.
 The two major categories in physical input devices are:
 Key board devices like standard keyboard, flexible keyboard, handheld
keyboard etc. These are used to provide character input like letters,
numbers, symbols etc.
 Pointing devices like mouse, track ball, light pen etc. These are used
to specify the position on the computer screen.

1. KEYBOARD: It is a general keyboard which has set of characters. We make


use of ASCII value to represent the character i.e. it interacts with the programmer
by passing the ASCII value of key pressed by programmer. Input can be given
either single character of array of characters to the program.

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. DATA TABLETS: It provides absolute positioning. It has rows and columns


of wires embedded under its surface. The position of the stylus is determined
through electromagnetic interactions between signals travelling through the wires
and sensors in the stylus.

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

5. JOYSTICK: The motion of the stick in two orthogonal directions is encoded,


interpreted as two velocities and integrated to indentify a screen location. The
integration implies that if the stick is left in its resting position, there is no change
in cursor position. The faster the stick is moved from the resting position; the faster
the screen location changes. Thus, joystick is variable sensitivity device.

The advantage is that it is designed using mechanical elements such as springs


and dampers which offer resistance to the user while pushing it. Such
mechanical feel is suitable for application such as the flight simulators, game
controllers etc.

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.

Other 3-Dimensional devices such as laser scanners, measure 3-D positions


directly. Numerous tracking systems used in virtual reality applications sense
the position of the user and so on.

LOGICAL INPUT DEVICES


 These are characterized by its high-level interface with the application
program rather than by its physical characteristics.
 Consider the following fragment of C code:
int x;
scanf(“%d”,&x);
printf(“%d”,x);
 The above code reads and then writes an integer. Although we run this
program on workstation providing input from keyboard and seeing output
on the display screen, the use of scanf() and printf() requires no knowledge
of the properties of physical devices such as keyboard codes or resolution
of the display.

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.

3. EVENT MODE: This mode can handle the multiple interactions.


 Suppose that we are in an environment with multiple input devices, each with
its own trigger and each running a measure process.
 Whenever a device is triggered, an event is generated.The device measure
including the identifier for the device is placed in an event queue.
 If the queue is empty, then the application program will wait until an event
occurs. If there is an event in a queue, the program can look at the first
event type and then decide what to do.

Another approach is to associate a function when an event occurs, which is called


as “call back.”

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.

DEFINITION AND EXECUTION OF DISPLAY LISTS

 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:

 OpenGL provides an API to retain the information by using stack – It is a data


structure in which the item placed most recently is removed first [LIFO].
 We can save the present values of the attributes and the matrices by pushing
them into the stack, usually the below function calls are placed at the
beginning of the display list,
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();

 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);

 The glCallLists has three arguments: (1) indicates number of lists to be


executed (2) indicates the type (3) is a pointer to an array of a type given
by second argument.

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.

DISPLAY LIST AND MODELING


 Display list can call other display list. Therefore, they are powerful tools
for building hierarchical models that can incorporate relationships among
parts of a model.
 Consider a simple face modeling system that can produce images as
follows:

 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.

USING POINTING DEVICES


 Pointing devices like mouse, trackball, data tablet allow programmer to
indicate a position on the display.
 There are two types of event associated with pointing device, which is
conventionally assumed to be mouse but could be trackball or data tablet
also.
1. MOVE EVENT – is generated when the mouse is move with one of the
button being pressed. If the mouse is moved without a button being pressed,
this event is called as “passive move event”.
2. MOUSE EVENT – is generated when one of the mouse buttons is either pressed
or released.

 The information returned to the application program includes button that


generated the event, state of the button after event (up or down), position
(x,y) of the cursor. Programming a mouse event involves two steps:
1. The mouse callback function must be defined in the form: void myMouse(int
button, int state, int x, int y) is written by the programmer.
For example,
void myMouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
exit(0);
}

16
The above code ensures whenever the left mouse button is pressed down,
execution of the program gets terminated.

2. Register the defined mouse callback function in the main function, by


means of GLUT function:
glutMouseFunc(myMouse);

Write an OpenGL program to display square when a left button is


pressed and to exit the program if right button is pressed.

#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);
}

void drawsq (int x, int y)


{
y=wh-y;
glBegin(GL_POLYGON);
glVertex2f(x+siz, y+siz);

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);
}

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


{
if(button==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
drawsq(x,y);
if(button==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(0);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(wh,ww);
glutCreateWindow(“square”);
glutDisplayFunc(display);
glutMouseFunc(myMouse);
myinit();
glutMainLoop();
}

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:

1. The keyboard callback function must be defined in the form:


void mykey (unsigned char key, int x, int y)
is written by the application programmer.
For example,
void mykey(unsigned char key, int x, int y)
{
if(key== „q‟ || key== „Q‟)
exit(0);
}

The above code ensures when „Q‟ or „q‟ key is pressed, the execution of the
program gets terminated.

2. The keyboard callback function must be registered in the main


functionby means of GLUT function:
glutKeyboardFunc(mykey);

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:

1. Window call back function must be defined in the form:


 void myReshape(GLsizei w, GLsizei h) is written by the application
programmer.
 Let us consider drawing square as an example, the square of same size
must be drawn regardless of window size.
void myReshape(GLsizei w, GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,(GLdouble) w, 0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewPort(0,0,w,h)
/*save new window size in global variables*/
ww=w;
wh=h;
}

2. The window callback function must be registered in the main function,


glutReshapeFunc(myReshape);

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.

 GLUT also supports the creation of hierarchical menus which is given


below:

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:

O Usage of back buffer and extra rendering:


 When we use double buffering it has two color buffers: front
and back buffers. The contents present in the front buffer is
displayed, whereas contents in back buffer is not displayed so
we can use back buffer for other than rendering the scene

 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.

PICKING AND SELECTION MODE


 The difficulty in implementing the picking is we cannot go backward directly
from the position of the mouse to the primitives.
 OpenGL provides “selection mode” to do picking. The glRenderMode() is used
to choose select mode by passing GL_SELECT value.
 When we enter selection mode and render a scene, each primitive within the
clipping volume generates a message called “hit” that is stored in a buffer
called “name stack”.
 The following functions are used in selection mode:
O void glSelectBuffer(GLsizei n, GLuint *buff) : specifies array
buffer of size „n‟ in which to place selection data.
O void glInitNames() : initializes the name stack.
O void glPushName(GLuint name) : pushes name on the name
stack.
O void glPopName() : pops the top name from the name stack.
O void glLoadName(GLuint name) : replaces the top of the name
stack with name.

 OpenGL allow us to set clipping volume for picking using gluPickMatrix()


which is applied before gluOrtho2D.
 gluPickMatrix(x,y,w,h,*vp) : creates a projection matrix for picking that
restricts drawing to a w x h area and centered at (x,y) in window
coordinates within the viewport vp.

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.

The following code provides the implementation of picking process:

#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);
}

void main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);

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();
}

A SIMPLE CAD PROGRAM

Applications like interactive painting, design of mechanical parts and creating


characters for a game are all examples of computer-aided design (CAD). CAD
programs allow –
 The use of multiple windows and viewports to display a variety of
information.
 The ability to create, delete and save user-defined objects.
 Multiple modes of operations employing menus, keyboard and mouse.
For example, consider the polygon-modeling CAD program which supports
following operations:
1. Creation of polygons
2. Deletion of polygons
3. Selection and movement of polygons
Refer appendix A.5 polygon modeling program for the entire code from the

prescribed text (Interactive Computer Graphics by Edward Angel 5th edition)

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.

ANIMATING INTERACTIVE PROGRAMS


 Using OpenGl, the programmer can design interactive programs. Programs
in which objects are not static rather they appear to be moving or changing
is considered as “Interactive programs”.
 Consider the following diagram:

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:

 Suppose that we change the value of  as the program is running, the


square appears to rotating about its origin. If the value of  is to be changed
by a fixed amount whenever nothing else is happening then an idle
callback function must be designed 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);

 Further in the display function, we have to include: glutSwapBuffers()


to exchange the contents of front and the back buffer.
 Using this approach, the problems associated with flicker can be
eliminated.

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:

glutTimerFunc(int delay, void(*timer_func (int), int value)

 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.

DESIGN OF INTERACTIVE PROGRAMS


The following are the features of most interactive program:
 A smooth display, showing neither flicker nor any artifacts of the refresh
process.
 A variety of interactive devices on the display
 A variety of methods for entering and displaying information
 An easy to use interface that does not require substantial effort to learn
 Feedback to the user
 Tolerance for user errors
 A design that incorporates consideration of both the visual and motor
properties of the human.

TOOLKITS, WIDGETS AND FRAME BUFFER:


The following two examples illustrate the limitations of geometric rendering.
1. Pop-up menus: When menu callback is invoked, the menu appears over
whatever was on the display. After we make our selection, the menu
disappears and screen is restored to its previous state.

32
2. Rubberbanding: It is a technique used to define the elastic nature of pointing
device to draw primitives.

 Consider paint application, if we want to draw a line, we indicate only two


end points of our desired line segment. i.e., after locating first point, as we
move the mouse, a line segment is drawn automatically [is updated on
each refresh] from first location to present position of mouse.
 Rubberbanding begin when mouse button is pressed and continue until
button is released at that time final line segment is drawn.
 We cannot implement this sequence of operations using only what we
have presented so for. We will explore it in next chapters.

5.1.13 LOGIC OPERATIONS


Two types of functions that define writing modes are:
1. Replacement mode 2. Exclusive OR (XOR)

 When program specifies about visible primitive then OpenGL renders it


into set of color pixels and stores it in the present drawing buffer.
 In case of default mode, consider we start with a color buffer then
has been cleared to black. Later we draw a blue color rectangle of size 10
x10 pixels then 100 blue pixels are copied into the color buffer, replacing
100 black pixels. Therefore, this mode is called as “copy or replacement
mode”.
 Consider the below model, where we are writing single pixel into color buffer.

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)

DRAWING ERASABLE LINES


One way to draw erasable lines is given below:
 Mouse is used to get first end point and store this in object coordinates.

 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:

Final form of code can be written as shown below:

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.

XOR AND COLOR


 Consider we would like to draw blue color line where 24 bit RGB values
(00000000, 00000000, 11111111).
 Suppose the screen is clear to write (11111111, 11111111, 11111111)
then when we draw blue line using XOR mode, then the resultant line
would appear in yellow color (11111111, 11111111, 00000000) because
XOR operation is applied bit-wise.
 This leads to form annoying visual effects.
 Therefore, we should use copy mode while drawing final output to get it
in required color.
CURSORS AND OVERLAY PLANES
 Rubberbanding and cursors can place a significant burden on graphics
system as they require the display to be updated constantly.
 Although XOR mode simplifies the process, it requires the system to read
present destination pixels before computing new destination pixels.

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.

You might also like