Example #1
0
        /**
         * Copies the elements of a mat3 into the upper 3x3 elements of a mat4
         *
         * @param {mat3} mat mat3 containing values to copy
         * @param {mat4} [dest] mat4 receiving copied values
         *
         * @returns {mat4} dest if specified, a new mat4 otherwise
         */
        mat4 toMat4(mat3 mat, mat4 dest)
        {
            if (dest == null)
            {
                dest = mat4.create();
            }

            dest[15] = 1;
            dest[14] = 0;
            dest[13] = 0;
            dest[12] = 0;

            dest[11] = 0;
            dest[10] = mat[8];
            dest[9]  = mat[7];
            dest[8]  = mat[6];

            dest[7] = 0;
            dest[6] = mat[5];
            dest[5] = mat[4];
            dest[4] = mat[3];

            dest[3] = 0;
            dest[2] = mat[2];
            dest[1] = mat[1];
            dest[0] = mat[0];

            return(dest);
        }
Example #2
0
        /**
         * Transposes a mat3 (flips the values over the diagonal)
         *
         * Params:
         * @param {mat3} mat mat3 to transpose
         * @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
         *
         * @returns {mat3} dest is specified, mat otherwise
         */
        public static mat3 transpose(mat3 mat, mat3 dest = null)
        {
            // If we are transposing ourselves we can skip a few steps but have to cache some values
            if (dest == null || ReferenceEquals(mat, dest))
            {
                var a01 = mat[1]; var a02 = mat[2];
                var a12 = mat[5];

                mat[1] = mat[3];
                mat[2] = mat[6];
                mat[3] = a01;
                mat[5] = mat[7];
                mat[6] = a02;
                mat[7] = a12;
                return(mat);
            }

            dest[0] = mat[0];
            dest[1] = mat[3];
            dest[2] = mat[6];
            dest[3] = mat[1];
            dest[4] = mat[4];
            dest[5] = mat[7];
            dest[6] = mat[2];
            dest[7] = mat[5];
            dest[8] = mat[8];
            return(dest);
        }
Example #3
0
 /**
  * Copies the values of one mat3 to another
  *
  * @param {mat3} mat mat3 containing values to copy
  * @param {mat3} dest mat3 receiving copied values
  *
  * @returns {mat3} dest
  */
 public static mat3 set(mat3 mat, mat3 dest)
 {
     dest[0] = mat[0];
     dest[1] = mat[1];
     dest[2] = mat[2];
     dest[3] = mat[3];
     dest[4] = mat[4];
     dest[5] = mat[5];
     dest[6] = mat[6];
     dest[7] = mat[7];
     dest[8] = mat[8];
     return(dest);
 }
Example #4
0
 /**
  * Sets a mat3 to an identity matrix
  *
  * @param {mat3} dest mat3 to set
  *
  * @returns dest if specified, otherwise a new mat3
  */
 public static mat3 identity(mat3 dest)
 {
     if (dest == null)
     {
         dest = mat3.create();
     }
     dest[0] = 1;
     dest[1] = 0;
     dest[2] = 0;
     dest[3] = 0;
     dest[4] = 1;
     dest[5] = 0;
     dest[6] = 0;
     dest[7] = 0;
     dest[8] = 1;
     return(dest);
 }
Example #5
0
        /**
         * Creates a new instance of a mat3 using the default array type
         * Any javascript array-like object containing at least 9 numeric elements can serve as a mat3
         *
         * @param {mat3} [mat] mat3 containing values to initialize with
         *
         * @returns {mat3} New mat3
         */
        public static mat3 create(mat3 mat = null)
        {
            var dest = new mat3();

            if (mat != null) {
                dest[0] = mat[0];
                dest[1] = mat[1];
                dest[2] = mat[2];
                dest[3] = mat[3];
                dest[4] = mat[4];
                dest[5] = mat[5];
                dest[6] = mat[6];
                dest[7] = mat[7];
                dest[8] = mat[8];
            }

            return dest;
        }
Example #6
0
        /**
         * Creates a new instance of a mat3 using the default array type
         * Any javascript array-like object containing at least 9 numeric elements can serve as a mat3
         *
         * @param {mat3} [mat] mat3 containing values to initialize with
         *
         * @returns {mat3} New mat3
         */
        public static mat3 create(mat3 mat = null)
        {
            var dest = new mat3();

            if (mat != null)
            {
                dest[0] = mat[0];
                dest[1] = mat[1];
                dest[2] = mat[2];
                dest[3] = mat[3];
                dest[4] = mat[4];
                dest[5] = mat[5];
                dest[6] = mat[6];
                dest[7] = mat[7];
                dest[8] = mat[8];
            }

            return(dest);
        }
Example #7
0
        /**
         * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
         * The resulting matrix is useful for calculating transformed normals
         *
         * Params:
         * @param {mat4} mat mat4 containing values to invert and copy
         * @param {mat3} [dest] mat3 receiving values
         *
         * @returns {mat3} dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
         */
        public static mat3 toInverseMat3(mat4 mat, mat3 dest = null)
        {
            // Cache the matrix values (makes for huge speed increases!)
            var a00 = mat[0]; var a01 = mat[1]; var a02 = mat[2];
            var a10 = mat[4]; var a11 = mat[5]; var a12 = mat[6];
            var a20 = mat[8]; var a21 = mat[9]; var a22 = mat[10];

            var b01 = a22 * a11 - a12 * a21;
            var b11 = -a22 * a10 + a12 * a20;
            var b21 = a21 * a10 - a11 * a20;

            var     d = a00 * b01 + a01 * b11 + a02 * b21;
            GLfloat id;

            if (d == 0)
            {
                return(null);
            }
            id = 1 / d;

            if (dest == null)
            {
                dest = mat3.create();
            }

            dest[0] = b01 * id;
            dest[1] = (-a22 * a01 + a02 * a21) * id;
            dest[2] = (a12 * a01 - a02 * a11) * id;
            dest[3] = b11 * id;
            dest[4] = (a22 * a00 - a02 * a20) * id;
            dest[5] = (-a12 * a00 + a02 * a10) * id;
            dest[6] = b21 * id;
            dest[7] = (-a21 * a00 + a01 * a20) * id;
            dest[8] = (a11 * a00 - a01 * a10) * id;

            return(dest);
        }
Example #8
0
 /**
  * Sets a mat3 to an identity matrix
  *
  * @param {mat3} dest mat3 to set
  *
  * @returns dest if specified, otherwise a new mat3
  */
 public static mat3 identity(mat3 dest)
 {
     if (dest == null) { dest = mat3.create(); }
     dest[0] = 1;
     dest[1] = 0;
     dest[2] = 0;
     dest[3] = 0;
     dest[4] = 1;
     dest[5] = 0;
     dest[6] = 0;
     dest[7] = 0;
     dest[8] = 1;
     return dest;
 }
Example #9
0
        /**
         * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
         * The resulting matrix is useful for calculating transformed normals
         *
         * Params:
         * @param {mat4} mat mat4 containing values to invert and copy
         * @param {mat3} [dest] mat3 receiving values
         *
         * @returns {mat3} dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
         */
        public static mat3 toInverseMat3(mat4 mat, mat3 dest=null)
        {
            // Cache the matrix values (makes for huge speed increases!)
            var a00 = mat[0]; var a01 = mat[1]; var a02 = mat[2];
            var a10 = mat[4]; var a11 = mat[5]; var a12 = mat[6];
            var a20 = mat[8]; var a21 = mat[9]; var a22 = mat[10];

            var b01 = a22 * a11 - a12 * a21;
            var b11 = -a22 * a10 + a12 * a20;
            var b21 = a21 * a10 - a11 * a20;

            var d = a00 * b01 + a01 * b11 + a02 * b21;
            GLfloat id;

            if (d == 0) { return null; }
            id = 1 / d;

            if (dest == null) { dest = mat3.create(); }

            dest[0] = b01 * id;
            dest[1] = (-a22 * a01 + a02 * a21) * id;
            dest[2] = (a12 * a01 - a02 * a11) * id;
            dest[3] = b11 * id;
            dest[4] = (a22 * a00 - a02 * a20) * id;
            dest[5] = (-a12 * a00 + a02 * a10) * id;
            dest[6] = b21 * id;
            dest[7] = (-a21 * a00 + a01 * a20) * id;
            dest[8] = (a11 * a00 - a01 * a10) * id;

            return dest;
        }
Example #10
0
        /**
         * Copies the elements of a mat3 into the upper 3x3 elements of a mat4
         *
         * @param {mat3} mat mat3 containing values to copy
         * @param {mat4} [dest] mat4 receiving copied values
         *
         * @returns {mat4} dest if specified, a new mat4 otherwise
         */
        mat4 toMat4(mat3 mat, mat4 dest)
        {
            if (dest == null) { dest = mat4.create(); }

            dest[15] = 1;
            dest[14] = 0;
            dest[13] = 0;
            dest[12] = 0;

            dest[11] = 0;
            dest[10] = mat[8];
            dest[9] = mat[7];
            dest[8] = mat[6];

            dest[7] = 0;
            dest[6] = mat[5];
            dest[5] = mat[4];
            dest[4] = mat[3];

            dest[3] = 0;
            dest[2] = mat[2];
            dest[1] = mat[1];
            dest[0] = mat[0];

            return dest;
        }
Example #11
0
        /**
         * Transposes a mat3 (flips the values over the diagonal)
         *
         * Params:
         * @param {mat3} mat mat3 to transpose
         * @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
         *
         * @returns {mat3} dest is specified, mat otherwise
         */
        public static mat3 transpose(mat3 mat, mat3 dest=null)
        {
            // If we are transposing ourselves we can skip a few steps but have to cache some values
            if (dest == null || ReferenceEquals(mat, dest)) {
                var a01 = mat[1]; var a02 = mat[2];
                var a12 = mat[5];

                mat[1] = mat[3];
                mat[2] = mat[6];
                mat[3] = a01;
                mat[5] = mat[7];
                mat[6] = a02;
                mat[7] = a12;
                return mat;
            }

            dest[0] = mat[0];
            dest[1] = mat[3];
            dest[2] = mat[6];
            dest[3] = mat[1];
            dest[4] = mat[4];
            dest[5] = mat[7];
            dest[6] = mat[2];
            dest[7] = mat[5];
            dest[8] = mat[8];
            return dest;
        }
Example #12
0
 /**
  * Copies the values of one mat3 to another
  *
  * @param {mat3} mat mat3 containing values to copy
  * @param {mat3} dest mat3 receiving copied values
  *
  * @returns {mat3} dest
  */
 public static mat3 set(mat3 mat, mat3 dest)
 {
     dest[0] = mat[0];
     dest[1] = mat[1];
     dest[2] = mat[2];
     dest[3] = mat[3];
     dest[4] = mat[4];
     dest[5] = mat[5];
     dest[6] = mat[6];
     dest[7] = mat[7];
     dest[8] = mat[8];
     return dest;
 }