REPORT2
REPORT2
INTRODUCTION
Below diagram shows the organization if the libraries for an x-window system
environment.
The function call to set up the menu and to link it to the right mouse button should
be placed in our main function. They are as follow:
glutAttachMenu(GLUT_RIGHHT_BUTTON); // To open menu by right click
Structure of hierarchical menus
The submenu contains the two entries for changing the size of the square
Sub_menu=glutCreateMenu(size_menu);
1.4 EVENT-DIVEN PROGRAMMING
1.4.1 CALLBACK FUNCTIONS:
The property of most windows based programs is that they are event driven. This
means that the program responds to various events, such as mouse click, the press
of a keyboard Key or the resizing of the screen window. The system automatically
manages an event queue, which receives messages that certain events have
occurred & deals with them on a first come, first served basis.
1.4.2 REGISTERING CALLBACK FUNCTIONS:
The way for a programmer to associate each type of event with the desired
callback function is called registering the callback function.. Each event type that
is used in the application must be registered with a callback function, which has a
name and definition of the programmer’s chosing.
Consider the eg. to use the GLUT to register the callback function named
mymouse() with a mouse event.
glutMouseFunc(mymouse);
This notifies the program that when a mouse button is depressed or
clicked”glutMouseFunc”(including the capitalization ) is fixed by the Glut
library,but the name “mymouse” can be chosen at programmer’s whim(the type of
event),the callback function mymouse() .which must be written by the
programmer,is to be called by the system. The programmer writes code for
mymouse() to handle each of the possible mouse actions of interest.
The function glutPostRedisplay() will refresh the window & avoid flickering effect.
A pseudocode for registering the essential types of callback functions for event
types, basically a skeleton of main() function for an event driven program is
shown below:
void main()
{
glutDisplayFunc(mydisplay);// register the redraw function.
glutReshapeFunc(myreshape);// register the reshape function.
glutMouseFunc(mymouse);// register the mouse action function.
glutMotionFunc(mymotion);// registers the mouse motion function.
glutKeyboardFunc(mykeyboard);// register the keyboard action function.
glutMainLoop();// enter the unending main loop.
}
Various callback functions are:
btnmouse button clicked, statestate of mouse after release, x & y are the
coordinates of mouse position(cursor) on screen.
1.5 LIGHTING
1.5.1 Light Sources
A general source can be characterized by a six-variable illumination function. Form the
prespective view of a surface illuminated by this source, we can obtain the total
contribution of the source by integrating over its surface .
The four basic types of light of sources; ambient lighting, point source , space lights,
distant light.
1.5.2 Colour sources
A color source through a three component intensity or illumination function each of
whose component is the intensity of the independent red, green, and blue component .
1.5.3 Ambient light
Ambient light is also called as uniform light & its source has red, green and blue
component & it depend on the color of the light source in the environment.
1.5.4 Point source
An ideal point source emits light equally in all directions
1.5.5 Spotlights
Spotlights are characterized by a narrow range of angles through which light is emitted.
1.5.6 Distant Light Sources
Most shading calculation are based on the direction from the point on the surface to
the light source. the light source can be calculated by parallel projection, they replace the
location pf the light source with the direction of the light source.
1.5.7 The Phong Lighting Model
The lighting model that we present was introduced by Phong & later modified by
Binn. The Phong model supports the three types of material interaction- ambient,
diffuse, and specular.
Ambient source color- the interaction of a light source with the surface int the
environment
Speccular source color- it is designed to produce the desired color of a specular
highlight.
[Link] Ambient reflection
The intensity of ambient light is the same at every point on the surface, some of this
light is high absorbed and some is reflected,the reflected light is given by the
ambient co-efficient.
[Link] Diffuse reflection
Diffuse reflections are characterized by rough surface ,rays of light hit the rough
surface at only angles are reflected back at markedly different angles. perfectly rough
surface are so rough that their is no preferred angle of reflection. such surface
sometimes called Lambertian surfaces.
[Link] .Specular reflection
If we employ on ambient and diffuse reflection, our images will be shaded
and will appear 3D .In diffuse surface is rough, a specular is smooth surface. At
smoother surface,the light reflected light is concentrared in a smaller range of
angles centered about the angle of a perfect reflector- a mirror or aperfectly
specular surface.
properties for the front and back faces of a surface. Material parameters are
defined through function:
glMaterialfv(GLenum face, GLenum type, GLfloat * pointer_to_arrray)
glMaterialf(GLenum face, GLenum type, GLfloat value)
for example, we might specify ambient, diffuse, and specular reflectivity coefficient
for each primary color through three arrays:
GLfloat ambient[ ]={};
GLfloat diffuae[ ]={};
GLfloat specular[ ]={};
We can defined small amount of white ambient reflectivity, yellow diffuse
properties, and white specular reflections. We set the material properties for the
front and back by the following calls:
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); // Both front &back
set to ambient
The shininess of a surface- the exponent in the specular-reflection term –is
specified by glMaterialf
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);
1.6 SHADING
Shading is the process of performing lighting computations and determining pixels colors
from them.
Flat shading
Interpolated/Smooth shading
Gouraud shading
Phong shading
The light source is at infinity , so N.L is constant across the polygon face
_ _
The viewer is at infinity , so N.V is constant across the polygon face
The polygon represents the actual surface being modeled, and is not an
approximation to a curved surface.
If a visible surface algorithm is used that outputs a list of polygon ,such as one of the list-
priority algorithms , constant shading can take advantages of ubiquitous single-color 2D
primitive.
In openGL ,we specify flag shading as follows:
glShadeModel(GL_FLAT);
a polygon span on a scan line ,between starting and ending normals for the
span .These normals are themselves interpolated along polygon edges from vertex
normals that are computed .
1.7 GEOMETRICAL TRANSFORMATIONS
Many applications use the geometric transformations to change the position,orientation,
and size of objects. There are mainly 3 types of transformations
Translation
Rotation
Scaling
1.7.1 Translation
We can translate points in the (x,y) plane to new positions by adding translation amounts
to the coordinates of the points.
For each point P(x,y) to be moved by dx units parallel to the x axis and by dy units
parallel to y axis to the new point P*(x*,y*) .
x*=x+dx, y*=y+dy.
T(dx,dy) is the translation matrix.
P*=P+T.
Implemented in opengl by function glTranslatef(dx,dy);
1.7.2 Rotation
Points can be rotated through an angle ‘θ’ about the origin or an arbitrary point. A
rotation is defined mathematically by
x*=[Link] θ-[Link] θ, y*=[Link] θ+[Link] θ.
Consider R is rotation matrix. R[2][2]={cos θ,-sin θ,sin θ,cos θ}.
P*(x*,y*)=R.P(x,y)
Implemented in opengl by function glRotatef(θ,x,y); (rotate θ degree along xy axes).
1.7.3 Scaling
Points can be scaled(stretched or reduced) by scaling factor,
Sx along the x axis & Sy along the y axis into new points by the multiplications
x*=[Link] y*=[Link]
Consider S is the scaling matrix.s[2][2]={Sx,0,0,Sy}.
P*(x*,y*)=S.P(x,y)
If(Sx==Sy) it is called uniform scaling,else it’s called differential scaling.
Implemented in opengl by function glScalef(Sx,Sy);
Product of an arbitrary sequence of rotation, translation and scale matrices are called
AFFINE TRANSFORMATIONS which have the property of preserving parallelism
of lines but not lengths and angles.
There are 3 crucial steps while performing transformations on objects.
Translate the object to the origin.
Perform the required transformations.
Translate such that the object at origin returns back to it’s original position.
All the types of transformations discussed above are 2D transformations. This can be
extended to 3Dtransformation by including the z-axis in the above discussion such as:
glTranslatef(dx,dy,dz);
glRotatef(θ,x,y,z);
glScalef(Sx,Sy,Sz);
1.8 DISPLAY LISTS
Display lists are designed to optimize performance . It is a simple way of
enhancing your openGL application making it run faster also increases frames
/second in your application When moving into world with multiple objects.
An openGL Display List must make a copy of all Data it requires to recreate the
call sequence that created [Link] openGL Display List is an object created and
stored in the memory that can be called multiple times without openGL having to
recreate the object again.
The graphical entities can be sent to display in one of the two ways
Immediate mode -as soon as the program executes a statement that defines a
primitive ,that primitive is sent to the server for possible display and no memory
of it is retained in the system.
Retained mode -in this we define the object once and then put its description in a
display list .The display list is stored in the server and redisplayed by a simple
function call issued from the client to the server .It provides reduced network
traffic and allows the overhead in executing commands to be done once and
have the results stored in display list on the graphics server.
Simple example to show the function uses
Glint BOX; /* To create a list ,first setup an integer to hold the list*/
#define BOX 1 /*or some other unused integer*/
/* creation of list */ BOX=glGenLists(1); /*Set the list to generate a list*/
glNewList (BOX,GL_COMPILE); /* make new list using box */
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0,1.0);
glVertex2f(-1.0,1.0);
glEnd( );
glEndList( );/* End the creation of current active list*/
If we want an immediate display of the contents while the list is being
constructed,we can make use of the following flag
GL_COMPILE_AND_EXECUTE
Each time we wish to draw the box on the server ,we execute the function as follows:
glCallList(BOX);
If we change the model-view or projection matrices between executions of the
display list ,the box will appear in different places or will no longer appear ,as the
following code fragment demonstrates:
glMatrixMode(GL_PROJECTION);
for( i=1 ; i<5 ; i++)
{
glLoadIdentity( );
gluOrtho2D(-2.0*i , 2.0*i ,-2.0*I ,2.0*i );
` glCallList(BOX);
}
Each time that glCallList is executed ,the box is redrawn,albeit with a larger
clipping rectangle.
1.9 VA_LIST
Signature of printf() is :
int printf(char *format,…);
Argument list of printf() has 2 main things
Char* format : a regular string.
… called “ellipses” means “any number of optional arguments can
go here”.
REQUIREMENT SPECIFICATION
64 MB RAM
16.5 MB hard disk space.
Pentium III and Above
16 Bit color Monitor.
Keyboard
Mouse
Turbo C/C++
Windows 95/98/2000/XP/VISTA
DESIGN
3.1 FLOW CHART:
enum { RED_PLASTIC, EMERALD, SLATE} MaterialType
enum {TORUS_MATERIAL = 1, TEAPOT_MATERIAL = 2, CUBE_METARIAL= 3 }
MaterialDisplayList
enum {LIGHT_OFF, LIGHT_RED, LIGHT_WHITE, LIGHT_GREEN } LightValues
GLfloat red_light[] ={1.0, 0.0, 0.0, 1.0}, green_light[] ={0.0, 1.0, 0.0, 1.0},
white_light[] ={1.0, 1.0, 1.0, 1.0}
GLfloat left_light_position[] ={-1.0, 0.0, 1.0, 0.0},
right_light_position[] ={1.0, 0.0, 1.0, 0.0}
GLfloat brass_ambient[] ={0.33, 0.22, 0.03, 1.0},
brass_diffuse[] ={0.78, 0.57, 0.11, 1.0},
brass_specular[] ={0.99, 0.91, 0.81, 1.0}, brass_shininess = 27.8
GLfloat red_plastic_ambient[] ={0.0, 0.0, 0.0},
red_plastic_diffuse[] ={0.5, 0.0, 0.0},
red_plastic_specular[] ={0.7, 0.6, 0.6},
red_plastic_shininess = 32.0
GLfloat emerald_ambient[] ={0.0215, 0.1745, 0.0215}, emerald_diffuse[]
={0.07568, 0.61424, 0.07568}, emerald_specular[] =
{0.633, 0.727811, 0.633}, emerald_shininess = 76.8
GLfloat slate_ambient[] ={0.02, 0.02, 0.02}, slate_diffuse[] =
{0.02, 0.01, 0.01}, slate_specular[] ={0.4, 0.4, 0.4},slate_shininess = .78125
read shade_model=GL_SMOOTH,*left_light,
*right_light, *cube_material,*teapot_material,
*torus_material
start start
main()
glutCreateWindow()
glutReshapeFunc()
glutDisplayFunc()
glutMouseFunc()
left_light_select()
right_light_select()
glutKeyboardFunc()
torus_select()
teapot_select()
cube_select()
return 0
stop
output()
va_start()
vsprintf_s()
glPushMatrix()
F
for p=buffer to
*p
T glPopMatrix()
p=p+1 glutStrokeCharacter()
stop
display()
F
axis==0
glRotatef(theta[0],1.0,0.0,0.0)
axis==1
stop F
T
C D
C D
glRotatef(theta[1], glRotatef(theta[2],0.0,0.0,1.0)
0.0,1.0,0.0)
stop output()
stop
mouse()
T
T
axis=0 axis=2
axis=1
theta[axis]+=2.0
theta[axis]>360
F display()
T stop
theta[axis]-=360
keys()
switch(key)
theta[axis]>360
theta[axis]>360 theta[axis]>360
F
F F
T
T T
theta[axis]
-=360 theta[axis] theta[axis]
-=360 -=360
display()
display()
display()
break
break
break
stop
light_select()
E
switch(value)
glDisable( glDisable(
which) which) glLightfv(which, GL_DIFFUSE,
glLightfv(which,
white_light)GL_DIFFUSE, green_light)
break break
break
break
glutPostRedisplay()
stop
material_select()
Switch(value)
return NULL
stop
main_menu_select()
value==
666 F
T glshadeModel()
exit(0)
stop G
glutPostRedisplay()
stop
myreshape()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
w<=h F
glOrtho(2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-2.0,
T
glOrtho(2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0)
glMatrixMode(GL_MODELVIEW)
stop
IMPLEMENTATION
Step 1: Initialize the display mode by double buffering , RGB combinations and
depth buffer.
Step 2: Register the callback functions.
Step 3: Attach menus & popups. Define light menu entries & material menu .
Entries.
Step 4: Initialise the left & right light sources.
Step 5: Select the object (torus,teapot,cube).
Step 6: Enable lighting, depth test & normalization.
Step 7: Initialize the camera view
Step 8: Perform transformations on the objects , lighting & shading effects.
Step 9: main event loop (go to step1)
GLfloat redlight[]={1.0,0.0,0.0,1.0}; //red light color code in RGB model . The 4th
argument is ALPHA in RGBA model with alpha=1.0 for transparency or opaqueness.
If(alpha==1.0) opaque; else transparent.
if(axis==0)
{
glRotatef(theta[0],1.0,0.0,0.0); //rotate along X axis.
}
glMatrixMode(GL_MODELVIEW); //object matrix.
glPushMatrix();
glScalef(1.3, 1.3, 1.3);//scaling factorssx,sy,sz.
glRotatef(20.0, 1.0, 0.0, 0.0);//rotate 20* along x axis.
glPushMatrix();
glTranslatef(-0.65, 0.7, 0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
glCallList(TORUS_MATERIAL);//display list called.
glutSolidTorus(0.175, 0.45, 5, 10);//tous created.
glPopMatrix();
glPushMatrix();
glTranslatef(-0.75, -0.8, 0.0);
glCallList(TEAPOT_MATERIAL);//display list called.
glutSolidTeapot(0.4);//teapot created.
glPopMatrix();
glPushMatrix();// push the object matrix on stack.
glTranslatef(1.0, 0.0, -1.0);//perform transformations.
glCallList(CUBE_MATERIAL);//display list called.
glutSolidCube();//cube created.
glPopMatrix();//pop the object matrix.
glPopMatrix();
glPushAttrib(GL_ENABLE_BIT);//A standard & safe procedure
to save present values of attributes & matrices by
pushing both the attributes & matrices on their own
stacks when we enter a display list & pop them when we
exit.
glPushMatrix();
glDisable(GL_DEPTH_TEST);//was enabled earlier,now needs to
be disabled.
theta[axis]-=360;
display();
break;
default: exit(0);//quit on pressing any other key.
}
}
*label = "off";
glDisable(which);//disable source of light.
break;
case LIGHT_RED:
*label = "red";
glLightfv(which,GL_DIFFUSE,red_light);
//represents(source of light,parameter,ptr to array).
break;
}
glutPostRedisplay();//call display function.
}
glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
//(face of material,type,array value).
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glEndList();//end of display list.
}
material(object,brass_ambient,brass_diffuse,brass_specular,
brass_shininess);//selects brass.
return "brass";
}
return NULL; /* avoid bogus warning! */
}
void main_menu_select(int value)
{
if (value == 666)
exit(0);//quit.
glShadeModel(shade_model=value);//set the shade model.
glutPostRedisplay();//call display function.
}
int main(int argc, char **argv)
{
glutReshapeFunc(myreshape);//callbackfunctionfor reshape.
glutDisplayFunc(display);//callback function for display.
glutMouseFunc(mouse); //callback function for mouse.
glutKeyboardFunc(keys); //callback function for keys.
glLightfv(GL_LIGHT0, GL_POSITION, left_light_position);
//source of light is GL_LIGHT0,parameter is GL_POSITION &
//left_light_position is the pointer to array.
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
//parameter for white_light is GL_SPECULAR.
glLightfv(GL_LIGHT1, GL_POSITION, right_light_position);
//source of light is GL_LIGHT1,parameter is GL_POSITION &
//right_light_position is the pointer to array.
glLightfv(GL_LIGHT1, GL_SPECULAR, white_light);
//parameter for white_light is GL_SPECULAR.
glMatrixMode(GL_PROJECTION);
gluPerspective( /* degrees field of view */ 50.0,
/* aspect ratio(=w/h) */ 1.0, /* Z near */ 1.0, /* Z far
*/ 10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0,/* center is at (0,0,0) */
0.0, 1.0, 0.);/* up is in positive Y direction (VUP)*/
glTranslatef(0.0, 0.0, -1.0);//come close along z axis.
glutMainLoop();//main event loop.
return 0; /* ANSI C requires main to return
int. */
TESTING
5.1 TEST CASE :
Smooth shading Sets the shading model Click on the menu All objects are
as smooth smooth shaded
Flat shading Sets the shading model Click on the menu All objects are
as Flat flat shaded
Left light Selects the left source Disable Switch off from
of light left side
Right light Selects the right source Disable Switch off from
of light right side
The following are few of the features that can be implemented in the future
Addition of button
CONCLUSION
The subject of Interactive Computer Graphics ,Top Down Approach using openGL in the
curriculum was of great help in accomplishing the project .
The project “Lighting House” implements some of the most commonly used graphical
functions . The project built satisfies some of the basic requirements and implements the
basics of “LIGHTING AND SHADING” .
The project also brings in the concepts of menus , display lists , va_list ,
callback functions , translation and user interactions.
BIBLIOGRAPHY