Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowles committed Nov 5, 2013
1 parent 7993266 commit 596e605
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 120 deletions.
18 changes: 0 additions & 18 deletions gl/bloom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
};
};
Expand Down
139 changes: 70 additions & 69 deletions gl/classic-noise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
};
2 changes: 1 addition & 1 deletion gl/curves.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 15 additions & 23 deletions gl/geom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions gl/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions gl/particles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion gl/pingpong.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion gl/shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion gl/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<script type="text/javascript" src="gl/curves.js"></script>

<script type="text/javascript" src="anim.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="main.min.js"></script>

<!-- DISPLACEMENT SHADERS -->
<script id="displace-fs" type="x-shader/x-fragment">
Expand Down Expand Up @@ -594,5 +594,7 @@
oncanplay="_audioReady = true;"
onplaying="_soundtrack._startTime = new Date().getTime() - _soundtrack.currentTime*1000; Lab.accum('tick', tick)">
</audio>
<script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">var pageTracker = _gat._getTracker("UA-3537030-1"); pageTracker._initData(); pageTracker._trackPageview();</script>
</body>
</html>

0 comments on commit 596e605

Please sign in to comment.