cg labs project final v1

49
VISVESVARAYA TECHNOLOGICAL UNIVERSITY BELGAUM A PROJECT REPORT On MULTI-WINDOW LIGHT EFFECTSSubmitted in partial fulfilment of the Bachelor Degree In Computer Science and Engineering VI SEM COMPUTER GRAPHICS AND VISUALIZATION LABORATORY PROJECT (06CSL67) For the academic Year: 2011-2012 By Mr. SAIPREETH MURALIDHARAN (1HK09CS046) & Mr. MUZAMMIL BASHA S.J. (IHK09CS033) Under the guidance of Mrs. SHAHZIA SIDDIQUA Lecturer, CSE HKBKCE, Bangalore Department of CSE, HKBKCE I 2011-2012

Upload: muzammilbasha-sj

Post on 28-Oct-2014

130 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CG Labs Project Final v1

VISVESVARAYA TECHNOLOGICAL UNIVERSITYBELGAUM

A PROJECT REPORT

On

“MULTI-WINDOW LIGHT EFFECTS”

Submitted in partial fulfilment of the Bachelor Degree

In

Computer Science and Engineering

VI SEM COMPUTER GRAPHICS AND VISUALIZATION LABORATORY PROJECT (06CSL67)

For the academic Year: 2011-2012By

Mr. SAIPREETH MURALIDHARAN (1HK09CS046)&

Mr. MUZAMMIL BASHA S.J. (IHK09CS033)Under the guidance of

Mrs. SHAHZIA SIDDIQUALecturer, CSE

HKBKCE, Bangalore

HKBK COLLEGE of ENGINEERINGNagawara, Bangalore – 560 045Email: [email protected]: www.hkbkeducation.org

Department of CSE, HKBKCE I 2011-2012

Page 2: CG Labs Project Final v1

HKBK COLLEGEofENGINEERING

N a g a w a r a , B e n g a l u r u – 5 6 0 0 4 5

Approved by AICTE & Affiliated to VTU

__________________________________ _____________________________________Ms. Shahzia Siddiqua Prof. Loganathan R.

Guide Head of the Department

Department of Computer Science and Engineering

Certificate

Certified that the project work entitled “Multi-Window Light Effects” is carried

out by Mr. Saipreeth Muralidharan-1HK09CS046 and Mr.Muzammil Basha.S.J.-

1HK09CS033, are bonafide students of HKBK COLLEGEofENGINEERING, in partial

fulfillment for the award of Bachelor of Engineering in Computer Science and

Engineering of the Visveshwaraya Technological University, Belgaum, during the year

2011–12. It is certified that all corrections/suggestions indicated for Internal

Assessment have been incorporated in the report deposited in the departmental

library. The project report has been approved as it satisfies the academic

requirements in respect of 06CSL67–Computer Graphics and Visualization Lab

prescribed for the said Degree.

Department of CSE, HKBKCE II 2011-2012

Page 3: CG Labs Project Final v1

Acknowledgement

We would like to express our regards and acknowledgement to all those who helped in

making this project possible.

First and foremost we would take this opportunity to express our heartfelt gratitude to the

Chairman Mr. C. M. Ibrahim and the Director Mr. C. M. Faiz Mohammed of our college,

HKBKCE.

We are also grateful to the Administrator Mr. Abdul Hameed S. A. and the Principal

Dr. T. C. Manjunath for providing facilities and untiring zeal, which constantly inspired us

towards the attainment of everlasting knowledge throughout the course.

We are deeply indebted to the H.O.D Prof. R. Loganathan of Computer Science and

Engineering department for the valuable suggestions and constant encouragement provided for

the successful completion of the mini project.

We would like to thank our guide Ms. Shahzia Siddiqua, Senior Lecturer of Computer

Science and Engineering department for the constant guidance for the successful completion of

mini project.

Finally, we would like to thank all the staff members of Computer Science and

Engineering department for their guidance and support .We are also thankful to our family and

friends who continue to give us best support.

1HK09CS046 SAIPREETH

MURALIDHARAN

1HK09CS033 MUZAMMIL BASHA S.J.

Department of CSE, HKBKCE III 2011-2012

Page 4: CG Labs Project Final v1

ABSTRACT

The purpose of this project is to illustrate the simple use of lighting effects on an object

showcasing different properties of colours and corresponding changes in the object.

The properties can be chosen along with colours – red, blue, green and white.

The properties are diffuse, ambient, specular, emission and shininess.

The other window shows scaling and rotation which can be controlled with the use of cursor

keys.

Department of CSE, HKBKCE IV 2011-2012

Page 5: CG Labs Project Final v1

TABLE OF CONTENT

Title Page I

Certificate II

Acknowledgment III

Abstract IV

Table of Contents V

List of Figures VI

Chapter-1 Introduction 7

Chapter-2 Requirement Specification 7

2.1 User Requirement Specification 8

2.2 Hardware and Software Requirements 8

2.3 Introduction to the Environment 8

Chapter-3 Design 11

3.1 Libraries Used 11

3.2 Working of the Program 11

Chapter 4 Implementation 13

Chapter 5 Snapshots 33

Chapter 6 Conclusion 34

Chapter 7 Scope for Future Enhancements 35

Bibliography 36

Department of CSE, HKBKCE V 2011-2012

Page 6: CG Labs Project Final v1

TABLE OF FIGURES

Fig. 5.1 Cursor position displayed on terminal for Window 1 33

Fig. 5.2 Cursor position displayed on terminal for Window 2 33

Fig. 5.3 Window 2 for Scaling & Rotation 33

Fig. 5.3 Light Effects for Window 1 33

Fig. 5.4 Light Effects for Window 1 33

Department of CSE, HKBKCE VI 2011-2012

Page 7: CG Labs Project Final v1

Department of CSE, HKBKCE VII 2011-2012

Page 8: CG Labs Project Final v1

Multi-Window Light Effects Introduction

Chapter-1 Introduction

OpenGL is an API (Application Programming Interface) which provides the actual drawing tool

through a collection of functions that are called within an application.

It suits well for use in computer graphics because of its device independence or portability. It

offers a rich and highly usable API for 2D, 3D graphics and image manipulation.

Computer graphics is concerned with all aspects of producing pictures or images using a

computer. OpenGL is a graphics software system which has become a widely accepted standard

for developing graphics applications. it is easy to learn and it possesses most of the

characteristics of other popular graphics system.

OpenGL is a software interface to graphics hardware. OpenGL is a library of functions for fast

3d rendering. The openGL utility library (GLU) provides many modelling features. It is a

standard part of every openGL implementation. It is ideal for 3d visualization but offers little

support for creating application user interfaces.

OpenGL’s main purpose is to render 2D & 3D objects into a frame buffer. These objects are

described as sequence of vertices (which define geometric objects) or pixels. (which define

images)

Department of CSE, HKBKCE 7 2011-2012

Page 9: CG Labs Project Final v1

Multi-Window Light Effects Requirement Specification

Chapter-2 Requirement Specification

2.1 User Requirement Specification

This software is developed with the following features:

A good graphical user interface to make the user comfortable with the software and make his

interaction a good experience.

To use openGL’s capability to plot the graphics. And make it to rotate. Should be a device

independent package. the program should be capable to run on any screen resolution.

2.2 Hardware and Software Requirements

The proper functioning of the software requires a minimum hardware and software configuration

as listed:

Intel pentium III or more powerful processor.

256 MB RAM or more. (512 MB RAM recommended)

A GPU powerful enough for fast rendering.

Microsoft windows 2000/xp/vista. Or Linux (Ubuntu)

Enough available disk space to store the program and associated libraries (*.lib and *.dll files).

A mouse and keyboard as an input devices and necessary drivers installed.

Department of CSE, HKBKCE 8 2011-2012

Page 10: CG Labs Project Final v1

Multi-Window Light Effects Requirement Specification

2.3 Introduction to the Environment

OpenGL (open graphics library) is a standard specification defining a cross-language, cross-

platform API for writing applications that produce 2D and 3D computer graphics. The interface

consists of over 250 different function calls which can be used to draw complex three-

dimensional scenes from simple primitives. Opengl was developed by silicon graphics inc. (SGI) in

1992 and is widely used in cad, virtual reality, scientific visualization, information visualization,

and flight simulation. It is also used in video games, where it competes with direct3d on microsoft

windows platforms (see direct3D vs. OpenGL). Opengl is managed by the non-profit technology

consortium, the khronos group.

At its most basic level opengl is a specification, meaning it is simply a document that describes a

set of functions and the precise behaviours that they must perform. From this specification,

hardware vendors create implementations — libraries of functions created to match the functions

stated in the opengl specification, making use of hardware acceleration where possible.

Hardware vendors have to meet specific tests to be able to qualify their implementation as an

opengl implementation. Opengl serves two main purposes:

To hide the complexities of interfacing with different 3D accelerators, by presenting the

programmer with a single, uniform api.

To hide the differing capabilities of hardware platforms, by requiring that all implementations

support the full opengl feature set (using software emulation if necessary).

Opengl's basic operation is to accept primitives such as points, lines and polygons, and convert

them into pixels. This is done by a graphics pipeline known as the opengl state machine. Most

opengl commands either issue primitives to the graphics pipeline, or configure how the pipeline

processes these primitives. Prior to the introduction of opengl 2.0, each stage of the pipeline

performed a fixed function and was configurable only within tight limits. Opengl 2.0 offers

several stages that are fully programmable using GLSL.

Opengl is a low-level, procedural api, requiring the programmer to dictate the exact steps

required to render a scene. This contrasts with descriptive (aka scene graph or retained mode) apis,

Department of CSE, HKBKCE 9 2011-2012

Page 11: CG Labs Project Final v1

Multi-Window Light Effects Requirement Specification

where a programmer only needs to describe a scene and can let the library manage the details of

rendering it. Opengl's low-level design requires programmers to have a good knowledge of the

graphics pipeline, but also gives a certain amount of freedom to implement novel rendering

algorithms.

Opengl has historically been influential on the development of 3d accelerators, promoting a base

level of functionality that is now common in consumer-level hardware:

A brief description of the process in the graphics pipeline could be Vertex operations,

transforming and lighting them depending on their material. Also clipping non visible parts of

the scene in order to produce the viewing volume. Rasterisation or conversion of the previous

information into pixels. The polygons are represented by the appropriate colour by means of

interpolation algorithms.

Per-fragment operations, like updating values depending on incoming and previously stored

depth values, or colour combinations, among others.lastly, fragments are inserted into the frame

buffer.

Many modern 3D accelerators provide functionality far above this baseline, but these new

features are generally enhancements of this basic pipeline rather than radical revisions of it.

Department of CSE, HKBKCE 10 2011-2012

Page 12: CG Labs Project Final v1

Multi-Window Light Effects Design

Chapter-3 Design

The project was developed using the C/C++ programming with the following functionalities:

3.1 Libraries Used

GLUT - The OpenGL Utility Toolkit

GLUT is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL

programs. It implements a simple windowing application programming interface (API) for

OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming.

GLUT provides a portable API so you can write a single OpenGL program that works across all

PC and workstation OS platforms.

GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is

well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a

full-featured toolkit so large applications requiring sophisticated user interfaces are better off

using native window system toolkits. GLUT is simple, easy, and small.

The GLUT library has both C, C++ (same as C), FORTRAN, and Ada programming bindings.

3.2 Working of the Program

The working of the program can be illustrated by the following abstract algorithm:

1. Initialization

Initialize to interact with the window system

Initialize the display mode that is double buffer mode and RGB color mode

Initialize the window position and the window size

Initialize and create the window

2. Every time the window is resized, the MyReshape function is called to resize the output.

Department of CSE, HKBKCE 11 2011-2012

Page 13: CG Labs Project Final v1

Multi-Window Light Effects Design

3. Menu’s and sub-menu’s are created. Depending on the value returned by the MyMenu

function, the suitable operation is performed.

4. Keyboard functions are also initialized. Depending on the value returned by the

MyKeyboard function, the suitable operations are performed.

5. The glutIdleFunc() is initialized to continuously make the objects to rotate.

6. The window can be activated by moving the cursor into that particular window. The

movement or position of the cursor, into the window, will be displayed in the terminal.

7. The keyboard functions can be activated when the appropriate key is pressed. The options

and their functions are shown below:

Window 1

“d” :- Diffuse

“a” :- Ambient

“s” :- Specular

“e” :- Emissive

“i” :- Shininess

“p” :- Light Position

“r” :- Red

“w” :- White

“g” :- Green

“b” :- Blue

Window 2

Left and Right Arrow keys for Rotation.

Up and Down Arrow keys for Scaling.

Department of CSE, HKBKCE 12 2011-2012

Page 14: CG Labs Project Final v1

Multi-Window Light Effects Implementation

Chapter-4 Implementation

/*An Interactive Program to create 3d objects*/

#include <GL/glut.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

#include <ctype.h>

#define ESC 27

// Dimensions of current window (reshaping is supported).

int width = 700;

int height = 600;

// Global variables that are set by keystrokes or

// mouse movements and used to control the display.

unsigned char key = ' '; // Last key pressed

int colour = -1; // Colour selection: 0=r, 1=g, 2=b, -1=w.

GLfloat ar = 1.33f; // Aspect ratio

GLfloat xm = 0.5; // X mouse position 0 < xm < 1

GLfloat ym = 0.5; // Y mouse position 0 < ym < 1

Department of CSE, HKBKCE 13 2011-2012

Page 15: CG Labs Project Final v1

Multi-Window Light Effects Implementation

// Light properties (controlled by mouse position)

GLfloat l_pos[] = { 5.0, 1.0, 5.0, 1.0 }; // Position

GLfloat l_diff[] = { 1.0, 1.0, 1.0, 1.0 }; // Diffuse

GLfloat l_amb[] = { 0.2f, 0.2f, 0.2f, 1.0 }; // Ambient

GLfloat l_spec[] = { 0.0, 0.0, 0.0, 1.0 }; // Specular

// Model properties (controlled by mouse position)

GLfloat m_diff[] = { 1.0, 1.0, 1.0, 1.0 }; // Diffuse

GLfloat m_amb[] = { 0.2f, 0.2f, 0.2f, 1.0 }; // Ambient

GLfloat m_spec[] = { 0.0, 0.0, 0.0, 1.0 }; // Specular

GLfloat m_emit[] = { 0.0, 0.0, 0.0, 1.0 }; // Emission

GLfloat m_shine[] = { 0.0 }; // Shininess

// Update the value of a property. If the current colour is

// 0, 1, or 2, change the corresponding colour component only.

// If colour=-1, change all components to the same value.

//for Window 2

#define DIM 100 // Number of lines on each side

double altitude[DIM][DIM]; // Array of heights for each vertex

double spin = 0.0, // Amount of spin

z = 0.0, // Zoom Factor

Department of CSE, HKBKCE 14 2011-2012

Page 16: CG Labs Project Final v1

Multi-Window Light Effects Implementation

maxAlt = 0.0,

minAlt = 0.0,

red = 1.0, green = 1.0, blue = 1.0; // RGB Components

/*GLUquadricObj *qobj;*/

//-----------------SUBROUTINES-----------------

//Subroutine Declaration...

//for window1

void keys (unsigned char thiskey, int x, int y);

void help ();

void drag (int x, int y);

void reshape (int w, int h);

void project (void);

void report ();

void show (GLfloat y, char *name, GLfloat a[], int n);

void showstring (GLfloat x, GLfloat y, char *string);

void set (GLfloat p[], GLfloat v);

//for window 2

Department of CSE, HKBKCE 15 2011-2012

Page 17: CG Labs Project Final v1

Multi-Window Light Effects Implementation

void setAltitude();

void pickColor(int a, int b ); // Function to determine drawing color

void Init(); // Initialization routine

void Reshape(int width, int height); // Determines Projection type

void keypressed(int key2, int x, int y); // Uses Special Function Keys

void drawMyStuff(); // Basic Drawing Routine

void display_win2();

// Window variables

int win1, win2;

void display(void) // Display routine for both Window 1 and 2

{

glClearColor(0.4, 0.4, 0.6, 1.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (glutGetWindow() == win1)

{ // Displaying Window 1

//glColor3f(1.0, 0.0, 0.0);

glPushMatrix();

project();

// glFlush();

glPopMatrix();

}

else

Department of CSE, HKBKCE 16 2011-2012

Page 18: CG Labs Project Final v1

Multi-Window Light Effects Implementation

{ // Displaying Window 2

glClearColor(0.5, 0.5, 0.5, 0.0);

glShadeModel(GL_FLAT);

glPushMatrix();

display_win2(); //calls display function for second window....

glFlush();

glPopMatrix();

}

//glFlush();

glutSwapBuffers();

}

/*void display_win1(void) // Initial Window 1 display calls the other routine

{

glPushMatrix();

display();

glPopMatrix();

}*/

// This function can tell which window is being left or entered

void which_window(int state)

{

printf("%s Window %d \n",

state == GLUT_LEFT ? "Leaving" : "Entering",

glutGetWindow());

}

// Simple Mouse Function

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

Department of CSE, HKBKCE 17 2011-2012

Page 19: CG Labs Project Final v1

Multi-Window Light Effects Implementation

{

printf("button: %d %s %d,%d\n", button, state == GLUT_UP ? "UP" : "down", x, y);

}

/*void init(void)

{

// Define first Quadric Object, a solid sphere as a call list.

gluQuadricDrawStyle(qobj, GLU_FILL);

glNewList(1, GL_COMPILE); //create sphere display list for Object 1

gluSphere(qobj, 1.0, 20, 20);

glEndList();

// Create a second Quadric Object, a wireframe sphere

gluQuadricDrawStyle(qobj, GLU_LINE);

glNewList(2, GL_COMPILE); //create sphere display list for Object 2

gluSphere(qobj, 0.5, 10, 10);

glEndList();

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);

gluPerspective( 40.0, 1.0, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.);

glTranslatef(0.0, 0.0, -1.0);

}*/

Department of CSE, HKBKCE 18 2011-2012

Page 20: CG Labs Project Final v1

Multi-Window Light Effects Implementation

int main(int argc, char **argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// Create First Window

glutInitWindowSize(width, height);

win1 = glutCreateWindow("Lighting and Materials. Press 'h' for instructions.");

glutEntryFunc(which_window);

//glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keys);

glutMotionFunc(drag);

glClearColor(0, 0, 0, 1.0);

glutDisplayFunc(display);

//glutIdleFunc(display);

glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

//glutMainLoop();

// Create Second Window

setAltitude(); // Initialize heights

glEnable(GL_DEPTH_TEST);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); // Initialize modes

Department of CSE, HKBKCE 19 2011-2012

Page 21: CG Labs Project Final v1

Multi-Window Light Effects Implementation

glutInitWindowSize( 550, 550);

glutInitWindowPosition( 720, 50);

win2 = glutCreateWindow( "Cool Equations");

glutEntryFunc(which_window);

//Init();

glutSpecialFunc(keypressed);

glutDisplayFunc(display);

glutIdleFunc(display);

glutReshapeFunc(Reshape);

glutMouseFunc(mouse);

//Init();

glutMainLoop();

return 0; /* ANSI C requires main to return int. */

}

//definations for win 2

void setAltitude() { // Initializes heights

int a, b;

for( a = 0; a < DIM; a++)

for( b = 0; b < DIM; b++) {

// Use some ugly math equation to generate a height value

altitude[a][b]= 16*sin(a/8.0)*cos(b/8.0);

// Figure out what the extremes in altitude might be

maxAlt = maxAlt > altitude[a][b] ? maxAlt : altitude[a][b];

minAlt = minAlt < altitude[a][b] ? minAlt : altitude[a][b];

}

Department of CSE, HKBKCE 20 2011-2012

Page 22: CG Labs Project Final v1

Multi-Window Light Effects Implementation

}

void pickColor(int a, int b ) { // Function to determine drawing color

double temp, range;

range = maxAlt - minAlt;

temp = altitude[a][b];

// Some kind of bizarre function to pick RGB values

green = 0.2 + fabs(temp *2/ range);

red = 1 + (temp*2/range);

blue = 0.2 + cos(temp*2/range);

}

void Init() { // Initialization routine

glClearColor(0.5, 0.5, 0.5, 0.0);

glShadeModel(GL_FLAT);

}

void Reshape(int width, int height) { // Determines Projection type

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Draw in Perspective rather than Orthographic Projection

// Args: (Field_of_View, Aspect_Ratio, Near_Clipping_Plane, Far_Clipping_Plane)

gluPerspective(60.0, (float)width / (float)height, 1.00, 2*DIM);

glMatrixMode(GL_MODELVIEW);

}

// Defined Keys:

Department of CSE, HKBKCE 21 2011-2012

Page 23: CG Labs Project Final v1

Multi-Window Light Effects Implementation

void keypressed(int key2, int x, int y) // Uses Special Function Keys

{ if (key2 == GLUT_KEY_LEFT)

{ spin -= 5; } // Rotate left 5 degrees

if (key2 == GLUT_KEY_RIGHT)

{ spin += 5; } // Rotate right 5 degrees

if (key2 == GLUT_KEY_UP )

{ z += 5; } // Move object away

if (key2 == GLUT_KEY_DOWN)

{ z -= 5; } // Move closer

glutPostRedisplay(); // Update the Display

}

void drawMyStuff() {// Basic Drawing Routine

int a, b;

glLoadIdentity();

// Specify View Position - (where you are looking.)

// Args: ( Eye_X, Eye_Y, Eye_Z, Cntr_X, Cntr_Y, Cntr_Z, Up_X, Up_Y, UpZ)

gluLookAt(0.0, DIM/4.0, DIM/2.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);

glTranslatef(0.0, 0.0, (-DIM/2-z) ); // Zoom In and Out

glRotatef(spin, 0.0, 1.0, 0.0); // Rotate Basic Surface around Y

// Comple Motion starts here

glPushMatrix(); // Establish a new reference point

glTranslatef(0.0, -35.0, 0.0); // Translate -35 in y-direction

glColor3f(1.0, 1.0, 0.0); // Change Drawing color to Yellow

glutWireCube(8.0); // Draw a wire frame Cube

glRotatef(-spin*2, 0.0, 1.0, 0.0); // Rotate axis in opposite direction

Department of CSE, HKBKCE 22 2011-2012

Page 24: CG Labs Project Final v1

Multi-Window Light Effects Implementation

glPushMatrix(); // Establish yet another reference point

glColor3f(1.0, 0.0, 0.0); // Change color to Red

glTranslatef(20.0, 0.0, 0.0); // Translate 20 units in x-direction

glutWireSphere(5.0, 8, 6); // Draw Wire Sphere with 5.0 radius

glPopMatrix(); // Return to previous reference

glPopMatrix(); // Return to first point of reference

// End of complex motion

for( a=0; a<DIM; a++) {

glBegin(GL_LINE_STRIP); // Draw Segments in one direction

for( b=0; b<DIM; b++) {

pickColor(a,b); // Change RGB Values

glColor3f(red, green, blue); // Choose New Color

glVertex3f((a - DIM/2 ), altitude[a][b], (b - DIM/2)); // Draw segment

}

glEnd();

}

for( b=0; b<DIM; b++) {

glBegin(GL_LINE_STRIP); // Draw Segments in other direction

for( a = 0; a < DIM; a++) {

pickColor(a,b); // Change RGB Values

glColor3f(red, green, blue); // Choose New Color

glVertex3f((a - DIM/2 ), altitude[a][b], (b - DIM/2)); // Draw segment

}

glEnd();

}

}

Department of CSE, HKBKCE 23 2011-2012

Page 25: CG Labs Project Final v1

Multi-Window Light Effects Implementation

void display_win2() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Color and Depth

drawMyStuff();

glFlush();

glutSwapBuffers(); // Double Buffering allows for smooth animation

}

//Win 1

void set (GLfloat p[], GLfloat v) {

if (colour >= 0)

p[colour] = v;

else {

int i;

for (i = 0; i < 3; i++)

p[i] = v;

}

}

// Display a bit-mapped character string.

void showstring (GLfloat x, GLfloat y, char *string) {

int len, i;

glRasterPos2f(x, y);

len = (int) strlen(string);

for (i = 0; i < len; i++)

glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);

}

Department of CSE, HKBKCE 24 2011-2012

Page 26: CG Labs Project Final v1

Multi-Window Light Effects Implementation

// Display the value of a property represented as an array with n components.

void show (GLfloat y, char *name, GLfloat a[], int n) {

char buf[100];

showstring(0.0, y, name);

if (n == 1)

sprintf(buf, "%8.3f", a[0]);

else

sprintf(buf, "%8.3f %8.3f %8.3f ", a[0], a[1], a[2]);

showstring(0.7f, y, buf);

}

// Display the values of all lighting parameters.

void report ()

{

char buf[100];

showstring(0.0, 0.0, "Light R G B");

show(-0.1f, "d Diffuse", l_diff, 3);

show(-0.2f, "a Ambient", l_amb, 3);

show(-0.3f, "s Specular", l_spec, 3);

show(-0.4f, "p Position", l_pos, 3);

showstring(0.0, -0.6f, "Model R G B");

show(-0.7f, "d Diffuse", m_diff, 3);

show(-0.8f, "a Ambient", m_amb, 3);

show(-0.9f, "s Specular", m_spec, 3);

show(-1.0f, "e Emissive", m_emit, 3);

Department of CSE, HKBKCE 25 2011-2012

Page 27: CG Labs Project Final v1

Multi-Window Light Effects Implementation

show(-1.1f, "i Shininess", m_shine, 1);

strcpy(buf, "");

switch (colour)

{

case -1:

strcpy(buf, "Colour: white.");

break;

case 0:

strcpy(buf, "Colour: red.");

break;

case 1:

strcpy(buf, "Colour: green.");

break;

case 2:

strcpy(buf, "Colour: blue.");

break;

}

showstring(0.0, -1.3f, buf);

strcpy(buf, "");

switch (key)

{

case 'd':

strcpy(buf, "Property: diffuse.");

break;

case 'a':

strcpy(buf, "Property: ambient.");

break;

case 's':

Department of CSE, HKBKCE 26 2011-2012

Page 28: CG Labs Project Final v1

Multi-Window Light Effects Implementation

strcpy(buf, "Property: specular.");

break;

case 'e':

strcpy(buf, "Property: emission.");

break;

case 'i':

strcpy(buf, "Property: shininess.");

break;

case 'p':

strcpy(buf, "Property: light position.");

break;

}

showstring(1.5, -1.3f, buf);

}

// The display function uses the current key to select light and model

// properties, then it uses the current mouse position to change those

// properties. After the properties have been set, the lights and model

// are defined and drawn on the screen, along with parameter values.

void project (void) {

// Use the mouse position to set values of the specified properties.

switch (key) {

case 'd':

set(l_diff, xm);

set(m_diff, ym);

break;

Department of CSE, HKBKCE 27 2011-2012

Page 29: CG Labs Project Final v1

Multi-Window Light Effects Implementation

case 'a':

set(l_amb, xm);

set(m_amb, ym);

break;

case 's':

set(l_spec, xm);

set(m_spec, ym);

break;

case 'e':

set(m_emit, ym);

break;

case 'i':

m_shine[0] = 128.0 * ym;

break;

case 'p':

l_pos[0] = 20.0 * (xm - 0.5f);

l_pos[1] = 20.0 * (ym - 0.5f);

break;

default:

break;

}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Assign the chosen values.

glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_diff);

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_amb);

Department of CSE, HKBKCE 28 2011-2012

Page 30: CG Labs Project Final v1

Multi-Window Light Effects Implementation

glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_spec);

glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, m_shine);

glLightfv(GL_LIGHT0, GL_DIFFUSE, l_diff);

glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb);

glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

// Position the light and then move the model into the viewing volume.

glLightfv(GL_LIGHT0, GL_POSITION, l_pos);

glTranslatef(-1.0, -1.0, -8.0);

// Disable lighting while displaying text.

glDisable(GL_LIGHTING);

glPushMatrix();

glTranslatef(0.8f, 0.8f, 0.0);

report();

glEnable(GL_LIGHTING);

glPopMatrix();

// Display the objects.

glPushMatrix();

glRotatef(90.0, -1.0, 0.0, 0.0);

Department of CSE, HKBKCE 29 2011-2012

Page 31: CG Labs Project Final v1

Multi-Window Light Effects Implementation

glutSolidCone(0.5, 2.0, 40, 40);

glPopMatrix();

glTranslatef(0.0, 2.5, 0.0);

glutSolidSphere(0.5, 40,40);

// Swap buffers for smooth animation.

glutSwapBuffers();

}

// Respond to reshape request by changing the viewport and the projection.

void reshape (int w, int h) {

glViewport(0, 0, w, h);

width = w;

height = h;

ar = (GLfloat) width / (GLfloat) height;

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(30.0, ar, 1.0, 10.0);

glutPostRedisplay();

}

// Respond to mouse movements by setting xm and ym to values in [0,1].

void drag (int x, int y) {

xm = (GLfloat) x / (GLfloat) width;

ym = 1.0 - (GLfloat) y / (GLfloat) height;

glutPostRedisplay();

Department of CSE, HKBKCE 30 2011-2012

Page 32: CG Labs Project Final v1

Multi-Window Light Effects Implementation

}

// Display some useful remarks in the Xterm window.

void help () {

printf("Press a key to select a property.\n");

printf("Move the mouse to see the effect of changing the property.\n\n");

printf("Key X motion (light) Y motion (object)\n");

printf(" d diffuse diffuse\n");

printf(" a ambient ambient\n");

printf(" s specular specular\n");

printf(" i shininess\n");

printf(" e emission\n\n");

printf("By default, mouse movement will change all colour components.\n");

printf("To select one colour component:\n");

printf(" Press 'r' for red.\n");

printf(" Press 'g' for green.\n");

printf(" Press 'b' for blue.\n");

printf(" Press 'w' (white) to change all components.\n\n");

}

// Respond to a keystroke. Some responses are processed here; the default

// action is to record the keystroke and use it in the display callback function.

void keys (unsigned char thiskey, int x, int y) {

switch (thiskey) {

Department of CSE, HKBKCE 31 2011-2012

Page 33: CG Labs Project Final v1

Multi-Window Light Effects Implementation

case ESC:

exit(0);

case 'h': // Display help in text window

help();

break;

case 'r': // Change red values only

colour = 0;

break;

case 'g': // Change green values only

colour = 1;

break;

case 'b': // Change blue values only

colour = 2;

break;

case 'w': // Change all colours

colour = -1;

break;

default: // Save key value for display()

key = thiskey;

break;

}

glutPostRedisplay();

}

Department of CSE, HKBKCE 32 2011-2012

Page 34: CG Labs Project Final v1

Multi-Window Light Effects Snapshots

Chapter-5 Snapshots

Department of CSE, HKBKCE 33 2011-2012

Fig 5.1 Cursor position displayed on terminal for Window 1

Fig 5.2 Cursor position displayed on terminal for Window 2

Fig 5.3 Window 2 for Scaling & Rotation Fig 5.4 Light effects for Window 1 Fig 5.6 Light Effects for Window 1

Page 35: CG Labs Project Final v1

Multi-Window Light Effects Conclusion

Chapter-6 Conclusion

The program is successfully implemented as required by user software requirements. The

program provides all the functionality required by the user in an optimal way. The

program is user friendly and provides the necessary options to the user whenever

required. The software is developed with a simple interface.

The main purpose of developing this graphics package was not to develop an unmatchable

graphics scene but to develop a one which could perform the most basic operations and during

the process aid us in learning about computer graphics and programming.

The graphics package has been tested under Windows and has been found to be user friendly

providing ease of use and manipulation to the user.

We found designing and developing this graphics package as a very interesting and learning

experience. It helped us to learn about computer graphics, design of Graphical User Interfaces

(GUI), interface to the user, user interaction handling and screen management. The graphics

package has been incorporated with more than the required features that have been detailed in

the university syllabi.

Department of CSE, HKBKCE 34 2011-2012

Page 36: CG Labs Project Final v1

Multi-Window Light Effects Future Enhancements

Chapter-7 Scope for Future Enhancements

The following are some of the features that are planned to be supported in the future versions of

our project.

The number of object and colors that are provided for drawing the object purposed can be

increased.

Support for more 3d drawings and transformations.

Improve the looks of my project and to reduce the cluttering due to available tools on the

screen.

Department of CSE, HKBKCE 35 2011-2012

Page 37: CG Labs Project Final v1

Bibliography

[1]. Interactive Computer Graphics A Top-Down Approach with OpenGL-Edward Angel,5th

edition,Addison-Wesley,2008.

[2]. Copmuter Graphics Using OpenGL-F.S Hill,Jr,2nd edition,Pearson Education,2001.

[3]. www.opengl.org. [online] www.opengl.org/resources/faq/technical/window.htm .

[4].opengl programming guide. http://glprogramming.com. [online]

http://glprogramming.com/red/appendixd.html.

Department of CSE, HKBKCE 36 2011-2012