• Utils.js

  • ¶
    var Context = require('./Context');
    
    module.exports.getCurrentContext = function() {
      return Context.currentContext;
    }
    
    module.exports.clearColor = function(color) {
      var gl = Context.currentContext;
      if (color)
        gl.clearColor(color.r, color.g, color.b, color.a);
      gl.clear(gl.COLOR_BUFFER_BIT);
      return this;
    };
    
    module.exports.clearDepth = function() {
      var gl = Context.currentContext;
      gl.clear(gl.DEPTH_BUFFER_BIT);
      return this;
    };
    
    module.exports.clearColorAndDepth = function(color) {
      var gl = Context.currentContext;
      color = color || { r: 0, g:0, b:0, a: 1};
      gl.clearColor(color.r, color.g, color.b, color.a);
      gl.depthMask(1);
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
      return this;
    };
    
    module.exports.enableDepthReadAndWrite = function(depthRead, depthWrite) {
      if (arguments.length == 2) {
  • ¶

    do nothing, just use the values

      }
      else if (arguments.length == 1) {
  • ¶

    use the same value for both read and write

        depthWrite = depthRead;
      }
      else {
  • ¶

    defaults

        depthRead = true;
        depthWrite = true;
      }
    
      var gl = Context.currentContext;
    
      if (depthWrite) gl.depthMask(1);
      else gl.depthMask(0);
    
      if (depthRead) gl.enable(gl.DEPTH_TEST);
      else gl.disable(gl.DEPTH_TEST);
    
      return this;
    };
    
    module.exports.enableAdditiveBlending = function() {
      return this.enableBlending("ONE", "ONE");
    };
    
    module.exports.enableAlphaBlending = function(src, dst) {
      return this.enableBlending("SRC_ALPHA", "ONE_MINUS_SRC_ALPHA");
    };
    
    module.exports.enableBlending = function(src, dst) {
      var gl = Context.currentContext;
      if (src === false) {
        gl.disable(gl.BLEND);
        return this;
      }
      gl.enable(gl.BLEND);
      gl.blendFunc(gl[src], gl[dst]);
      return this;
    };
  • ¶

    OpenGL viewport 0,0 is in bottom left corner

    0,h——-w,h | | | | 0,0——-w,0

    module.exports.viewport = function(x, y, w, h) {
      var gl = Context.currentContext;
      gl.viewport(x, y, w, h);
      return this;
    };
    
    module.exports.scissor = function(x, y, w, h) {
      var gl = Context.currentContext;
      if (x === false) {
        gl.disable(gl.SCISSOR_TEST);
      }
      else if (x.width != null) {
        var rect = x;
        gl.enable(gl.SCISSOR_TEST);
        gl.scissor(rect.x, rect.y, rect.width, rect.height);
      }
      else {
        gl.enable(gl.SCISSOR_TEST);
        gl.scissor(x, y, w, h);
      }
      return this;
    };
    
    module.exports.cullFace = function(enabled) {
      enabled = (enabled !== undefined) ? enabled : true
      var gl = Context.currentContext;
      if (enabled)
        gl.enable(gl.CULL_FACE);
      else
        gl.disable(gl.CULL_FACE);
      gl.cullFace(gl.BACK);
      return this;
    };
    
    module.exports.lineWidth = function(width) {
      var gl = Context.currentContext;
      gl.lineWidth(width);
      return this;
    }