-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
1513 lines (1180 loc) · 62.3 KB
/
main.js
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
var canvas = document.getElementById('my_Canvas');
const gl = canvas.getContext('webgl');
var textCanvas = document.getElementById("text");
const ctx = textCanvas.getContext("2d");
const ext = gl.getExtension('WEBGL_depth_texture');
if (!ext) {
console.log('need WEBGL_depth_texture'); // eslint-disable-line
}
//======================= INITIAL PARAMETERS =========================//
var canvas_width=850;
var canvas_height=500;
var aspect = canvas_width/ canvas_height;
var zmin = 1;
var zmax = 100;
var fov = 40;
var THETA=1.60, PHI=0.21;
var D = 10;
var initial_target = [-1.3, -0.5, 40]; //cambia per cambiare punto di vista es. casco, retro etc..
var initial_target_default = [-1.3, -0.5, 40];
var target=[]; //updated target current
var up = [0, 1, 0];
var mo_matrix, mo_matrix1; //mo_matrix usata per la corrozzeria, mo_matrix1 per le 4 ruote
var mo_matrix_shuttle;
var end=0;
var start=0;
var shuttle_lancio=0;
var offset=0;
var win = false;
var tempo;
var tempoFinale;
var seconds = 0;
var shadows=false;
var bump_mapping=false;
var pilota_automatico=false;
var luce_on=false;
var gamepad=false;
var gp;
var gp_start;
var ctrl=1;
var proj_matrix;
var view_matrix;
var camera;
var texture_matrix;
var lightProjectionMatrix;
//main light information
var lightPosition = [0, 7.5, 40];
var initial_lightPosition = [0,7.5, 40];
var lightAmbient = [0.2, 0.2, 0.2];
var lightDiffuse = [1.0, 1.0, 1.0];
var lightSpecular = [1.0, 1.0, 1.0];
//sia luce che materiali sono vettori -> MACCHINA
var materialAmbientCar = [0.25, 0.20725, 0.20725];
var materialDiffuseCar = [1.0, 0.829, 0.829];
var materialSpecularCar = [0.2966648, 0.2966648, 0.2966648];
var materialShininessCar = 11.264; //Esponente di Phong
//sia luce che materiali sono vettori -> CIRCUITO
var materialAmbientCircuit = [0.25, 0.20, 0.20];
var materialDiffuseCircuit = [1.0, 0.829, 0.829];
var materialSpecularCircuit = [0.0,0.0,0.0];
var materialShininessCircuit = 11.4; //Esponente di Phong
//luce fari dx e sx
var lightPosition_faro_dx = [-1.2, -1, 40.5];
var lightPosition_faro_sx = [-1.5, -1, 40.5];
var limit=degToRad(20); //raggio d'azione fari spot
var initial_lightPosition_faro_dx = [-1.2, -1, 40.5];
var initial_lightPosition_faro_sx = [-1.5, -1, 40.5];
var target_faro_dx=[-1, -2, 30];
var target_faro_sx=[-1.7, -2, 30];
var initial_target_faro_dx = [-1, -2, 30];
var initial_target_faro_sx = [-1.7, -2, 30];
var lightAmbient_fari = [0.0, 0.0, 0.0];
var lightDiffuse_fari = [1.0,1.0, 1.0];
var lightSpecular_fari = [0.8, 0.8, 0.8];
/*============================== FLOOR GEOMETRY INFORMATION ==========================*/
//informazioni sul terreno
const W=1000; // size
const L=1000;
const H=-0.7; // altezza
var numVerticesFloor = 6;
var vertices_floor=[
[-W,H,L],
[-W,H,-L],
[W,H,-L],
[W,H,L]];
var vertices_floor_array = [];
var normals_floor_array = [];
var tangent_floor_array = [];
var texcoord2D_floor_array = [];
//sia luce che materiali sono vettori -> FLOOR
var materialAmbientFloor = [0.19225, 0.19225, 0.19225];
var materialDiffuseFloor = [0.50754, 0.50754, 0.50754];
var materialSpecularFloor = [0.0,0.0,0.0];
var materialShininessFloor = 10; //Esponente di Phong
//cordinate texture floor
uv1=[0,0];
uv2=[70,0];
uv3=[70,70];
uv4=[0,70];
uv_floor=[uv1,uv2,uv3,uv4];
/*============================== LOAD OBJ + FLOOR GEOMETRY DEFINITION ==========================*/
//car obj components
carrozzeriaObj = new objLoader ("resources/obj/carrozzeria.obj", false);
parabrezzaObj = new objLoader ("resources/obj/parabrezza.obj", false);
ruota_dxObj = new objLoader ("resources/obj/ruota_dx.obj", true);
ruota_sxObj = new objLoader ("resources/obj/ruota_sx.obj", true);
coperchio_sup_grandeObj = new objLoader ("resources/obj/coperchio_sup_grande.obj", true);
coperchio_sup_smallObj = new objLoader ("resources/obj/coperchio_sup_small.obj", true);
fari_grandiObj = new objLoader ("resources/obj/fari_grandi.obj", true);
fari_piccoliObj = new objLoader ("resources/obj/fari_piccoli.obj", true);
//track obj components
roadObj = new objLoader ("resources/obj/road.obj", true);
boosterObj = new objLoader ("resources/obj/booster.obj", true);
deboosterObj = new objLoader ("resources/obj/debooster.obj", true);
grigliaObj = new objLoader ("resources/obj/griglia.obj", true);
cartelloneObj = new objLoader ("resources/obj/cartellone.obj", true);
cartellone_strutturaObj = new objLoader ("resources/obj/cartellone_struttura.obj", false);
camera_cartelloObj = new objLoader ("resources/obj/camera_cartello.obj", true);
//shuttle
shuttleObj = new objLoader ("resources/obj/shuttle.obj", true);
//floor
compute_floor_geometry();
for(var i=0; i<numVerticesFloor; i++) normals_floor_array[i] = Array.prototype.slice.call(m4.normalize(normals_floor_array[i])); //normalize the normals
vertices_floor_array=m4.flatten(vertices_floor_array);
normals_floor_array=m4.flatten(normals_floor_array);
texcoord2D_floor_array = m4.flatten(texcoord2D_floor_array);
tangent_floor_array = m4.flatten(tangent_floor_array);
//calcolo centro della macchina per collissioni
var center_carr=[];
initial_center_carr=compute_center(carrozzeriaObj.vertices, carrozzeriaObj.nvert);
initial_center_carr[0]=initial_center_carr[0]-1.3;
initial_center_carr[2]=initial_center_carr[2]+40;
//calcolo dimensioni tracciato per collissioni
var track_dimension=[];
track_dimension=compute_track_width();
/*===================================================== NURBS CURVE ===========================================*/
//Bezier Curve definition
//la curva è stata prima definita in Blender e poi estratti manualmente i controll points dal file generato
var bezierC={
deg: 13, // grado della curva
cp: [ -1.275960 , 0.000000, 40.019226,
0.419437, 0.000000, 10.864552,
0.440632, 0.000000, 0.080433,
1.347425, 0.000000, -21.854113,
2.223205, 0.000000, -50.617020,
0.604158, 0.000000, -80.267113,
3.446360, 0.000000, -121.497093,
3.734644, 0.000000, -191.883804,
4.445250, 0.000000, -266.122986,
4.380293, 0.000000, -329.413574,
4.380293, 0.000000, -370.413574,
2.380293, 0.000000, -400.413574,
2.180293, 0.000000, -410.413574,
0.113259, 0.000000, -430.818451],
ab: [0,1],
};
//Bezier Curve CP
var cpxyz=[];
cpxyz=m4.flatten(bezierC.cp);
//Bezier Curve discretization points
var t=[],xyz=[],dxyz=[],ddxyz=[];
var np=600, nD=3;
t=linspace(bezierC.ab[0],bezierC.ab[1],np);
//valutazione punti curva con algoritmo 2
//var v=decast_valder(bezierC, nD, 2, t)
//xyz=m4.flatten(v.D0);
//dxyz=m4.flatten(v.D1);
//ddxyz=m4.flatten(v.D2);
//valutazione punti curva con algoritmo 1 (più efficente dal punto di vista computazionale)
var cc=bezier_valder(bezierC, nD, 2, t)
xyz=m4.flatten(cc.D0);
dxyz=m4.flatten(cc.D1);
ddxyz=m4.flatten(cc.D2);
//init frenet frame e step
var FF=m4.identity;
var step=0;
/*===================================================== DEFINING THE BUFFERS ===========================================*/
//car buffers
carrozzeriaBuffers = new objBuffersCollection (gl, carrozzeriaObj, false);
parabrezzaBuffers= new objBuffersCollection (gl, parabrezzaObj, false);
ruota_dxBuffers = new objBuffersCollection (gl, ruota_dxObj, true);
ruota_sxBuffers = new objBuffersCollection (gl, ruota_sxObj, true);
coperchio_sup_grandeBuffers = new objBuffersCollection (gl, coperchio_sup_grandeObj, true);
coperchio_sup_smallBuffers = new objBuffersCollection (gl, coperchio_sup_smallObj, true);
fari_grandiBuffers = new objBuffersCollection (gl, fari_grandiObj, true);
fari_piccoliBuffers = new objBuffersCollection (gl, fari_piccoliObj, true);
//track buffers
roadBuffers = new objBuffersCollection (gl, roadObj, true);
boosterBuffers = new objBuffersCollection (gl, boosterObj, true);
deboosterBuffers = new objBuffersCollection (gl, deboosterObj, true);
grigliaBuffers = new objBuffersCollection (gl, grigliaObj, true);
cartelloneBuffers = new objBuffersCollection (gl, cartelloneObj, true);
cartellone_strutturaBuffers = new objBuffersCollection (gl, cartellone_strutturaObj, false);
camera_cartelloBuffers = new objBuffersCollection (gl, camera_cartelloObj, true);
//shuttle buffers
shuttleBuffers = new objBuffersCollection (gl, shuttleObj, true);
//floor buffers
var vertex_buffer_floor = gl.createBuffer ();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer_floor);
gl.bufferData(gl.ARRAY_BUFFER, vertices_floor_array, gl.STATIC_DRAW);
var normal_buffer_floor = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, normal_buffer_floor );
gl.bufferData( gl.ARRAY_BUFFER, normals_floor_array, gl.STATIC_DRAW );
var texcoord_buffer_floor = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texcoord_buffer_floor);
gl.bufferData(gl.ARRAY_BUFFER, texcoord2D_floor_array, gl.STATIC_DRAW);
var tanget_buffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, tanget_buffer );
gl.bufferData( gl.ARRAY_BUFFER, tangent_floor_array, gl.STATIC_DRAW );
// bezier curve buffers
var vertex_buffer4 = gl.createBuffer ();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer4);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(xyz), gl.STATIC_DRAW);
var vertex_buffer5 = gl.createBuffer ();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer5);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(cpxyz), gl.STATIC_DRAW);
/*=========================================== DEFINING AND LOADING TEXTURE =============================================*/
//struttura dati che contiene le texture e il relativo path
const textureInfos = [{text: texture_coperchio_grande = gl.createTexture(), url: "resources/texture/coperchio_grande.jpg",},
{text: texture_coperchio_piccolo = gl.createTexture(), url: "resources/texture/coperchio_piccolo.jpg",},
{text: texture_fari_grandi = gl.createTexture(), url: "resources/texture/fari_big.jpg",},
{text: texture_fari_piccoli = gl.createTexture(), url: "resources/texture/fari_small.jpg",},
{text: texture_ruote = gl.createTexture(), url: "resources/texture/ruote.jpg",},
{text: texture_track = gl.createTexture(), url: "resources/texture/road.jpg",},
{text: texture_booster = gl.createTexture(), url: "resources/texture/booster.jpg",},
{text: texture_debooster = gl.createTexture(), url: "resources/texture/debooster.jpg",},
{text: texture_griglia = gl.createTexture(), url: "resources/texture/griglia.jpg",},
{text: texture_cartellone = gl.createTexture(), url: "resources/texture/spacex.jpg",},
{text: texture_cartello_camera = gl.createTexture(), url: "resources/texture/cartello_camera.jpg",},
{text: texture_normal = gl.createTexture(), url: "resources/texture/bump_moon.jpg",},
];
//for each entry della struttura textureInfos
textureInfos.forEach((textureInfo) => {
const {text, url} = textureInfo;
gl.bindTexture(gl.TEXTURE_2D, text);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 255, 255]));
//asynchronously load an image
var image = new Image();
image.src = url;
image.addEventListener('load', function() {
//now that the image has loaded make copy it to the texture
gl.bindTexture(gl.TEXTURE_2D, text);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, image);
//Check if the image is a power of 2 in both dimensions
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
//se è potenza del 2 allora MIPMAP
gl.generateMipmap(gl.TEXTURE_2D);
} else {
//se non è potenza del 2: Turn of mips and set wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
});
});
/*============================== LOAD IMG 2D CONTEXT ==========================*/
//sfondo pannello di controllo
var image_menu = new Image();
image_menu.src = "resources/others/menu.jpg";
image_menu.addEventListener('load', function() {});
//frecce direzionali su schermo
var directional_keys= new Image();
directional_keys.src = "resources/others/directional.png";
directional_keys.addEventListener('load', function() {});
//bottoni
var button1 = new Image();
button1.src = "resources/buttons/bottone1.jpg";
button1.addEventListener('load', function() {});
var button2 = new Image();
button2.src = "resources/buttons/bottone2.jpg";
button2.addEventListener('load', function() {});
var button3 = new Image();
button3.src = "resources/buttons/bottone3.jpg";
button3.addEventListener('load', function() {});
var buttonON= new Image();
buttonON.src = "resources/buttons/bottoneON.jpg";
buttonON.addEventListener('load', function() {});
var buttonOFF= new Image();
buttonOFF.src = "resources/buttons/bottoneOFF.jpg";
buttonOFF.addEventListener('load', function() {});
var button_pilota_automatico= new Image();
button_pilota_automatico.src = "resources/buttons/pilota_bottone.jpg";
button_pilota_automatico.addEventListener('load', function() {});
var button_pilota_automatico_NO= new Image();
button_pilota_automatico_NO.src = "resources/buttons/pilota_bottone_NO.jpg";
button_pilota_automatico_NO.addEventListener('load', function() {});
//bottone fari
var fari_on = new Image();
fari_on.src = "resources/buttons/fari_on.jpg";
fari_on.addEventListener('load', function() {});
var fari_off = new Image();
fari_off.src = "resources/buttons/fari_off.jpg";
fari_off.addEventListener('load', function() {});
//timer
var timer_img = new Image();
timer_img.src = "resources/others/timer.png";
timer_img.addEventListener('load', function() {});
//pip boy
var boy_felice = new Image();
boy_felice.src = "resources/others/boy_felice.png";
boy_felice.addEventListener('load', function() {});
var boy_triste = new Image();
boy_triste.src = "resources/others/boy_triste.png";
boy_triste.addEventListener('load', function() {});
//retry button
var retry = new Image();
retry.src = "resources/buttons/retry.png";
retry.addEventListener('load', function() {});
/*=========================================== SHADOWS INFORMATION =============================================*/
//create depth Texture
const depthTexture = gl.createTexture();
const depthTextureSize = 2048;
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, depthTextureSize, depthTextureSize, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
//create depthBuffer
const depthFramebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, depthFramebuffer);
// attach depthTexture to the depthFramebuffer
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthTexture, 0);
// create a color texture "unusedTexture" of the same size as the depth texture (need for some browser such as Safari)
const unusedTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, unusedTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, depthTextureSize, depthTextureSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// attach the "unusedTexture" to the depthFramebuffer
gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, unusedTexture, 0);
/*=========================================== SKYBOX INFORMATION =============================================*/
var vertex_buffer_skybox = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer_skybox );
var positions = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1,]); //quad
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
var texture_skybox = gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture_skybox);
//info texture immagini facce dello skybox
const faceInfos = [{target: gl.TEXTURE_CUBE_MAP_POSITIVE_X, url: 'resources/skybox/pos-x.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_X, url: 'resources/skybox/neg-x.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Y, url: 'resources/skybox/pos-y.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, url: 'resources/skybox/neg-y.jpg',},
{target: gl.TEXTURE_CUBE_MAP_POSITIVE_Z, url: 'resources/skybox/pos-z.jpg',},
{target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, url: 'resources/skybox/neg-z.jpg',},
];
//for each texture in faceInfos
faceInfos.forEach((faceInfo) => {
const {target, url} = faceInfo;
//Upload the canvas to the cubemap face
const level = 0;
const internalFormat = gl.RGBA;
const width = 1024;
const height = 1024;
const format = gl.RGBA;
const type = gl.UNSIGNED_BYTE;
//setup each face so it's immediately renderable
gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, null);
//Asynchronously load an image
const image = new Image();
image.src = url;
image.addEventListener('load', function() {
//Now that the image has loaded make copy it to the texture
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture_skybox);
gl.texImage2D(target, level, internalFormat, format, type, image);
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
});
});
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
/*========================= SHADER PROGRAMS BUILD ==========================*/
//SHADERS VERSIONE SENZA OMBRE
//shader di base
var shaderprogram_color = webglUtils.createProgramFromScripts(gl, ['color-vertex-shader', 'color-fragment-shader']);
//shader per l'enviroment mapping
var shaderprogram_env = webglUtils.createProgramFromScripts(gl, ["vertex-shader-envmap", "fragment-shader-envmap"]);
//shader per lo skybox
var shaderprogram_skybox = webglUtils.createProgramFromScripts(gl, ["vertex-shader-skybox", "fragment-shader-skybox"]);
//shader per l'illuminazione secondo Phong: NO texture
var shaderprogram_phong = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong", "fragment-shader-phong"]);
//shader per l'illuminazione secondo Phong: YES texture
var shaderprogram_phong_text = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-text", "fragment-shader-phong-text"]);
//shader per l'illuminazione secondo Phong + SPOT LIGHTS: YES texture
var shaderprogram_phong_text_spotlight = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-text-spotlight", "fragment-shader-phong-text-spotlight"]);
//shader per l'illuminazione secondo Phong + BUMP MAPPING
var shaderprogram_phong_normal_mapping = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-normal-mapping", "fragment-shader-phong-normal-mapping"]);
//SHADERS VERSIONE CON OMBRE
//shader per l'illuminazione secondo Phong: NO texture + SHADOWS
var shaderprogram_shadow_notext = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-notexture-shadow", "fragment-shader-phong-notexture-shadow"]);
//shader per l'illuminazione secondo Phong: YES texture + SHADOWS
var shaderprogram_shadow_text = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-texture-shadow", "fragment-shader-phong-texture-shadow"]);
//shader per l'illuminazione secondo Phong + SPOT LIGHTS: YES texture + SHADOWS
var shaderprogram_shadow_phong_text_spotlight = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-text-spotlight-shadow", "fragment-shader-phong-text-spotlight-shadow"]);
//shader per l'illuminazione secondo Phong + BUMP MAPPING + SHADOWS
var shaderprogram_shadow_phong_normal_mapping = webglUtils.createProgramFromScripts(gl, ["vertex-shader-phong-normal-mapping-shadow", "fragment-shader-phong-normal-mapping-shadow"]);
/*======== Associating attributes to vertex shader =====*/
uniform_Color_Collection = new uniformShaderCollection(gl, "shaderprogram_color");
uniform_Skybox_Collection = new uniformShaderCollection(gl, "shaderprogram_skybox");
uniform_Enviroment_Mapping_Collection = new uniformShaderCollection(gl, "shaderprogram_env");
uniform_Phong_Collection = new uniformShaderCollection(gl, "shaderprogram_phong");
uniform_Text_Collection = new uniformShaderCollection(gl, "shaderprogram_phong_text");
uniform_Spotlight_Collection = new uniformShaderCollection(gl, "shaderprogram_phong_text_spotlight");
uniform_NormalMapping_Collection = new uniformShaderCollection(gl, "shaderprogram_phong_normal_mapping");
uniform_ShadowPhong_Collection = new uniformShaderCollection(gl, "shaderprogram_shadow_notext");
uniform_ShadowText_Collection = new uniformShaderCollection(gl, "shaderprogram_shadow_text");
uniform_ShadowSpotlight_Collection = new uniformShaderCollection(gl, "shaderprogram_shadow_phong_text_spotlight");
uniform_ShadowNormalMapping_Collection = new uniformShaderCollection(gl, "shaderprogram_shadow_phong_normal_mapping");
/*============================================ INITIAL EVENTS ============================================*/
//more info on the others events in events.js
window.addEventListener("click", checkButtonClick);
window.addEventListener('keydown', doKeyDown, true);
window.addEventListener('keyup', doKeyUp, true);
window.addEventListener('touchstart', doTouchDown, true);
window.addEventListener('touchend', doTouchUp, true);
window.addEventListener('mousedown', doMouseDown, true);
window.addEventListener('mouseup', doMouseUp, true);
/*============================================ DRAW FUNCTIONS ====================================*/
//draw Carrozzeria auto
function renderCarrozzeria(){
gl.bindBuffer(gl.ARRAY_BUFFER, carrozzeriaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, carrozzeriaBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.drawArrays(gl.TRIANGLES, 0, carrozzeriaObj.nface*3);
}
//draw CoperchioGrande dell'auto
function renderCoperchioGrande(){
gl.bindBuffer(gl.ARRAY_BUFFER, coperchio_sup_grandeBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, coperchio_sup_grandeBuffers.normal_buffer );
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, coperchio_sup_grandeBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_coperchio_grande);
gl.drawArrays(gl.TRIANGLES, 0, coperchio_sup_grandeObj.nface*3);
}
//draw CoperchioPiccolo dell'auto
function renderCoperchioPiccolo(){
gl.bindBuffer(gl.ARRAY_BUFFER, coperchio_sup_smallBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, coperchio_sup_smallBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, coperchio_sup_smallBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_coperchio_piccolo);
gl.drawArrays(gl.TRIANGLES, 0, coperchio_sup_smallObj.nface*3);
}
//draw FariGrandi dell'auto
function renderFariGrandi(){
gl.bindBuffer(gl.ARRAY_BUFFER, fari_grandiBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, fari_grandiBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, fari_grandiBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_fari_grandi);
gl.drawArrays(gl.TRIANGLES, 0, fari_grandiObj.nface*3 );
}
//draw FariPiccoli dell'auto
function renderFariPiccoli(){
gl.bindBuffer(gl.ARRAY_BUFFER, fari_piccoliBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, fari_piccoliBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, fari_piccoliBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_fari_piccoli);
gl.drawArrays(gl.TRIANGLES, 0, fari_piccoliObj.nface*3 );
}
//draw Parabrezza dell'auto
function renderParabrezza(){
gl.bindBuffer(gl.ARRAY_BUFFER, parabrezzaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Enviroment_Mapping_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Enviroment_Mapping_Collection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, parabrezzaBuffers.normal_buffer );
gl.vertexAttribPointer(uniform_Enviroment_Mapping_Collection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniform_Enviroment_Mapping_Collection._normal);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture_skybox);
gl.drawArrays(gl.TRIANGLES, 0, parabrezzaObj.nface*3 );
}
//draw Ruote dell'auto
function renderWheel(pos){
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_ruote);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
if(pos=="dx"){
gl.bindBuffer(gl.ARRAY_BUFFER, ruota_dxBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, ruota_dxBuffers.normal_buffer );
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, ruota_dxBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.drawArrays(gl.TRIANGLES, 0, ruota_dxObj.nface*3 );
}
if(pos=="sx"){
gl.bindBuffer(gl.ARRAY_BUFFER, ruota_sxBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, ruota_sxBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, ruota_sxBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.drawArrays(gl.TRIANGLES, 0, ruota_sxObj.nface*3 );
}
}
//draw Strada
function renderRoad(){
gl.bindBuffer(gl.ARRAY_BUFFER, roadBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, roadBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, roadBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_track);
gl.drawArrays(gl.TRIANGLES, 0, roadObj.nface*3 );
}
//draw Limitatori di velocità
function renderBooster(){
gl.bindBuffer(gl.ARRAY_BUFFER, boosterBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, boosterBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, boosterBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_booster);
gl.drawArrays(gl.TRIANGLES, 0, boosterObj.nface*3 );
}
//draw Potenziatore di velocità
function renderDebooster(){
gl.bindBuffer(gl.ARRAY_BUFFER, deboosterBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, deboosterBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, deboosterBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_debooster);
gl.drawArrays(gl.TRIANGLES, 0, deboosterObj.nface*3 );
}
//draw Griglia
function renderGriglia(){
gl.bindBuffer(gl.ARRAY_BUFFER, grigliaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, grigliaBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, grigliaBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_griglia);
gl.drawArrays(gl.TRIANGLES, 0, grigliaObj.nface*3 );
}
//draw Suolo lunare
function renderFloor(){
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer_floor);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, normal_buffer_floor );
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
if(program!=shaderprogram_phong && program!=shaderprogram_shadow_notext){
gl.bindBuffer(gl.ARRAY_BUFFER, tanget_buffer);
gl.vertexAttribPointer(uniformCollection._tangent, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._tangent);
gl.bindBuffer(gl.ARRAY_BUFFER, texcoord_buffer_floor);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
}
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_normal);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.drawArrays(gl.TRIANGLES, 0, numVerticesFloor);
}
//draw Cartellone autore
function renderCartellone(){
gl.bindBuffer(gl.ARRAY_BUFFER, cartelloneBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, cartelloneBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, cartelloneBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_cartellone);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.drawArrays(gl.TRIANGLES, 0, cartelloneObj.nface*3 );
}
//draw Cartellone pubblicitario
function renderCartelloSpaceInvaders(){
gl.bindBuffer(gl.ARRAY_BUFFER, camera_cartelloBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, camera_cartelloBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.bindBuffer(gl.ARRAY_BUFFER, camera_cartelloBuffers.texcoord_buffer);
gl.vertexAttribPointer(uniformCollection._texcoord, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._texcoord);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture_cartello_camera);
gl.drawArrays(gl.TRIANGLES, 0, camera_cartelloObj.nface*3 );
}
//draw Struttura del Cartello autore
function renderStrutturaCartellone(){
gl.bindBuffer(gl.ARRAY_BUFFER, cartellone_strutturaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, cartellone_strutturaBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
gl.drawArrays(gl.TRIANGLES, 0, cartellone_strutturaObj.nface*3);
}
//draw dello Space Shuttle
function renderShuttle(){
gl.bindBuffer(gl.ARRAY_BUFFER, shuttleBuffers.vertex_buffer);
gl.vertexAttribPointer(uniformCollection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniformCollection._position);
gl.bindBuffer( gl.ARRAY_BUFFER, shuttleBuffers.normal_buffer);
gl.vertexAttribPointer(uniformCollection._normal, 3, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray(uniformCollection._normal);
gl.drawArrays(gl.TRIANGLES, 0, shuttleObj.nface*3);
}
//draw dello Skybox cielo stellato
function renderSkybox(){
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer_skybox);
gl.vertexAttribPointer(uniform_Skybox_Collection._position, 2, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Skybox_Collection._position);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture_skybox);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
//calcolo della shadow map considerando le sole entita attive nel calcolo delle ombre (ex. no suolo lunare)
function compute_shadows(){
gl.useProgram(shaderprogram_color);
//punto di vista della luce
gl.uniformMatrix4fv(uniform_Color_Collection._Pmatrix, false, lightProjectionMatrix);
//view_matrix = inversa della camera matrix light
gl.uniformMatrix4fv(uniform_Color_Collection._Vmatrix, false, m4.inverse(lightWorldMatrix));
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, mo_matrix);
gl.uniform4fv(uniform_Color_Collection._color,[0,0,1,1]);
//carrozzeria
gl.bindBuffer(gl.ARRAY_BUFFER, carrozzeriaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Color_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Color_Collection._position);
gl.drawArrays(gl.TRIANGLES, 0, carrozzeriaObj.nface*3);
//parabrezza
gl.bindBuffer(gl.ARRAY_BUFFER, parabrezzaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Color_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Color_Collection._position);
gl.drawArrays(gl.TRIANGLES, 0, parabrezzaObj.nface*3);
//primo cartellone
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, m4.identity());
gl.bindBuffer(gl.ARRAY_BUFFER, cartelloneBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Color_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Color_Collection._position);
gl.drawArrays(gl.TRIANGLES, 0, cartelloneObj.nface*3);
//ultimo cartellone
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, m4.translate(m4.identity(),3.6,0,-372));
gl.drawArrays(gl.TRIANGLES, 0, cartelloneObj.nface*3);
//space invaders
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, m4.translate(m4.identity(),0,0,-60));
gl.bindBuffer(gl.ARRAY_BUFFER, camera_cartelloBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Color_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Color_Collection._position);
gl.drawArrays(gl.TRIANGLES, 0, camera_cartelloObj.nface*3 );
//struttura
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, m4.identity());
gl.bindBuffer(gl.ARRAY_BUFFER, cartellone_strutturaBuffers.vertex_buffer);
gl.vertexAttribPointer(uniform_Color_Collection._position, 3, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(uniform_Color_Collection._position);
gl.drawArrays(gl.TRIANGLES, 0, cartellone_strutturaObj.nface*3);
//ultima struttura
gl.uniformMatrix4fv(uniform_Color_Collection._Mmatrix, false, m4.translate(m4.identity(),4,0,-372));
gl.drawArrays(gl.TRIANGLES, 0, cartellone_strutturaObj.nface*3);
}
/* //disegna i CP della curva di Bezier
function drawCPBezier(){
draw_col=[1,0,0,0];
gl.uniform3fv(gl.getUniformLocation(shaderprogram_color, "u_color"), draw_col);
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer5);
gl.vertexAttribPointer(gl.getAttribLocation(shaderprogram_color, "a_position"), nD, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(gl.getAttribLocation(shaderprogram_color, "a_position"));
gl.drawArrays(gl.LINE_STRIP, 0, bezierC.deg+1);
}
//disegna la curva di Bezier
function drawBezier(){
draw_col=[0,0,0,0];
gl.uniform3fv(gl.getUniformLocation(shaderprogram_color, "u_color"), draw_col);
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer4);
gl.vertexAttribPointer(gl.getAttribLocation(shaderprogram_color, "a_position"), nD, gl.FLOAT, false,0,0);
gl.enableVertexAttribArray(gl.getAttribLocation(shaderprogram_color, "a_position"));
gl.drawArrays(gl.LINE_STRIP, 0, np);
} */
/*============================================ RENDER FUNCTION + FPS ====================================*/
var render=function() {
//aggiorno target in base allo spostamento dell macchina
target[0]=initial_target[0]+px;
target[1]=initial_target[1]+py;
target[2]=initial_target[2]+pz;
//la luce principale segue in avanti e indietro lo spostameno della macchina
lightPosition[2]=initial_lightPosition[2]+pz;
//la luce dei fari segue avanti e indietro lo spostameno della macchina
lightPosition_faro_dx[0]=initial_lightPosition_faro_dx[0]+px;
lightPosition_faro_dx[2]=initial_lightPosition_faro_dx[2]+pz;
lightPosition_faro_sx[0]=initial_lightPosition_faro_sx[0]+px;
lightPosition_faro_sx[2]=initial_lightPosition_faro_sx[2]+pz;
//aggiorno target luci fari in base allo spostamento dell macchina
target_faro_dx[0]=initial_target_faro_dx[0]+px;
target_faro_dx[2]=initial_target_faro_dx[2]+pz;
target_faro_sx[0]=initial_target_faro_sx[0]+px;
target_faro_sx[2]=initial_target_faro_sx[2]+pz;
//aggiorno il centro della macchina per eventuali collissioni
center_carr[0]=initial_center_carr[0]+px;
center_carr[1]=initial_center_carr[1]+py;
center_carr[2]=initial_center_carr[2]+pz;