0% found this document useful (0 votes)
18 views

Train

The document contains code for drawing and animating a train moving along tracks using OpenGL. It defines functions for drawing the sky, tracks and train and contains the main loop to continuously redraw the train's position as it translates along the x-axis over time.

Uploaded by

Pravin Khune
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Train

The document contains code for drawing and animating a train moving along tracks using OpenGL. It defines functions for drawing the sky, tracks and train and contains the main loop to continuously redraw the train's position as it translates along the x-axis over time.

Uploaded by

Pravin Khune
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 #include<stdio.

h>
2 #include<stdlib.h>
3 #include<glut.h>
4 #include <math.h>
5 #include <time.h>
6 #include <windows.h>
7 #define M_PI 3.14
8
9 int c=0,d=500,g=380,h=970,i=540,j=970,k=380,l=965,m=540,n=970;
10 GLfloat x=0.0;
11 int z=0,a;
12 int train1,basic;
13
14 //matrix
15
16
17 void sky()
18 {
19
20 glBegin(GL_POLYGON);
21 glColor3f(0,1,1);
22 glVertex2f(0,730);
23 glVertex2f(999,730);
24 glVertex2f(999,999);
25 glVertex2f(0,999);
26 glEnd();
27
28 }
29
30 void sleep(unsigned int mseconds)
31 {
32 clock_t goal = mseconds + clock();
33 while (goal > clock());
34 }
35
36
37 void TRAINS(int x1,int y1,int a,int b)
38 { int i=0;
39
40 glBegin(GL_QUADS);
41 glColor3f(0,0.0,1.0); //ENGINE
42 glVertex2f(x1,y1); //lengh of engine=60;height of engine=30;
43 glColor3f(0,0.0,1.0);
44 glVertex2f(x1+60,y1);
45 glColor3f(1.0,0.0,0.0);
46 glVertex2f(x1+60,y1-30);
47 glColor3f(0,0.0,0.0);
48 glVertex2f(x1,y1-30);
49 glEnd();
50
51 while(i<3)
52 { glBegin(GL_QUADS); //BOGIES
53 glColor3f(1.0,0.0,0.0); //For right train a=795,b=510
54 glVertex2f(a,b);
55 glColor3f(1.0,0.0,0.0);
56 glVertex2f(a+60,b);
57 glColor3f(1.0,0.0,0.0);
58 glVertex2f(a+60,b-20);
59 glColor3f(1.0,0.0,0.0);
60 glVertex2f(a,b-20);
61 glEnd();
62 a+=65;
63 i++;
64 }
65
66 }
67
68
69 void TRACKS()
70 {
71
72 sky();
73 /** Track **/
74 glBegin(GL_LINES);
75 glColor3f(0.0,0.0,0.0);
76 glVertex2f(0,500);
77 glVertex2f(1000,500);
78 glVertex2f(0,499);
79 glVertex2f(1000,499);
80
81 glVertex2f(0,485);
82 glVertex2f(1000,485);
83 glVertex2f(0,486);
84 glVertex2f(1000,486);
85 while(c!=1000)
86 {
87 glVertex2f(c,d);
88 glVertex2f(c,d-15);
89 c+=10;
90 }
91 glEnd();
92 }
93
94
95
96 void myinit()
97 { glClearColor(0.0,0.5,0.0,0.0); //background color
98 gluOrtho2D(0,999,0,999);
99 glPointSize(2.0);
100 glLineWidth(2.0);
101
102 basic = glGenLists(1);
103 glNewList(basic, GL_COMPILE);
104 TRACKS();
105 glEndList();
106
107 train1 = glGenLists(1);
108 glNewList(train1, GL_COMPILE);
109 TRAINS(730,520,795,510);
110 glEndList();
111 }
112
113
114 void draw(void)
115 {
116
117 glClear(GL_COLOR_BUFFER_BIT);
118 glPushMatrix();
119
120 glPushMatrix();
121 glCallList(basic);
122 glPopMatrix();
123
124 glPushMatrix();
125 glTranslatef(x,0.0,0.0);
126 glCallList(train1);
127 glFlush();
128 glPopMatrix();
129 if(z<198)
130 { a+=5;
131 x=x-5.0;
132 sleep(100);
133 z++;
134 }
135
136 glPopMatrix();
137 glutPostRedisplay();
138 glFlush();
139
140 }
141
142
143 void main()
144 { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
145 glutInitWindowPosition(0,0);
146 glutInitWindowSize(1200,700);
147 glutCreateWindow("Running Trains");
148 glutDisplayFunc(draw);
149 myinit();
150 glutMainLoop ();
151 }

You might also like