-
Notifications
You must be signed in to change notification settings - Fork 6
/
Sproingies.html
3023 lines (2493 loc) · 91.1 KB
/
Sproingies.html
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
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Sproingies in WebGL</title>
<link rel="icon"
type="image/jpg"
href="images/icon_Sproingie.jpg">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<style type="text/css">
body {
font-family: Sans-Serif;
font-size: 0.7em;
background: #000;
}
#glCanvas {
display: block;
border: none;
width: 100%;
height: 100%;
}
#infoDialog {
font-size: 0.9em;
line-height: 1.5em;
}
.infoIcon {
display: block;
float: left;
border: 0;
padding: 0;
margin: 0 16px 5px 0;
}
.version {
color: #888;
padding-top: 0.4em;
}
.version2, .showFPS {
display: block;
font-size: 0.9em;
position: absolute;
bottom: 2em;
left: 1.2em;
color: #888;
}
.redBox {
background-color: #FFAAAA;
padding: 0.5em 1em;
}
#loadingWebGL {
padding-top: 40%;
color: #eee;
text-align: center;
font-size: 1.2em;
}
#missingWebGL {
display: none;
width: 600px;
font-size: 1.1em;
background-color: #EDEDED;
padding: 0.5em 1em;
margin: 5px auto;
text-align: center;
line-height: 1.5em;
}
#missingWebGL a {
display: block;
margin-top: 1em;
font-weight: bold;
}
#ui {
display: block;
position: fixed;
top: 10%;
right: 100px;
text-align: center;
background-color: #A6C9E2;
}
#toolbar {
display: block;
position: fixed;
left: 0;
bottom: 20px;
margin: 0 auto;
text-align: center;
width: 100%;
}
#toolbar .ui-button {
margin-right: 0.4em;
}
.sliderBox {
display: block;
float: left;
text-align: center;
border: 1px solid #A6C9E2;
margin: 5px;
padding: 2px 10px;
width: 5em;
}
.sliderBox p {
clear: both;
margin: 5px 0;
line-height: 1.4em;
}
#settingsSliders span {
height: 140px;
display: block;
margin: 0 auto;
}
#glCanvas {
/* display will be set to "block" after javascript confirms WebGL availability. */
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -10;
}
</style>
<!--
** Please read the credits & software licenses:
** https://bitbucket.org/emackey/sproingies/wiki/Licenses
-->
<link rel="stylesheet" href="css/start/jquery-ui-1.8.13.custom.css" />
<script type="text/javascript" src="js/glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="js/webgl-utils.js"></script>
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="js/S1_b.js"></script>
<script type="text/javascript" src="js/Teapot.js"></script>
<script type="text/javascript" src="js/macbook.js"></script>
<!--
** Sproingie fragment & vertex shaders
-->
<script id="shader-fs-sproingie" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
//
// This is the fragment shader for the Sproingies.
//
uniform vec4 uKeyLightColor;
uniform vec3 uKeyLightPos; // Light positions should be premultiplied with thier own view matrix.
uniform vec4 uFillLightColor;
uniform vec3 uFillLightPos;
uniform vec4 uMaterialColor;
uniform vec4 uMaterialSpecular;
uniform vec4 uMaterialAltColor;
varying vec3 vNormal;
varying vec4 vViewPos;
varying vec2 vUVCoords;
void pointLight(in vec3 N, in vec3 V, in float shininess,
inout vec4 diffuse, inout vec4 specular,
in vec4 lightColor, in vec3 lightPos)
{
vec3 D = lightPos - V;
vec3 L = normalize(D);
float nDotL = dot(N,L);
if (nDotL > 0.0)
{
vec3 halfVector = normalize(L + vec3(0.0, 0.0, 1.0));
float pf = pow(max(0.0, dot(N, halfVector)), shininess);
diffuse += lightColor * nDotL;
specular += lightColor * pf;
}
}
float modf(float x, float y) {
return x - (y * floor(x/y));
}
void main(void) {
vec3 n = normalize(vNormal);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
pointLight(n, vViewPos.xyz, 1000.0, diffuse, specular, uKeyLightColor, uKeyLightPos);
pointLight(n, vViewPos.xyz, 50.0, diffuse, specular, uFillLightColor, uFillLightPos);
vec4 surfaceColor = uMaterialColor;
if (modf(floor(vUVCoords.y), 2.0) > 0.5) {
surfaceColor = uMaterialAltColor;
}
vec4 color = (diffuse * surfaceColor) +
(specular * uMaterialSpecular);
color = clamp(vec4(color.rgb, 1.0), 0.0, 1.0);
gl_FragColor = color;
}
</script>
<script id="shader-vs-sproingie" type="x-shader/x-vertex">
//
// This is the vertex shader for the Sproingies.
//
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aUVCoords;
uniform mat4 uMVMatrix;
uniform mat4 uProjMatrix;
uniform mat3 uNormalMatrix;
varying vec3 vNormal;
varying vec4 vViewPos;
varying vec2 vUVCoords;
void main(void) {
vNormal = normalize(uNormalMatrix * aVertexNormal);
vViewPos = uMVMatrix * vec4(aVertexPosition, 1.0);
vUVCoords = aUVCoords;
gl_Position = uProjMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
</script>
<!--
** Sproingie explosion fragment & vertex shaders
-->
<script id="shader-fs-boom" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
//
// This is the fragment shader for exploding Sproingies.
//
uniform vec4 uColor;
uniform sampler2D uDotSampler;
void main(void) {
float alpha = texture2D(uDotSampler, gl_PointCoord).r;
gl_FragColor = vec4(uColor.rgb, alpha);
if (alpha < 0.0001) {
discard;
}
}
</script>
<script id="shader-vs-boom" type="x-shader/x-vertex">
//
// This is the vertex shader for exploding Sproingies.
//
attribute vec3 aVertexPosition;
uniform mat4 uMVMatrix;
uniform mat4 uProjMatrix;
uniform float uPointSize;
void main(void) {
gl_PointSize = uPointSize;
gl_Position = uProjMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
</script>
<!--
** Dying Sproingie explosion(2) fragment & vertex shaders
-->
<script id="shader-fs-explode" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 uColor;
varying float vLifetime;
varying vec2 vTextureCoords;
uniform sampler2D sTexture;
void main(void) {
vec4 texColor;
texColor = texture2D(sTexture, vTextureCoords);
gl_FragColor = vec4(uColor) * texColor;
gl_FragColor.a *= vLifetime;
}
</script>
<script id="shader-vs-explode" type="x-shader/x-vertex">
uniform float uTime;
uniform vec3 uCenterPosition;
uniform mat4 uMVMatrix;
uniform mat4 uProjMatrix;
//uniform float uPointsize;
attribute float aLifetime;
attribute vec3 aStartPosition;
attribute vec3 aEndPosition;
attribute vec2 aOffset;
attribute vec2 aTextureCoords;
attribute vec3 aVertexPosition;
varying float vLifetime;
varying vec2 vTextureCoords;
void main(void) {
//aVertexPosition = aStartPosition + (uTime * aEndPosition);
if (uTime <= aLifetime) {
/*gl_Position.xyz = aStartPosition + (uTime * aEndPosition);
gl_Position.xyz += uCenterPosition;
gl_Position.w = 1.0;*/
gl_Position = uProjMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
} else {
gl_Position = uProjMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
//gl_Position = vec4(-1000, -1000, 0, 0);
}
vLifetime = 1.0 - (uTime / aLifetime);
vLifetime = clamp(vLifetime, 0.0, 1.0);
float size = (vLifetime * vLifetime) * 0.1;
gl_Position.xy += aOffset.xy * size;
vTextureCoords = aTextureCoords;
}
</script>
<!--
** Ground fragment & vertex shaders
-->
<script id="shader-fs-ground" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
//
// This is the fragment shader for the ground (staircase) under the Sproingies.
//
varying vec4 vColor;
varying vec3 vGroundPos;
void main(void) {
// This extra bit of per-pixel logic fades the top & bottom of the staircase to black.
float dist = clamp((2.0 - abs(vGroundPos.y / 45.0)), 0.0, 1.0);
// You can swap the commented line for the one below to
// see the fadeout zone in red, for debugging/adjusting.
//gl_FragColor = vec4(1.0-dist, (vColor.gb * dist), 1.0); // Red fadeout zone
gl_FragColor = vec4((vColor.rgb * dist), 1.0);
}
</script>
<script id="shader-vs-ground" type="x-shader/x-vertex">
//
// This is the vertex shader for the ground (staircase) under the Sproingies.
//
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 uMVMatrix;
uniform mat4 uProjMatrix;
uniform mat3 uNormalMatrix;
uniform vec4 uCameraMatrix;
uniform vec4 uKeyLightColor;
uniform vec3 uKeyLightPos; // Light positions should be premultiplied with thier own view matrix.
uniform vec4 uFillLightColor;
uniform vec3 uFillLightPos;
uniform vec4 uMaterialColor;
uniform vec4 uMaterialSpecular;
uniform vec4 uMaterialAltColor;
uniform vec3 uGroundPos;
varying vec4 vColor;
varying vec3 vGroundPos;
// This is a rare case where per-vertex lighting looks nicer than per-pixel.
// With per-vertex, a group of ground cube side walls all have different
// shades, and offer some contrast from each other. But with per-pixel lit
// ground cubes, all the side walls blend seamlessly and the edges disappear.
// So we light the ground per-vertex, and we light the Sproingies per-pixel.
void pointLight(in vec3 N, in vec3 V, in float shininess,
inout vec4 diffuse, inout vec4 specular,
in vec4 lightColor, in vec3 lightPos)
{
vec3 D = lightPos - V;
vec3 L = normalize(D);
float nDotL = dot(N,L);
if (nDotL > 0.0)
{
vec3 halfVector = normalize(L + vec3(0.0, 0.0, 1.0));
float pf = pow(max(0.0, dot(N, halfVector)), shininess);
diffuse += lightColor * nDotL;
specular += lightColor * pf;
}
}
void main(void) {
vec3 vNormal = normalize(uNormalMatrix * aVertexNormal);
vec4 vViewPos = uMVMatrix * vec4(aVertexPosition, 1.0);
vGroundPos = aVertexPosition + uGroundPos;
gl_Position = uProjMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vec3 n = normalize(vNormal);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
vec3 viewPos3 = (vec3 (vViewPos)) / vViewPos.w;
pointLight(n, viewPos3, 50.0, diffuse, specular, uKeyLightColor, uKeyLightPos);
pointLight(n, viewPos3, 50.0, diffuse, specular, uFillLightColor, uFillLightPos);
vec4 color = (diffuse * uMaterialColor) +
(specular * uMaterialSpecular);
// Make the tops of the ground a lighter color.
if (aVertexNormal.y > 0.5) {
// Arbitrary gamma tweaking, because it looks like the original when I do this.
diffuse = 1.0 - ((1.0 - diffuse) * 0.8);
specular = 1.0 - ((1.0 - specular) * 0.8);
color = // tops
(diffuse * vec4(0.392157, 0.784314, 0.941176, 1.0)) +
(specular * vec4(0.2, 0.2, 0.2, 1.0));
}
vColor = clamp(vec4(color.rgb, 1.0), 0.0, 1.0);
}
</script>
<script id="macbook-shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
uniform float uMaterialShininess;
uniform bool uShowSpecularHighlights;
uniform bool uUseLighting;
uniform bool uUseTextures;
uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
uniform sampler2D uSampler;
void main(void) {
vec3 lightWeighting;
if (!uUseLighting) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
vec3 normal = normalize(vTransformedNormal);
float specularLightWeighting = 0.0;
if (uShowSpecularHighlights) {
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
}
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
}
vec4 fragmentColor;
if (uUseTextures) {
fragmentColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
fragmentColor = vec4(0.25, 0.41, 0.88, 1.0);
}
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script id="macbook-shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = uNMatrix * aVertexNormal;
}
</script>
<script id="teapot-shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
uniform float uMaterialShininess;
uniform bool uShowSpecularHighlights;
uniform bool uUseLighting;
uniform bool uUseTextures;
uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
uniform sampler2D uSampler;
void main(void) {
vec3 lightWeighting;
if (!uUseLighting) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
vec3 normal = normalize(vTransformedNormal);
float specularLightWeighting = 0.0;
if (uShowSpecularHighlights) {
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), uMaterialShininess);
}
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
}
vec4 fragmentColor;
if (uUseTextures) {
fragmentColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
fragmentColor = vec4(0.25, 0.41, 0.88, 1.0);
}
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script id="teapot-shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = uNMatrix * aVertexNormal;
}
</script>
<script type="text/javascript">
var DIRECTION_UP = -1;
var DIRECTION_DOWN = 1;
var TYPE_SLINKY = 0;
var TYPE_JUMP = 1;
var TYPE_ROLL = 2;
var TYPE_TEAPOT = 3;
var TYPE_macbook = 4;
var NUM_TYPES = 5;
var DEFAULT_NUM_SPROINGIES = 13;
var DEFAULT_SPEED = 10;
var DEFAULT_GROUND = 2;
var DEFAULT_WEIRDNESS = 4;
var DEFAULT_TYPE = TYPE_SLINKY;
var HERO_SPEED_JUMPING = 4;
var HERO_SPEED_NOT_JUMPING = 2;
var HERO_SPEED_JUMPING = 4;
var HERO_SPEED_NOT_JUMPING = 2;
var CAMERA_SLIDE_RATE = 0.998;
var BOOM_STAGE = 50.0;
var S_RES = 28;
var M_TWOPI = Math.PI * 2.0;
var DegToRad = Math.PI / 180.0;
// Bits for m_weirdBits field in Sproingies.
var SP_BIT_AIRDROP = 1;
var SP_BIT_STRIPES = 2;
var SP_BIT_FAT = 4;
var sproingieList = [];
var mouseDown = -1;
var lastMouseX = 0;
var lastMouseY = 0;
var target_wait = 0.0001,
max_target_wait = 100.0;
var ground = 0.0,
rotx = 0.0,
roty = -45.0,
dist = 32.0;
var target_rx = 0.0,
target_ry = -45.0,
target_dist = 32.0;
var lookAway = 1.5;
var looking = -0.5;
var howfast = DEFAULT_SPEED;
var groundlevel = DEFAULT_GROUND;
var weirdness = DEFAULT_WEIRDNESS;
var KILL_ALL = false;
var LevelTimer;
var gl; // the WebGL context
var glCanvasName = "glCanvas";
var MAX_TOOLBAR_LIFE = 5;
var TOOLBAR_FADE_SPEED = 500;
var toolbarLife = MAX_TOOLBAR_LIFE;
var toolbarVisible = true;
var fpsFrames = 0,
fpsTime = 0;
var lastTime = 0;
var isPaused = false;
var groundBuffers;
var sproingieBuffers;
var teapotBuffers;
var macbookBuffers;
var sproingieBoomBuffers;
var sproingieExplodeBuffers;
var groundShaderProgram;
var sproingieShaderProgram;
var tpshaderProgram;
var macbookshaderProgram;
var sproingieBoomShaderProgram;
var sproingieExplodeShaderProgram;
var dotTexture;
var pointLifetimesBuffer;
var pointStartPositionsBuffer;
var pointEndPositionsBuffer;
var pointOffsetsBuffer;
var pointTextureCoordsBuffer;
var color = [Math.random() / 2 + 0.5, Math.random() / 2 + 0.5, Math.random() / 2 + 0.5, 0.5];
var colorTime = 0;
var groundRGB = [0.156863, 0.156863, 0.392157];
var nextGroundRGB = [0.156863, 0.156863, 0.392157];
var worldWidth = 8;
var worldHeight = 3;
var BOXINESS = 0;
var gameLevel = 1;
var totalScore = 0;
var playerLives;
var levelScore;
var playAgain;
var SPECULAR = 0.5;
var keyPos = [0.0, 0.0, -16.0];
var lightRot = 0;
var lightRotSpeed = 0.05;
var rotCameraX = [0.0, 0.0, 0.0, 0.0];
var rotCameraY = [0.0, 0.0, 0.0, 0.0];
var rotCameraZ = 0.0;
var KEYDOWN = false;
var ALT = false;
var eye = [0.0, 0.0, 16.0];
var lookAt = [0.0, 1.0, 0.0];
var up = [0.0, 1.0, 0.0];
var cameraMatrix = [eye, lookAt, up];
var zoomLevel = 100;
var zoom = [0.80, 0.15, 0.9, 0.5];
var selectedCam = 0;
var camera1 = [0.0, 0.0, -16.0];
var camera2 = [1.0, 10.0, -3.0];
var camera3 = [20.0, 0.0, -64.0];
var camera4 = [-0.0, 4.0, 16.0];
var cameras = [camera1, camera2, camera3, camera4];
var galvanizedTexture;
// Misc helpful URLs
// Sproingie Wiki: https://bitbucket.org/emackey/sproingies/wiki/
// JSLint: http://www.jslint.com/
// WebGL Spec: http://www.khronos.org/registry/webgl/specs/latest/
// GLSL Spec: http://www.opengl.org/sdk/docs/manglsl/
// glMatrix lib: http://code.google.com/p/glmatrix/
// WebGL Inspector: http://benvanik.github.com/WebGL-Inspector/
//
// Initialize WebGL
//
function initGL(canvas) {
try {
gl = WebGLUtils.setupWebGL(canvas);
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (ex) {}
if (gl) {
document.getElementById("loadingWebGL").style.display = "none";
document.getElementById("glCanvas").style.display = "block";
resizeGL();
} else {
document.getElementById("loadingWebGL").style.display = "none";
document.getElementById("missingWebGL").style.display = "block";
$("#play,#showInspector,#showSettings").button("option", "disabled", true);
}
}
function resizeGL() {
var canvas = document.getElementById(glCanvasName);
gl.viewportWidth = canvas.width = canvas.clientWidth;
gl.viewportHeight = canvas.height = canvas.clientHeight;
//document.getElementById("info").innerHTML = "Size " + gl.viewportWidth + " x " + gl.viewportHeight;
}
//
// Fetch a shader from another script block, and compile it.
//
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType === 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type === "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type === "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(id + " compile error:\n\n" + gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
//
// Init a shader program.
//
function initShaders(shaderProgram, fsName, vsName) {
var fragmentShader = getShader(gl, fsName);
var vertexShader = getShader(gl, vsName);
if ((fragmentShader !== null) && (vertexShader !== null)) {
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
// attribute vec2 aUVCoords;
shaderProgram.aUVCoords = gl.getAttribLocation(shaderProgram, "aUVCoords");
if (shaderProgram.aUVCoords) {
gl.enableVertexAttribArray(shaderProgram.aUVCoords);
}
// Vertex shader uniforms
// uniform mat4 uMVMatrix;
// uniform mat4 uProjMatrix;
// uniform mat4 uNormalMatrix;
// uniform vec3 uGroundPos;
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uProjMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.normalMatrixUniform = gl.getUniformLocation(shaderProgram, "uNormalMatrix");
shaderProgram.groundPosUniform = gl.getUniformLocation(shaderProgram, "uGroundPos");
// Fragment shader uniforms
// uniform vec4 uKeyLightColor;
// uniform vec3 uKeyLightPos; // Light positions should be premultiplied with thier own view matrix.
// uniform vec4 uFillLightColor;
// uniform vec3 uFillLightPos;
// uniform vec4 uMaterialColor;
// uniform vec4 uMaterialSpecular;
// uniform vec4 uMaterialAltColor;
shaderProgram.uKeyLightColor = gl.getUniformLocation(shaderProgram, "uKeyLightColor");
shaderProgram.uKeyLightPos = gl.getUniformLocation(shaderProgram, "uKeyLightPos");
shaderProgram.uFillLightColor = gl.getUniformLocation(shaderProgram, "uFillLightColor");
shaderProgram.uFillLightPos = gl.getUniformLocation(shaderProgram, "uFillLightPos");
shaderProgram.uMaterialColor = gl.getUniformLocation(shaderProgram, "uMaterialColor");
shaderProgram.uMaterialSpecular = gl.getUniformLocation(shaderProgram, "uMaterialSpecular");
shaderProgram.uMaterialAltColor = gl.getUniformLocation(shaderProgram, "uMaterialAltColor");
}
function initmacbookShaders(shaderProgram, fsName, vsName) {
var fragmentShader = getShader(gl, fsName);
var vertexShader = getShader(gl, vsName);
if ((fragmentShader !== null) && (vertexShader !== null)) {
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise macbook shaders");
}
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
shaderProgram.materialShininessUniform = gl.getUniformLocation(shaderProgram, "uMaterialShininess");
shaderProgram.showSpecularHighlightsUniform = gl.getUniformLocation(shaderProgram, "uShowSpecularHighlights");
shaderProgram.useTexturesUniform = gl.getUniformLocation(shaderProgram, "uUseTextures");
shaderProgram.useLightingUniform = gl.getUniformLocation(shaderProgram, "uUseLighting");
shaderProgram.ambientColorUniform = gl.getUniformLocation(shaderProgram, "uAmbientColor");
shaderProgram.pointLightingLocationUniform = gl.getUniformLocation(shaderProgram, "uPointLightingLocation");
shaderProgram.pointLightingSpecularColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingSpecularColor");
shaderProgram.pointLightingDiffuseColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingDiffuseColor");
}
function handlemacbookLoadedTexture(texture) {
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}
function initmacbookTextures() {
galvanizedTexture = gl.createTexture();
galvanizedTexture.image = new Image();
galvanizedTexture.image.onload = function () {
handlemacbookLoadedTexture(galvanizedTexture)
}
galvanizedTexture.image.src = "images/arroway.de_metal+structure+06_d100_flat.jpg";
}
function initTeapotShaders(shaderProgram, fsName, vsName) {
var fragmentShader = getShader(gl, fsName);
var vertexShader = getShader(gl, vsName);
if ((fragmentShader !== null) && (vertexShader !== null)) {
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise teapot shaders");
}
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
shaderProgram.materialShininessUniform = gl.getUniformLocation(shaderProgram, "uMaterialShininess");
shaderProgram.showSpecularHighlightsUniform = gl.getUniformLocation(shaderProgram, "uShowSpecularHighlights");
shaderProgram.useTexturesUniform = gl.getUniformLocation(shaderProgram, "uUseTextures");
shaderProgram.useLightingUniform = gl.getUniformLocation(shaderProgram, "uUseLighting");
shaderProgram.ambientColorUniform = gl.getUniformLocation(shaderProgram, "uAmbientColor");
shaderProgram.pointLightingLocationUniform = gl.getUniformLocation(shaderProgram, "uPointLightingLocation");
shaderProgram.pointLightingSpecularColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingSpecularColor");
shaderProgram.pointLightingDiffuseColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingDiffuseColor");
}
function handleTeapotLoadedTexture(texture) {
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}
function initTeapotTextures() {
galvanizedTexture = gl.createTexture();
galvanizedTexture.image = new Image();
galvanizedTexture.image.onload = function () {
handleTeapotLoadedTexture(galvanizedTexture)
}
galvanizedTexture.image.src = "images/arroway.de_metal+structure+06_d100_flat.jpg";
}
function initBoomShaders(shaderProgram, fsName, vsName) {
var fragmentShader = getShader(gl, fsName);
var vertexShader = getShader(gl, vsName);
if ((fragmentShader !== null) && (vertexShader !== null)) {
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise boom shaders");
}
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
// Vertex shader uniforms
// uniform mat4 uMVMatrix;
// uniform mat4 uProjMatrix;
// uniform float uPointSize;
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uProjMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");