17
Page | 1 Dept .of Computer science & engineering UBDTCE Serial no. CONTENTS Page no. Acknowledgement 2 1. 1.1 1.2 1.3 Project Description 3 Purpose Scope Objects used 2. 2.1 Introduction to Graphics 4 Introduction to OpenGL 3. Requirements 5 4. Implementation 6-11 5. Snapshots 12-15 6. Conclusion 16

CG mini project

Embed Size (px)

Citation preview

Page 1: CG mini project

P a g e | 1

Dept .of Computer science & engineering UBDTCE

Serial no. CONTENTS Page no.

Acknowledgement 2

1.

1.1

1.2

1.3

Project Description

3 Purpose

Scope

Objects used

2.

2.1

Introduction to Graphics

4 Introduction to OpenGL

3. Requirements 5

4. Implementation 6-11

5. Snapshots 12-15

6. Conclusion 16

Page 2: CG mini project

P a g e | 2

Dept .of Computer science & engineering UBDTCE

ACKNOWLEDGEMENT

The satisfaction that accompanies the successful completion of any task

would be incomplete without mentioning the people who made it possible,

whose constant guidance and encouragement crowned my effort with success.

First and foremost, I would like to express our sincere words of gratitude

and respect to the organization “University B.D.T. COLLEGE OF

ENGINEERING, DAVANGERE” for providing us an opportunity to carry out

my project work.

I thank the principal, Dr.Shivaprasad B Dandegi, U.B.D.T. College of

Engineering, for providing us with all the facilities that helped us to carry out

the work easily.

I would like to express my deepest thanks to Dr. K S Shreedhara , PhD

in DOS, H.O.D, Department of Computer Science & Engineering, for his

encouragement.

I am very grateful to our guide Srinivas Naik C L Professor, Department of

Computer Science & Engineering, for their insightful comments and for sharing

with us his knowledge and experience. I really appreciate his help to improve

the quality of my report.

Last but not the least; I extend my thanks to all the people in the department of

Computer Science & Engineering, for always being helpful over the years. I am

very grateful to my parents and well wishers for their continuous moral support

and encouragement.

Page 3: CG mini project

P a g e | 3

Dept .of Computer science & engineering UBDTCE

AIM:-

Program to display The Rotating effect on TEAPOT by using Keyboard

and Mouse.

1. PROJECT DESCRIPTION:-

The example program provides a simple application of the Rotate

primitives of OpenGL. The graphics image consists of TEAPOT.

1.1 PURPOSE:-

The purpose of this document is to explain the functionality of Rotating

in OpenGL, the study of requirement specification is focused specifically on

functioning, it allows the developer or analyst to understand the Rotation

options in OpenGL system.

1.2 SCOPE:-

This project used to understand and modified the developed Rotation

system without much complication. Hence it can be used for better convenience

and learning without any bearing effect on the system or the data contained in it.

The project helps in understanding true Rotating in graphics

1.3 OBJECT USED:-

1) Rotation

2) Display list

3) GLUT manage windows call back

4) Mouse and Keyboard to simulate rotation of Teapot .

Page 4: CG mini project

P a g e | 4

Dept .of Computer science & engineering UBDTCE

2.INTRODUCTION TO GRAPHICS

Graphics provides one of the most natural means of communicating with a

computer, since our highly developed 2D and 3D pattern-recognition abilities

allows us to perceive and process pictorial data rapidly and a efficiently.

Interactive computer graphics is the most important means of producing

pictures since the invention of photography and television. It has the added

advantage that, with the computer, we can pictures not only of concrete real

world objects but also of abstract, synthetic objects, such as mathematical

surfaces and of data that have no inherent geometry, such as survey results.

Pictorial synthesis of real/imaginary objects from computer based

models is computer graphics. There is virtually no area in which graphical

displays cannot be used to some advantage, and so it is not surprising to find the

use of computer graphics so wide spread. Today, we find computer graphics

used routinely in such diverse areas such as science, engineering, medicine,

business, industry, government, art, entertainment, advertising, education and

training.

Because of the wide use of computer graphics in all the fields, we developed a

game to exploit all the features of graphics and understand it in a better way.

Here we have attempted to design a game which we have named Lighting

Spheres, incorporating as many features of a simple lighting as possible and

adding a few of our own using the algorithms and skills that we have learnt in

class. It is our first insight into the vast world of interactive graphics.

2.1 INTRODUCTION TO OPENGL

OpenGL is a software interface to graphics hardware. This interface consists of

about 120 distinct commands, which you use to specify the objects and

operations needed to produce interactive three dimensional applications.

OpenGL is designed to work efficiently even if the computer displays the

graphics you create is not the computer that runs your graphics program. This

Page 5: CG mini project

P a g e | 5

Dept .of Computer science & engineering UBDTCE

might be the case if you work in a networked computer environment where

many computers are connected to one another by wires cable of carrying digital

data.

3. REQUIREMENTS

REQUIREMANT SPECIFICATION:-

The hardware and software requirement for the efficient working of our project

are specified as follows:

3.1Hardware specification:-

Pentium 3 processor.

Standard QWERTY serial or PS/2 keyboard.

AT&T Compatible mouse.

Processor speed of 300MHz and above.

128Mb of RAM.

200Mb of minimum disk space.

VGA 256 color video with min 16bit color qualitity.

3.2 Software specification:-

Windows XP operating system.

A visual C++ compiler [Microsoft Visual Studio].

OpenGL libraries- gl, glut, OpenGL and glaux

Page 6: CG mini project

P a g e | 6

Dept .of Computer science & engineering UBDTCE

4. IMPLEMENTATION

#include<GLUT/glut.h>

#include<gl.h>

#include<glut.h>

#include<stdlib.h>

void wall(double thickness)

{

//draw thin wall with top=xz-plane,corner at orgine

glPushMatrix();

glTranslated(0.5,0.5*thickness,0.5);

glScaled(1.0,thickness,1.0);

glutSolidCube(1.0);

glPopMatrix();

}

//draw one table leg

voidtableleg(double thick,doublelen)

{

glPushMatrix();

glTranslated(0,len/2,0);

glScaled(thick,len,thick);

glutSolidCube(1.0);

glPopMatrix();

}

Page 7: CG mini project

P a g e | 7

Dept .of Computer science & engineering UBDTCE

void table(double topwid,double topthick,double legthick,double leglen)

{

glPushMatrix();

glTranslated(0,leglen,0);

glScaled(topwid,topthick,topwid);

glutSolidCube(1.0);

glPopMatrix();

doubledist=0.95*topwid/2.0-legthick/2.0;

glPushMatrix();

glTranslated(dist,0,dist);

tableleg(legthick,leglen);

glTranslated(0.0,0.0,-2.0*dist);

tableleg(legthick,leglen);

glTranslated(-2*dist,0,2*dist);

tableleg(legthick,leglen);

glTranslated(0,0,-2*dist);

tableleg(legthick,leglen);

glPopMatrix();

}

Static GLfloat theta[]={0.0,0.0,0.0};

Static GLint axis=2;

Static GLdouble viewer[]={0.0,0.0,5.0};

void displaysolid(void)

{

//set surface material properties

Page 8: CG mini project

P a g e | 8

Dept .of Computer science & engineering UBDTCE

GLfloatmat_ambient[]={0.7f,0.7f,0.7f,1.0f};

GLfloatmat_diffuse[]={0.5f,0.5f,0.5f,1.0f};

GLfloatmat_specular[]={1.0f,1.0f,1.0f,1.0f};

GLfloatmat_shininess[]={50.0f};

glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);

glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);

glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);

glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);

//set light source properties

GLfloatlightIntensity[]={0.7f,0.7f,0.7f,1.0f};

GLfloatlight_position[]={2.0f,6.0f,3.0f,0.0f};

glLightfv(GL_LIGHT0,GL_POSITION,light_position);

glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);

//set camera

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

doublewinht=1.0; //half height of window

glOrtho(-winht*64/48.0,winht*64/48.0,-winht,winht,0.1,100.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.25,0.0,0.0,1.0,0.0);

//start drawing

Page 9: CG mini project

P a g e | 9

Dept .of Computer science & engineering UBDTCE

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glTranslated(0.4,0.4,0.6);

glRotated(45,0,0,1);

glScaled(0.08,0.08,0.08);

glPopMatrix();

glPushMatrix();

glTranslated(0.6,0.38,0.5);

glRotatef(theta[0],1,0,0);

glRotatef(theta[1],0,1,0);

glRotatef(theta[2],0,0,1);

glutSolidTeapot(0.08);

glPopMatrix();

glPushMatrix();

glTranslated(0.4,0,0.4);

table(0.6,0.02,0.02,0.3);

glPopMatrix();

wall(0.02);

glPushMatrix();

glRotated(90.0,0.0,0.0,1.0);

wall(0.02);

glPopMatrix();

Page 10: CG mini project

P a g e | 10

Dept .of Computer science & engineering UBDTCE

glPushMatrix();

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

wall(0.02);

glPopMatrix();

glFlush();

glutSwapBuffers();

}

void mouse(intbtn,intstate,intx,int y)

{

if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)axis=0;

if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)axis=1;

if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)axis=2;

theta[axis]+=2.0;

if(theta[axis]>360.0)theta[axis]-=360.0;

displaysolid();

}

void keys(unsigned char key,int x,int y)

{

if(key=='x')viewer[0]+=1.0;

if(key=='a')viewer[0]-=1.0;

if(key=='y')viewer[1]+=1.0;

if(key=='b')viewer[1]-=1.0;

if(key=='z')viewer[2]+=1.0;

if(key=='c')viewer[2]-=1.0;

displaysolid();

Page 11: CG mini project

P a g e | 11

Dept .of Computer science & engineering UBDTCE

}

int w=900,h=600;

void main(int argc,char **argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(w,h);

glutInitWindowPosition(300,50);

glutCreateWindow("TEAPOT PRAGRAM");

glutDisplayFunc(displaysolid);

glutMouseFunc(mouse);

glutKeyboardFunc(keys);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glShadeModel(GL_SMOOTH);

glEnable(GL_DEPTH_TEST);

glEnable(GL_NORMALIZE);

glClearColor(0.5,0.3,0.2,1.0);

glViewport(0,0,w,h);

glutMainLoop();

}

Page 12: CG mini project

P a g e | 12

Dept .of Computer science & engineering UBDTCE

5.SNAPSHOTS

Fig5.1:Default view of object when it is executed

When the program is executed, the object is viewed as shown in fig 5.1.

Page 13: CG mini project

P a g e | 13

Dept .of Computer science & engineering UBDTCE

Fig5.2:Perspective view of camera along X-axis.

This object is obtained by interfacing keyboard function keys to, which gives a perspective

view along X-axis as shown in fig 5.2.

Using ‘x’ and ‘a’ key the camera can be viewed along X-axis, if we consider ‘a’ key to

view camera along positive X-axis then ‘x’ key is used to camera along negative X-axis

Page 14: CG mini project

P a g e | 14

Dept .of Computer science & engineering UBDTCE

Fig5.3:Rotation of Teapot.

In this fig5.3 Teapot is rotated along Y-axis by interfacing mouse functions. This image

is obtained by clicking RIGHT Mouse Button.

Here in this fig5.3 the complete image is rotated along Y-axis using ‘y’ and ‘b’ key. If we

consider ‘b’ to rotate the image along Positive Y-axis then ‘y’ is used to rotate the image

along Negative Y-axis.

Also in this fig5.3, the Teapot is also rotated using mouse buttons, where the Teapot is

rotated along Z-axis by clicking LEFT Mouse Button.

Page 15: CG mini project

P a g e | 15

Dept .of Computer science & engineering UBDTCE

Fig5.4:Rotation of Teapot along Y-axis.

In this fig5.4 Teapot is rotated along X-axis by interfacing mouse functions. This image is

obtained by clicking RIGHT Mouse Button.

This image is same as previous image shown in fig5.3, where the Teapot is rotated along

Y-axis by clicking RIGHT Mouse Button.

Similarly, the same Teapot can be rotated along X-axis by clicking MIDDLE Mouse

Button.

Page 16: CG mini project

P a g e | 16

Dept .of Computer science & engineering UBDTCE

6.CONCLUSION

After the completion of this project we came to know how to work with

Microsoft visual studio and how we can implement a project using an open

source OpenGL tool kit. By implementing a project using OpenGL, we came to

know how to use the functions like lighting, rotation, translation and scaling.

With the completion of this project we have achieved a sense of happiness and

we want to thank all those who helped us directly or indirectly to make this idea

come true.

Page 17: CG mini project

P a g e | 17

Dept .of Computer science & engineering UBDTCE