From 425a674a27c0d73a1bfe41eb34b912395bdf6e28 Mon Sep 17 00:00:00 2001 From: Manuel Cabral Date: Thu, 7 May 2015 23:30:12 +0100 Subject: [PATCH 1/2] Add common file with GLMAT_ARRAY_TYPE, GLMAT_EPSILON and GLMAT_RANDOM --- clone.js | 4 +++- common.js | 5 +++++ create.js | 4 +++- lookAt.js | 8 +++++--- rotate.js | 6 ++++-- tools/parser.js | 13 +++++++++---- 6 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 common.js diff --git a/clone.js b/clone.js index e5c7438..392dc02 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..27295b7 --- /dev/null +++ b/common.js @@ -0,0 +1,5 @@ +module.exports = { + GLMAT_EPSILON: 0.000001, + GLMAT_RANDOM: Math.random, + GLMAT_ARRAY_TYPE: (typeof Float32Array !== 'undefined') ? Float32Array : Array +}; \ No newline at end of file diff --git a/create.js b/create.js index 3aa4ccd..c154bc0 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..5032871 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..7ee24ef 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..8a07169 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' From 45a16e8ec1dc8298d906aaf72be4123c8dc8477c Mon Sep 17 00:00:00 2001 From: Manuel Cabral Date: Thu, 14 May 2015 16:54:40 +0100 Subject: [PATCH 2/2] Export common values as functions so they cannot be changed --- clone.js | 2 +- common.js | 6 +++--- create.js | 2 +- lookAt.js | 2 +- rotate.js | 2 +- tools/parser.js | 6 +++--- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/clone.js b/clone.js index 392dc02..1f59c8a 100644 --- a/clone.js +++ b/clone.js @@ -1,6 +1,6 @@ module.exports = clone; -var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE; +var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE(); /** * Creates a new mat4 initialized with values from an existing matrix diff --git a/common.js b/common.js index 27295b7..69fda27 100644 --- a/common.js +++ b/common.js @@ -1,5 +1,5 @@ module.exports = { - GLMAT_EPSILON: 0.000001, - GLMAT_RANDOM: Math.random, - GLMAT_ARRAY_TYPE: (typeof Float32Array !== 'undefined') ? Float32Array : Array + 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 c154bc0..8a76b1e 100644 --- a/create.js +++ b/create.js @@ -1,6 +1,6 @@ module.exports = create; -var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE; +var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE(); /** * Creates a new identity mat4 diff --git a/lookAt.js b/lookAt.js index 5032871..9ca1892 100644 --- a/lookAt.js +++ b/lookAt.js @@ -2,7 +2,7 @@ var identity = require('./identity'); module.exports = lookAt; -var GLMAT_EPSILON = require('./common').GLMAT_EPSILON; +var GLMAT_EPSILON = require('./common').GLMAT_EPSILON(); /** * Generates a look-at matrix with the given eye position, focal point, and up axis diff --git a/rotate.js b/rotate.js index 7ee24ef..d84a4d2 100644 --- a/rotate.js +++ b/rotate.js @@ -1,6 +1,6 @@ module.exports = rotate; -var GLMAT_EPSILON = require('./common').GLMAT_EPSILON; +var GLMAT_EPSILON = require('./common').GLMAT_EPSILON(); /** * Rotates a mat4 by the given angle around the given axis diff --git a/tools/parser.js b/tools/parser.js index 8a07169..0919454 100644 --- a/tools/parser.js +++ b/tools/parser.js @@ -33,13 +33,13 @@ while (match = reg.exec(orig)) { 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" + 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" + 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 += "var GLMAT_EPSILON = require('./common').GLMAT_EPSILON();\n\n" }; file += lastMatch[1]+'\nfunction '+name+body.trim()