toji/gl-matrix — Issue #8: Proposed new functions
Author: raybellis
Created: 2011-12-14 15:04:41+00:00
State: open

Inspired by the WebGL lessons site, I created these two functions:

mat4.push = function(m) {
if (typeof m.stack === "undefined") {
m.stack = [];
}

var copy = mat4.create(m);
copy.stack = m.stack;
m.stack.push(copy);
};

mat4.pop = function(m) {
if (typeof m.stack === "undefined" || m.stack.length === 0) {
throw "invalid mat4.pop";
}
mat4.set(m.stack.pop(), m);
};

Usage:

mat4.push(mvMatrix);

... do other transforms

mat4.pop(mvMatrix);

The intention being that any matrix can now be "pushed" or "popped", without need to write functions specific to the current model view matrix variable.

The stack is stored (but only when "push" is used) as an additional array stored on the matrix, and each element in the stack maintains its own reference to that array.

Feel free to include it, if you think it's useful.