Train Signal
CONTENTS
SL.NO particulars PAGE.NO
1 Abstract 2
2 System Specifications 3
3 Introduction to openGL 4
5 Implementation 7
6 Interaction 9
7 Source Code 10
8 Output 27
9 Conclusion 29
10 Bibliography 30
1 Dept. of Computer Science & Engineering.
Train Signal
Abstract
Main aim of this Mini Project is to illustrate the concepts and usage of
pre-built functions in OpenGL.
Train signal project simulated the railway crossing where two tracks
are connected.
When there’s just single railway track then a signal is used to stop one
train and let other train to cross over using crossing track.
Finally once the train has crossed over then the signal is turned back
to green and the stopped train is allowed to go normally.
We have used input devices like mouse and key board to interact with
program
2 Dept. of Computer Science & Engineering.
Train Signal
System specifications
SOFTWARE REQUIREMENTS :
MICROSOFT VISUAL C++
OPENGL
HARDWARE REQUIREMENT :
GRAPHICS SYSTEM,
Pentium P4 with 256 of Ram(Min)
3 Dept. of Computer Science & Engineering.
Train Signal
Introduction to openGL
As a software interface for graphics hardware, OpenGL's main purpose is to
render two- and three-dimensional objects into a frame buffer.
These objects are described as sequences of vertices or pixels.
OpenGL performs several processing steps on this data to convert it to pixels to
form the final desired image in the frame buffer.
OpenGL Fundamentals
This section explains some of the concepts inherent in OpenGL.
Primitives and Commands
OpenGL draws primitives—points, line segments, or polygons—subject to several
selectable modes.
You can control modes independently of each other; that is, setting one mode
doesn't affect whether other modes are set .Primitives are specified, modes are
set, and other OpenGL operations are described by issuing commands in the form
of function calls.
Primitives are defined by a group of one or more vertices. A vertex defines a
point, an endpoint of a line, or a corner of a polygon where two edges meet. Data
is associated with a vertex, and each vertex and its associated data are processed
independently, in order, and in the same way. The type of clipping depends on
which primitive the group of vertices represents.
Commands are always processed in the order in which they are received,
although there may be an indeterminate delay before a command takes effect.
This means that each primitive is drawn completely before any subsequent
command takes effect. It also means that state-querying commands return data
that's consistent with complete execution of all previously issued OpenGL
commands.
4 Dept. of Computer Science & Engineering.
Train Signal
Basic OpenGL Operation
The figure shown below gives an abstract, high-level block diagram of how
OpenGL processes data. In the diagram, commands enter from the left and
proceed through what can be thought of as a processing pipeline. Some
commands specify geometric objects to be drawn, and others control how the
objects are handled during the various processing stages.
Figure . OpenGL Block Diagram
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.
Rasterization produces a series of frame buffer addresses and associated values
using a two-dimensional description of a point, line segment, or polygon.
Each fragment so produced is fed into the last stage,
per-fragment operations, which performs the final operations on the data before
it's stored as pixels in the frame buffer. These operations include conditional
5 Dept. of Computer Science & Engineering.
Train Signal
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.
6 Dept. of Computer Science & Engineering.
Train Signal
Implementation
This program is implemented using various openGL functions which are
shown below.
Various functions used in this program.
glutInit() : interaction between the windowing system and OPENGL is
initiated
glutInitDisplayMode() : used when double buffering is required and depth
information is required
glutCreateWindow() : this opens the OPENGL window and displays the title
at top of the window
glutInitWindowSize() : specifies the size of the window
glutInitWindowPosition() : specifies the position of the window in screen
co-ordinates
glutKeyboardFunc() : handles normal ascii symbols
glutSpecialFunc() : handles special keyboard keys
7 Dept. of Computer Science & Engineering.
Train Signal
glutReshapeFunc() : sets up the callback function for reshaping the window
glutIdleFunc() : this handles the processing of the background
glutDisplayFunc() : this handles redrawing of the window
glutMainLoop() : this starts the main loop, it never returns
glViewport() : used to set up the viewport
glVertex3fv() : used to set up the points or vertices in three dimensions
glColor3fv() : used to render color to faces
glFlush() : used to flush the pipeline
glutPostRedisplay() : used to trigger an automatic redrawal of the object
glMatrixMode() : used to set up the required mode of the matrix
glLoadIdentity() : used to load or initialize to the identity matrix
glTranslatef() : used to translate or move the rotation centre from one
point to another in three dimensions
8 Dept. of Computer Science & Engineering.
Train Signal
glRotatef() : used to rotate an object through a specified rotation angle
Interaction with program
This program includes interaction through keyboard.
P Start the Project
1 -> Toggle signal number 1 which is at the top.
2-> Toggle signal number 2 which is at the bottom.
3 -> toggle both signals at once.
X/x-> rotate the scene about x axis.
Y/y-> rotate the scene about y axis.
Z/z-> rotate the scene about z axis.
Q-> Quit
9 Dept. of Computer Science & Engineering.
Train Signal
Source Code
#include <windows.h>
#include<string.h>
#include<stdarg.h>
#include<stdio.h>
#include <glut.h>
#include <math.h>
static double x=0.0;
static double move=-60;
10 Dept. of Computer Science & Engineering.
Train Signal
bool newModel=false;
bool sig_1=false;
bool sig_2=false;
bool sig_3=false;
bool goUp=false;
bool goDown=false;
float x1=11,x2=-11; //starting train location
static float y_1=0;
static float y_2=0;
void
stroke_output(GLfloat x, GLfloat y, char *format,...)
{
va_list args;
char buffer[200], *p;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glPushMatrix();
11 Dept. of Computer Science & Engineering.
Train Signal
glTranslatef(-2.5, y, 0);
glScaled(0.003, 0.005, 0.005);
for (p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
void track(){
glPushMatrix();
glScaled(100.8,0.1,0.1);
glutSolidCube(1);
glPopMatrix();
void mat(){
glPushMatrix();
glScaled(100,0.35,1);
glBegin(GL_POLYGON);
12 Dept. of Computer Science & Engineering.
Train Signal
glVertex2f(-1,-1);
glVertex2f(1,-1);
glVertex2f(1,1);
glVertex2f(-1,1);
glEnd();
glPopMatrix();
void stripes(){
glPushMatrix();
glScaled(0.1,1.57,0.1);
glutSolidCube(.5);
glPopMatrix();
void tyre(){
13 Dept. of Computer Science & Engineering.
Train Signal
glPushMatrix();
glScaled(0.5,0.5,0.5);
glutSolidTorus(0.1,0.3,20,20);
glPopMatrix();
void train(){
// engine
// chimni
glPushMatrix();
glTranslatef(-3.7,1.5,0);
//glutSolidSphere(0.5,20,20);
glScaled(0.2,3,0.2);
14 Dept. of Computer Science & Engineering.
Train Signal
glRotatef(90,1,0,0);
glutSolidTorus(0.2,0.6,50,50);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.5,1,0);
glColor3f(0.0,1,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-4.0,0.8,0.0);
glRotatef(-45,0,0,1);
glScaled(0.6,.6,.6);
glColor3d(1,1,0);
glutSolidTetrahedron();
glPopMatrix();
glPushMatrix();
glTranslatef(0.3,1.0,0);
15 Dept. of Computer Science & Engineering.
Train Signal
glRotatef(135,0,0,1);
glScaled(3.2,0.1,0.1);
glutSolidCube(0.5);
glPopMatrix();
void signals(){
glPushMatrix();
glScaled(0.7,0.7,0.7);
glutSolidSphere(0.5,20,20);
glPopMatrix();
16 Dept. of Computer Science & Engineering.
Train Signal
glPushMatrix();
glColor3f(0,1,1);
glScaled(1.0,1.3,0.3);
glutSolidCube(1.0);
glPopMatrix();
int angX,angY,angZ=0;
// Main Drawing
void trainSimulation()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
17 Dept. of Computer Science & Engineering.
Train Signal
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
glRotatef(angX,0,1,1);
glRotatef(angY,1,0,1);
glRotatef(angZ,1,1,0);
// track 1
glPushMatrix();
glTranslatef(0,-1.0,0);
glPushMatrix();
glTranslatef(0,3.7,-10);
18 Dept. of Computer Science & Engineering.
Train Signal
track();
glPopMatrix();
glPushMatrix();
glTranslatef(0,3.5,-10);
mat();
glPopMatrix();
glPushMatrix();
glTranslatef(0,3,-10);
track();
glPopMatrix();
float j;
// Stripes
for(j=0;j<=50;j+=0.5){
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-10+j,3.4,-10);
stripes();
glPopMatrix();
}
19 Dept. of Computer Science & Engineering.
Train Signal
glPopMatrix();
glPushMatrix();
//2nd track
glPushMatrix();
glTranslatef(0,0.8,-10);
track();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.5,-10);
mat();
glPopMatrix();
20 Dept. of Computer Science & Engineering.
Train Signal
glPushMatrix();
glTranslatef(0,0,-10);
track();
glPopMatrix();
//2nd Stripes
for(j=0;j<=50;j+=0.5){
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-10+j,0.5,-10);
stripes();
glPopMatrix();
}
glPopMatrix();
// track 3
glPushMatrix();
glTranslatef(0,1.0,0);
21 Dept. of Computer Science & Engineering.
Train Signal
//3rd track
glPushMatrix();
glTranslatef(0,-2.2,-10);
track();
glPopMatrix();
glPushMatrix();
glTranslatef(0,-2.5,-10);
glColor3f(1,0,0);
mat();
glPopMatrix();
glPushMatrix();
glTranslatef(0,-3,-10);
track();
glPopMatrix();
//3rd Stripes
22 Dept. of Computer Science & Engineering.
Train Signal
for(j=0;j<=50;j+=0.5){
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-10+j,-2.5,-10);
stripes();
glPopMatrix();
}
glPopMatrix();
/******** Track ENDS *************/
glPushMatrix();
joiner();
glPopMatrix();
glPushMatrix();
glTranslatef(-3.5,2.8,0);
if(sig_1 || sig_3){
glColor3f(1,1,0);
}
23 Dept. of Computer Science & Engineering.
Train Signal
else{
glColor3f(1,0,0);
signals();
glPopMatrix();
glPushMatrix();
glTranslatef(3.5,-2.8,0);
if(sig_2 || sig_3){
glColor3f(1,1,0);
}
else{
glColor3f(1,0,0);
signals();
glPopMatrix();
24 Dept. of Computer Science & Engineering.
Train Signal
/********* Train Construction ************/
glPushMatrix();
if(x1<=5 && goUp){
if(y_1<=1.9){
y_1+=0.01;
glRotatef(-10,0,0,1);
}
glTranslatef(x1,y_1,-10);
else {
glTranslatef(x1,y_1,-10);
}
glColor3f(1,1,0);
train();
25 Dept. of Computer Science & Engineering.
Train Signal
glPopMatrix();
//move second Train
glPushMatrix();
if(x2>=-5 && goDown){
if(y_2>=-2.1){
y_2-=0.01;
glRotatef(-10,0,0,1);
}
glTranslatef(x2,y_2,-10);
}
else {
glTranslatef(x2,y_2,-10);
}
glRotatef(180,0,1,0);
train();
26 Dept. of Computer Science & Engineering.
Train Signal
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void p()
{
if(sig_1){
if(x2>=-11 && x2<=-4)
{
goUp=true;
}
27 Dept. of Computer Science & Engineering.
Train Signal
x1-=0.01;
}else{
goUp=false; }
if(sig_2){
if(x1<=11 && x1>=4){
goDown=true;
}x2+=0.01;
}else { goDown=false;}
if(sig_3){
goUp=true;
goDown=true;
x1-=0.01;
x2+=0.01;
}
28 Dept. of Computer Science & Engineering.
Train Signal
trainSimulation();
void P(){
void doInit()
{
/* Background and foreground color */
glClearColor(0.2,0.2,0.8,0.0);
glColor3f(.0,1.0,1.0);
glViewport(0,0,640,480);
/* Select the projection matrix and reset it then
setup our view perspective */
29 Dept. of Computer Science & Engineering.
Train Signal
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f,(GLfloat)640/(GLfloat)480,0.1f,200.0f);
/* Select the modelview matrix, which we alter with rotatef() */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearDepth(2.0f);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
30 Dept. of Computer Science & Engineering.
Train Signal
stroke_output(-2.0, 1.7, "Welcome");
stroke_output(-2.0, 1.0, "p--> Start the Simulation");
stroke_output(-2.0, -1.1, "q--> quit");
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[]={3.7f,0.7f,0.7f,1.0f};
GLfloat light_position[]={0.0f,3.0f,2.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
31 Dept. of Computer Science & Engineering.
Train Signal
glFlush();
glutSwapBuffers();
void menu(int id)
{
switch(id)
{
case 1:glutIdleFunc(P);
break;
case 2: glutIdleFunc(p);
break;
break;
case 5:exit(0);
32 Dept. of Computer Science & Engineering.
Train Signal
break;
}
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void mykey(unsigned char key,int x,int y)
{
if(key=='p')
{
glutIdleFunc(p);
}
if(key=='x')
{
angX++;
glutIdleFunc(p);
}
33 Dept. of Computer Science & Engineering.
Train Signal
if(key=='y')
{
angY++;
glutIdleFunc(p);
}
if(key=='z')
{
angZ++;
glutIdleFunc(p);
}
if(key=='X')
{
angX--;
glutIdleFunc(p);
}
if( key=='Y')
{
angY--;
glutIdleFunc(p);
34 Dept. of Computer Science & Engineering.
Train Signal
}
if(key=='Z')
{
angZ--;
glutIdleFunc(p);
}
if(key=='1')
{
sig_1=!sig_1;
if(key=='2')
{
sig_2=!sig_2;
35 Dept. of Computer Science & Engineering.
Train Signal
if(key=='3')
{
sig_1=false;
sig_2=false;
sig_3=!sig_3;
if(key=='P')
{
glutIdleFunc(P);
}
if(key=='r'){
x1=11;
36 Dept. of Computer Science & Engineering.
Train Signal
x2=-11;
y_1=0;
y_2=0;
angX=angY=angZ=0;
if(key=='q'||key=='Q')
{
exit(0);
}
int main(int argc, char *argv[])
{
37 Dept. of Computer Science & Engineering.
Train Signal
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(1000,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Pyramid");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutKeyboardFunc(mykey);
glutCreateMenu(menu);
glutAddMenuEntry("Start 'p'",2);
glutAddMenuEntry("Reverse The Simulation 'P'",1);
glutAddMenuEntry("Quit 'q'",5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
doInit();
glutMainLoop();
return 0;
38 Dept. of Computer Science & Engineering.
Train Signal
39 Dept. of Computer Science & Engineering.
Train Signal
OUTPUT OF THE PROGRAM
40 Dept. of Computer Science & Engineering.
Train Signal
41 Dept. of Computer Science & Engineering.
Train Signal
Conclusions
The project “Train signal” clearly demonstrates the usage of OpenGL library.
Train signal simulation has been shown clearly and train crossing has been
demonstrated using OpenGL.
Finally we conclude that this program clearly illustrate the concepts of openGL
and has been completed successfully and is ready to be demonstrated.
42 Dept. of Computer Science & Engineering.
Train Signal
Bibliography
WE HAVE OBTAINED INFORMATION FROM MANY RESOURCES TO DESIGN AND
IMPLEMENT OUR PROJECT SUCCESSIVELY. WE HAVE ACQUIRED MOST OF THE
KNOWLEDGE FROM RELATED WEBSITES. THE FOLLOWING ARE SOME OF THE
RESOURCES :
TEXT BOOKS :
INTERACTIVE COMPUTER GRAPHICS A TOP-DOWN APPROACH
-By Edward Angel.
COMPUTER GRAPHICS,PRINCIPLES & PRACTICES
- Foley van dam
- Feiner hughes
WEB REFERENCES:
http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson3.php
http://google.com
43 Dept. of Computer Science & Engineering.
Train Signal
http://opengl.org
44 Dept. of Computer Science & Engineering.