webgl-tools

diff vmath.js @ 0:4fe036e28796

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 15 Jun 2011 20:04:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vmath.js	Wed Jun 15 20:04:49 2011 +0300
     1.3 @@ -0,0 +1,206 @@
     1.4 +/*
     1.5 +libvmath javascript port (original C version: http://gfxtools.sourceforge.net)
     1.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU General Public License as published by
    1.10 +the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +function v3_cons(res, x, y, z)
    1.23 +{
    1.24 +	res[0] = x;
    1.25 +	res[1] = y;
    1.26 +	res[2] = z;
    1.27 +}
    1.28 +
    1.29 +function v3_add(res, a, b)
    1.30 +{
    1.31 +	res[0] = a[0] + b[0];
    1.32 +	res[1] = a[1] + b[1];
    1.33 +	res[2] = a[2] + b[2];
    1.34 +}
    1.35 +
    1.36 +function v3_sub(res, a, b)
    1.37 +{
    1.38 +	res[0] = a[0] - b[0];
    1.39 +	res[1] = a[1] - b[1];
    1.40 +	res[2] = a[2] - b[2];
    1.41 +}
    1.42 +
    1.43 +function v3_mul(res, a, b)
    1.44 +{
    1.45 +	res[0] = a[0] * b[0];
    1.46 +	res[1] = a[1] * b[1];
    1.47 +	res[2] = a[2] * b[2];
    1.48 +}
    1.49 +
    1.50 +function v3_div(res, a, b)
    1.51 +{
    1.52 +	res[0] = a[0] / b[0];
    1.53 +	res[1] = a[1] / b[1];
    1.54 +	res[2] = a[2] / b[2];
    1.55 +}
    1.56 +
    1.57 +function v3_dot(a, b)
    1.58 +{
    1.59 +	return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
    1.60 +}
    1.61 +
    1.62 +function v3_cross(res, a, b)
    1.63 +{
    1.64 +	res[0] = a[1] * b[2] - a[2] * b[1];
    1.65 +	res[1] = a[2] * b[0] - a[0] * b[2];
    1.66 +	res[2] = a[0] * b[1] - a[1] * b[0];
    1.67 +}
    1.68 +
    1.69 +function v3_length(v)
    1.70 +{
    1.71 +	return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
    1.72 +}
    1.73 +
    1.74 +function v3_length_sq(v)
    1.75 +{
    1.76 +	return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
    1.77 +}
    1.78 +
    1.79 +function v3_normalize(v)
    1.80 +{
    1.81 +	var len = v3_length(v);
    1.82 +	v[0] /= len;
    1.83 +	v[1] /= len;
    1.84 +	v[2] /= len;
    1.85 +}
    1.86 +
    1.87 +function v3_transform(res, v, m)
    1.88 +{
    1.89 +	res[0] = v[0] * m[0] + v[1] * m[1] + v[2] * m[2] + m[3];
    1.90 +	res[1] = v[0] * m[4] + v[1] * m[5] + v[2] * m[6] + m[7];
    1.91 +	res[3] = v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11];
    1.92 +}
    1.93 +
    1.94 +/* --- matrix 4x4 --- */
    1.95 +
    1.96 +function m4_cons(res, m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, ma, mb, mc, md, me, mf)
    1.97 +{
    1.98 +	res[0] = m0;
    1.99 +	res[1] = m1;
   1.100 +	res[2] = m2;
   1.101 +	res[3] = m3;
   1.102 +	res[4] = m4;
   1.103 +	res[5] = m5;
   1.104 +	res[6] = m6;
   1.105 +	res[7] = m7;
   1.106 +	res[8] = m8;
   1.107 +	res[9] = m9;
   1.108 +	res[10] = ma;
   1.109 +	res[11] = mb;
   1.110 +	res[12] = mc;
   1.111 +	res[13] = md;
   1.112 +	res[14] = me;
   1.113 +	res[15] = mf;
   1.114 +}
   1.115 +
   1.116 +function m4_identity(res)
   1.117 +{
   1.118 +	res[0] = res[5] = res[10] = res[15] = 1.0;
   1.119 +	res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = 0.0;
   1.120 +	res[8] = res[9] = res[11] = res[12] = res[13] = res[14] = 0.0;
   1.121 +}
   1.122 +
   1.123 +function m4_copy(res, m)
   1.124 +{
   1.125 +	res[0] = m[0]; res[1] = m[1]; res[2] = m[2]; res[3] = m[3];
   1.126 +	res[4] = m[4]; res[5] = m[5]; res[6] = m[6]; res[7] = m[7];
   1.127 +	res[8] = m[8]; res[9] = m[9]; res[10] = m[10]; res[11] = m[11];
   1.128 +	res[12] = m[12]; res[13] = m[13]; res[14] = m[14]; res[15] = m[15];
   1.129 +}
   1.130 +
   1.131 +function m4_mul(res, a, b)
   1.132 +{
   1.133 +	var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
   1.134 +	var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
   1.135 +	var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
   1.136 +	var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
   1.137 +	var b00 = b[0], b01 = b[1], b02 = b[2], b03 = b[3];
   1.138 +	var b10 = b[4], b11 = b[5], b12 = b[6], b13 = b[7];
   1.139 +	var b20 = b[8], b21 = b[9], b22 = b[10], b23 = b[11];
   1.140 +	var b30 = b[12], b31 = b[13], b32 = b[14], b33 = b[15];
   1.141 +
   1.142 +	res[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
   1.143 +	res[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
   1.144 +	res[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
   1.145 +	res[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
   1.146 +	res[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
   1.147 +	res[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
   1.148 +	res[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
   1.149 +	res[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
   1.150 +	res[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
   1.151 +	res[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
   1.152 +	res[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
   1.153 +	res[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
   1.154 +	res[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
   1.155 +	res[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
   1.156 +	res[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
   1.157 +	res[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
   1.158 +}
   1.159 +
   1.160 +function m4_translation(res, x, y, z)
   1.161 +{
   1.162 +	res[0] = res[5] = res[10] = res[15] = 1.0;
   1.163 +	res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = res[8] = res[9] = res[11] = 0.0;
   1.164 +	res[12] = x;
   1.165 +	res[13] = y;
   1.166 +	res[14] = z;
   1.167 +}
   1.168 +
   1.169 +function m4_rotation(res, angle, x, y, z)
   1.170 +{
   1.171 +	var sina = Math.sin(angle);
   1.172 +	var cosa = Math.cos(angle);
   1.173 +	var one_minus_cosa = 1.0 - cosa;
   1.174 +	var nxsq = x * x;
   1.175 +	var nysq = y * y;
   1.176 +	var nzsq = z * z;
   1.177 +
   1.178 +	res[0] = nxsq + (1.0 - nxsq) * cosa;
   1.179 +	res[4] = x * y * one_minus_cosa - z * sina;
   1.180 +	res[8] = x * z * one_minus_cosa + y * sina;
   1.181 +	res[1] = x * y * one_minus_cosa + z * sina;
   1.182 +	res[5] = nysq + (1.0 - nysq) * cosa;
   1.183 +	res[9] = y * z * one_minus_cosa - x * sina;
   1.184 +	res[2] = x * z * one_minus_cosa - y * sina;
   1.185 +	res[6] = y * z * one_minus_cosa + x * sina;
   1.186 +	res[10] = nzsq + (1.0 - nzsq) * cosa;
   1.187 +
   1.188 +	res[3] = res[7] = res[11] = res[12] = res[13] = res[14] = 0.0;
   1.189 +	res[15] = 1.0;
   1.190 +}
   1.191 +
   1.192 +function m4_scale(res, x, y, z)
   1.193 +{
   1.194 +	res[0] = x;
   1.195 +	res[5] = y;
   1.196 +	res[10] = z;
   1.197 +	res[15] = 1.0;
   1.198 +	res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = 0.0;
   1.199 +	res[8] = res[9] = res[11] = res[12] = res[13] = res[14] = 0.0;
   1.200 +}
   1.201 +
   1.202 +function m4_string(mat)
   1.203 +{
   1.204 +	return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] + ']\n' +
   1.205 +		'['+ mat[4] + ', ' + mat[5] + ', ' + mat[6] + ', ' + mat[7] + ']\n' +
   1.206 +		'['+ mat[8] + ', ' + mat[9] + ', ' + mat[10] + ', ' + mat[11] + ']\n' +
   1.207 +		'['+ mat[12] + ', ' + mat[13] + ', ' + mat[14] + ', ' + mat[15] + ']\n';
   1.208 +
   1.209 +}