-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·856 lines (705 loc) · 25.2 KB
/
index.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
<html>
<head>
<title>Ludum Dare 22</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<script type="text/javascript" src="textureScheme.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = textureColor;//vec4(textureColor.rgb,textureColor.a);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
<script type="text/javascript">
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
}
}
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(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
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.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
}
function handleLoadedTexture(texture) {
try{
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);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}catch(e){
alert("exception in Handled:"+texture.image.src);
}
}
var rockTexture;
var textureList = new Array();
var texturesLoaded = 0;
function loadTexture(src, callback) {
var texture = gl.createTexture();
texture.image = new Image();
texture.image.onload = function(){
handleLoadedTexture(texture);
textureList.push(texture);
}
texture.image.src = src;
}
function initTextures(){
for(i=0; i<textureFileList.length;i++){
loadTexture(textureFileList[i],0);
}
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
var currentlyPressedKeys = {};
function handleKeyDown(event) {
currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {
currentlyPressedKeys[event.keyCode] = false;
}
var camera;
var circa;
//GLOBAL CONSTANTS
var START_POS =[1.0,0.0,1.0];
var PLAYER_SPEED = 0.004;
var PLAYER_ACCEL = 0.0001;
var PLAYER_FORCE = 1;
var PLAYER_FRICTION =1;
var PLAYER_MASS = 1;
var CAMERA_DISTANCE = 6;
var CAMERA_PITCH = -45;
//END GLOBAL
stageList = new Array();
function initObjects(){
currentStage = 0;
var newStage = new Stage();
newStage.backGroundMusic = "ludum1-2";
newStage.cameraPitch = -45;
newStage.cameraDistance = 4;
newStage.boundary = 0;
newStage.backGroundColor = [0.0,0.0,0.0];
newStage.splash = 0;
stageList.push(newStage);
var newStage = new Stage();
newStage.backGroundMusic = "ludum1-2";
newStage.cameraPitch = -45;
newStage.cameraDistance = 4;
newStage.boundary = 18;
newStage.backGroundColor = [0.0,0.0,0.0];
newStage.splash = 0;
stageList.push(newStage);
var newStage = new Stage();
newStage.backGroundMusic = "ludum2-1";
newStage.cameraPitch = -45;
newStage.cameraDistance = 4;
newStage.boundary = 25;
newStage.backGroundColor = [0.0,0.0,0.0];
newStage.splash = 1;
stageList.push(newStage);
var newStage = new Stage();
newStage.backGroundMusic = "ludum1-2";
newStage.cameraPitch = -45;
newStage.cameraDistance = 6;
newStage.boundary = 15;
newStage.backGroundColor = [0.3,0.3,0.3];
newStage.splash = 3;
stageList.push(newStage);
var newStage = new Stage();
newStage.backGroundMusic = "ludum2-1";
newStage.cameraPitch = -45;
newStage.cameraDistance = 7;
newStage.boundary = 30;
newStage.backGroundColor = [0.3,0.5,0.7];
newStage.splash = 4;
stageList.push(newStage);
circa = new Sphere();
circa.direction = [0,0];
circa.pos = START_POS;
circa.texture = textureList[2];
circa.rotation = mat4.create();
mat4.identity(circa.rotation);
circa.radius = 0.5;
circa.speed = PLAYER_SPEED;
circa.accel = PLAYER_ACCEL;
circa.vel = [0,0,0];
circa.force = PLAYER_FORCE;
circa.friction = PLAYER_FRICTION;
circa.mass = PLAYER_MASS;
camera = new Camera();
camera.pitch = CAMERA_PITCH;
camera.distance = CAMERA_DISTANCE;
billBoard = new Splash(0);
}
var billBoard;
function Drawable(){
this.texture = null;
this.pos = [0,0,0];
this.vertexPositionBuffer = gl.createBuffer();
this.vertexTextureCoordBuffer = gl.createBuffer();
this.drawtype = "";
this.numItems = 0;
}
function drawTriangleStrip(myTexture, myVertexPositionBuffer, myTextureCoordBuffer, numItems){
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, myTexture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, myTextureCoordBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, myVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, numItems);
}
function Camera(){
this.distance = 0;
this.yaw = 0;
this.pitch = 0;
this.roll = 0;
this.pos = vec3;
this.pos = [0,0,0];
this.vel = vec3;
this.vel = [0,0,0];
}
var speed = 0;
var spaceBarPressed = 0;
function handleKeys() {
if (currentlyPressedKeys[32]){
spaceBarPressed = 1;
loaded = 1;
}else
spaceBarPressed = 0;
if (currentlyPressedKeys[37] || currentlyPressedKeys[65]) {
// Left cursor key or A
circa.vel[0]-=circa.accel;
} else if (currentlyPressedKeys[39] || currentlyPressedKeys[68]) {
// Right cursor key or D
circa.vel[0]+=circa.accel;
} else {
circa.vel[0]=0.99*circa.vel[0];
}
if (currentlyPressedKeys[38] || currentlyPressedKeys[87]) {
// Up cursor key or W
circa.vel[2]-=circa.accel;
} else if (currentlyPressedKeys[40] || currentlyPressedKeys[83]) {
// Down cursor key
circa.vel[2]+=circa.accel;
} else {
circa.vel[2]=0.99*circa.vel[2];
}
if(circa.vel[0] > circa.maxVel)
circa.vel[0] = circa.maxVel;
if(circa.vel[0] < -circa.maxVel)
circa.vel[0] = -circa.maxVel;
if(circa.vel[2] > circa.maxVel)
circa.vel[2] = circa.maxVel;
if(circa.vel[2] < -circa.maxVel)
circa.vel[2] = circa.maxVel;
}
var worldVertexPositionBuffer = null;
var worldVertexTextureCoordBuffer = null;
var blockWallVertexPositionBuffer = null;
var blockWallVertextTextureCoordBuffer =null;
var blockCeilingVertexPositionBuffer = null;
var blockCeilingVertextTextureCoordBuffer =null;
var sphereVertexPositionBuffer = null;
var sphereVertexTextureCoordBuffer = null;
var sphereVertexIndexBuffer = null;
function Sphere(thisPos, thisRadius, thisSurfaceTexture, thisRotation){
this.maxVel = 3;
this.force =0.001;
this.friction = 0.0003;
this.speed = 0;
this.accel = 0.001;
this.mass = 1;
this.vel = [0,0];
this.pos = thisPos;
this.radius = thisRadius;
this.surfaceTexture = thisSurfaceTexture;
this.rotation = thisRotation;
}
Sphere.prototype.draw = function(){
mvPushMatrix();
mat4.translate(mvMatrix, this.pos);
mat4.translate(mvMatrix, [0,this.radius/2,0]);
mat4.multiply(mvMatrix, this.rotation);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textureList[0]);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER,sphereVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, sphereVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, sphereVertexTextureCoordBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, sphereVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphereVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, sphereVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
mvPopMatrix();
}
function loadSphereModel(){
var latitudeBands = 20;
var longitudeBands = 20;
var radius = 0.33;
var vertexPositionData = [];
var textureCoordData = [];
for (var latNumber = 0; latNumber <= latitudeBands; latNumber++) {
var theta = latNumber * Math.PI / latitudeBands;
var sinTheta = Math.sin(theta);
var cosTheta = Math.cos(theta);
for (var longNumber = 0; longNumber <= longitudeBands; longNumber++) {
var phi = longNumber * 2 * Math.PI / longitudeBands;
var sinPhi = Math.sin(phi);
var cosPhi = Math.cos(phi);
var x = cosPhi * sinTheta;
var y = cosTheta;
var z = sinPhi * sinTheta;
var u = 1 - (longNumber / longitudeBands);
var v = 1 - (latNumber / latitudeBands);
textureCoordData.push(u);
textureCoordData.push(v);
vertexPositionData.push(radius * x);
vertexPositionData.push(radius * y);
vertexPositionData.push(radius * z);
}
}
var indexData = [];
for (var latNumber = 0; latNumber < latitudeBands; latNumber++) {
for (var longNumber = 0; longNumber < longitudeBands; longNumber++) {
var first = (latNumber * (longitudeBands + 1)) + longNumber;
var second = first + longitudeBands + 1;
indexData.push(first);
indexData.push(second);
indexData.push(first + 1);
indexData.push(second);
indexData.push(second + 1);
indexData.push(first + 1);
}
}
sphereVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, sphereVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordData), gl.STATIC_DRAW);
sphereVertexTextureCoordBuffer.itemSize = 2;
sphereVertexTextureCoordBuffer.numItems = textureCoordData.length / 2;
sphereVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, sphereVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexPositionData), gl.STATIC_DRAW);
sphereVertexPositionBuffer.itemSize = 3;
sphereVertexPositionBuffer.numItems = vertexPositionData.length / 3;
sphereVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphereVertexIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), gl.STATIC_DRAW);
sphereVertexIndexBuffer.itemSize = 1;
sphereVertexIndexBuffer.numItems = indexData.length;
}
function Splash(thisTexture){
this.textureNumber = thisTexture;
}
Splash.prototype.draw = function() {
mvPushMatrix();
mat4.identity(mvMatrix);
var stretch = [4/3,1.0,1.0];
mat4.scale(mvMatrix,stretch);
mat4.translate(mvMatrix,[-0.5,-0.5,-1]);
gl.disable(gl.DEPTH_TEST);
drawTriangleStrip(textureList[this.textureNumber], splashVertexPositionBuffer, splashVertexTextureCoordBuffer, 4);
gl.enable(gl.DEPTH_TEST);
mvPopMatrix();
}
function Block(thisPos, thisCeilingTexture, thisWallTexture){
this.pos = thisPos;
this.ceilingTexture = thisCeilingTexture;
this.wallTexture = thisWallTexture;
}
Block.prototype.draw = function() {
mvPushMatrix();
mat4.translate(mvMatrix, this.pos);
drawTriangleStrip(this.ceilingTexture, blockCeilingVertexPositionBuffer, blockCeilingVertexTextureCoordBuffer, 4);
drawTriangleStrip(this.wallTexture, blockWallVertexPositionBuffer, blockWallVertexTextureCoordBuffer, 10);
mvPopMatrix();
}
var splashVertexPositionBuffer;
var splashVertexTextureCoordBuffer;
function loadSplash(){
var vertexCount = 4;
var vertexPositions = [
0,0,0,1,0,0,0,1,0,1,1,0
];
var vertexTextureCoords = [
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
];
splashVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, splashVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexPositions), gl.STATIC_DRAW);
splashVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, splashVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexTextureCoords), gl.STATIC_DRAW);
}
function loadBlockModel(){
var wallVertexCount=10;
var wallVertexPositions = [
0,0,0,0,1,0,1,0,0,1,1,0,1,0,1,1,1,1,0,0,1,0,1,1,0,0,0,0,1,0
];
var wallVertexTextureCoords = [
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
];
//sets the global buffers and creates them on graphics card
blockWallVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, blockWallVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(wallVertexPositions), gl.STATIC_DRAW);
blockWallVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, blockWallVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(wallVertexTextureCoords), gl.STATIC_DRAW);
var ceilingVertexCount=4;
var ceilingVertexPositions = [
0,1,0,1,1,0,0,1,1,1,1,1
];
var ceilingVertexTextureCoords = [
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
];
//sets the global buffers and creates them on graphics card
blockCeilingVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, blockCeilingVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(ceilingVertexPositions), gl.STATIC_DRAW);
blockCeilingVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, blockCeilingVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(ceilingVertexTextureCoords), gl.STATIC_DRAW);
}
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.perspective(55, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
mat4.translate(mvMatrix, [0, 0, -camera.distance]);
mat4.rotate(mvMatrix, degToRad(-camera.pitch), [1, 0, 0]);
//the player is a ways away from the origin
mat4.translate(mvMatrix, [-circa.pos[0], -circa.pos[1], -circa.pos[2]]);
gl.uniform1i(shaderProgram.samplerUniform, 0);
//draw Blocks
for(i =0; i<drawables.length; i++){
var thingToDraw = drawables[i];
thingToDraw.draw();
}
if(splashWaitTime>currentTime){
billBoard.draw();
}
}
splashWaitTime = 0;
var lastTime = 0;
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0 && splashWaitTime<currentTime) {
//update acceleration
var elapsed = timeNow - lastTime;
var intendedPosx = circa.pos[0];
var intendedPosz = circa.pos[2];
if (circa.vel[0] != 0) {
intendedPosx =circa.pos[0] + circa.vel[0] * elapsed;
}
if (circa.vel[2] != 0) {
intendedPosz =circa.pos[2] + circa.vel[2] * elapsed;
}
if(currentLevel.length>0){
var actualMovex = circa.pos[0];
var actualMovez = circa.pos[2];
if (currentLevel[Math.floor(intendedPosz)][
Math.floor(intendedPosx)] != 1){
actualMovex = intendedPosx;
actualMovez = intendedPosz;
}else
if (currentLevel[Math.floor(intendedPosz)][
Math.floor(circa.pos[0])] != 1){
actualMovez = intendedPosz;
circa.vel[0] = -circa.vel[0]*.5;
}else
if (currentLevel[Math.floor(circa.pos[2])][
Math.floor(intendedPosx)] != 1){
actualMovex = intendedPosx;
circa.vel[2] = -circa.vel[2]*.5;
}
var deltax = actualMovex - circa.pos[0];
var deltaz = actualMovez - circa.pos[2];
var radsRotated = Math.abs(circa.vel[0] + circa.vel[2])* elapsed/circa.radius;
var newRotation = mat4.create();
mat4.identity(newRotation);
mat4.rotate(newRotation,radsRotated,
[deltaz,0,-deltax]);
mat4.multiply(newRotation, circa.rotation, circa.rotation);
circa.pos = [actualMovex,0,actualMovez];
}
}
lastTime = timeNow;
if(circa.pos[0]>stageList[currentStage].boundary){
currentStage++;
var newStage = stageList[currentStage];
camera.pitch = newStage.cameraPitch;
camera.distance = newStage.cameraDistance;
circa.vel = [0,0,0];
newBGColor = stageList[currentStage].backGroundColor;
gl.clearColor(newBGColor[0],newBGColor[1] ,newBGColor[2] ,1.0);
billBoard = new Splash(newStage.splash);
splashWaitTime = currentTime + 4000;
}
}
var currentStage = 0;
var loopFinishTime = 0;
function loopAudio(){
if(loopFinishTime<currentTime){
var currentBGM = stageList[currentStage].backGroundMusic;
play_multi_sound(currentBGM);
loopFinishTime = currentTime + document.getElementById(currentBGM).duration * 1000;
}
}
var currentTime;
var loaded = 0;
var loadFinishTime = 0;
function tick() {
thistime = new Date();
currentTime = thistime.getTime();
handleKeys();
animate();
loopAudio();
drawScene();
requestAnimFrame(tick);
}
var drawables = new Array();
var pixel = new Array();
var currentLevel = new Array();;
function readBMP(){
var viewWidth = levelLoader.viewportWidth;
var viewHeight = levelLoader.viewportHeight;
for(i=0;i<viewWidth;i++){
currentLevel[i] = new Array();
pixel[i] = new Array();
var imgData = levelLoader.getImageData(0, i, viewHeight, viewWidth);
for(j=0;j<viewHeight;j++){
var red = imgData.data[j*4];
var green = imgData.data[j*4+1];
var blue = imgData.data[j*4+2];
var alpha = imgData.data[j*4+3];
pixel[i][j] = red * 0x10000 + green * 0x100 + blue;
if(green!=0xFF){
currentLevel[i][j] = 1;
}
else{
currentLevel[i][j] = 0;
}
}
}
}
function loadLevel(){
levelBMP = new Image();
currentLevel = new Array();
drawables = new Array();
drawables.push(circa);
levelBMP.onload = function () {
levelLoader.drawImage(levelBMP, 0, 0);
readBMP();
for(i=0; i<pixel.length;i++){
for(j=0; j<pixel[i].length; j++){
rgb = pixel[i][j];
var tempCeiling = textureList[0];
var tempWalls = textureList[0];
var defaultScheme = textureSchemes[0];
var tempCeiling = textureList[defaultScheme.ceilingTexture];
var tempWalls = textureList[defaultScheme.wallTexture];
for(k=0; k<textureSchemes.length; k++){
thisScheme = textureSchemes[k];
if( thisScheme.color == rgb){
tempCeiling=textureList[thisScheme.ceilingTexture]
tempWalls=textureList[thisScheme.wallTexture]
}
}
var blockPosition = [j,currentLevel[i][j]-1,i];
blockToDraw = new Block(blockPosition,tempCeiling,tempWalls);
//if(rgb != 0xffffff)
drawables.push(blockToDraw);
}
}
}
levelBMP.src = "lavaPit.bmp";
}
function Stage(music,pitch,distance,boundary,backColor){
this.backGroundMusic=music;
this.cameraPitch = pitch;
this.cameraDistance = distance;
this.boundary = boundary;
this.backGroundColor = backColor;
this.splash = 0;
}
var splashContext;
function webGLStart() {
var GLCanvas = document.getElementById("game-canvas");
var bitmapCanvas = document.getElementById("level-canvas");
levelLoader =bitmapCanvas.getContext("2d");
levelLoader.viewportWidth = bitmapCanvas.width;
levelLoader.viewportHeight = bitmapCanvas.height;
configTextureSchemes();
initGL(GLCanvas);
initShaders();
loadBlockModel();
loadSphereModel();
loadSplash();
initObjects();
initTextures();
loadLevel();
loadSplash();
drawables.push(circa);
stopWaiting =0;
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
thistime = new Date();
currentTime = thistime.getTime();
loadFinishTime = currentTime + 6000;
tick();
}
var channel_max = 10;
audiochannels = new Array();
for (a=0;a<channel_max;a++) {
audiochannels[a] = new Array();
audiochannels[a]['channel'] = new Audio();
audiochannels[a]['finished'] = -1;
}
function play_multi_sound(s) {
for (a=0;a<audiochannels.length;a++) {
thistime = new Date();
if (audiochannels[a]['finished'] < thistime.getTime()) { // is this channel finished?
audiochannels[a]['finished'] = thistime.getTime() + document.getElementById(s).duration*1000;
audiochannels[a]['channel'].src = document.getElementById(s).src;
audiochannels[a]['channel'].load();
audiochannels[a]['channel'].play();
break;
}
}
}
</script>
</head>
<body onload="webGLStart();" align="center" bgcolor = "111111">
<audio id="ludum1a" src="audio/ludum1a.mp3" preload = "auto"></audio>
<audio id="ludum18bit" src="audio/ludum18bit.wav" preload = "auto"></audio>
<audio id="ludum1-2" src="audio/ludum1-2.mp3" preload = "auto"></audio>
<audio id="ludum2-1" src="audio/ludum2-1.mp3" preload = "auto"></audio>
<canvas id="game-canvas" style="border: none;" width="800" height="600"></canvas>
<br>
<p style = "align:center;color:white">
Circa eighty-seven I was a primitive<br>
a speck in a mighty small world<br>
<br>
By '94, a game-gloved kid after everything-everything more<br>
As for girls and 1998, I'd be an angsty anxious maybe at a me sized angle<br>
<br>
Scorned in fact by twenty-hundred and i was rejection entire <br>
An effort to define. I thought I'd strike-out while the iron's hot!<br>
<br>
therefore revision and twenty-ten are synonomous<br>
Course that was all foolhardy, Older now in 2k12 a reverse Haiku<br>
<br>
a river of worlds of repeats<br>
a river<br>
Time is the river and I am the stone<br>
<canvas id="level-canvas" style="border: none;" width="20" height="20"></canvas>
</body>
</html>