sousou03 / glmatrix

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

Inline dests in mat4.lookAt #15

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Something like this (didn't test it)?

mat4.lookAt = function(eye, center, up, dest) {
        var eyex = eye[0],
                eyey = eye[1],
                eyez = eye[2],
                upx = up[0],
                upy = up[1],
                upz = up[2],
                centerx = center[0],
                centery = center[1],
                centerz = center[2];

        if (eyex == centerx && eyey == centery && eyez == centerz) {
                        return mat4.identity();
        }

        var z0,z1,z2,x0,x1,x2,y0,y1,y2,len;

        //vec3.direction(eye, center, z);
        z0 = eyex - center[0];
        z1 = eyey - center[1];
        z2 = eyez - center[2];

        // normalize (no check needed for 0 becuase of early return)
        len = Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
        z0 = z0/len;
        z1 = z1/len;
        z2 = z2/len;

        //vec3.normalize(vec3.cross(up, z, x));
        x0 = upy * z2 - upz * z1;
        x1 = upz * z0 - upx * z2;
        x2 = upx * z1 - upy * z0;
        len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
        if (!len) {
                dest[0] = 0;
                dest[4] = 0;
                dest[8] = 0;
                dest[12] = 0;
        } else {
                x0 = x0/len;
                x1 = x1/len;
                x2 = x2/len;
                dest[0] = x0;
                dest[4] = x1;
                dest[8] = x2;
                dest[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
        };

        //vec3.normalize(vec3.cross(z, x, y));
        y0 = z1 * x2 - z2 * x1;
        y1 = z2 * x0 - z0 * x2;
        y2 = z0 * x1 - z1 * x0;

        len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
        if (!len) {
                dest[1] = 0;
                dest[5] = 0;
                dest[9] = 0;
                dest[13] = 0;
        } else {
                y0 = y0/len;
                y1 = y1/len;
                y2 = y2/len;
                dest[1] = y0;
                dest[5] = y1;
                dest[9] = y2;
                dest[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
        }        

        dest[2] = z0;
        dest[3] = 0;
        dest[6] = z1;
        dest[7] = 0;
        dest[10] = z2;
        dest[11] = 0;
        dest[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
        dest[15] = 1;

        return dest;
};

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

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 1 Jul 2010 at 9:36

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
        z0 = eyex - center[0];
        z1 = eyey - center[1];
        z2 = eyez - center[2];
to 

        z0 = eyex - centerx;
        z1 = eyey - centery;
        z2 = eyez - centerz;

and maybe also don't calculate the square root in len = Math.sqrt(x0 * x0 + x1 
* x1 + x2 * x2), but later and than invert it also and do a multiplication in 
the else block.

Original comment by danielhe...@gmail.com on 28 Feb 2011 at 9:52