You are on page 1of 7

1 /* includes */

2 #include <math.h>
3 #include <stdio.h>
4 #include <sstream>
5 #include <GL/glut.h>
6 #include "imageloader.h"
7 #define SELECT_BUFFER 32
8
9 /* globals */
10 GLuint signal_list = 0; /* display list for traffic signal */
11 GLuint select_buffer[SELECT_BUFFER]; /* selection buffer */
12 GLint mouse_state = -1;
13 GLint mouse_button = -1;
14 GLuint picked = 1; /* current light that is picked */
15 GLUquadricObj* quadric;
16 GLuint loadTexture(Image* image) {
17 GLuint textureId;
18 glGenTextures(1, &textureId); //Make room for our texture
19 glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
20 //Map the image to the texture
21 glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
22 0, //0 for now
23 GL_RGB, //Format OpenGL uses for image
24 image->width, image->height, //Width and height
25 0, //The border of the image
26 GL_RGB, //GL_RGB, because pixels are stored in RGB format
27 GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
28 //as unsigned numbers
29 image->pixels); //The actual pixel data
30 return textureId; //Returns the id of the texture
31 }
32
33 GLuint _textureId;
34
35 /* functions */
36
37
38
39
40 GLfloat x=-48;
41
42 /*sigonal pole*/
43 void lists()
44 {
45 quadric = gluNewQuadric();
46 gluQuadricDrawStyle(quadric, GLU_FILL);
47 gluQuadricNormals(quadric, GLU_SMOOTH);
48 gluQuadricOrientation(quadric, GLU_OUTSIDE);
49
50 if (signal_list)
51
52 glDeleteLists(signal_list, 1);
53
54 signal_list = glGenLists(1);
55 glNewList(signal_list, GL_COMPILE);
56 glBegin(GL_QUADS);
57 glColor3f(0.4, 0.4, 0.4);
58 glNormal3f(0.0, 0.0, 1.0);
59 glVertex3f(-1.0, 3.0, 1.0);
60 glVertex3f(-1.0, -3.0, 1.0);
61 glVertex3f( 1.0, -3.0, 1.0);
62 glVertex3f( 1.0, 3.0, 1.0);
63 glNormal3f(1.0, 0.0, 0.0);
64 glVertex3f( 1.0, 3.0, 1.0);
65 glVertex3f( 1.0, -3.0, 1.0);
66 glVertex3f( 1.0, -3.0, -1.0);
67 glVertex3f( 1.0, 3.0, -1.0);
68 glNormal3f(0.0, 0.0, -1.0);
69 glVertex3f( 1.0, 3.0, -1.0);
70 glVertex3f( 1.0, -3.0, -1.0);
71 glVertex3f(-1.0, -3.0, -1.0);
72 glVertex3f(-1.0, 3.0, -1.0);
73 glNormal3f(-1.0, 0.0, 0.0);
74 glVertex3f(-1.0, 3.0, -1.0);
75 glVertex3f(-1.0, -3.0, -1.0);
76 glVertex3f(-1.0, -3.0, 1.0);
77 glVertex3f(-1.0, 3.0, 1.0);
78 glNormal3f( 0.0, 1.0, 0.0);
79 glVertex3f( 1.0, 3.0, -1.0);
80 glVertex3f(-1.0, 3.0, -1.0);
81 glVertex3f(-1.0, 3.0, 1.0);
82 glVertex3f( 1.0, 3.0, 1.0);
83 glNormal3f( 0.0, -1.0, 0.0);
84 glVertex3f( 1.0, -3.0, 1.0);
85 glVertex3f(-1.0, -3.0, 1.0);
86 glVertex3f(-1.0, -3.0, -1.0);
87 glVertex3f( 1.0, -3.0, -1.0);
88 glEnd();
89 glEndList();
90 gluQuadricOrientation(quadric, GLU_INSIDE);
91
92 }
93 /* to create vehicle*/
94 void lis()
95 {
96
97 glBegin(GL_QUADS);
98 glColor3f(1,0,0);
99 glNormal3f(0.0, 0.0, 1.0);
100 glVertex3f(-1.0, 3.0, 1.0);
101 glVertex3f(-1.0, -3.0, 1.0);
102 glVertex3f( 1.0, -3.0, 1.0);
103 glVertex3f( 1.0, 3.0, 1.0);
104 glNormal3f(1.0, 0.0, 0.0);
105 glVertex3f( 1.0, 3.0, 1.0);
106 glVertex3f( 1.0, -3.0, 1.0);
107 glVertex3f( 1.0, -3.0, -1.0);
108 glVertex3f( 1.0, 3.0, -1.0);
109 glNormal3f(0.0, 0.0, -1.0);
110 glVertex3f( 1.0, 3.0, -1.0);
111 glVertex3f( 1.0, -3.0, -1.0);
112 glVertex3f(-1.0, -3.0, -1.0);
113 glVertex3f(-1.0, 3.0, -1.0);
114 glNormal3f(-1.0, 0.0, 0.0);
115 glVertex3f(-1.0, 3.0, -1.0);
116 glVertex3f(-1.0, -3.0, -1.0);
117 glVertex3f(-1.0, -3.0, 1.0);
118 glVertex3f(-1.0, 3.0, 1.0);
119 glNormal3f( 0.0, 1.0, 0.0);
120 glVertex3f( 1.0, 3.0, -1.0);
121 glVertex3f(-1.0, 3.0, -1.0);
122 glVertex3f(-1.0, 3.0, 1.0);
123 glVertex3f( 1.0, 3.0, 1.0);
124 glNormal3f( 0.0, -1.0, 0.0);
125 glVertex3f( 1.0, -3.0, 1.0);
126 glVertex3f(-1.0, -3.0, 1.0);
127 glVertex3f(-1.0, -3.0, -1.0);
128 glVertex3f( 1.0, -3.0, -1.0);
129 glEnd();
130
131 }
132 /*win functio to create side window and win1 to create back window*/
133 void win()
134 {
135
136 glBegin(GL_QUADS);
137 glColor3f(0,0,0);
138 glNormal3f( 0.0, -1.0, 0.0);
139 glVertex3f(-1.0, .8, -1.0);
140 glVertex3f(-1.0, -.8, -1.0);
141 glVertex3f(-1.0, -.8, 1.0);
142 glVertex3f(-1.0, .8, 1.0);
143
144 glEnd();
145
146 }
147
148 void win1()
149 {
150 glBegin(GL_QUADS);
151 glColor3f(0,0,0);
152 glNormal3f(0.0, 0.0, 1.0);
153 glVertex3f(-1.0, 1.9, 1.0);
154 glVertex3f(-1.0, -1.9, 1.0);
155 glVertex3f( 1.0, -1.9, 1.0);
156 glVertex3f( 1.0, 1.9, 1.0);
157
158 glEnd();
159 }
160
161
162
163
164 /* function to create the vechile ,its windows and tyres*/
165 void veh()
166 {
167 glTranslatef(0,0,-x);
168 glPushMatrix();
169 glPushMatrix();
170 glScalef(0.5,3,3);
171 glColor3f(0,0,0);
172 glTranslated (4, -.8, 2.4);
173 glutSolidSphere (0.15, 15, 15); //to create tyre
174 glPopMatrix();
175
176 glPushMatrix();
177 glScalef(0.5,3,3);
178 glColor3f(0,0,0);
179 glTranslated (4, -.8, .4);
180 glutSolidSphere (0.15, 15, 15);// to create tyre
181 glPopMatrix();
182 glPushMatrix();
183 glScalef(.5,3,3);
184 glColor3f(0,0,0);
185 glTranslated (12, -.8, 2.4);
186 glutSolidSphere (0.15, 15, 15); // to create the tyre
187 glPopMatrix();
188 glTranslatef(4, -2.,5);
189 glScalef(2,.2,5);
190 lis();
191 glPushMatrix();
192 glTranslatef(-.01,1.,.0);
193 win(); // to create side window
194 glPopMatrix();
195
196 glPushMatrix();
197 glTranslatef(-.05, 1.4,0.05);
198 win1(); // to create back window
199 glPopMatrix();
200 glPopMatrix();
201
202 }
203
204 /* to initate the program*/
205 void init(void)
206 {
207 /* lighting */
208
209 glEnable(GL_COLOR_MATERIAL);
210 glEnable(GL_DEPTH_TEST);
211 //glDepthFunc(GL_LEQUAL);
212 glEnable(GL_CULL_FACE);
213 glClearColor(0, 0,0, 1.0);
214 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
215 glSelectBuffer(SELECT_BUFFER, select_buffer);
216 Image* image = loadBMP("road.bmp");
217 _textureId = loadTexture(image);
218
219
220 }
221
222
223
224 int timeopen=0;
225
226 /* to resize the window*/
227
228
229 void reshape(int w, int h)
230 {
231 GLfloat light_position[4] = { 0.3, 0.3, 0.3, 1 };
232 glViewport(0, 0, w, h);
233 glMatrixMode(GL_PROJECTION);
234 glLoadIdentity();
235 gluPerspective(60.0, (GLfloat)h / (GLfloat)w ,1.0, 128.0);
236 glMatrixMode(GL_MODELVIEW);
237 glLoadIdentity();
238 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
239 glTranslatef(0.0, 0.0, -8.0);
240 glRotatef(45.0, 0.0, 1.0, 0.0);
241 }
242
243
244 int k=0,call=0;
245 /* function to display text*/
246 void dispchar(int x,int y,char *s,void *font)
247 {
248 int len,i;
249
250 glRasterPos2f(x,y);
251 len=(int)strlen(s);
252
253 for(i=0;i<len;i++)
254 glutBitmapCharacter(font,s[i]);
255 }
256 /* function to display three different light*/
257 void ligh()
258 {
259 glTranslatef(-2.9, -1.1, 1);
260 if (picked == 1) {
261 glDisable(GL_LIGHTING);
262 glColor3f(0.03, 0.0, 0.0);
263 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
264 glColor3f(1.0, 0.0, 0.0);
265 glutSolidSphere(0.75, 32, 32);
266 glEnable(GL_LIGHTING);
267
268 k=1;
269
270 } else {
271 glColor3f(0.4, 0.4, 0.4);
272 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
273 glColor3f(0.3, 0.0, 0.0);
274 glutSolidSphere(0.75, 32, 32);
275 k=0;
276 }
277 glTranslatef(0.0, -1.75, 0.0);
278 if (picked == 2) {
279 glDisable(GL_LIGHTING);
280 glColor3f(0.3, 0.3, 0.0);
281 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
282 glColor3f(1.0, 1.0, 0.0);
283 glutSolidSphere(0.75, 32, 32);
284 glEnable(GL_LIGHTING);
285
286 } else {
287 glColor3f(0.4, 0.4, 0.4);
288 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
289 glColor3f(0.3, 0.3, 0.0);
290 glutSolidSphere(0.75, 32, 32);
291
292 }
293 glTranslatef(0.0, -1.75, 0.0);
294 if (picked == 3) {
295 glDisable(GL_LIGHTING);
296 glColor3f(0.0, 0.3, 0.0);
297 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
298 glColor3f(0.0, 1.0, 0.0);
299 glutSolidSphere(0.75, 32, 32);
300 glEnable(GL_LIGHTING);
301
302 } else {
303 glColor3f(0.4, 0.4, 0.4);
304 gluCylinder(quadric, 0.74, 0.74, 0.75, 32, 32);
305 glColor3f(0.0, 0.3, 0.0);
306 glutSolidSphere(0.75, 32, 32);
307 k=1;
308 }
309
310 glutPostRedisplay();
311 }
312
313
314
315
316
317
318
319
320 /* to display 1st screen*/
321
322 void screen()
323 {
324
325 glClear(GL_COLOR_BUFFER_BIT);
326
327 glPushMatrix();
328 gluOrtho2D(0,430,0,700);
329 glColor3f(1,0,0);
330 dispchar(400,490," ",GLUT_BITMAP_TIMES_ROMAN_24);
331 dispchar(-400,1000," JENGINEERING ",GLUT_BITMAP_TIMES_ROMAN_24);
332 dispchar(-100,950," CG MINI PROJECT ON",GLUT_BITMAP_TIMES_ROMAN_24);
333
334 glColor3f(0.7,0.7,0.7);
335 dispchar(-100,700,"TRAFFIC SIGNAL",GLUT_BITMAP_TIMES_ROMAN_24);
336 glColor3f(0.3,0.7,0.2);
337 dispchar(-490,350," PROJECT ASSOCIATES",GLUT_BITMAP_TIMES_ROMAN_24);
338 dispchar(700,350," GUIDENCE ",GLUT_BITMAP_TIMES_ROMAN_24);
339 glColor3f(0.1,0.9,0.7);
340 dispchar(-490,200," ",GLUT_BITMAP_TIMES_ROMAN_24);
341 dispchar(-500,100," ",GLUT_BITMAP_TIMES_ROMAN_24);
342 glColor3f(0.1,0.9,0.7);
343 dispchar(550,180," ",GLUT_BITMAP_TIMES_ROMAN_24);
344 dispchar(550,0," ",GLUT_BITMAP_TIMES_ROMAN_24);
345 glColor3f(0.0,0.9,0.0);
346 dispchar(-100,-400,"PRESS ENTER TO START ",GLUT_BITMAP_TIMES_ROMAN_24);
347 dispchar(-100,-600,"PRESS Esc TO EXIT ",GLUT_BITMAP_TIMES_ROMAN_24);
348 glPopMatrix();
349 glutPostRedisplay();
350 }
351 /*to display instructions */
352 void ins()
353 {
354 glPushMatrix();
355 gluOrtho2D(0,430,0,700);
356 glColor3f(1,0,0);
357 dispchar(400,490," ",GLUT_BITMAP_TIMES_ROMAN_24);
358 dispchar(-400,1300," Press r for red light",GLUT_BITMAP_TIMES_ROMAN_24);
359 dispchar(-400,1200," Press o for orange light",GLUT_BITMAP_TIMES_ROMAN_24);
360 dispchar(-400,1100," Press g for green light ",GLUT_BITMAP_TIMES_ROMAN_24);
361 dispchar(-400,1000," Press x to exit,",GLUT_BITMAP_TIMES_ROMAN_24);
362 dispchar(-400,900," Press tab to run car",GLUT_BITMAP_TIMES_ROMAN_24);
363
364
365 glColor3f(0.7,0.7,0.7);
366 dispchar(-100,700,"",GLUT_BITMAP_TIMES_ROMAN_24);
367 glPopMatrix();
368 glutPostRedisplay();
369 }
370 void display(void)
371
372 {
373 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
374 if(timeopen==0)
375 {
376 screen();
377 glutPostRedisplay();
378 }
379 else if(timeopen!=0)
380 {
381 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
382 ins();
383 glDisable(GL_LIGHT1);
384 glClearColor(.02,0,.02, 0);
385 glDisable(GL_CULL_FACE);
386 glPushMatrix();
387 lists();
388
389 glMatrixMode(GL_MODELVIEW);
390 glLoadIdentity();
391 GLfloat ambientLight[] = {1, 1, 1, 1};
392 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
393 GLfloat directedLight[] = {1, 0.7f, 0.7f, 1.0f};
394 GLfloat directedLightPos[] = {-10.0f, 15.0f, 20.0f, 0.0f};
395 glLightfv(GL_LIGHT0, GL_DIFFUSE, directedLight);
396 glLightfv(GL_LIGHT0, GL_POSITION, directedLightPos);
397 float height = -4.0f;
398 glEnable(GL_TEXTURE_2D);
399 glBindTexture(GL_TEXTURE_2D, _textureId);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
402 glDisable(GL_NORMALIZE);
403 glTranslatef(0.5f, 1.0f, -6.0f);
404 glBegin(GL_POLYGON);
405 glTexCoord2f(1, 0); glVertex3f(-10, height, 2);
406 glTexCoord2f(1, 1); glVertex3f(-10, height, -20);
407 glTexCoord2f(0, 1); glVertex3f(10, height, -20);
408 glTexCoord2f(0, 0); glVertex3f(10, height, 2);
409 glEnd();
410
411 glEnable(GL_CULL_FACE);
412 glTranslatef(-.1, .3, 0);
413 glScalef(.08,.15,.1);
414 ligh();
415 glTranslatef(0.0, -3.7, -1.0);
416 glScalef(1,3,1);
417 glCallList(signal_list);
418 if(call==1&&k==0)
419 x=x+.07;
420
421 if(k==0||k==1)
422 if(x>-14&&x<=12)
423 {
424
425 x=x+.07;
426
427 }
428 if(k==0||k==1)
429 {
430 if(x>12)
431 {
432 x=x+.07;
433
434 }
435 }
436 veh();
437
438 if(x>=60)
439 x=-48;
440 glPopMatrix();
441 glPopMatrix();
442 }
443
444
445 glutSwapBuffers();
446 glutPostRedisplay();
447 }
448
449 void handleKeypress(unsigned char key, int x, int y)
450 {
451
452
453 switch (key)
454 {
455 case '\t': // tab key
456 call=1;
457 break;
458 case 'r': // r key
459 picked =1;
460 break;
461
462 case 'o': // o key
463 picked =2;
464
465 break;
466 case 'g': // g key
467 picked =3;
468
469 break;
470 case '\r': // enter key
471 timeopen++;
472
473 break;
474
475 case 'x': //x key
476 exit(0);
477
478 break;
479
480 }
481
482 }
483
484
485
486
487
488
489 main(int argc, char** argv)
490 {
491 glutInit(&argc, argv);
492 glutInitWindowSize(512, 512);
493 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
494 glutCreateWindow("Pick");
495 glutReshapeFunc(reshape);
496 glutDisplayFunc(display);
497 glutKeyboardFunc(handleKeypress);
498 init();
499 glutMainLoop();
500 return 0;
501 }

You might also like