Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common file with GLMAT_ARRAY_TYPE, GLMAT_EPSILON and GLMAT_RANDOM #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clone.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module.exports = clone;

var GLMAT_ARRAY_TYPE = require('./common').GLMAT_ARRAY_TYPE();

/**
* Creates a new mat4 initialized with values from an existing matrix
*
* @param {mat4} a matrix to 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];
Expand Down
5 changes: 5 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
@@ -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; }
};
4 changes: 3 additions & 1 deletion create.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 5 additions & 3 deletions lookAt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions rotate.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions tools/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down