-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengl.cpp
769 lines (686 loc) · 24.6 KB
/
opengl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // needed to sleep
#include <string>
#include <iostream>
#include "road.hpp"
#include "vehicle.hpp"
////parser
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include "/home/h_bond/rapidxml-1.13/rapidxml.hpp" //To the USER: Change this to the path of where the rapidXML hpp file is in your computer.
using namespace rapidxml;
using namespace std;
#define ESCAPE 27
#define SPACE 32
using namespace std;
// Helper functions
Road *r;
vector<Vehicle *> vehicles;
vector<int> sig_time;
bool road_empty;
int time_step;
int sig_time_counter;
int vehicle_counter;
int enter=0;
vector<int> check_movable_zone(Vehicle a,vector<vector<char>> rd)
{
int x_pos=a.get_pos()[0];
int y_pos=a.get_pos()[1];
int length=a.get_length();
int width=a.get_width();
int x_velocity=a.get_velocity()[0];
int rd_width=rd[0].size()-1;
int x_new_pos=x_pos+x_velocity;
int y_new_pos=y_pos;
int lane_change=5; //0 not possible, decelerate. -1 right. 1 left. 5 move_forward. 4 signal_stop.
bool is_signal=false;
int m;
// for(int i=x_new_pos; i>x_new_pos - width; i--)
// cout << x_pos << " " << x_new_pos << endl;
for(int i=x_pos+1; i<=x_new_pos; i++)
{
// for(int j=y_new_pos; j>y_new_pos - length; j--)
for(int j=y_pos+length-1; j>=y_pos; j--)
{
if(i<rd.size() && i>=0 && j<rd[0].size() && j>=0)
{
if(rd[i][j]!=' ')
{
m=i-1;
lane_change=0;
if(rd[i][j]=='|')
{
is_signal=true;
break;
}
}
}
}
if(lane_change!=5)
{
break;
}
}
if(lane_change==5)
{
vector<int> a{0,5}; //return = new_x_pos, decision
return a;
}
if(lane_change==0)
{
if(is_signal)
{
vector<int> a{m,4};
return a;
}
else if(m==x_pos && (length>1 || rd[m+1][y_pos]=='b'))
{
vector<int> a{m,0};
return a;
}
}
//With my current velocity can I move right without hitting something else ?
bool move_right=true;
if(y_pos-length>=0) //can move right
{
for(int i=x_pos-width+1;i<=x_new_pos;i++)
{
for(int j=y_pos+length-2;j>=y_pos-1;j--)
{
if(i<rd.size() && i>=0)
{
if(!((i >= x_pos-width+1) && (i<=x_pos) && (j<=y_pos+length-1) && (j>=y_pos)))
{
if(rd[i][j]!=' ')
{
move_right=false;
break;
}
}
}
}
if(move_right==false)
{
break;
}
}
}
else
{
move_right=false;
}
if(move_right)
{
vector<int> a{x_new_pos,-1};
return a;
}
else
{
//With my current velocity, since I can't move right, can I move left without hitting something ?
bool move_left=true;
if(y_pos+1<=rd.size()-1)
{
for(int i=x_pos-width+1;i<=x_new_pos;i++)
{
for(int j=y_pos+1;j<=y_pos+length;j++)
{
if(i<rd.size() && i>=0)
{
if(!((i >= x_pos-width+1) && (i<=x_pos) && (j<=y_pos+length-1) && (j>=y_pos)))
{
if(rd[i][j]!=' ')
{
move_left=false;
break;
}
}
}
}
if(move_left==false)
{
break;
}
}
}
else
{
move_left=false;
}
if(move_left)
{
vector<int> a{x_new_pos,1};
return a;
}
else
{
x_new_pos=m;
//With my reduced velocity can I move right without hitting something else ?
move_right=true;
if(y_pos-1>=0) //can move right
{
for(int i=x_pos-width+1;i<=x_new_pos;i++)
{
for(int j=y_pos+length-2;j>=y_pos-1;j--)
{
if(i<rd.size() && i>=0)
{
if(!((i >= x_pos-width+1) && (i<=x_pos) && (j<=y_pos+length-1) && (j>=y_pos)))
{
if(rd[i][j]!=' ')
{
move_right=false;
break;
}
}
}
}
if(move_right==false)
{
break;
}
}
}
else
{
move_right=false;
}
if(move_right)
{
vector<int> a{x_new_pos,-1};
return a;
}
else
{
//With my current velocity, since I can't move right, can I move left without hitting something ?
move_left=true;
if(y_pos+1<=rd.size()-1) // Can I move left without falling off the road ?
{
for(int i=x_pos-width+1;i<=x_new_pos;i++)
{
for(int j=y_pos+1;j<=y_pos+length;j++)
{
if(i<rd.size() && i>=0)
{
if(!((i >= x_pos-width+1) && (i<=x_pos) && (j<=y_pos+length-1) && (j>=y_pos)))
{
if(rd[i][j]!=' ')
{
move_left=false;
break;
}
}
}
}
if(move_left==false)
{
break;
}
}
}
else
{
move_left=false;
}
if(move_left)
{
vector<int> a{x_new_pos,1};
return a;
}
else
{
//Since I can't move left or right with reduced or normal velocity,
// my only option is stop before the bumper of the vehicle.
vector<int> a{m,0};
return a;
}
}
}
}
}
vector<Vehicle *> interaction_update(Road *r, vector<Vehicle *> a, vector<int> sig_time)
{
int road_length=r->get_length();
int road_width=r->get_width();
vector<vector<char>> *road_map = &r->road_map;
vector<Vehicle> list;
for(int i=0; i<a.size(); i++)
{
list.push_back(*a[i]); //I'm making a copy of all the objects here cause I don't want to disturb the state of all the objects.
}
for(int i=0;i<a.size();i++)
{
if(a[i]->get_pos()[1]!=-1) //If my vehicle is on the road, then update.
{
a[i]->opengl_diagonal=0;
Vehicle cur = list[i];
int signal_distance=r->get_sig_distance();
int max=cur.get_max_speed();
int x = cur.get_pos()[0];
//Under normal circumstances, if the vehicle is not at max speed I want it to keep increasing speed till it reaches max.
int temp_vx = cur.get_velocity()[0];
temp_vx=temp_vx+cur.get_acceleration();
if(temp_vx>max)
{
temp_vx=max;
}
cur.set_velocity(temp_vx,0);
vector<int> option = check_movable_zone(cur, *road_map);
// cout<<option[0]<<" "<<option[1]<<" "<<(char)cur.get_display_char()<<" "<<cur.get_velocity()[0]<<" "<<cur.get_pos()[0]<<" "<<cur.get_pos()[1]<<endl;
if(option[1] == 5)
{
a[i]->set_velocity(cur.get_velocity());
int px = cur.get_pos()[0]+cur.get_velocity()[0];
int py = cur.get_pos()[1];
a[i]->set_pos(px,py);
}
else if(option[1] == 4)
{
a[i]->set_velocity(0,0);
int px = option[0];
int py = cur.get_pos()[1];
a[i]->set_pos(px,py);
}
else if (option[1] == 0)
{
a[i]->set_velocity(cur.get_velocity());
int px = option[0];
int py = cur.get_pos()[1];
a[i]->set_pos(px,py);
}
else if(option[1] == -1 || option[1] == 1)
{
if(a[i]->get_length()==1)
{
a[i]->opengl_diagonal=option[1];
}
a[i]->set_velocity(cur.get_velocity());
int px = option[0];
int py = cur.get_pos()[1]+option[1];
a[i]->set_pos(px,py);
}
}
}
r->update(a);
return a;
}
void draw_cube(float px, float py, float length, float width, float height, string col, int diag=0)
{
int rgb=stoi(col);
float col_b = ((1.0)*(rgb%10))/10;
rgb=rgb/10;
float col_g = ((1.0)*(rgb%10))/10;
rgb=rgb/10;
float col_r = ((1.0)*(rgb%10))/10;
glLoadIdentity();
// glTranslatef(-10.0f, -10.0f, -40.0f);
// glRotatef(45.0f,0.0f,0.0f,1.0f);
glTranslatef(0.0f, -10.0f, -40.0f);
glRotatef(-65.0f,1.0f,0.0f,0.0f);
glRotatef(65.0f,0.0f,0.0f,1.0f);
glBegin(GL_QUADS); // start drawing the cube.
//top of cube
glColor3f(col_r*1.0f,col_g*1.0f,col_b*1.0f);
glVertex3f(px*1.0f,(py-(length-1)+diag/5.0f)*1.0f, height*1.0f); // Top Right Of The Quad (Top)
glVertex3f(px*1.0f,(py+diag/5.0f)*1.0f, height*1.0f); // Top Left Of The Quad (Top)
glVertex3f((px - (width-1))*1.0f,py*1.0f,height*1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( (px - (width-1))*1.0f,(py-(length-1))*1.0f,height*1.0f); // Bottom Right Of The Quad (Top)
// bottom of cube
glColor3f(col_r*0.95f,col_g*0.95f,col_b*0.95f);
glVertex3f(px*1.0f,(py-(length-1)+(diag/5.0f))*1.0f, 0.0f); // Top Right Of The Quad (Bottom)
glVertex3f(px*1.0f,(py+(diag/5.0f))*1.0f, 0.0f); // Top Left Of The Quad (Bottom)
glVertex3f((px - (width-1))*1.0f,py*1.0f,0.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( (px - (width-1))*1.0f,(py-(length-1))*1.0f,0.0f); // Bottom Right Of The Quad (Bottom)
// front of cube
glColor3f(col_r*0.9f,col_g*0.9f,col_b*0.9f);
glVertex3f( px*1.0f, (py-(length-1)+(diag/5.0f))*1.0f, height*1.0f); // Top Right Of The Quad (Front)
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, height*1.0f); // Top Left Of The Quad (Front)
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, 0.0f); // Top Left Of The Quad (Front)
glVertex3f( px*1.0f, ((py+diag/5.0f)-(length-1))*1.0f, 0.0f); // Top Right Of The Quad (Front)
//back of cube.
glColor3f(col_r*0.85f,col_g*0.85f,col_b*0.85f);
glVertex3f( (px - (width-1))*1.0f,(py-(length-1))*1.0f,height*1.0f); // Top Right Of The Quad (Back)
glVertex3f((px - (width-1))*1.0f,py*1.0f,height*1.0f); // Top Left Of The Quad (Back)
glVertex3f((px - (width-1))*1.0f, py*1.0f, 0.0f); // Bottom Left Of The Quad (Back)
glVertex3f( (px - (width-1))*1.0f, (py-(length-1))*1.0f,0.0f); // Bottom Right Of The Quad (Back)
// left of cube
glColor3f(col_r*0.8f,col_g*0.8f,col_b*0.8f);
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, height*1.0f); // Top Right Of The Quad (Left)
glVertex3f((px - (width-1))*1.0f, py*1.0f, height*1.0f); // Top Left Of The Quad (Left)
glVertex3f((px - (width-1))*1.0f,py*1.0f,0.0f); // Bottom Left Of The Quad (Left)
glVertex3f(px*1.0f,(py+(diag/5.0f))*1.0f, 0.0f); // Bottom Right Of The Quad (Left)
// Right of cube
glColor3f(col_r*0.75f,col_g*0.75f,col_b*0.75f);
glVertex3f( px*1.0f, (py-(length-1)+(diag/5.0f))*1.0f,height*1.0f); // Top Right Of The Quad (Right)
glVertex3f( (px - (width-1))*1.0f, (py-(length-1))*1.0f, height*1.0f); // Top Left Of The Quad (Right)
glVertex3f( (px - (width-1))*1.0f,(py-(length-1))*1.0f, 0.0f); // Bottom Left Of The Quad (Right)
glVertex3f( px*1.0f,(py-(length-1)+(diag/5.0f))*1.0f,0.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Cube
}
void draw_cube_bike(float px, float py, float length, float width, float height, string col, int diag=0)
{
int rgb=stoi(col);
float col_b = ((1.0)*(rgb%10))/10;
rgb=rgb/10;
float col_g = ((1.0)*(rgb%10))/10;
rgb=rgb/10;
float col_r = ((1.0)*(rgb%10))/10;
glLoadIdentity();
// glTranslatef(-10.0f, -10.0f, -40.0f);
// glRotatef(45.0f,0.0f,0.0f,1.0f);
glTranslatef(0.0f, -10.0f, -40.0f);
glRotatef(-65.0f,1.0f,0.0f,0.0f);
glRotatef(65.0f,0.0f,0.0f,1.0f);
glBegin(GL_QUADS); // start drawing the cube.
//top of cube
glColor3f(col_r*1.0f,col_g*1.0f,col_b*1.0f);
glVertex3f(px*1.0f,(py-(length-0.5f)+diag/5.0f)*1.0f, height*1.0f); // Top Right Of The Quad (Top)
glVertex3f(px*1.0f,(py+diag/5.0f)*1.0f, height*1.0f); // Top Left Of The Quad (Top)
glVertex3f((px - (width-1))*1.0f,py*1.0f,height*1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( (px - (width-1))*1.0f,(py-(length-0.5f))*1.0f,height*1.0f); // Bottom Right Of The Quad (Top)
// bottom of cube
glColor3f(col_r*0.95f,col_g*0.95f,col_b*0.95f);
glVertex3f(px*1.0f,(py-(length-0.5f)+(diag/5.0f))*1.0f, 0.0f); // Top Right Of The Quad (Bottom)
glVertex3f(px*1.0f,(py+(diag/5.0f))*1.0f, 0.0f); // Top Left Of The Quad (Bottom)
glVertex3f((px - (width-1))*1.0f,py*1.0f,0.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( (px - (width-1))*1.0f,(py-(length-0.5f))*1.0f,0.0f); // Bottom Right Of The Quad (Bottom)
// front of cube
glColor3f(col_r*0.9f,col_g*0.9f,col_b*0.9f);
glVertex3f( px*1.0f, (py-(length-0.5f)+(diag/5.0f))*1.0f, height*1.0f); // Top Right Of The Quad (Front)
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, height*1.0f); // Top Left Of The Quad (Front)
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, 0.0f); // Top Left Of The Quad (Front)
glVertex3f( px*1.0f, ((py+diag/5.0f)-(length-0.5f))*1.0f, 0.0f); // Top Right Of The Quad (Front)
//back of cube.
glColor3f(col_r*0.85f,col_g*0.85f,col_b*0.85f);
glVertex3f( (px - (width-1))*1.0f,(py-(length-0.5f))*1.0f,height*1.0f); // Top Right Of The Quad (Back)
glVertex3f((px - (width-1))*1.0f,py*1.0f,height*1.0f); // Top Left Of The Quad (Back)
glVertex3f((px - (width-1))*1.0f, py*1.0f, 0.0f); // Bottom Left Of The Quad (Back)
glVertex3f( (px - (width-1))*1.0f, (py-(length-0.5f))*1.0f,0.0f); // Bottom Right Of The Quad (Back)
// left of cube
glColor3f(col_r*0.8f,col_g*0.8f,col_b*0.8f);
glVertex3f(px*1.0f, (py+(diag/5.0f))*1.0f, height*1.0f); // Top Right Of The Quad (Left)
glVertex3f((px - (width-1))*1.0f, py*1.0f, height*1.0f); // Top Left Of The Quad (Left)
glVertex3f((px - (width-1))*1.0f,py*1.0f,0.0f); // Bottom Left Of The Quad (Left)
glVertex3f(px*1.0f,(py+(diag/5.0f))*1.0f, 0.0f); // Bottom Right Of The Quad (Left)
// Right of cube
glColor3f(col_r*0.75f,col_g*0.75f,col_b*0.75f);
glVertex3f( px*1.0f, (py-(length-0.5f)+(diag/5.0f))*1.0f,height*1.0f); // Top Right Of The Quad (Right)
glVertex3f( (px - (width-1))*1.0f, (py-(length-0.5f))*1.0f, height*1.0f); // Top Left Of The Quad (Right)
glVertex3f( (px - (width-1))*1.0f,(py-(length-0.5f))*1.0f, 0.0f); // Bottom Left Of The Quad (Right)
glVertex3f( px*1.0f,(py-(length-0.5f)+(diag/5.0f))*1.0f,0.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Cube
}
/* The number of our GLUT window */
int window;
/* rotation angle for the quadrilateral. */
float rquad = 5.0f;
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;
glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// glTranslatef(-10.0f, -10.0f, -40.0f);
// glRotatef(-45.0f,0.0f,0.0f,1.0f);
// glRotatef(45.0f,0.0f,0.0f,1.0f);
glTranslatef(0.0f, -10.0f, -40.0f);
glRotatef(-65.0f,1.0f,0.0f,0.0f);
glRotatef(65.0f,0.0f,0.0f,1.0f);
glBegin(GL_QUADS);
glColor3f(0.2f , 0.2f, 0.2f);
glVertex3f(0.0f,-1.0f,0.0f);
glVertex3f((r->get_width())*1.0f,-1.0f, 0.0f);
glVertex3f((r->get_width())*1.0f,(r->get_length())*1.0f -1.0f, 0.0f);
glVertex3f( 0.0f,(r->get_length())*1.0f -1.0f,0.0f);
glEnd();
int signal_dist=r->get_sig_distance();
glBegin(GL_QUADS);
if(r->get_signal_color()==0)
{
glColor3f(1.0f , 0.3f, 0.3f);
}
else
{
glColor3f(0.5f , 1.0f, 0.5f);
}
glVertex3f(signal_dist*1.0f,-2.0f,0.0f);
glVertex3f(signal_dist*1.0f,-1.0f,0.0f);
glVertex3f(signal_dist*1.0f,-1.0f,3.0f);
glVertex3f(signal_dist*1.0f,-2.0f,3.0f);
glEnd();
usleep(300000);//sleep for 0.25 second
////////////To make vehicles enter
int max_no_vehicles;
int max_width=0;
srand(time(0));
int rand_var = (rand()%4)+2;
int road_length=r->get_length();
int road_width=r->get_width();
vector<vector<char>> *road_map = &r->road_map;
if(enter>rand_var)
{
for(int i=0; i<vehicles.size(); i++)
{
if(max_width<vehicles[i]->get_length())
{
max_width=vehicles[i]->get_length();
}
}
max_no_vehicles=r->get_length()/max_width; //What is the max no. of vehicles I can put on the road ?
bool is_start_line_empty=true;
for(int i=0;i<road_length;i++)
{
if((*road_map)[0][i]!=' ')
{
is_start_line_empty=false;
break;
}
}
if(is_start_line_empty && vehicle_counter>=0)
{
int start_point=road_length-1-vehicles[vehicle_counter]->get_length()+1;
while(vehicle_counter>=0)
{
vehicles[vehicle_counter]->set_pos(0,start_point);
if(start_point<0)
{
vehicles[vehicle_counter]->set_pos(-1,-1);
break;
}
start_point=start_point - 1 - (3-(rand()%3));
vehicle_counter--;
road_empty=false;
}
}
r->update(vehicles);
enter=0;
}
else
{
enter++;
}
//////////////////
if(!road_empty)
{
road_empty=true;
vector<Vehicle *> a = interaction_update(r,vehicles,sig_time); //redundant. a is not necessary. but kept for readability
r->display();
for(int i=0;i<r->get_width();i++)
{
for(int j=0;j<r->get_length();j++)
{
if((r->road_map)[i][j]!=' ' && (r->road_map)[i][j]!='|')
{
road_empty=false;
break;
}
}
}
time_step++;
cout<<"Time step = "<<time_step<<endl<<endl;
if(time_step==sig_time[sig_time_counter] && sig_time_counter<sig_time.size())
{
r->set_sig_colour(1-r->get_signal_color());
sig_time_counter++;
}
for(int i=0;i<a.size();i++)
{
int px = a[i]->get_pos()[0];
int py = a[i]->get_pos()[1];
int length = a[i]->get_length();
int width = a[i]->get_width();
int height = a[i]->get_height();
string col=a[i]->get_color();
if(px>=0 && px<r->get_width() && length>1)
{
draw_cube(px,py,length,width,height,col,a[i]->opengl_diagonal);
}
else if(px>=0 && px<r->get_width() && length==1)
{
draw_cube_bike(px,py,length,width,height,col,a[i]->opengl_diagonal);
}
}
}
glutSwapBuffers();
}
/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this call */
usleep(100);
/* If escape is pressed, kill everything. */
if (key == ESCAPE)
{
/* shut down our window */
glutDestroyWindow(window);
/* exit the program...normal termination. */
exit(0);
}
}
int main(int argc, char **argv)
{
//////////Parsing
string config = "XML_config.xml";
vector < vector < string> > ans;
cout << "Start" << endl;
xml_document<> doc;
xml_node<> * root_node;
// Read the xml file into a vector
ifstream theFile (config);
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
// Parse the buffer using the xml file parsing library into doc
doc.parse<0>(&buffer[0]);
// Find our root node
root_node = doc.first_node("road");
int width = stoi(root_node->first_attribute("length")->value());
int length = stoi(root_node->first_attribute("width")->value());
int dist = stoi(root_node->first_attribute("sig_dist")->value());
Road rd(length,width,dist);
r=&rd;
//sees the initial colour for the signal;
string initial_color = ((root_node->first_node("signals"))->first_attribute("initial_color")->value());
int t_signal; //0 for red, 1 for green
if (initial_color.compare("green")==0)
{
t_signal = 1;
}
else
{
t_signal = 0;
}
r->set_sig_colour(t_signal);
//to make a vector which stores the time after which you need to change the signal, taken care of last signal not being red
xml_node<> * signals_node = root_node->first_node("signals");
for (xml_node<> * signal_node = signals_node->first_node("signal"); signal_node; signal_node = signal_node->next_sibling())
{
sig_time.push_back(stoi(signal_node->first_attribute("time")->value()));
}
if ((sig_time.size() % 2 == 0 and t_signal == 1) or (sig_time.size() % 2 == 1 and t_signal == 0))
{
sig_time.push_back(1);
}
vector<int> pos(2,-1);
//to make a vector of vehicles to be sent on road
vector<Vehicle > sig_vehicles;
xml_node<> * vehicles_node = root_node->first_node("Vehicles");
for (xml_node<> * vehicle_node = vehicles_node->first_node("vehicle"); vehicle_node; vehicle_node = vehicle_node->next_sibling())
{
int wid = stoi(vehicle_node->first_node("length")->value());
int len = stoi(vehicle_node->first_node("width")->value());
int height = stoi(vehicle_node->first_node("height")->value());
string col = vehicle_node->first_node("color")->value();
int max_v = stoi(vehicle_node->first_node("max_v")->value());
int acc = stoi(vehicle_node->first_node("acc")->value());
char disp = vehicle_node->first_node("display")->value()[0];
int id = sig_vehicles.size();
Vehicle v(r, len, wid, height, col, max_v, acc, disp, id, pos);
sig_vehicles.push_back(v);
}
for(int i=0;i<sig_vehicles.size();i++)
{
vehicles.push_back(&sig_vehicles[i]);
}
road_empty=false;
time_step=0;
sig_time_counter=0;
vehicle_counter=vehicles.size()-1;
////////////OpenGL
/* Initialize GLUT state - glut will take any command line arguments that pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&argc, argv);
/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffered for automatic clipping */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
/* get a 640 x 480 window */
glutInitWindowSize(640, 480);
/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);
/* Open a window */
window = glutCreateWindow("Traffic Simulator");
/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&DrawGLScene);
/* Go fullscreen. This is as soon as possible. */
glutFullScreen();
/* Even if there are no events, redraw our gl scene. */
glutIdleFunc(&DrawGLScene);
/* Register the function called when our window is resized. */
glutReshapeFunc(&ReSizeGLScene);
/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&keyPressed);
/* Initialize our window. */
InitGL(640, 480);
/* Start Event Processing Engine */
glutMainLoop();
return 1;
}