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

REPORT2

Uploaded by

jaiswal1000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views39 pages

REPORT2

Uploaded by

jaiswal1000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LIGHTING HOUSE

INTRODUCTION

1.1 OpenGL Technology

OpenGL is the premier environment for developing portable, interactive 2D and 3D


graphics applications. Since its introduction in 1992, OpenGL has become the industry's
most widely used and supported 2D and 3D graphics application programming interface
(API), bringing thousands of applications to a wide variety of computer platforms.

OpenGL fosters innovation and speeds application development by incorporating a


broad set of rendering, texture mapping, special effects, and other powerful visualization
functions.
Developers can leverage the power of OpenGL across all popular desktop and
workstation platforms, ensuring wide application deployment.
OpenGL Available Everywhere: Supported on all UNIX® workstations, and shipped
standard with every Windows 95/98/2000/NT and MacOS PC, no other graphics API
operates on a wider range of hardware platforms and software environments.
OpenGL runs on every major operating system including Mac OS, OS/2, UNIX,
Windows 95/98, Windows 2000, Windows NT, Linux, OPENStep, and BeOS; it also
works with every major windowing system, including Win32, MacOS, Presentation
Manager, and X-Window System. OpenGL is callable from Ada, C, C++, Fortran,
Python, Perl and Java and offers complete independence from network protocols and
topologies.
1.2 Open GL interface
Applications are designed to access open GL directly through functions in three libraries.
Function in the main GL library have names that begin with letter gl and are stored in a
library GL.
 Open GL utility library (GLU) contains code for creating common objects it
began with glu.
 Open GL utility toolkit (GLUT) to interface with the window system and to get
input from external devices.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

 For the windows system , this library is called GLX.


Windows-wgl
Macintosh-agl

Below diagram shows the organization if the libraries for an x-window system
environment.

Figure: Library Organisation


1.3 Menus
 GLUT provides one additional feature pop-up menus, that we can use with mouse
to create sophisticates interactive application.
 Using menus involves taking few simple steps we must define the action
corresponding to each entry in the menu.
 We must link the menu to a particular mouse button. Finally we must register a
call back function for each menu.
glutCreateMenu(demo_menu);
 The function glutCreateMenu register the call back function demo_menu.
 The second argument in each entry definition is the identifier passed to the
callback function the entry is selected.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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:

 glutDisplayFunc(mydisplay): Whenever the system determines that a


screen window should be redrawn it issues a redraw event. This occurs when the
window is first opened, and when the window is exposed by moving another
window off of it. Here the function mydisplay() is registered as the callback
function for a redraw event.
 glutReshapeFunc(myreshape): Screen windows can be reshaped by the
user, usually by dragging a corner of the window to a new position with the
mouse.(simply moving the window does not produce a reshape event). Here the
function myreshape() is registered with the reshape event and it is automatically
passed arguments that report the new width and height of the reshaped window.

 glutMouseFunc(mymouse): When one of the mouse buttons is pressed &


released, a mouse event is issued. Here mymouse() is registered as the callback
function when a mouse event occurs. mymouse() is automatically passed
arguments that describe the mouse location and the nature of button action.
void mymouse(int btn,int state,int x,int y)

btnmouse button clicked, statestate of mouse after release, x & y are the
coordinates of mouse position(cursor) on screen.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

 glutMotionFunc(mymotion): This function generates an event when the


mouse is moved with one or more mouse buttons pressed. It’s not necessary to
release mouse button for event to be generated.
 glutPassiveMotionFunc(): generates an event when the mouse enters the
window with no buttons pressed.
 glutIdleFunc(idle):returns NULL by default. The function generates event
when there is no interaction or when the system is idle.
 glutKeyboardFunc(mykeyboard): This registers the function
mykeyboard() with the event of pressing & releasing some key on the keyboard.
mykeyboard() is automatically passed arguments that tell which key was pressed.
This function also makes available to the system the x &y co-ordinates (relative to
screen window) of the mouse at the time a key was pressed.
 void myKeyboard(unsigned char key,int x,int y)

keythe character being pressed.(ascii code is passed).


X &y coordinates of the 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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

1.5.8 Light Source in OPENGL


 OpenGL support the four types of light sources – point, spotlight, ambient and
distant, these light source allow at least eight sources in a program. Although
there are parameters that must be specified, they are exactly the parameters
required by the modified Phong model lighting model. The Phong model
 glLightfv (GLenum source,GLenum parameter, GLfloat *pointer_to_array)
 glLightf(GLenum source,GLenum parameter, GLfloat value)
specified the required vector and scalar parameters, respectively.
 There are four vectors parameters that we can set;the position of the light source
and the amount of ambient, diffuse, and specular light associated with the source.
 For example first source GL_LIGHT0 and to locate it at the point:
GLfloat light0_pos[ ]={}; //position of light source 0
 With the fourth component set to zero, the point source becomes a distant source
with direction vector
 GLfloat light0_dir[ ]={}; //Direction of point light source 0
 For our single light source, if we want a while specular component and red
ambient and diffuse component, we can use the following code:
GLfloat diffuse[ ]={};
GLfloat ambient[ ]={};
GLfloat specular[ ]={};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_ LIGHT0, GL_POSITION, light0_pos); //source of light LIGHT0 is at
position GL_POSITION
glLightfv(GL_ LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_ LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_ LIGHT0, GL_SPECULAR, specular0);
 Note that we must enable both lighting and particular source.
1.5.9 Specification of Material in OPENGL
 Material properties in OpenGL match up directly with the supported light source
and with the modified Phong lighting model, we can specify different material

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

1.6.1 FLAT SHADING


The Flat shading approach applies an illumination model once to determine a
single intensity value that is then used to shade an entire polygon .In this we sample the
value of the illumination equation once for each polygon and hold the value across the
polygon to reconstruct the polygon’s shade .This approach is valid if several assumptions
are true : _ _

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

1.6.2 INTERPOLATED / SMOOTH SHADING


 In Interpolated shading ,the shading information is linearly interpolated across a
triangle from values determined for its vertices . Gouraud generalized this
technique to arbitrary polygons. This is particularly easy for scan-line algorithm
that already interpolates the z value across a span from interpolated z values
computed for the span’s [Link] our final assumption is that the
polygon accurately represents the surface being modeled , is most often the
incorrect one ,which has a more substantial effect on the resulting image than
does the failure of the other two.
 Smooth shading is default in [Link] can also set the mode explicitly as
follows:
glShadeModel(GL_SMOOTH);
1.6.3 GOURAUD SHADING
 Gouraud shading extends the concept of interpolation shading applied to
individual polygons by interpolating polygon vertex illumination values that take
into account the surface being approximated .The Gouraud shading process
requires that the normal be known for each vertex of the polygon mesh .Gouraud
was able to compute these vertex normals directly from an analytical description
of the surface. Alternatively,
1.6.4 PHONG SHADING

 Phong shading is also known as normal-vector interpolation shading , interpolates


the Surface normal vector N ,rather than the intensity .Interpolation occurs across

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

 Variable argument lists.


 Array type is defined in stdarg.h
 C function Printf() can accept a varying number of arguments since it uses a
Va_list.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

There are special macros va_* which include:


 va_list
 va_start
 va_end
 va_arg
 We can write our own function that have their own sticky code to process a set of
variable arguments.
 First we create a pointer that will be used to point to the first element of the
variable argument list.
va_list listpointer;
 Currently the listpointer is uninitialised,so make it point to first argument
in the List.
va_start(listpointer,numargs);
 Here numargs is the last mandatory argument (last argument just
before”…”) that the function takes.
 Next,we start to actually retrieve the values from the va_list itself. We
must know the datatype of the data we are retrieving from va_list.
int arg=va_arg(listpointer,int);
 Get an argument from va_list. We must know the type of the arg to
retrieve it from the va_list.
 Finally we cleanup by saying va_end(). It must be done before the
function returns .
va_end(listpointer);

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

REQUIREMENT SPECIFICATION

2.1 HARDWARE SPECIFICATION

 64 MB RAM
 16.5 MB hard disk space.
 Pentium III and Above
 16 Bit color Monitor.
 Keyboard
 Mouse

2.2 SOFTWARE SPECIFICATION

 Turbo C/C++
 Windows 95/98/2000/XP/VISTA

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

main()

read left_light_m, right_light_m, torus_m, teapot_m, cube_m

glutCreateWindow()

glutReshapeFunc()

glutDisplayFunc()

glutMouseFunc()
left_light_select()
right_light_select()
glutKeyboardFunc()
torus_select()
teapot_select()
cube_select()

return 0

stop

output()

read args, buffer[200], *p

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

C D

glRotatef(theta[1], glRotatef(theta[2],0.0,0.0,1.0)
0.0,1.0,0.0)

stop output()

stop
mouse()

btn=glb F Btn=gmb Btn=grb


F

T
T
axis=0 axis=2
axis=1

theta[axis]+=2.0

theta[axis]>360
F display()

T stop

theta[axis]-=360

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

keys()

switch(key)

axis=0 axis=1 axis=2 exit(0)

Theta[axis] Theta[axis] Theta[axis]


+=2.0 +=2.0 +=2.0

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

switch(value)

*label = “off” *label = “red” *label = *label = “green”


“white”

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)

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

material() material() material() material()

return return red plastic return emerald return slate


brass

return NULL

stop

main_menu_select()

value==
666 F

T glshadeModel()

exit(0)

stop G

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

IMPLEMENTATION

4.1 ALGORITHM: LIGHTING HOUSE

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)

4.2 Implementation of Important modules of code:

glEnable(GL_LIGHTING); //need to enable as we are using light sources.


glEnable(GL_DEPTH_TEST); //hidden surface removal.
glEnable(GL_NORMALIZE);// normalization.

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.

GLfloat leftlightposition[]={-1.0,0.0,1.0,0.0}; // the position of left light . 1 st 3 arguments


represent the position along 3 axes. The last argument specifies the type of source of
light. In this case it’s point source of light(0.0).

GLfloat Brass_ambient[]={0.33,0.22,0.03,1.0} // the ambiency of brass material. 1 st 3


arguments represent the strength of RGB mix. The last argument is for opaqueness(1.0).

Shade_Model=GL_SMOOTH. // sets the shading model as smooth type.

va_list args;//create a pointer that will be used to


point to the first element of the VARIABLE
ARGUMENT LIST.

char buffer[200], *p;

va_start(args, format); //currently the args is


uninitialised so make it pointto first argument in list.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

vsprintf_s(buffer, format, args);//retrieve the values


from the va_list.
va_end(args);//finally cleanup.

for (p = buffer; *p; p++)


glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);//access a
single character from a monotype, or evenly spaced font by
this function call.
Each invocation includes a translation to the bottom right
of the character’s box to prepare for next character.
GLUT_STROKE_ROMAN provides proportionally spaced characters.

void display(void) //display function.


{

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 factorssx,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

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

exit.

glPushMatrix();
glDisable(GL_DEPTH_TEST);//was enabled earlier,now needs to
be disabled.

glDisable(GL_LIGHTING);//disable the enabled lighting.

// print monotype text.


output(80, 2800, "Torus: %s", torus_material);
shade_model == GL_SMOOTH ? "smooth" : "flat");//sets the
shade [Link] of Conditional operator.
glPopAttrib();//pops the pushed enable bit attribute.
glutSwapBuffers();//swaps the front & back buffer.
}

void mouse(int btn,int state,int x,int y)


//mouse function userinterface
{
if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)axis=0;
//rotate about x axis.
}//rotate via mouse interface.
void keys(unsigned char key,int x,int y)
//keyboard user interface.
{
switch(key)
{
case 'x': axis=0;//rotate along x axis.
theta[axis]+=2.0;
if(theta[axis]>360)
theta[axis]-=360;
display();
break;

theta[axis]-=360;
display();
break;
default: exit(0);//quit on pressing any other key.
}
}

void light_select(GLenum which, int value, char **label)


{
glEnable(which);//enable source of light.
switch (value) {
case LIGHT_OFF:

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

*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.
}

void left_light_select(int value)


{
light_select(GL_LIGHT0, value, &left_light);
//selects left light source.
}

void right_light_select(int value)


{

light_select(GL_LIGHT1,value,&right_light);// select right


light source.
}

void material(int dlist, GLfloat * ambient, GLfloat *


diffuse,GLfloat * specular, GLfloat shininess)
{
glNewList(dlist, GL_COMPILE);//tells system to send the
list to server but not to display it’s [Link] we want
immediate display of contents while list is being
constructed,we can use the GL_COMPILE_AND_EXECUTE flag
instead.

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

char *material_select(int object, int value)


{
glutPostRedisplay();
switch (value) {
case BRASS:

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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 :

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

MENU NAME DESCRIPTION INPUT OUTPUT

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

Red Left light set to


Red color

White Left light set to


White color

Green Left light set to


Green color

Right light Selects the right source Disable Switch off from
of light right side

Red Right light set


to Red color

White Right light set


to White color

Green Right light set


to Green color

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

MENU NAME DESCRIPTION INPUT OUTPUT

Torus Select the torus Brass Front face of the


material object torus object will
be set with
material type
BRASS

Red plastic Front face of the


torus object will
be set with
material type
RED PLASTIC

Emerald Front face of the


torus object will
be set with
material type
EMERALD

Slate Front face of the


torus object will
be set with
material type
SLATE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

MENU NAME DESCRIPTION INPUT OUTPUT

Teapot Select the teapot Brass Front face of the


material object teapot object will
be set with
material type
BRASS

Red plastic Front face of the


Teapot object will
be set with
material type
RED PLASTIC

Emerald Front face of the


teapot object will
be set with
material type
EMERALD

Slate Front face of the


teapot object will
be set with
material type
SLATE

MENU NAME DESCRIPTION INPUT OUTPUT

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

Cube Select the cube Brass Front face of the


material object cube object will
be set with
material type
BRASS

Red plastic Front face of the


Cube object will
be set with
material type
RED PLASTIC

Emerald Front face of the


Cube object will
be set with
material type
EMERALD

Slate Front face of the


Cube object will
be set with
material type
SLATE
Quit Exits from program Any other key exit(0);
except X Y Z

Keyboard User keyboard interaction Press key ‘X ‘ Rotation along


X axis

Press key ‘Y’ Rotation along


Y axis

Press key’Z’ Rotation along


Z axis

Mouse User mouse interaction Press left button Rotation along


X axis

Press middle button Rotation along


Y axis

Press right button Rotation along


Z axis
FUTURE ENHANCEMENTS

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

The following are few of the features that can be implemented in the future

 Support for scaling

 View port adjustment while transformations

 Addition of button

 Transformations of a single object

 Menu display with keyboard interaction

CONCLUSION

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

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.

Therefore the project is very simple to implement and easy to understand.

BIBLIOGRAPHY

 JAMES FOLEY,ANDRES VAN DAM,STEVENS FEINER,


JOHN F HUGHES COMPUTER GRAPHICS.

 INTERACIVE COMPUTER GRAPHICS:A TOP DOWN APPROACH


USING OPENGL BY EDWARD ANGEL.

 COMPUTER GRAPHICS BY [Link]

 TURBO C++ ONLINE HELP.

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010


LIGHTING HOUSE

DEPT CSE,SCE 2009-2010

You might also like