Skip to content

Commit

Permalink
Refactor enable/disable interface and make it cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ttencate committed Aug 20, 2012
1 parent 13a44aa commit 5da1c90
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
5 changes: 2 additions & 3 deletions examples/cube.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ <h1>Gladder : Cube example</h1>
// callCallback: function(msg) { console.info(msg); },
});

gla.enableDepthTest();
gla.enableCullFace();
gla.enable(gla.Capability.DEPTH_TEST, gla.Capability.CULL_FACE);

var program = new gla.Program({
vertexShader: "vertex-shader",
Expand Down Expand Up @@ -92,7 +91,7 @@ <h1>Gladder : Cube example</h1>
program: program,
uniforms: { transform: transform },
attributes: { position: buffer },
type: gla.draw.Mode.TRIANGLES,
type: gla.DrawMode.TRIANGLES,
count: buffer.numItems,
});
});
Expand Down
4 changes: 2 additions & 2 deletions examples/framebuffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ <h1>Gladder : Framebuffer example</h1>
uniforms: { sampler: 0, offset: [0, 1 / gla.canvas.height] },
attributes: { position: unitQuadBuffer },
count: unitQuadBuffer.numItems,
mode: gla.draw.Mode.TRIANGLE_FAN,
mode: gla.DrawMode.TRIANGLE_FAN,
});
});
});
Expand All @@ -162,7 +162,7 @@ <h1>Gladder : Framebuffer example</h1>
uniforms: { sampler: 0, offset: [1 / gla.canvas.width, 0] },
attributes: { position: unitQuadBuffer },
count: unitQuadBuffer.numItems,
mode: gla.draw.Mode.TRIANGLE_FAN,
mode: gla.DrawMode.TRIANGLE_FAN,
});
});
});
Expand Down
60 changes: 37 additions & 23 deletions src/gladder.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,44 @@ function Gladder(args) {
gl.clear(bits);
};

////////////////
// STATE BITS //
////////////////
//////////////////
// CAPABILITIES //
//////////////////

this.Capability = {
BLEND: gl.BLEND,
CULL_FACE: gl.CULL_FACE,
DEPTH_TEST: gl.DEPTH_TEST,
DITHER: gl.DITHER,
POLYGON_OFFSET_FILL: gl.POLYGON_OFFSET_FILL,
SAMPLE_ALPHA_TO_COVERAGE: gl.SAMPLE_ALPHA_TO_COVERAGE,
SAMPLE_COVERAGE: gl.SAMPLE_COVERAGE,
SCISSOR_TEST: gl.SCISSOR_TEST,
STENCIL_TEST: gl.STENCIL_TEST,
}

// TODO cache state bit values
function createStateBitFunc(funcName, bitName) {
gla["enable" + funcName] = function(enable) {
if (enable || enable === undefined) {
gl.enable(gl[bitName]);
} else {
gl.disable(gl[bitName]);
var capabilityState = {};
capabilityState[this.Capability.DITHER] = true;

this.enable = function() {
for (var i = 0; i < arguments.length; ++i) {
var cap = arguments[i];
if (!capabilityState[cap]) {
gl.enable(arguments[i]);
capabilityState[cap] = true;
}
};
gla["disable" + funcName] = function() {
gl.disable(gl[bitName]);
};
}
}
};

createStateBitFunc('Blend', 'BLEND');
createStateBitFunc('CullFace', 'CULL_FACE');
createStateBitFunc('DepthTest', 'DEPTH_TEST');
createStateBitFunc('Dither', 'DITHER');
createStateBitFunc('PolygonOffsetFill', 'POLYGON_OFFSET_FILL');
createStateBitFunc('StencilTest', 'STENCIL_TEST');
this.disable = function() {
for (var i = 0; i < arguments.length; ++i) {
var cap = arguments[i];
if (capabilityState[cap]) {
gl.disable(arguments[i]);
capabilityState[cap] = false;
}
}
};

///////////////
// ANIMATION //
Expand Down Expand Up @@ -721,7 +735,7 @@ function Gladder(args) {
program: REQUIRED,
uniforms: {},
attributes: {},
mode: gla.draw.Mode.TRIANGLES,
mode: gla.DrawMode.TRIANGLES,
first: 0,
count: REQUIRED,
});
Expand All @@ -738,7 +752,7 @@ function Gladder(args) {
gl.drawArrays(args.mode, args.first, args.count);
};

this.draw.Mode = {
this.DrawMode = {
POINTS: gl.POINTS,
LINE_STRIP: gl.LINE_STRIP,
LINE_LOOP: gl.LINE_LOOP,
Expand Down

0 comments on commit 5da1c90

Please sign in to comment.