elefher / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

Use inverse and bitshifting for ortho #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Didn't test / benchmark it, maybe it's slower.

mat4.ortho = function(left, right, bottom, top, near, far, dest) {
        var lmr = 1 / (left - right);
        var tmb = 1 / (top - bottom);
        dest[0] = lmr << 1;
        dest[1] = 0;
        dest[2] = 0;
        dest[3] = 0;
        dest[4] = 0;
        dest[5] = tmb << 1;
        dest[6] = 0;
        dest[7] = 0;
        dest[8] = 0;
        dest[9] = 0;
        dest[10] = -2 / (far - near);
        dest[11] = 0;
        dest[12] = (left + right) * lmr;
        dest[13] = (top + bottom) * tmb;
        dest[14] = (far + near) / (far - near);
        dest[15] = 1;
        return dest;
};

OR

mat4.ortho = function(left, right, bottom, top, near, far, dest) {
        var lmr = 1 / (left - right);
        var tmb = 1 / (top - bottom);
        var fmn = 1 / (far - near);
        dest[0] = lmr << 1;
        dest[1] = 0;
        dest[2] = 0;
        dest[3] = 0;
        dest[4] = 0;
        dest[5] = tmb << 1;
        dest[6] = 0;
        dest[7] = 0;
        dest[8] = 0;
        dest[9] = 0;
        dest[10] = -2 * fmn;
        dest[11] = 0;
        dest[12] = (left + right) * lmr;
        dest[13] = (top + bottom) * tmb;
        dest[14] = (far + near) * fmn;
        dest[15] = 1;
        return dest;
};

OR maybe with
dest[10] = ~fmn << 1 + 1;

(~ changes the sign)

Original issue reported on code.google.com by danielhe...@gmail.com on 7 Jun 2010 at 10:14

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 12 Jun 2010 at 5:20

GoogleCodeExporter commented 8 years ago
I doubt the bit shift actually saves anything here, since you're still doing 
the same number of divides (and 2/x is going to be the same speed as 1/x). The 
idea of caching the subtraction pairs is a decent one, though. I'll get that 
into the next release.

Original comment by Tojiro@gmail.com on 13 Jun 2010 at 3:30

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 13 Jun 2010 at 4:13

GoogleCodeExporter commented 8 years ago
Hm, I did it because it also change this
dest[12] = (left + right) / (left - right);
to 
(left + right) * lmr
so 2 divides -> 1 + caching the pairs. It could be just a little bit faster.

Original comment by danielhe...@gmail.com on 13 Jun 2010 at 6:17