|
4 | 4 | <title>Learning WebGL — Mandelbrot shader on a spinning cube</title> |
5 | 5 | <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> |
6 | 6 |
|
7 | | -<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script> |
8 | | -<script type="text/javascript" src="webgl-utils.js"></script> |
| 7 | +<script type="text/javascript" src="../js/gl-matrix-2.2.1.js"></script> |
| 8 | +<script type="text/javascript" src="../js/webgl-utils.js"></script> |
9 | 9 |
|
10 | 10 |
|
11 | 11 | <script id="color-shader-fs" type="x-shader/x-fragment"> |
|
125 | 125 | var gl; |
126 | 126 | function initGL(canvas) { |
127 | 127 | try { |
128 | | - gl = canvas.getContext("experimental-webgl"); |
| 128 | + gl = canvas.getContext("webgl"); |
129 | 129 | gl.viewportWidth = canvas.width; |
130 | 130 | gl.viewportHeight = canvas.height; |
131 | 131 | } catch(e) { |
|
221 | 221 |
|
222 | 222 | function mvPushMatrix() { |
223 | 223 | var copy = mat4.create(); |
224 | | - mat4.set(mvMatrix, copy); |
| 224 | + // Update: mat4.set(mvMatrix, copy); mat4.set() was removed from gl-matrix, use mat4.copy(). |
| 225 | + mat4.copy(copy, mvMatrix); |
225 | 226 | mvMatrixStack.push(copy); |
226 | 227 | } |
227 | 228 |
|
|
244 | 245 | } |
245 | 246 |
|
246 | 247 |
|
247 | | - var cubes = Array(); |
| 248 | + var cubes = []; |
248 | 249 | function MandelbrotCube(xPos, yPos, zPos, xVel, yVel, zVel, xRot, yRot, zRot) { |
249 | 250 | this.xPos = xPos; |
250 | 251 | this.yPos = yPos; |
|
307 | 308 | } |
308 | 309 | } |
309 | 310 | } |
310 | | - } |
| 311 | + }; |
311 | 312 |
|
312 | 313 |
|
313 | 314 | MandelbrotCube.prototype.draw = function() { |
314 | 315 | setCurrentProgram(mandelbrotProgram); |
315 | 316 |
|
316 | 317 | mat4.identity(mvMatrix); |
317 | 318 |
|
318 | | - mat4.translate(mvMatrix, [this.xPos, this.yPos, this.zPos]); |
| 319 | + mat4.translate(mvMatrix, mvMatrix, [this.xPos, this.yPos, this.zPos]); |
319 | 320 |
|
320 | | - mat4.rotate(mvMatrix, degToRad(this.xRot), [1, 0, 0]); |
321 | | - mat4.rotate(mvMatrix, degToRad(this.yRot), [0, 1, 0]); |
322 | | - mat4.rotate(mvMatrix, degToRad(this.zRot), [0, 0, 1]); |
| 321 | + mat4.rotate(mvMatrix, mvMatrix, degToRad(this.xRot), [1, 0, 0]); |
| 322 | + mat4.rotate(mvMatrix, mvMatrix, degToRad(this.yRot), [0, 1, 0]); |
| 323 | + mat4.rotate(mvMatrix, mvMatrix, degToRad(this.zRot), [0, 0, 1]); |
323 | 324 |
|
324 | 325 | gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); |
325 | 326 | gl.vertexAttribPointer(mandelbrotProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); |
|
328 | 329 | gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
329 | 330 | var positions = []; |
330 | 331 | for (var i = 0; i < 6; i++) { |
331 | | - positions.push(-2.2 / this.zooms[i] + this.centerOffsetsX[i], -1.2 / this.zooms[i] + this.centerOffsetsY[i]);; |
| 332 | + positions.push(-2.2 / this.zooms[i] + this.centerOffsetsX[i], -1.2 / this.zooms[i] + this.centerOffsetsY[i]); |
332 | 333 | positions.push( 0.7 / this.zooms[i] + this.centerOffsetsX[i], -1.2 / this.zooms[i] + this.centerOffsetsY[i]); |
333 | | - positions.push( 0.7 / this.zooms[i] + this.centerOffsetsX[i], 1.2 / this.zooms[i] + this.centerOffsetsY[i]) |
| 334 | + positions.push( 0.7 / this.zooms[i] + this.centerOffsetsX[i], 1.2 / this.zooms[i] + this.centerOffsetsY[i]); |
334 | 335 | positions.push(-2.2 / this.zooms[i] + this.centerOffsetsX[i], 1.2 / this.zooms[i] + this.centerOffsetsY[i]) |
335 | 336 | } |
336 | 337 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); |
|
341 | 342 | gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); |
342 | 343 |
|
343 | 344 | gl.deleteBuffer(positionBuffer); |
344 | | - } |
| 345 | + }; |
345 | 346 |
|
346 | 347 |
|
347 | 348 | var boundingBoxVertexPositionBuffer; |
|
376 | 377 | -5, 5, -10, |
377 | 378 | -5, 5, -25, |
378 | 379 | 5, 5, -25, |
379 | | - 5, 5, -10, |
| 380 | + 5, 5, -10 |
380 | 381 | ]; |
381 | 382 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); |
382 | 383 | boundingBoxVertexPositionBuffer.itemSize = 3; |
|
408 | 409 | 0.3, 0.3, 0.3, 1, |
409 | 410 | 0.3, 0.3, 0.3, 1, |
410 | 411 | 0.3, 0.3, 0.3, 1, |
411 | | - 0.3, 0.3, 0.3, 1, |
| 412 | + 0.3, 0.3, 0.3, 1 |
412 | 413 | ]; |
413 | 414 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); |
414 | 415 | boundingBoxVertexColorBuffer.itemSize = 4; |
|
464 | 465 | -1.0, -1.0, -1.0, |
465 | 466 | -1.0, -1.0, 1.0, |
466 | 467 | -1.0, 1.0, 1.0, |
467 | | - -1.0, 1.0, -1.0, |
| 468 | + -1.0, 1.0, -1.0 |
468 | 469 | ]; |
469 | 470 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexPositions), gl.STATIC_DRAW); |
470 | 471 | cubeVertexPositionBuffer.itemSize = 3; |
|
479 | 480 | 12, 13, 14, 12, 14, 15, // Bottom face |
480 | 481 | 16, 17, 18, 16, 18, 19, // Right face |
481 | 482 | 20, 21, 22, 20, 22, 23 // Left face |
482 | | - ] |
| 483 | + ]; |
483 | 484 | gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(vertexIndices), gl.STATIC_DRAW); |
484 | 485 | cubeVertexIndexBuffer.itemSize = 1; |
485 | 486 | cubeVertexIndexBuffer.numItems = 36; |
|
490 | 491 | gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); |
491 | 492 | gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
492 | 493 |
|
493 | | - mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix); |
| 494 | + // Update: mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix); mat4.perspective() API has changed. |
| 495 | + mat4.perspective (pMatrix, 45.0, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0); |
| 496 | + |
| 497 | + // Update: mat4.translate(mvMatrix, [0, 0, -6]); mat4.translate() API has changed to mat4.translate(out, a, v) |
| 498 | + // where out is the receiving matrix, a is the matrix to translate, and v is the vector to translate by. z altered to |
| 499 | + // approximate original scene. |
| 500 | + mat4.translate(mvMatrix, mvMatrix, [0, 0, -4.7]); |
494 | 501 |
|
495 | 502 | for (var i in cubes) { |
496 | 503 | cubes[i].draw(); |
|
500 | 507 | mat4.identity(mvMatrix); |
501 | 508 |
|
502 | 509 | gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexPositionBuffer); |
| 510 | + |
| 511 | + // Update: mat4.translate(mvMatrix, mvMatrix, [0, 0, 6]); added to more closely approximate original scene. |
| 512 | + mat4.translate(mvMatrix, mvMatrix, [0, 0, 6]); |
| 513 | + |
503 | 514 | gl.vertexAttribPointer(colorProgram.vertexPositionAttribute, boundingBoxVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); |
504 | 515 |
|
505 | 516 | gl.bindBuffer(gl.ARRAY_BUFFER, boundingBoxVertexColorBuffer); |
|
0 commit comments