diff --git a/clone.js b/clone.js index e5c7438..1f59c8a 100644 --- a/clone.js +++ b/clone.js @@ -1,5 +1,7 @@ module.exports = clone; +var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE(); + /** * Creates a new mat4 initialized with values from an existing matrix * @@ -7,7 +9,7 @@ module.exports = clone; * @returns {mat4} a new 4x4 matrix */ function clone(a) { - var out = new Float32Array(16); + var out = new GLMAT_ARRAY_TYPE(16); out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; diff --git a/common.js b/common.js new file mode 100644 index 0000000..69fda27 --- /dev/null +++ b/common.js @@ -0,0 +1,5 @@ +module.exports = { + GLMAT_EPSILON: function() { return 0.000001; }, + GLMAT_RANDOM: function() { return Math.random; }, + GLMAT_ARRAY_TYPE: function() { return (typeof Float32Array !== 'undefined') ? Float32Array : Array; } +}; \ No newline at end of file diff --git a/create.js b/create.js index 3aa4ccd..8a76b1e 100644 --- a/create.js +++ b/create.js @@ -1,12 +1,14 @@ module.exports = create; +var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE(); + /** * Creates a new identity mat4 * * @returns {mat4} a new 4x4 matrix */ function create() { - var out = new Float32Array(16); + var out = new GLMAT_ARRAY_TYPE(16); out[0] = 1; out[1] = 0; out[2] = 0; diff --git a/lookAt.js b/lookAt.js index 117e439..9ca1892 100644 --- a/lookAt.js +++ b/lookAt.js @@ -2,6 +2,8 @@ var identity = require('./identity'); module.exports = lookAt; +var GLMAT_EPSILON = require('./common').GLMAT_EPSILON(); + /** * Generates a look-at matrix with the given eye position, focal point, and up axis * @@ -23,9 +25,9 @@ function lookAt(out, eye, center, up) { centery = center[1], centerz = center[2]; - if (Math.abs(eyex - centerx) < 0.000001 && - Math.abs(eyey - centery) < 0.000001 && - Math.abs(eyez - centerz) < 0.000001) { + if (Math.abs(eyex - centerx) < GLMAT_EPSILON && + Math.abs(eyey - centery) < GLMAT_EPSILON && + Math.abs(eyez - centerz) < GLMAT_EPSILON) { return identity(out); } diff --git a/rotate.js b/rotate.js index 418d954..d84a4d2 100644 --- a/rotate.js +++ b/rotate.js @@ -1,7 +1,9 @@ module.exports = rotate; +var GLMAT_EPSILON = require('./common').GLMAT_EPSILON(); + /** - * Rotates a mat4 by the given angle + * Rotates a mat4 by the given angle around the given axis * * @param {mat4} out the receiving matrix * @param {mat4} a the matrix to rotate @@ -20,7 +22,7 @@ function rotate(out, a, rad, axis) { b10, b11, b12, b20, b21, b22; - if (Math.abs(len) < 0.000001) { return null; } + if (Math.abs(len) < GLMAT_EPSILON) { return null; } len = 1 / len; x *= len; diff --git a/tools/parser.js b/tools/parser.js index 7742758..0919454 100644 --- a/tools/parser.js +++ b/tools/parser.js @@ -6,10 +6,6 @@ var path = require('path') // ^ had to manually remove aliases like 'mul' from the above before parsing var orig = fs.readFileSync(__dirname+'/original.js', 'utf8') -//TODO: common module that exports these? -orig = orig.replace(/GLMAT\_EPSILON/g, '0.000001') - .replace(/GLMAT\_ARRAY\_TYPE/g, 'Float32Array') - .replace(/GLMAT\_RANDOM/g, 'Math.random') // var block = /(\/\*[^]*?\*\/)/g // var func = /mat4\.([a-z0-9\-\_]+).*\=.*function/ig @@ -36,6 +32,15 @@ while (match = reg.exec(orig)) { var name = lastMatch[2].trim() var body = orig.substring( start, end ) var file = 'module.exports = '+name+';\n\n' + if(body.indexOf('GLMAT_ARRAY_TYPE') > -1) { + file += "var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE();\n\n" + }; + if(body.indexOf('GLMAT_RANDOM') > -1) { + file += "var GLMAT_RANDOM = require('./common').GLMAT_RANDOM();\n\n" + }; + if(body.indexOf('GLMAT_EPSILON') > -1) { + file += "var GLMAT_EPSILON = require('./common').GLMAT_EPSILON();\n\n" + }; file += lastMatch[1]+'\nfunction '+name+body.trim() var filename = name+'.js'