-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeed_breaker.js
545 lines (484 loc) · 19.7 KB
/
speed_breaker.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
/// <reference path="webgl.d.ts" />
// const buffers = initBuffers(gl);
//
// initBuffers
//
// Initialize the buffers we'll need. For this demo, we just
// have one object -- a simple three-dimensional cube.
//
let speed_breaker = class {
constructor(gl,pos,level)
{
// Create a buffer for the cube's vertex positions.
this.positionBuffer = gl.createBuffer();
this.rotation = 0;
this.pos = pos;
this.length = 2.0;
this.breadth = 3.0;
this.level = level;
this.height = 0.3*(this.level+1);
this.pos[1] = this.pos[1]+this.height + 14.0*this.level;
// Select the positionBuffer as the one to apply buffer
// operations to from here out.
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
// Now create an array of positions for the cube.
this.positions = [
-this.breadth, this.height*(this.level-1), this.length, // Front face
this.breadth, this.height*(this.level-1), this.length,
this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
-this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
-this.breadth, this.height*(this.level-1), -this.length, //Back Face
this.breadth, this.height*(this.level-1), -this.length,
this.breadth/1.44, this.height*(this.level+1), -this.length/2.4,
-this.breadth/1.44, this.height*(this.level+1), -this.length/2.4,
-this.breadth/1.44, this.height*(this.level+1), -this.length/2.4, //Top Face
this.breadth/1.44, this.height*(this.level+1), -this.length/2.4,
this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
-this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
-this.breadth, this.height*(this.level-1), -this.length, //Bottom Face
this.breadth, this.height*(this.level-1), -this.length,
this.breadth, this.height*(this.level-1), this.length,
-this.breadth, this.height*(this.level-1), this.length,
-this.breadth, this.height*(this.level-1), -this.length, //Left Face
-this.breadth/1.44, this.height*(this.level+1), -this.length/2.4,
-this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
-this.breadth, this.height*(this.level-1), this.length,
this.breadth, this.height*(this.level-1), -this.length, //Right Face
this.breadth/1.44, this.height*(this.level+1), -this.length/2.4,
this.breadth/1.44, this.height*(this.level+1), this.length/2.4,
this.breadth, this.height*(this.level-1), this.length,
];
this.positions_cloud = [
-this.breadth/1.44, -this.height*(this.level+1), this.length/2.4, // Front face
this.breadth/1.44, -this.height*(this.level+1), this.length/2.4,
this.breadth, -this.height*(this.level-1), this.length,
-this.breadth, -this.height*(this.level-1), this.length,
-this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4, //Back Face
this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4,
this.breadth, -this.height*(this.level-1), -this.length,
-this.breadth, -this.height*(this.level-1), -this.length,
-this.breadth, -this.height*(this.level-1), -this.length, //Top Face
this.breadth, -this.height*(this.level-1), -this.length,
this.breadth, -this.height*(this.level-1), this.length,
-this.breadth, -this.height*(this.level-1), this.length,
-this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4, //Bottom Face
this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4,
this.breadth/1.44, -this.height*(this.level+1), this.length/2.4,
-this.breadth/1.44, -this.height*(this.level+1), this.length/2.4,
-this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4, //Left Face
-this.breadth, -this.height*(this.level-1), -this.length,
-this.breadth, -this.height*(this.level-1), this.length,
-this.breadth/1.44, -this.height*(this.level+1), this.length/2.4,
this.breadth/1.44, -this.height*(this.level+1), -this.length/2.4, //Right Face
this.breadth, -this.height*(this.level-1), -this.length,
this.breadth, -this.height*(this.level-1), this.length,
this.breadth/1.44, -this.height*(this.level+1), this.length/2.4,
];
if(this.level+1 == 2)
{
this.positions = this.positions.concat(this.positions_cloud);
}
// Now pass the list of positions into WebGL to build the
// shape. We do this by creating a Float32Array from the
// JavaScript array, then use it to fill the current buffer.
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.positions), gl.STATIC_DRAW);
// Now set up the colors for the faces. We'll use solid colors
// for each face.
this.textureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer);
this.textureCoordinates = [
// Front
0.0, 1.0+this.level*2,
1.0+this.level, 1.0+this.level*2,
1.0+this.level, 0.0,
0.0, 0.0,
// Back
1.0+this.level, 1.0+this.level*2,
0.0, 1.0+this.level*2,
0.0, 0.0,
1.0+this.level, 0.0,
// Top
0.0, 0.0,
9.0, 0.0,
9.0, 3.0,
0.0, 3.0,
// Bottom
0.0, 0.0,
9.0, 0.0,
9.0, 3.0,
0.0, 3.0,
// Right
1.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0,
// Left
1.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0,
];
this.textureCoordinates_cloud = [
// Front
0.0, 3.0,
2.0, 3.0,
2.0, 0.0,
0.0, 0.0,
// Back
2.0, 3.0,
0.0, 3.0,
0.0, 0.0,
2.0, 0.0,
// Top
0.0, 0.0,
9.0, 0.0,
9.0, 3.0,
0.0, 3.0,
// Bottom
0.0, 0.0,
9.0, 0.0,
9.0, 3.0,
0.0, 3.0,
// Right
1.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0,
// Left
1.0, 1.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0,
];
if(this.level+1 == 2)
{
this.textureCoordinates = this.textureCoordinates.concat(this.textureCoordinates_cloud);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.textureCoordinates),
gl.STATIC_DRAW);
// Build the element array buffer; this specifies the indices
// into the vertex arrays for each face's vertices.
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
// This array defines each face as two triangles, using the
// indices into the vertex array to specify each triangle's
// position.
this.indices = [
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // Back
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // bottom
16, 17, 18, 16, 18, 19, // right
20, 21, 22, 20, 22, 23, // left
];
this.indices_cloud = [
0+24, 1+24, 2+24, 0+24, 2+24, 3+24, // front
4+24, 5+24, 6+24, 4+24, 6+24, 7+24, // Back
8+24, 9+24, 10+24, 8+24, 10+24, 11+24, // top
12+24, 13+24, 14+24, 12+24, 14+24, 15+24, // bottom
16+24, 17+24, 18+24, 16+24, 18+24, 19+24, // right
20+24, 21+24, 22+24, 20+24, 22+24, 23+24, // left
];
if(this.level+1 == 2)
{
this.indices = this.indices.concat(this.indices_cloud);
}
// Now send the element array to GL
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(this.indices), gl.STATIC_DRAW);
this.normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
this.vertexNormals = [
// Front
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Back
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
// Top
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
// Bottom
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
// Right
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
// Left
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
];
this.vertexNormals_cloud = [
// Front
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Back
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
// Top
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
// Bottom
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
// Right
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
// Left
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
];
if(this.level+1 == 2)
{
this.vertexNormals = this.vertexNormals.concat(this.vertexNormals_cloud);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertexNormals),
gl.STATIC_DRAW);
this.buffers = {
position: this.positionBuffer,
normal: this.normalBuffer,
texture: this.textureCoordBuffer,
indices: indexBuffer,
};
var url = ['./Textures/breaker.jpg','./Textures/cloud.jpg'];
if(this.level == 0)
this.texture = loadTexture(gl, url[0]);
else
this.texture = loadTexture(gl, url[1]);
}
tick()
{
this.pos[2] = this.pos[2] + GAME_SPEED;
if(this.pos[2]> 300)
{
this.pos[2] = END;
}
// console.log(this.pos);
}
draw(gl, projectionMatrix, programInfoTexture, deltaTime)
{
const modelViewMatrix = mat4.create();
mat4.translate(
modelViewMatrix,
modelViewMatrix,
this.pos
);
//Write your code to Rotate the cube here//
// mat4.rotate(modelViewMatrix,modelViewMatrix,cubeRotation,[1,1,1]);
// Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute
{
const numComponents = 3;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.position);
gl.vertexAttribPointer(
programInfoTexture.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfoTexture.attribLocations.vertexPosition);
}
// Tell WebGL how to pull out the colors from the color buffer
// into the vertexColor attribute.
{
const numComponents = 2;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.texture);
// console.log(programInfoTexture.attribLocations.textureCoord);
gl.vertexAttribPointer(
programInfoTexture.attribLocations.textureCoord,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfoTexture.attribLocations.textureCoord);
}
// Tell WebGL which indices to use to index the vertices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.buffers.indices);
// Tell WebGL to use our program when drawing
gl.useProgram(programInfoTexture.program);
// Set the shader uniforms
gl.uniformMatrix4fv(
programInfoTexture.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
programInfoTexture.uniformLocations.modelViewMatrix,
false,
modelViewMatrix);
// Specify the texture to map onto the faces.
// Tell WebGL we want to affect texture unit 0
gl.activeTexture(gl.TEXTURE0);
// Bind the texture to texture unit 0
// var image = new Image();
// image.src = './Textures/wall.jpg';
gl.bindTexture(gl.TEXTURE_2D, this.texture);
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE,
// new Uint8Array([0, 0, 255, 255]));
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
// gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// Tell the shader we bound the texture to texture unit 0
gl.uniform1i(programInfoTexture.uniformLocations.uSampler, 0);
{
var vertexCount = 36;
if(this.level == 1)
{
vertexCount = 72;
}
const type = gl.UNSIGNED_SHORT;
const offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
// Update the rotation for the next draw
// cubeRotation += deltaTime;
}
drawLight(gl, projectionMatrix, programInfoTexture, deltaTime)
{
const modelViewMatrix = mat4.create();
mat4.translate(
modelViewMatrix,
modelViewMatrix,
this.pos
);
//Write your code to Rotate the cube here//
// mat4.rotate(modelViewMatrix,modelViewMatrix,cubeRotation,[1,1,1]);
// Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute
{
const numComponents = 3;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.position);
gl.vertexAttribPointer(
programInfoTexture.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfoTexture.attribLocations.vertexPosition);
}
// Tell WebGL how to pull out the colors from the color buffer
// into the vertexColor attribute.
{
const numComponents = 2;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.texture);
// console.log(programInfoTexture.attribLocations.textureCoord);
gl.vertexAttribPointer(
programInfoTexture.attribLocations.textureCoord,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfoTexture.attribLocations.textureCoord);
}
{
const numComponents = 3;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.normal);
gl.vertexAttribPointer(
programInfoTexture.attribLocations.vertexNormal,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfoTexture.attribLocations.vertexNormal);
}
const normalMatrix = mat4.create();
mat4.invert(normalMatrix, modelViewMatrix);
mat4.transpose(normalMatrix, normalMatrix);
// Tell WebGL which indices to use to index the vertices
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.buffers.indices);
// Tell WebGL to use our program when drawing
gl.useProgram(programInfoTexture.program);
// Set the shader uniforms
gl.uniformMatrix4fv(
programInfoTexture.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
programInfoTexture.uniformLocations.modelViewMatrix,
false,
modelViewMatrix);
gl.uniformMatrix4fv(
programInfoTexture.uniformLocations.normalMatrix,
false,
normalMatrix);
// Specify the texture to map onto the faces.
// Tell WebGL we want to affect texture unit 0
gl.activeTexture(gl.TEXTURE0);
// Bind the texture to texture unit 0
// var image = new Image();
// image.src = './Textures/wall.jpg';
gl.bindTexture(gl.TEXTURE_2D, this.texture);
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE,
// new Uint8Array([0, 0, 255, 255]));
// gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
// gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// Tell the shader we bound the texture to texture unit 0
gl.uniform1i(programInfoTexture.uniformLocations.uSampler, 0);
{
var vertexCount = 36;
if(this.level == 1)
{
vertexCount = 72;
}
const type = gl.UNSIGNED_SHORT;
const offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
// Update the rotation for the next draw
// cubeRotation += deltaTime;
}
}