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
| "use strict";
const { vec3 } = glMatrix;
var AppA = { canvas: null, gl: null, points: [], numTimesToSubdivide: 4,
init: function() { this.canvas = document.getElementById("gl-canvas-a"); this.gl = this.canvas.getContext("webgl2"); if (!this.gl) { alert("WebGL isn't available"); }
this.gl.enable(this.gl.DEPTH_TEST); this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
const sliderA = document.getElementById("subdivision-slider-a"); const sliderValueA = document.getElementById("slider-value-a"); sliderA.value = this.numTimesToSubdivide; sliderValueA.innerHTML = this.numTimesToSubdivide;
sliderA.oninput = (function() { this.numTimesToSubdivide = parseInt(sliderA.value); sliderValueA.innerHTML = this.numTimesToSubdivide; this.points = []; this.divideTriangle(vec3.fromValues(-1, -1, 0), vec3.fromValues(0, 1, 0), vec3.fromValues(1, -1, 0), this.numTimesToSubdivide); this.renderTriangles(); }).bind(this);
this.divideTriangle(vec3.fromValues(-1, -1, 0), vec3.fromValues(0, 1, 0), vec3.fromValues(1, -1, 0), this.numTimesToSubdivide);
this.gl.viewport(0, 0, this.canvas.width, this.canvas.height); this.gl.clearColor(1.0, 1.0, 1.0, 1.0);
var programA = initShaders(this.gl, "vertex-shader-a", "fragment-shader-a"); this.gl.useProgram(programA);
var vertexBufferA = this.gl.createBuffer(); this.gl.bindBuffer(this.gl.ARRAY_BUFFER, vertexBufferA); this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(this.points), this.gl.STATIC_DRAW);
var vPositionA = this.gl.getAttribLocation(programA, "vPosition"); this.gl.vertexAttribPointer(vPositionA, 3, this.gl.FLOAT, false, 0, 0); this.gl.enableVertexAttribArray(vPositionA);
this.renderTriangles(); },
triangle: function(a, b, c) { this.points.push(a[0], a[1], a[2]); this.points.push(b[0], b[1], b[2]); this.points.push(c[0], c[1], c[2]); },
divideTriangle: function(a, b, c, count) { if (count == 0) { this.triangle(a, b, c); } else { var ab = vec3.create(); vec3.lerp(ab, a, b, 0.5); var bc = vec3.create(); vec3.lerp(bc, b, c, 0.5); var ca = vec3.create(); vec3.lerp(ca, c, a, 0.5);
this.divideTriangle(a, ab, ca, count - 1); this.divideTriangle(b, bc, ab, count - 1); this.divideTriangle(c, ca, bc, count - 1); } },
renderTriangles: function() { this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT); this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(this.points), this.gl.STATIC_DRAW); this.gl.drawArrays(this.gl.TRIANGLES, 0, this.points.length / 3); } };
|