Traffic Signal CG Mini Project Using OpenGL Report
Traffic Signal CG Mini Project Using OpenGL Report
Traffic Signal CG Mini Project Using OpenGL Report
CONTENTS
Abstract
Main aim of this Mini Project is to illustrate the concepts and usage of
pre-built functions in OpenGL.
The car built using cubes can be moved using arrow keys and based
on traffic signal light the user can obey the traffic rules.
System specifications
SOFTWARE REQUIREMENTS :
HARDWARE REQUIREMENT :
GRAPHICS SYSTEM,
Pentium P4 with 256 of Ram(Min)
Introduction to openGL
OpenGL Fundamentals
This section explains some of the concepts inherent in OpenGL.
As shown by the first block in the diagram, rather than having all commands
proceed immediately through the pipeline, you can choose to accumulate some
of them in a display list for processing at a later time.
updates to the frame buffer based on incoming and previously stored z-value s
(for z-buffering) and blending of incoming pixel colors with stored colors, as well
as masking and other logical operations on pixel values.
All elements of OpenGL state, including the contents of the texture memory and
even of the frame buffer, can be obtained by an OpenGL application.
Implementation
This program is implemented using various openGL functions which are
shown below.
glutCreateWindow() : this opens the OPENGL window and displays the title
at top of the window
i Student Information
Q-> Quit
Source Code
#include <windows.h>
#include<string.h>
#include<stdarg.h>
#include<stdio.h>
#include <glut.h>
void *font;
void *currentfont;
currentfont=font;
char *c;
glRasterPos3f(x,y,z);
for(c=string;*c!='\0';c++)
{ glColor3f(0.0,1.0,1.0);
glutBitmapCharacter(currentfont,*c);
}
11 Dept. of Computer Science & Engineering.
Traffic Signal
void tree(){
glPushMatrix();
glColor3f(0,0.2,0);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(-.5,0.5);
glVertex2f(0,0.7);
glVertex2f(-0.3,1);
glVertex2f(0.2,0.9);
12 Dept. of Computer Science & Engineering.
Traffic Signal
glVertex2f(0.8,2);
glVertex2f(0.8,0.9);
glVertex2f(1.2,1);
glVertex2f(0.8,0.8);
glVertex2f(1.2,0.5);
glVertex2f(0.5,0);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,-0.4,0);
glScaled(0.2,1,0.1);
glColor3f(0.5,0.1,0.1);
glutSolidCube(1.0);
13 Dept. of Computer Science & Engineering.
Traffic Signal
glPopMatrix();
void road(){
//Background
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex2f(-6,6);
glVertex2f(6,6);
glVertex2f(6,-6);
glVertex2f(-6,-6);
glEnd();
// Horizontal Road
glColor3f(0.4,0.4,0.4);
glBegin(GL_POLYGON);
glVertex2f(-6,2.5);
glVertex2f(6,2.5);
14 Dept. of Computer Science & Engineering.
Traffic Signal
glVertex2f(6,1.5);
glVertex2f(-6,1.5);
glEnd();
// Horizontal Stripes
glColor3f(0,0,0);
for(float x1=0;x1<=100;x1+=0.4){
glBegin(GL_POLYGON);
glVertex2f(-6+x1,2);
glVertex2f(-5.8+x1,2);
glVertex2f(-5.8+x1,1.9);
glVertex2f(-6+x1,1.9);
glEnd();
// Vertical road
glColor3f(0.4,0.4,0.4);
glBegin(GL_POLYGON);
glVertex2f(0.4,6);
15 Dept. of Computer Science & Engineering.
Traffic Signal
glVertex2f(-0.4,6);
glVertex2f(-0.4,-6);
glVertex2f(0.4,-6);
glEnd();
//Vertical Stripes
glColor3f(0,0,0);
for(float y1=0;y1<=100;y1+=1){
glBegin(GL_QUADS);
glVertex2f(0.04,-6+y1);
glVertex2f(0.04,-5.5+y1);
glVertex2f(-0.04,-5.5+y1);
glVertex2f(-0.04,-6+y1);
glEnd();
{
16 Dept. of Computer Science & Engineering.
Traffic Signal
glPushMatrix();
glTranslatef(x1,y1,0);
glScaled(0.5,0.4,2.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glScaled(0.3,0.5,0.5);
glTranslatef(x1,y1,0);
glPushMatrix();
glColor3f(1,0,1);
glutSolidCube(1);
glPopMatrix();
//wheel 1
glPushMatrix();
glRotatef(90,1.0f,0.0f,0.0f);
17 Dept. of Computer Science & Engineering.
Traffic Signal
glTranslatef(-0.45,0.15,-0.02);
glScaled(0.15,0.15,0.6);
glColor3f(.5,.5,.5);
glutSolidTorus(1,0.8,20,20);
glPopMatrix();
//wheel 2
glPushMatrix();
glRotatef(90,1.0f,0.0f,0.0f);
glTranslatef(0.35,0.15,-0.02);
glScaled(0.15,0.15,0.6);
glColor3f(.5,.5,.5);
glutSolidTorus(1,0.8,20,20);
glPopMatrix();
//footer cube
glPushMatrix();
glTranslatef(-0.2,0,0);
glScaled(1.5,1,.7);
glColor3f(0,0,1);
18 Dept. of Computer Science & Engineering.
Traffic Signal
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glScaled(0.3,0.5,0.5);
glTranslatef(x1,y1,0);
glRotatef(-90,0,0,1);
glPushMatrix();
glColor3f(1,0,1);
glutSolidCube(1);
glPopMatrix();
//wheel 1
glPushMatrix();
glRotatef(90,1.0f,0.0f,0.0f);
glTranslatef(-0.45,0.15,-0.02);
19 Dept. of Computer Science & Engineering.
Traffic Signal
glScaled(0.15,0.15,0.6);
glColor3f(.5,.5,.5);
glutSolidTorus(1,0.8,20,20);
glPopMatrix();
//wheel 2
glPushMatrix();
glRotatef(90,1.0f,0.0f,0.0f);
glTranslatef(0.35,0.15,-0.02);
glScaled(0.15,0.15,0.6);
glColor3f(.5,.5,.5);
glutSolidTorus(1,0.8,20,20);
glPopMatrix();
//footer cube
glPushMatrix();
glTranslatef(-0.2,0,0);
glScaled(1.5,1,.7);
glColor3f(0,0,1);
glutSolidCube(1);
20 Dept. of Computer Science & Engineering.
Traffic Signal
glPopMatrix();
glPopMatrix();
glPushMatrix();
glScaled(0.2,0.6,0.1);
glTranslatef(x1,y1,0);
glPushMatrix();
glColor3f(0,0,1);
glutSolidCube(1);
glPopMatrix();
for(int tx=0;tx<=4;tx++)
}else if(tx==0)
21 Dept. of Computer Science & Engineering.
Traffic Signal
x[tx]+=0.07;
else if(tempx[tx]<=tempx[tx-1]+2)
else
x[tx]+=0.07;
cars(tempx[tx],3.5);
for(int ty1=0;ty1<=4;ty1++)
glScaled(0.2,0.2,0.2);
glutSolidSphere(0.35,30,30);
glPopMatrix();
//green signal
22 Dept. of Computer Science & Engineering.
Traffic Signal
glPushMatrix();
if(green)
glColor3f(0,1,0);
else
glColor3f(0,0,0);
glTranslatef(0.65,2.5,0.9);
glScaled(0.2,0.2,0.2);
glutSolidSphere(0.35,30,30);
glPopMatrix();
//Pole
glPushMatrix();
glTranslatef(-0.65,0.8,0);
glScaled(0.2,0.4,4.5);
glutSolidCube(0.5);
glPopMatrix();
//red signal
glPushMatrix();
23 Dept. of Computer Science & Engineering.
Traffic Signal
if(red)
glColor3f(0,1,0);
else
glColor3f(0,0,0);
glTranslatef(-0.6,0.8,0.7);
glScaled(0.2,0.5,0.5);
glutSolidSphere(0.25,30,30);
glPopMatrix();
//green signal
glPushMatrix();
if(green)
glColor3f(1,0,0);
else
glColor3f(0,0,0);
glTranslatef(-0.6,0.8,1);
glScaled(0.2,0.5,0.5);
glutSolidSphere(0.25,30,30);
glPopMatrix();
//Garden trees
glPushMatrix();
glTranslatef(2.5,-2.8,1);
glRotatef(70,1,0,0);
glScaled(0.4,0.4,0.4);
tree();
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,-2.8,1);
glRotatef(70,1,0,0);
glScaled(0.4,0.4,0.4);
tree();
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,-2.8,1);
glRotatef(70,1,0,0);
glScaled(0.4,0.4,0.4);
tree();
25 Dept. of Computer Science & Engineering.
Traffic Signal
glPopMatrix();
glPushMatrix();
glTranslatef(2.5,-4.8,1);
glRotatef(70,1,0,0);
glScaled(0.4,0.4,0.4);
tree();
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,-4.8,1);
glRotatef(70,1,0,0);
glScaled(0.4,0.4,0.4);
tree();
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,-4.8,1);
glRotatef(70,1,0,0);
26 Dept. of Computer Science & Engineering.
Traffic Signal
glScaled(0.4,0.4,0.4);
tree();
glPopMatrix();
glFlush();
glutSwapBuffers();
void i()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0,0.5,0.5,0);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
drawstring(-1,1,-1.0,"Submitted By");
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
27 Dept. of Computer Science & Engineering.
Traffic Signal
drawstring(-2,0,-1.0,"Student 1");
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
drawstring(-2,-1,-1.0,"Student 2");
glEnable(GL_COLOR_MATERIAL);
glFlush();
glutSwapBuffers();
void c()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.5,0.0,0.5,0);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
28 Dept. of Computer Science & Engineering.
Traffic Signal
glColor3f(1,1,1);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1,1,1);
glEnable(GL_COLOR_MATERIAL);
glFlush();
glutSwapBuffers();
void gameOver(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,0);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0,0,0);
drawstring(-1,0,-1.0,"Accident");
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0,0,0);
glFlush();
glutSwapBuffers();
void p()
//automate signals
if(s1<=10){
if(s1<5){
red=true;
green=false;
s1+=0.006;
if(s1>5){
red=false;
green=true;
s1+=0.006;
if(s1>=9.5){
31 Dept. of Computer Science & Engineering.
Traffic Signal
s1=0;
if(crash)
gameOver();
else{
//if(!stop)
//x+=0.07;
trafic(x[0]);
void P()
32 Dept. of Computer Science & Engineering.
Traffic Signal
//x += .07;
trafic(x[0]);
void doInit()
glClearColor(1.0,1.0,1.0,0);
glViewport(0,0,640,480);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearDepth(2.0f);
glEnable(GL_DEPTH_TEST);
glEnable( GL_COLOR_MATERIAL );
33 Dept. of Computer Science & Engineering.
Traffic Signal
glDepthFunc(GL_LEQUAL);
void display()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,0);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0,0,0);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0,0,0);
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
34 Dept. of Computer Science & Engineering.
Traffic Signal
glColor3f(0,0,0);
GLfloat mat_ambient[]={0.0f,1.0f,2.0f,1.0f};
GLfloat mat_diffuse[]={0.0f,1.5f,.5f,1.0f};
GLfloat mat_specular[]={5.0f,1.0f,1.0f,1.0f};
GLfloat mat_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);
GLfloat lightIntensity[]={1.7f,1.7f,1.7f,1.0f};
GLfloat light_position3[]={2.0f,10.0f,3.0f,1.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_position3);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
GLfloat lightIntensity1[]={1.7f,1.7f,1.7f,1.0f};
GLfloat light_position1[]={-2.0f,1.0f,-5.0f,1.0f};
35 Dept. of Computer Science & Engineering.
Traffic Signal
glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
glLightfv(GL_LIGHT1,GL_DIFFUSE,lightIntensity1);
glEnable(GL_COLOR_MATERIAL);
glFlush();
glutSwapBuffers();
if(key=='2')
{ red=false;
green=true;
glutIdleFunc(p);
if(key=='r')
36 Dept. of Computer Science & Engineering.
Traffic Signal
{ red=false;
green=true;
crash=false;
glClearColor(1,1,1,1);
glutIdleFunc(p);
if(key=='i')
glutIdleFunc(i);
if(key=='c')
glutIdleFunc(c);
if(key=='q'||key=='Q')
exit(0);
}
37 Dept. of Computer Science & Engineering.
Traffic Signal
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(1000,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Traffic Control");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutKeyboardFunc(mykey);
glutCreateMenu(menu);
38 Dept. of Computer Science & Engineering.
Traffic Signal
glutAddMenuEntry("Start 'p'",1);
glutAddMenuEntry("Quit 'q'",5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
doInit();
glutMainLoop();
return 0;
Conclusions
The project “Traffic Signal” clearly demonstrates the simulation of traffic signal
using OpenGL.
Finally we conclude that this program clearly illustrate the traffic signal using
openGL and has been completed successfully and is ready to be demonstrated.
Bibliography
42 Dept. of Computer Science & Engineering.
Traffic Signal
TEXT BOOKS :
INTERACTIVE COMPUTER GRAPHICS A TOP-DOWN APPROACH
-By Edward Angel.
- Feiner hughes
WEB REFERENCES:
http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson3.php
http://google.com
http://opengl.org