-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1649 lines (1399 loc) · 48.1 KB
/
main.c
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UAS Refdinal 171511023
*
* p Toggles first person/perspective projection
* c Change Day and Night
* arrow keys Change view angle
* ESC Exit
* w Up
* a Turn Left
* s Back
* d Turn Right
*/
#include "JTKPOLBAN.h"
#include "loadtexbmp.c"
#include "fatal.c"
#include "Errcheck.c"
#include "print.c"
#include "project.c"
#ifndef GL_CLAMP_TO_EDGE
#define GL_CLAMP_TO_EDGE 0x812F
#define ROAD_WIDTH 2
#define ROAD_THICK 0.15
#define CUBE_TOP_LEFT 1
#define CUBE_TOP_RIGHT 2
#define CUBE_BOTTOM_LEFT 3
#define CUBE_BOTTOM_RIGHT 4
#define ANGLE_TURN 6
#define DAY 1
#define NIGHT 0
#define CAR_MOVE 70
#endif
int mode=0; // Projection mode
float thf=105;
int th=105; // Azimuth of view angle
int ph=0; // Elevation of view angle
int fov=55; // Field of view (for perspective)
int light=1; // Lighting
double asp=1.333; // Aspect ratio
double dim=15; // Size of world
int carMove = 70;
// Light values
int one = 1; // Unit value
int distance = 25; // Light distance
int local = 0; // Local Viewer Model
int emission = 30; // Emission intensity (%)
int ambient = 80; // Ambient intensity (%)
int diffuse = 100; // Diffuse intensity (%)
int specular = 0; // Specular intensity (%)
int shininess = 0; // Shininess (power of two)
float shiny = 1; // Shininess (value)
int zh = 0; // Light azimuth
float ylight = 13; // Elevation of light
int earth = DAY; //Day and Night
int at0=100; // Constant attenuation %
int at1=20; // Linear attenuation %
int at2=20; // Quadratic attenuation %
int step= 0;
//First person camera location
double fpX = 0;
double fpY = 0.8;
double fpZ = 0;
float xPos = -6;
double xRotateTurn = 0;
double ZRotateTurn = 0;
double turnXInc = 0;
double turnZInc = 0;
//Enemy
float centerXIncrement = 0;
float centerZIncrement = 0;
float xRotate = 0.5;
float zRotate = 0;
float carRotate2 = 0;
//Enemy
float centerXIncrement2 = 0;
float centerZIncrement2 = 0;
float xRotate2 = 0.5;
float zRotate2 = 0;
float carRotate3 = 0;
//Person Control
float carRotateX = 0.5;
float carRotateZ = 0;
float carXIncrement = 0;
float carZIncrement = 0;
float carRotate = 0;
float temp;
//Race
int start = 0;
//x, y, z for refrence point in glLookAt() for FP mode
double refX = 0;
double refY = 0;
double refZ = 0;
//Texture Variables
int tMode = 0;
float texScale = 0.5;
//Light Vecotrs
float Ambient[] = {0.8 ,0.8 ,0.8 ,1.0};
float Diffuse[] = {1.0,1.0,1.0,1.0};
float Specular[] = {0,0,0,1.0};
GLuint _textureBasicMetal, _textureGlass, _textureWheel, _textureTire,
_textureWoodFence, _textureGrass, _textureCinderBlock, _textureCarGrill,
_textureHeadLamp, _textureCarbonFiber, _textureSidewalk,
_textureGarageDoor, _textureWalkway, _textureHedge, _textureGreyBrick,
_textureWoodBeam, _textureFrontDoor2, _textureWindow1, _textureSkyboxFront,
_textureSkyboxBack, _textureSkyboxRight, _textureSkyboxLeft, _textureSkyboxTop,
_textureAsphalt, _textureBrownBrick, _textureWhiteBrick, _textureMetalRoof,
_textureWarehouseWindow, _textureSupport;
;
void initTexture() {
_textureSkyboxFront = LoadTexBMP("texture/skybox-front.bmp");
_textureSkyboxBack = LoadTexBMP("texture/skybox-back.bmp");
_textureSkyboxRight = LoadTexBMP("texture/skybox-right.bmp");
_textureSkyboxLeft = LoadTexBMP("texture/skybox-left.bmp");
_textureSkyboxTop = LoadTexBMP("texture/skybox-top.bmp");
_textureBrownBrick = LoadTexBMP("texture/brown-brick.bmp");
_textureSupport = LoadTexBMP("texture/support.bmp");
_textureAsphalt = LoadTexBMP("texture/asphalt.bmp");
_textureGarageDoor = LoadTexBMP("texture/garage-door.bmp");
_textureSidewalk = LoadTexBMP("texture/sidewalk.bmp");
_textureBasicMetal = LoadTexBMP("texture/basic-metal.bmp");
_textureGlass = LoadTexBMP("texture/glass.bmp");
_textureWheel = LoadTexBMP("texture/car-wheel.bmp");
_textureTire = LoadTexBMP("texture/tire-tread.bmp");
_textureCarGrill = LoadTexBMP("texture/car-grill.bmp");
_textureHeadLamp = LoadTexBMP("texture/headlamp.bmp");
_textureGlass = LoadTexBMP("texture/glass.bmp");
_textureCarbonFiber = LoadTexBMP("texture/carbon-fiber.bmp");
_textureGreyBrick = LoadTexBMP("texture/grey-brick.bmp");
_textureGrass = LoadTexBMP("texture/grass.bmp");
_textureWoodBeam = LoadTexBMP("texture/wood-beam.bmp");
_textureMetalRoof = LoadTexBMP("texture/metal-roof.bmp");
}
static void cube(double x,double y,double z,
double dx,double dy,double dz,
double th)
{
// Save transformation
glPushMatrix();
// Offset, scale and rotate
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
//Texture repitition values
float texRepX = 1.0;
float texRepY = 1.0;
// Cube
glBegin(GL_QUADS);
// Front
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f( 0, 0, 1);
glTexCoord2f(0.0,0.0); glVertex3f(-1,-1, 1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,-1, 1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,+1, 1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,+1, 1);
// Back
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f( 0, 0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(+1,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(-1,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(-1,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(+1,+1,-1);
// Right
texRepX = dz/texScale;
texRepY = dy/texScale;
glNormal3f(+1, 0, 0);
glTexCoord2f(0.0,0.0); glVertex3f(+1,-1,+1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(+1,+1,+1);
// Left
texRepX = dz/texScale;
texRepY = dy/texScale;
glNormal3f(-1, 0, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-1,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(-1,-1,+1);
glTexCoord2f(texRepX,texRepY); glVertex3f(-1,+1,+1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,+1,-1);
// Top
texRepX = dx/texScale;
texRepY = dz/texScale;
glNormal3f( 0,+1, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-1,+1,+1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,+1,+1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,+1,-1);
// Bottom
texRepX = dx/texScale;
texRepY = dz/texScale;
glNormal3f( 0,-one, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-1,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,-1,+1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,-1,+1);
// End
glEnd();
// Undo transofrmations
glPopMatrix();
}
//Car Start
/*
* Draw a section of the car body
* at (x,y,z)
* dimensions (dx,dy,dz)
* rotated th about the y axis
* w (1 to color windows for car body, 0 otherwise)
*/
static void body(double x,double y,double z,
double dx,double dy,double dz,
double th,
int w)
{
// Set specular color to white
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
glBindTexture(GL_TEXTURE_2D,_textureBasicMetal);
texScale = 0.1;
glEnable(GL_POLYGON_OFFSET_FILL);
// Save transformation
glPushMatrix();
// Offset
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
//Texture repitition values
float texRepX = 1.0;
float texRepY = 1.0;
// Cube
glBegin(GL_QUADS);
// Top
texRepX = dx/texScale;
texRepY = dz/texScale;
glNormal3f( 0,+1, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-1,+1,+1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,+1,+1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,+1,-1);
// Bottom
texRepX = dx/texScale;
texRepY = dz/texScale;
glNormal3f( 0,-one, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-1,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,-1,+1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,-1,+1);
// Front
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f( 0, 0, 1);
glTexCoord2f(0.0,0.0); glVertex3f(-1,-1, 1);
glTexCoord2f(texRepX,0.0); glVertex3f(+1,-1, 1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+1,+1, 1);
glTexCoord2f(0.0,texRepY); glVertex3f(-1,+1, 1);
// Back
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f( 0, 0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(+1,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(-1,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(-1,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(+1,+1,-1);
// End
glEnd();
glDisable(GL_POLYGON_OFFSET_FILL);
//Color and texture to add windows
if(w == 1) {
glColor3f(0.8, 0.8, 1);
glBindTexture(GL_TEXTURE_2D,_textureGlass);
texRepX = 1.0;
texRepY = 1.0;
glBegin(GL_QUADS);
// Front
glNormal3f(0, 0, 1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8,-1, 1);
glTexCoord2f(texRepX,0.0); glVertex3f(+0.8,-1, 1);
glTexCoord2f(texRepX,texRepY); glVertex3f(+0.8,+1, 1);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.8,+1, 1);
// Back
glNormal3f(0, 0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(+0.8,-1,-1);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8,-1,-1);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.8,+1,-1);
glTexCoord2f(0.0,texRepY); glVertex3f(+0.8,+1,-1);
glEnd();
}
glEnable(GL_POLYGON_OFFSET_FILL);
// Undo transformations
glPopMatrix();
}
static void wheel(double x,double y,double z,
double dx,double dy,double dz,
double th,
int d,
int s)
{
// Set specular color to white
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
//Turn off shininess for the rubber tire
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
// Save transformation
glPushMatrix();
// Offset
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
glBindTexture(GL_TEXTURE_2D,_textureWheel);
glColor3f(0.8,0.8,0.8);
//Tire
glBegin(GL_TRIANGLE_FAN);
glNormal3f(0, 0, -1);
glTexCoord2f(0.5,0.5);
glVertex3f(0, 0, -0.05);
for (th=0;th<=360;th+=s)
{
double ph = d-90;
glTexCoord2f(0.5*Cos(th)+0.5,0.5*Sin(th)+0.5);
glVertex3d(Sin(th)*Cos(ph), Cos(th)*Cos(ph), -0.05);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glNormal3f(0, 0, 1);
glTexCoord2f(0.5,0.5);
glVertex3f(0, 0, 0.05);
for (th=360;th>=0;th-=s)
{
double ph = d-90;
glTexCoord2f(0.5*Cos(th)+0.5,0.5*Sin(th)+0.5);
glVertex3d(Sin(th)*Cos(ph) , Cos(th)*Cos(ph), 0.05);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, _textureTire);
glColor3f(0.5,0.5,0.55);
glBegin(GL_QUAD_STRIP);
for (th=0;th<=360;th+=s)
{
double ph = d-90;
glNormal3f(Sin(th)*Cos(ph), Cos(th)*Cos(ph), 0);
glTexCoord2f(0,0.1*th); glVertex3d(Sin(th)*Cos(ph), Cos(th)*Cos(ph), -0.05);
glTexCoord2f(1,0.1*th); glVertex3d(Sin(th)*Cos(ph), Cos(th)*Cos(ph), 0.05);
}
// glNormal3f(Sin(0)*Cos(-90), Cos(0)*Cos(-90), 0);
// glVertex3d(Sin(0)*Cos(-90), Cos(0)*Cos(-90), -0.05);
// glVertex3d(Sin(0)*Cos(-90), Cos(0)*Cos(-90), 0.05);
glEnd();
// Undo transformations
glPopMatrix();
}
static void bumper(double x,double y,double z,
double dx,double dy,double dz,
double th,
int m)
{
// Set specular color to white
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
// Save transformation
glPushMatrix();
// Offset
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
//Texture repitition values
float texRepX = 1.0;
float texRepY = 1.0;
glBindTexture(GL_TEXTURE_2D,_textureBasicMetal);
texScale = 0.1;
//Offset the bumper so that the lights and grill are drawn directly on the surface
glEnable(GL_POLYGON_OFFSET_FILL);
//Bumper
//Base
texRepX = dx/texScale;
texRepY = dy/texScale;
glBegin(GL_POLYGON);
glNormal3f(0, -1, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0,0,0.4);
glTexCoord2f(texRepX,0.0); glVertex3f(0,0,-0.4);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.1,0,-0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0.1,0,0.35);
glEnd();
//Front Panels
glBegin(GL_QUADS);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(0.447214, 0, 0.894427);
glTexCoord2f(0.0,0.0); glVertex3f(0.1, 0, 0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0.1, 0.2, 0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0, 0.2, 0.4);
glTexCoord2f(0.0,texRepY); glVertex3f(0, 0, 0.4);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(1, 0, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0.1, 0, -0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0.1, 0.2, -0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.1, 0.2, 0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0.1, 0, 0.35);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(0.447214, 0, -0.894427);
glTexCoord2f(0.0,0.0); glVertex3f(0.1, 0, -0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0, 0, -0.4);
glTexCoord2f(texRepX,texRepY); glVertex3f(0, 0.2, -0.4);
glTexCoord2f(0.0,texRepY); glVertex3f(0.1, 0.2, -0.35);
glEnd();
//Upper Bumper
glBegin(GL_QUADS);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(0.447214, 0.894427, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0, 0.25, 0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0.1, 0.2, 0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.1, 0.2, -0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0, 0.25, -0.35);
glEnd();
glBegin(GL_TRIANGLES);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(0.333333, 0.666667, 0.666667);
glTexCoord2f(0.0, 0.0); glVertex3f(0, 0.2, 0.4);
glTexCoord2f(texRepX/2, texRepY); glVertex3f(0.1, 0.2, 0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(0, 0.25, 0.35);
texRepX = dx/texScale;
texRepY = dy/texScale;
glNormal3f(0.333333, 0.666667, -0.666667);
glTexCoord2f(0.0, 0.0); glVertex3f(0, 0.25, -0.35);
glTexCoord2f(texRepX/2, texRepY); glVertex3f(0.1, 0.2, -0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(0, 0.2, -0.4);
glEnd();
// Disable polygon offset
glDisable(GL_POLYGON_OFFSET_FILL);
if (m == 1) {
glColor3f(0.2,0.2,0.2);
glBindTexture(GL_TEXTURE_2D,_textureCarGrill);
//Grill
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0.1, 0.15, 0.18);
glTexCoord2f(0.5,0.0); glVertex3f(0.1, 0.03, 0.18);
glTexCoord2f(0.5,1.0); glVertex3f(0.1, 0.03, -0.18);
glTexCoord2f(0.0,1.0); glVertex3f(0.1, 0.15, -0.18);
glEnd();
}
//Lights (taillights or headlights)
float emColor[4];
if(m == 1) {
emColor[0] = 1.0 * emission;
emColor[1] = 1.0 * emission;
emColor[2] = 0.8 * emission;
emColor[3] = 1.0 * emission;
glColor3f(1, 1, 0.8);
} else {
emColor[0] = 0.8 * emission;
emColor[1] = 0.0 * emission;
emColor[2] = 0.0 * emission;
emColor[3] = 1.0 * emission;
glColor3f(.8, 0, 0);
}
glMaterialf(GL_FRONT,GL_SHININESS,0);
glMaterialfv(GL_FRONT,GL_SPECULAR,emColor);
glMaterialfv(GL_FRONT,GL_EMISSION,emColor);
glBindTexture(GL_TEXTURE_2D,_textureHeadLamp);
glBegin(GL_TRIANGLE_FAN);
glNormal3f(1, 0, 0);
glTexCoord2f(0.5,0.5); glVertex3f(0.1, 0.13, -0.25);
for (th=0;th<=360;th+=10)
{
double ph = 3-90;
glTexCoord2f(0.5*Cos(th)+0.5,0.5*Sin(th)+0.5);
glVertex3d(0.1, 0.13+(Cos(th)*Cos(ph)), -0.25+(Sin(th)*Cos(ph)));
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glNormal3f(1, 0, 0);
glTexCoord2f(0.5,0.5); glVertex3f(0.1, 0.13, 0.25);
for (th=0;th<=360;th+=10)
{
double ph = 3-90;
glTexCoord2f(0.5*Cos(th)+0.5,0.5*Sin(th)+0.5);
glVertex3d(0.1, 0.13+(Cos(th)*Cos(ph)), 0.25+(Sin(th)*Cos(ph)));
}
glEnd();
glEnable(GL_POLYGON_OFFSET_FILL);
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
//Undo transformations
glPopMatrix();
}
static void car(double x,double y,double z,
double dx,double dy,double dz,
double th,
float cr, float cb, float cg)
{
// Set specular color to white
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
// Save transformation
glPushMatrix();
// Offset
glTranslated(x,y,z);
glRotated(th,0,1,0);
glScaled(dx,dy,dz);
glPolygonOffset(1,1);
wheel(0.6,0,0.4, 1,1,1, 0, 8, 10);
wheel(-0.6,0,-0.4, 1,1,1, 0, 8, 10);
wheel(0.6,0,-0.4, 1,1,1, 0, 8, 10);
wheel(-0.6,0,0.4, 1,1,1, 0, 8, 10);
glColor3f(cr, cb, cg);
//Lower Body
body(0,0.1,0, 0.8,0.1,0.4, 0, 0);
//Cabin
body(-0.1,0.3,0, 0.3,0.1,0.35, 0, 1);
texScale = 1.0;
glColor3f(cr, cb, cg);
//Front Bumper
bumper(0.8,0,0, 1,1,1, 0, 1);
glColor3f(cr, cb, cg);
//Rear Bumper
bumper(-0.8,0,0, 1,1,1, 180, 0);
// Set specular color to white
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
glEnable(GL_POLYGON_OFFSET_FILL);
glColor3f(cr, cb, cg);
glBindTexture(GL_TEXTURE_2D,_textureBasicMetal);
//Texture repitition values
float texRepX = 1.0;
float texRepY = 1.0;
//Hood and upper side pannels
texRepX = dx/texScale;
texRepY = dz/texScale;
glBegin(GL_QUADS);
glNormal3f(0, 0.707107, 0.707107);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8, 0.25, 0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8, 0.2, 0.4);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.8, 0.2, 0.4);
glTexCoord2f(0.0,texRepY); glVertex3f(0.8, 0.25, 0.35);
glNormal3f(0, 1, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0.4, 0.25, 0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0.8, 0.25, 0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.8, 0.25, -0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0.4, 0.25, -0.35);
glNormal3f(0, 0.707107, -0.707107);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8, 0.2, -0.4);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8, 0.25, -0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.8, 0.25, -0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0.8, 0.2, -0.4);
glEnd();
//Trunk
texRepX = dx/texScale;
texRepY = dz/texScale;
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8, 0.25, -0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8, 0.25, 0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.6, 0.25, 0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.6, 0.25, -0.35);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureGlass);
glColor3f(0.8, 0.8, 1);
//Windshield
texRepX = 1.0;
texRepY = 1.0;
glBegin(GL_QUADS);
glNormal3f(0.6, 0.8, 0);
glTexCoord2f(0.0,0.0); glVertex3f(0.4,0.25,0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(0.4,0.25,-0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(0.2,0.4,-0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(0.2,0.4,0.35);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(0,0,1);
glTexCoord2f(0.0,0.0); glVertex3f(0.2,0.4,0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(0.2,0.25,0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(0.4,0.25,0.35);
glNormal3f(0,0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(0.4,0.25,-0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(0.2,0.25,-0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(0.2,0.4,-0.35);
glEnd();
//Rear Window
texRepX = 1.0;
texRepY = 1.0;
glBegin(GL_QUADS);
glNormal3f(-0.6, 0.8, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-0.6,0.25,-0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.6,0.25,0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.4,0.4,0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.4,0.4,-0.35);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3f(0,0,1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.6,0.25,0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.4,0.25,0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.4,0.4,0.35);
glNormal3f(0,0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.4,0.4,-0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.4,0.25,-0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.6,0.25,-0.35);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureCarbonFiber);
//Spoiler
glColor3f(0.3,0.3,0.3);
cube(-0.75,0.28,0.3, 0.02,0.03,0.02, 0);
cube(-0.75,0.28,-0.3, 0.02,0.03,0.02, 0);
texRepX = 5.0;
texRepY = 1.0;
glBegin(GL_QUADS);
glNormal3f(0, -1, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-0.7,0.31,-0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.7,0.31,0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.8,0.31,0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.8,0.31,-0.35);
glNormal3f(0.196116, 0.980581, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8,0.33,-0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8,0.33,0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.7,0.31,0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.7,0.31,-0.35);
texRepX = 5.0;
texRepY = 0.2;
glNormal3f(-1, 0, 0);
glTexCoord2f(0.0,0.0); glVertex3f(-0.8,0.33,0.35);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.8,0.33,-0.35);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.8,0.31,-0.35);
glTexCoord2f(0.0,texRepY); glVertex3f(-0.8,0.31,0.35);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureBasicMetal);
glColor3f(cr, cb, cg);
//Spoiler Fins
texRepX = dx/texScale;
texRepY = dy/texScale;
glBegin(GL_TRIANGLES);
glNormal3f(0,0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.68,0.31,-0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.82,0.31,-0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.82,0.35,-0.35);
glNormal3f(0,0,1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.82,0.35,0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.82,0.31,0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.68,0.31,0.35);
//Duplicate to draw both sides (with inverted normals) when face culling is on
glNormal3f(0,0,1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.68,0.31,-0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.82,0.31,-0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.82,0.35,-0.35);
glNormal3f(0,0,-1);
glTexCoord2f(0.0,0.0); glVertex3f(-0.82,0.35,0.35);
glTexCoord2f(texRepX, 0.0); glVertex3f(-0.82,0.31,0.35);
glTexCoord2f(texRepX, texRepY); glVertex3f(-0.68,0.31,0.35);
glEnd();
//Undo transformations
glPopMatrix();
}
//Car End
static void support(double x, double y, double z)
{
glBindTexture(GL_TEXTURE_2D,_textureSupport );
glPushMatrix();
glTranslated(x,y,z);
cube(0,-0.05,-0.5, 2,0.15,0.2, 0);
glPopMatrix();
}
static void road(double x, double y, double z)
{
glBindTexture(GL_TEXTURE_2D,_textureAsphalt);
glPushMatrix();
glTranslated(x,y,z);
cube(0,-0.05,-0.5, 2,0.15,1, 0);
glPopMatrix();
}
static void lightRoad(double x, double y, double z)
{
glBindTexture(GL_TEXTURE_2D,_textureHeadLamp);
texScale = 0.1;
glPushMatrix();
// cube(-4,4,13, 0.07,0.02,0.1, 0);
glTranslated(x,y,z);
glRotated(5,1,0,0);
cube(-4,4,13, 1.5,0.5,0.1, 0);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, _textureBasicMetal);
glPushMatrix();
glTranslated(x,y,z);
cube(-4,1.6,13.2, 0.1 ,1.6,0.1, 0);
glPopMatrix();
}
static void pitstop(double x, double y, double z)
{
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
glBindTexture(GL_TEXTURE_2D,_textureSupport);
glPushMatrix();
// Offset
glTranslated(x,y,z);
//Building
cube(0,1.6,0, 2,0.4,1, 0); //Top
glBindTexture(GL_TEXTURE_2D,_textureBrownBrick);
cube(-1.75,0.65,0, 0.25,0.55,1, 0); //Left
cube(1.75,0.65,0, 0.25,0.55,1, 0); //Right
cube(0,0.65,0, 0.2,0.55,1, 0); //Middle
glPopMatrix();
float texRepX = 1.0;
float texRepY = 1.0;
//Door left
glBindTexture(GL_TEXTURE_2D,_textureGarageDoor);
glPushMatrix();
glTranslated(x,y,z);
glBegin(GL_QUADS);
//Left
glTexCoord2f(0.0,0.0); glVertex3f(-1.5, 0.1, 0.9);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.2, 0.1, 0.9);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.2, 1.2, 0.9);
glTexCoord2f(0.0,texRepY); glVertex3f(-1.5, 1.2, 0.9);
glEnd();
glPopMatrix();
//Door Right
glPushMatrix();
glTranslated(x+1.7,y,z);
glBegin(GL_QUADS);
//Left
glTexCoord2f(0.0,0.0); glVertex3f(-1.5, 0.1, 0.9);
glTexCoord2f(texRepX,0.0); glVertex3f(-0.2, 0.1, 0.9);
glTexCoord2f(texRepX,texRepY); glVertex3f(-0.2, 1.2, 0.9);
glTexCoord2f(0.0,texRepY); glVertex3f(-1.5, 1.2, 0.9);
glEnd();
glPopMatrix();
//Floor
glBindTexture(GL_TEXTURE_2D,_textureSidewalk );
glPushMatrix();
glTranslated(x,y,z);
cube(0,-0.05,0.9, 2,0.15,0.1, 0);
glPopMatrix();
//Sidewalk
glPushMatrix();
glTranslated(x,0, 0);
cube(0,-0.05,z+1.5, 2,0.15,0.5, 0); // Along Street
glTranslated(0,0, 1);
cube(0,-0.05,z+1.5, 2,0.15,0.5, 0); // Along Street
glPopMatrix();
//
support(x,y, z+3.7);
//Road
road(x, y,z+4.91);
// road(x, y,z+8);
}
static void skybox(float dim) {
glDisable(GL_POLYGON_OFFSET_FILL);
// Set specular color to white
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
glColor3f(0.7, 0.7, 0.7);
glBindTexture(GL_TEXTURE_2D,_textureSkyboxFront);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
glTexCoord2f(1.0,0.0); glVertex3f(+dim,-dim, dim);
glTexCoord2f(0.0,0.0); glVertex3f(-dim,-dim, dim);
glTexCoord2f(0.0,1.0); glVertex3f(-dim,+dim, dim);
glTexCoord2f(1.0,1.0); glVertex3f(+dim,+dim, dim);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureSkyboxBack);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glTexCoord2f(1.0,0.0); glVertex3f(-dim,-dim, -dim);
glTexCoord2f(0.0,0.0); glVertex3f(+dim,-dim, -dim);
glTexCoord2f(0.0,1.0); glVertex3f(+dim,+dim, -dim);
glTexCoord2f(1.0,1.0); glVertex3f(-dim,+dim, -dim);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureSkyboxRight);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glNormal3f(-1, 0, 0);
glTexCoord2f(1.0,0.0); glVertex3f(dim,-dim, -dim);
glTexCoord2f(0.0,0.0); glVertex3f(dim,-dim, +dim);
glTexCoord2f(0.0,1.0); glVertex3f(dim,+dim, +dim);
glTexCoord2f(1.0,1.0); glVertex3f(dim,+dim, -dim);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureSkyboxLeft);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glTexCoord2f(1.0,0.0); glVertex3f(-dim,-dim, +dim);
glTexCoord2f(0.0,0.0); glVertex3f(-dim,-dim, -dim);
glTexCoord2f(0.0,1.0); glVertex3f(-dim,+dim, -dim);
glTexCoord2f(1.0,1.0); glVertex3f(-dim,+dim, +dim);
glEnd();
glBindTexture(GL_TEXTURE_2D,_textureSkyboxTop);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBegin(GL_QUADS);
glNormal3f(0, -1, 0);
glTexCoord2f(1.0,0.0); glVertex3f(+dim,dim, +dim);
glTexCoord2f(0.0,0.0); glVertex3f(-dim,dim, +dim);
glTexCoord2f(0.0,1.0); glVertex3f(-dim,dim, -dim);
glTexCoord2f(1.0,1.0); glVertex3f(+dim,dim, -dim);
glEnd();
}
static void straightRoad(double tx, double ty, double tz, double roadLong, double degree, int edge)
{
int xt, yt, zt;
// glPushMatrix();
switch (edge) {
case CUBE_TOP_LEFT :
xt = tx + ROAD_WIDTH;
yt = ty + ROAD_THICK;
zt = tz - roadLong;
break;
case CUBE_TOP_RIGHT :
xt = tx + ROAD_WIDTH;
yt = ty + ROAD_THICK;
zt = tz + roadLong;
break;
case CUBE_BOTTOM_LEFT :
xt = tx - roadLong;
yt = ty + ROAD_THICK;
zt = tz - ROAD_WIDTH;
break;
case CUBE_BOTTOM_RIGHT :
xt = tx - ROAD_WIDTH;
yt = ty + ROAD_THICK;
zt = tz + roadLong;
break;
default:
xt = 0;
yt = 0;
zt = 0;
break;
}
glBindTexture(GL_TEXTURE_2D,_textureAsphalt);
glPushMatrix();
glTranslated(xt, yt, zt);
glRotated(degree,0,1,0);
glTranslated(-xt, -yt, -zt);
cube(tx, ty, tz, roadLong, ROAD_THICK, ROAD_WIDTH, 0);
glPopMatrix();
}
static void turnRoad(double tx, double ty, double tz, double roadLong){
straightRoad(tx, ty, tz, 1, -15, CUBE_BOTTOM_LEFT);
straightRoad(tx+1.5, ty, tz+0.15, 1, -30, CUBE_BOTTOM_LEFT);
straightRoad(tx+3.8, ty, tz+1, 1, -45, CUBE_BOTTOM_LEFT);
straightRoad(tx+5, ty, tz+3, 1, -60, CUBE_BOTTOM_LEFT);
straightRoad(tx+6, ty, tz+4.2, 1, -75, CUBE_BOTTOM_LEFT);