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

Two parameter methods #4

Open
mikkoh opened this issue Mar 9, 2015 · 2 comments
Open

Two parameter methods #4

mikkoh opened this issue Mar 9, 2015 · 2 comments

Comments

@mikkoh
Copy link
Member

mikkoh commented Mar 9, 2015

Most of the matrix composition functions take three parameters. out output matrix, a matrix to to be modified and general a third parameter which could be for instance: how much to translate, rotate, scale by.

To make it nicer to work with the gl-matrices would it make sense to just simply check if two parameters are passed. If so then out and a matrices would be the same.

For example the gl-mat4/translate function could look like this:

/**
 * Translate a mat4 by the given vector
 *
 * @param {mat4} out the receiving matrix
 * @param {mat4} a the matrix to translate
 * @param {vec3} v vector to translate by
 * @returns {mat4} out
 */
function translate(out, a, v) {

    // if two parameters were passed simply use the `out` and `a` as the same
    // expect a to be translation vector
    if( arguments.length == 2 ) {
        v = a;
        a = out;
    }

    var x = v[0], y = v[1], z = v[2],
        a00, a01, a02, a03,
        a10, a11, a12, a13,
        a20, a21, a22, a23;

    if (a === out) {
        out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
        out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
        out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
        out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
    } else {
        a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
        a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
        a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];

        out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
        out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
        out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;

        out[12] = a00 * x + a10 * y + a20 * z + a[12];
        out[13] = a01 * x + a11 * y + a21 * z + a[13];
        out[14] = a02 * x + a12 * y + a22 * z + a[14];
        out[15] = a03 * x + a13 * y + a23 * z + a[15];
    }

    return out;
};
@mattdesl
Copy link
Member

mattdesl commented Mar 9, 2015

The main issue is that this would diverge a bit from gl-matrix. So far these have just been straight ports, and there's even been some chat about pulling in updates upstream automatically.

at one point @hughsk was suggesting a "user friendly" module on top of these (maybe repurposing vecmath) which has some features like method chaining etc.

@deathcap
Copy link
Contributor

Two other possible issues with

    if( arguments.length == 2 ) {
        v = a;
        a = out;
    }
  • assigning to argument variables (here v and a) can deoptimize (see http://jsperf.com/don-t-assign-arguments - also some browsers warn to warn about this deoptimization in their profilers)
  • using arguments is falling out of favor, for example, it is not allowed in the proposed strong mode because it also can be difficult to optimize

for these reasons, agreed it'd be preferred to have a separate module for this feature if desired, wouldn't want current usage of gl-mat4 to have this potential performance impact..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants