From 596e6058caa68bfd7c3afaa5be55909bf3ae34d5 Mon Sep 17 00:00:00 2001 From: Jeremy Cowles Date: Tue, 5 Nov 2013 11:02:15 -0800 Subject: [PATCH] More code cleanup --- gl/bloom.js | 18 ------ gl/classic-noise.js | 139 ++++++++++++++++++++++---------------------- gl/curves.js | 2 +- gl/geom.js | 38 +++++------- gl/grid.js | 4 +- gl/particles.js | 3 - gl/pingpong.js | 1 - gl/shader.js | 1 - gl/texture.js | 1 - index.html | 4 +- 10 files changed, 91 insertions(+), 120 deletions(-) diff --git a/gl/bloom.js b/gl/bloom.js index 486a16d..34312f6 100644 --- a/gl/bloom.js +++ b/gl/bloom.js @@ -76,15 +76,6 @@ Lab.Bloom = function(width, height) { gl.uniform1i(shader.prog.huge, (i > 0 && horizontal) ? 1 : 0); var ppi = this.pass0[i]; ppi.bind(gl, shader.prog.tex0); - /* - gl.viewport(0,0,ppi.width,ppi.height); - gl.framebufferTexture2D(gl.FRAMEBUFFER, - gl.COLOR_ATTACHMENT0, - gl.TEXTURE_2D, - ppi._dst, - 0); - gl.bindTexture(gl.TEXTURE_2D, ppi._src); - */ var offset = 1.2 / ppi.height; if (horizontal) @@ -124,15 +115,10 @@ Lab.Bloom = function(width, height) { gl.uniform1i(texAttrib[i], i); gl.activeTexture(texId[i]); gl.bindTexture(gl.TEXTURE_2D, this.pass0[i]._src); - //gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); - //gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); - //gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); - //gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } gl.uniform1i(shader.prog.texCount, 4); gl.uniform1f(shader.prog.cutoff, 0.0); - //gl.clear(gl.COLOR_BIT | gl.DEPTH_BIT); this._drawQuad(gl, this.blt.shader); this.pass0[0].unbind(gl); @@ -168,7 +154,6 @@ Lab.Bloom = function(width, height) { Lab.trace("Bloom-DownSample", function() { _this.downSample(gl); for (var i = 0; i < _this.pass0.length; i++) { _this.pass0[i].swap(); } - //gl.uniform1f(_this.blt.shader.prog.stretchFactor, 1.0); }); if (this.doBlur) { @@ -201,10 +186,7 @@ Lab.Bloom = function(width, height) { gl.uniform1i(shd.prog.tex0, 0); gl.uniform1i(shd.prog.combine, 0); gl.bindTexture(gl.TEXTURE_2D, destTexture); - //gl.bindTexture(gl.TEXTURE_2D, this.pass0[this.pass0.length-1]._src) this._drawQuad(gl, shd); - //throw("debug"); - // debug } }; }; diff --git a/gl/classic-noise.js b/gl/classic-noise.js index 658a8d0..a35e6b0 100644 --- a/gl/classic-noise.js +++ b/gl/classic-noise.js @@ -12,19 +12,20 @@ * It is assumed to have a random() method. */ var ClassicalNoise = function(r) { // Classic Perlin noise in 3D, for comparison - if (r == undefined) r = Math; - this.grad3 = [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0], + if (r == undefined) + r = Math; + this.grad3 = [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0], [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1], [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]]; - this.p = []; - for (var i=0; i<256; i++) { - this.p[i] = Math.floor(r.random()*256); - } - // To remove the need for index wrapping, double the permutation table length - this.perm = []; - for(var i=0; i<512; i++) { - this.perm[i]=this.p[i & 255]; - } + this.p = []; + for (var i=0; i<256; i++) { + this.p[i] = Math.floor(r.random()*256); + } + // To remove the need for index wrapping, double the permutation table length + this.perm = []; + for(var i=0; i<512; i++) { + this.perm[i]=this.p[i & 255]; + } }; ClassicalNoise.prototype.dot = function(g, x, y, z) { @@ -41,63 +42,63 @@ ClassicalNoise.prototype.fade = function(t) { // Classic Perlin noise, 3D version ClassicalNoise.prototype.noise = function(x, y, z) { - // Find unit grid cell containing point - var X = Math.floor(x); - var Y = Math.floor(y); - var Z = Math.floor(z); - - // Get relative xyz coordinates of point within that cell - x = x - X; - y = y - Y; - z = z - Z; - - // Wrap the integer cells at 255 (smaller integer period can be introduced here) - X = X & 255; - Y = Y & 255; - Z = Z & 255; - - // Calculate a set of eight hashed gradient indices - var gi000 = this.perm[X+this.perm[Y+this.perm[Z]]] % 12; - var gi001 = this.perm[X+this.perm[Y+this.perm[Z+1]]] % 12; - var gi010 = this.perm[X+this.perm[Y+1+this.perm[Z]]] % 12; - var gi011 = this.perm[X+this.perm[Y+1+this.perm[Z+1]]] % 12; - var gi100 = this.perm[X+1+this.perm[Y+this.perm[Z]]] % 12; - var gi101 = this.perm[X+1+this.perm[Y+this.perm[Z+1]]] % 12; - var gi110 = this.perm[X+1+this.perm[Y+1+this.perm[Z]]] % 12; - var gi111 = this.perm[X+1+this.perm[Y+1+this.perm[Z+1]]] % 12; - - // The gradients of each corner are now: - // g000 = grad3[gi000]; - // g001 = grad3[gi001]; - // g010 = grad3[gi010]; - // g011 = grad3[gi011]; - // g100 = grad3[gi100]; - // g101 = grad3[gi101]; - // g110 = grad3[gi110]; - // g111 = grad3[gi111]; - // Calculate noise contributions from each of the eight corners - var n000= this.dot(this.grad3[gi000], x, y, z); - var n100= this.dot(this.grad3[gi100], x-1, y, z); - var n010= this.dot(this.grad3[gi010], x, y-1, z); - var n110= this.dot(this.grad3[gi110], x-1, y-1, z); - var n001= this.dot(this.grad3[gi001], x, y, z-1); - var n101= this.dot(this.grad3[gi101], x-1, y, z-1); - var n011= this.dot(this.grad3[gi011], x, y-1, z-1); - var n111= this.dot(this.grad3[gi111], x-1, y-1, z-1); - // Compute the fade curve value for each of x, y, z - var u = this.fade(x); - var v = this.fade(y); - var w = this.fade(z); - // Interpolate along x the contributions from each of the corners - var nx00 = this.mix(n000, n100, u); - var nx01 = this.mix(n001, n101, u); - var nx10 = this.mix(n010, n110, u); - var nx11 = this.mix(n011, n111, u); - // Interpolate the four results along y - var nxy0 = this.mix(nx00, nx10, v); - var nxy1 = this.mix(nx01, nx11, v); - // Interpolate the two last results along z - var nxyz = this.mix(nxy0, nxy1, w); + // Find unit grid cell containing point + var X = Math.floor(x); + var Y = Math.floor(y); + var Z = Math.floor(z); - return nxyz; + // Get relative xyz coordinates of point within that cell + x = x - X; + y = y - Y; + z = z - Z; + + // Wrap the integer cells at 255 (smaller integer period can be introduced here) + X = X & 255; + Y = Y & 255; + Z = Z & 255; + + // Calculate a set of eight hashed gradient indices + var gi000 = this.perm[X+this.perm[Y+this.perm[Z]]] % 12; + var gi001 = this.perm[X+this.perm[Y+this.perm[Z+1]]] % 12; + var gi010 = this.perm[X+this.perm[Y+1+this.perm[Z]]] % 12; + var gi011 = this.perm[X+this.perm[Y+1+this.perm[Z+1]]] % 12; + var gi100 = this.perm[X+1+this.perm[Y+this.perm[Z]]] % 12; + var gi101 = this.perm[X+1+this.perm[Y+this.perm[Z+1]]] % 12; + var gi110 = this.perm[X+1+this.perm[Y+1+this.perm[Z]]] % 12; + var gi111 = this.perm[X+1+this.perm[Y+1+this.perm[Z+1]]] % 12; + + // The gradients of each corner are now: + // g000 = grad3[gi000]; + // g001 = grad3[gi001]; + // g010 = grad3[gi010]; + // g011 = grad3[gi011]; + // g100 = grad3[gi100]; + // g101 = grad3[gi101]; + // g110 = grad3[gi110]; + // g111 = grad3[gi111]; + // Calculate noise contributions from each of the eight corners + var n000= this.dot(this.grad3[gi000], x, y, z); + var n100= this.dot(this.grad3[gi100], x-1, y, z); + var n010= this.dot(this.grad3[gi010], x, y-1, z); + var n110= this.dot(this.grad3[gi110], x-1, y-1, z); + var n001= this.dot(this.grad3[gi001], x, y, z-1); + var n101= this.dot(this.grad3[gi101], x-1, y, z-1); + var n011= this.dot(this.grad3[gi011], x, y-1, z-1); + var n111= this.dot(this.grad3[gi111], x-1, y-1, z-1); + // Compute the fade curve value for each of x, y, z + var u = this.fade(x); + var v = this.fade(y); + var w = this.fade(z); + // Interpolate along x the contributions from each of the corners + var nx00 = this.mix(n000, n100, u); + var nx01 = this.mix(n001, n101, u); + var nx10 = this.mix(n010, n110, u); + var nx11 = this.mix(n011, n111, u); + // Interpolate the four results along y + var nxy0 = this.mix(nx00, nx10, v); + var nxy1 = this.mix(nx01, nx11, v); + // Interpolate the two last results along z + var nxyz = this.mix(nxy0, nxy1, w); + + return nxyz; }; diff --git a/gl/curves.js b/gl/curves.js index 9d5ce07..d6637f9 100644 --- a/gl/curves.js +++ b/gl/curves.js @@ -36,7 +36,7 @@ Lab.Curves = function () { this.quad = Lab.Geom.makeQuad2D(gl); - this.curvesTex = new Lab.PingPongBuffer(this.curveCount, 2, null); //updateShader.prog.tex0); + this.curvesTex = new Lab.PingPongBuffer(this.curveCount, 2, null); this.curvesTex.init(gl, new Float32Array(this.cvs.concat(this.cds))); // temp: move _dst -> _src diff --git a/gl/geom.js b/gl/geom.js index 02bec22..acd09ac 100644 --- a/gl/geom.js +++ b/gl/geom.js @@ -118,21 +118,19 @@ Lab.Geom.Cubes = function() { ]; this.init = function(gl) { - if (this.buff) gl.deleteBuffer(this.buff); + if (this.buff) + gl.deleteBuffer(this.buff); + this.buff = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, this.buff); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.all), gl.STATIC_DRAW); - //gl.bufferData(gl.ARRAY_BUFFER, - // new Float32Array(this.points.concat(this.normals, - // this.colors, - // this.uvs)), - // gl.STATIC_DRAW); + + gl.bindBuffer(gl.ARRAY_BUFFER, this.buff); + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.all), gl.STATIC_DRAW); + this.buff.itemSize = 3; - this.buff.length = this.points.length/3; + this.buff.length = this.points.length/3; this.buff.normStart = this.points.length; this.buff.colorStart = this.points.length+this.normals.length; this.buff.uvStart = this.buff.colorStart + this.colors.length; - //delete this.points; }; this.draw = function(gl, prog) { @@ -142,35 +140,30 @@ Lab.Geom.Cubes = function() { var size = (3+3+4+2) * 4; if ('vertex' in prog) { gl.vertexAttribPointer(prog.vertex, this.buff.itemSize, gl.FLOAT, false, size, 0); - //gl.vertexAttribPointer(prog.vertex, this.buff.itemSize, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(prog.vertex); } if ('normal' in prog){ gl.vertexAttribPointer(prog.normal, this.buff.itemSize, gl.FLOAT, false, size, 3*4); - //gl.vertexAttribPointer(prog.normal, this.buff.itemSize, gl.FLOAT, false, 0, 4*this.buff.normStart); gl.enableVertexAttribArray(prog.normal); } if ('color' in prog) { gl.vertexAttribPointer(prog.color, this.buff.itemSize+1, gl.FLOAT, false, size, (3+3)*4); - //gl.vertexAttribPointer(prog.color, this.buff.itemSize+1, gl.FLOAT, false, 0, 4*this.buff.colorStart); gl.enableVertexAttribArray(prog.color); } if ('uv' in prog){ gl.vertexAttribPointer(prog.uv, this.buff.itemSize-1, gl.FLOAT, false, size, (3+3+4)*4); - //gl.vertexAttribPointer(prog.uv, this.buff.itemSize-1, gl.FLOAT, false, 0, 4*this.buff.uvStart); gl.enableVertexAttribArray(prog.uv); } gl.drawArrays(gl.TRIANGLES, 0, this.primCount); - //gl.drawArrays(gl.TRIANGLES, 0, this.buff.length); }; this.push = function(x, y, z, size, maxX, maxY) { // the x,y coords dictate the pseudo screen space location - //console.log(""+x+","+y+","+maxX+","+maxY+","+(x/maxX)+","+(y/maxY)); + //console.log(""+x+","+y+","+maxX+","+maxY+","+(x/maxX)+","+(y/maxY)); // loop over each face for (var face = 0; face < 6; face++) { @@ -227,18 +220,17 @@ Lab.Geom.Lines = function() { this.buff = null; this.init = function(gl) { - if (this.buff) gl.deleteBuffer(this.buff); - this.buff = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, this.buff); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.points), gl.STATIC_DRAW); + if (this.buff) gl.deleteBuffer(this.buff); + this.buff = gl.createBuffer(); + gl.bindBuffer(gl.ARRAY_BUFFER, this.buff); + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.points), gl.STATIC_DRAW); this.buff.itemSize = 3; - this.buff.length = this.points.length / 3 ; - //delete this.points; + this.buff.length = this.points.length / 3 ; }; this.draw = function(gl, prog) { gl.bindBuffer(gl.ARRAY_BUFFER, this.buff); - gl.vertexAttribPointer(prog.vertex, this.buff.itemSize, gl.FLOAT, false, 0, 0); + gl.vertexAttribPointer(prog.vertex, this.buff.itemSize, gl.FLOAT, false, 0, 0); gl.vertexAttribPointer(prog.normal, this.buff.itemSize, gl.FLOAT, false, 0, 0); gl.drawArrays(gl.LINES, 0, this.buff.length); }; diff --git a/gl/grid.js b/gl/grid.js index cf55334..373a320 100644 --- a/gl/grid.js +++ b/gl/grid.js @@ -11,11 +11,11 @@ Lab.Grid = function(x1, z1, x2, z2, y) { this.init = function(gl) { for (var x = this.x1; x <= this.x2; x++) { this.lines.push(x, this.z1, x, this.z2, this.y); - } + } for (var z = this.z1; z <= this.z2; z++) { this.lines.push(this.x1, z, this.x2, z, this.y); } - this.lines.init(gl); + this.lines.init(gl); }; this.draw = function(gl, prog) { diff --git a/gl/particles.js b/gl/particles.js index 19fac41..6dcbdf0 100644 --- a/gl/particles.js +++ b/gl/particles.js @@ -36,9 +36,6 @@ Lab.ParticleField = function(width, height) { this.pos.init(gl, posShader, positions); this.vel = new Lab.Field(this.width, this.height); this.vel.init(gl, velShader, velocities); - //this.accel = new Lab.Field(this.width, this.height); - //this.accel.init(gl, accelShader, accelerations); - //accelShader.checkVars([ "mode", "tex0", "tex1", "tex2" ], [ "vertex" ]); this.accel = new Lab.Fbo(this.width, this.height, gl.NEAREST); this.accel.init(gl, accelerations); diff --git a/gl/pingpong.js b/gl/pingpong.js index 0da99af..cca06ce 100644 --- a/gl/pingpong.js +++ b/gl/pingpong.js @@ -39,7 +39,6 @@ Lab.PingPongBuffer = function (width, height, uniformTex0, interp, format) { // bind the dest texture this.fbo.bind(gl, setViewport); - //gl.bindTexture(gl.TEXTURE_2D, this._dst); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._dst, 0); if (this.inputSrc) { diff --git a/gl/shader.js b/gl/shader.js index 603d3fc..74ed687 100644 --- a/gl/shader.js +++ b/gl/shader.js @@ -79,7 +79,6 @@ Lab.Shader = function(fsId, vsId, uniforms, attribs) { for (i in this._attribs) { var attrib = this._attribs[i]; this.prog[attrib] = gl.getAttribLocation(this.prog, attrib); - //gl.enableVertexAttribArray(this.prog[attrib]) len++; // incase anyone is screwing with the array prototype if (len == this._attribs.length) break; diff --git a/gl/texture.js b/gl/texture.js index 137e232..1d48cd3 100644 --- a/gl/texture.js +++ b/gl/texture.js @@ -25,7 +25,6 @@ Lab.Texture = function(width, height) { gl.bindTexture(gl.TEXTURE_2D, this.tex); // target levels, intformat, width, height, border, format, type, data - //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.FLOAT, data); gl.texImage2D(gl.TEXTURE_2D, 0, intformat, this.width, this.height, 0, format, type, data); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, this.magFilter); diff --git a/index.html b/index.html index 38125df..017a7ee 100644 --- a/index.html +++ b/index.html @@ -24,7 +24,7 @@ - + +