webgl-tools

view vmath.js @ 8:056da157f21f

merged something ... can't remember what
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Aug 2011 00:09:14 +0300
parents
children
line source
1 /*
2 libvmath javascript port (original C version: http://gfxtools.sourceforge.net)
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 function v3_cons(res, x, y, z)
20 {
21 res[0] = x;
22 res[1] = y;
23 res[2] = z;
24 }
26 function v3_add(res, a, b)
27 {
28 res[0] = a[0] + b[0];
29 res[1] = a[1] + b[1];
30 res[2] = a[2] + b[2];
31 }
33 function v3_sub(res, a, b)
34 {
35 res[0] = a[0] - b[0];
36 res[1] = a[1] - b[1];
37 res[2] = a[2] - b[2];
38 }
40 function v3_mul(res, a, b)
41 {
42 res[0] = a[0] * b[0];
43 res[1] = a[1] * b[1];
44 res[2] = a[2] * b[2];
45 }
47 function v3_div(res, a, b)
48 {
49 res[0] = a[0] / b[0];
50 res[1] = a[1] / b[1];
51 res[2] = a[2] / b[2];
52 }
54 function v3_dot(a, b)
55 {
56 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
57 }
59 function v3_cross(res, a, b)
60 {
61 res[0] = a[1] * b[2] - a[2] * b[1];
62 res[1] = a[2] * b[0] - a[0] * b[2];
63 res[2] = a[0] * b[1] - a[1] * b[0];
64 }
66 function v3_length(v)
67 {
68 return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
69 }
71 function v3_length_sq(v)
72 {
73 return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
74 }
76 function v3_normalize(v)
77 {
78 var len = v3_length(v);
79 v[0] /= len;
80 v[1] /= len;
81 v[2] /= len;
82 }
84 function v3_transform(res, v, m)
85 {
86 res[0] = v[0] * m[0] + v[1] * m[1] + v[2] * m[2] + m[3];
87 res[1] = v[0] * m[4] + v[1] * m[5] + v[2] * m[6] + m[7];
88 res[3] = v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11];
89 }
91 /* --- matrix 4x4 --- */
93 function m4_cons(res, m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, ma, mb, mc, md, me, mf)
94 {
95 res[0] = m0;
96 res[1] = m1;
97 res[2] = m2;
98 res[3] = m3;
99 res[4] = m4;
100 res[5] = m5;
101 res[6] = m6;
102 res[7] = m7;
103 res[8] = m8;
104 res[9] = m9;
105 res[10] = ma;
106 res[11] = mb;
107 res[12] = mc;
108 res[13] = md;
109 res[14] = me;
110 res[15] = mf;
111 }
113 function m4_identity(res)
114 {
115 res[0] = res[5] = res[10] = res[15] = 1.0;
116 res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = 0.0;
117 res[8] = res[9] = res[11] = res[12] = res[13] = res[14] = 0.0;
118 }
120 function m4_copy(res, m)
121 {
122 res[0] = m[0]; res[1] = m[1]; res[2] = m[2]; res[3] = m[3];
123 res[4] = m[4]; res[5] = m[5]; res[6] = m[6]; res[7] = m[7];
124 res[8] = m[8]; res[9] = m[9]; res[10] = m[10]; res[11] = m[11];
125 res[12] = m[12]; res[13] = m[13]; res[14] = m[14]; res[15] = m[15];
126 }
128 function m4_mul(res, a, b)
129 {
130 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
131 var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
132 var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
133 var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
134 var b00 = b[0], b01 = b[1], b02 = b[2], b03 = b[3];
135 var b10 = b[4], b11 = b[5], b12 = b[6], b13 = b[7];
136 var b20 = b[8], b21 = b[9], b22 = b[10], b23 = b[11];
137 var b30 = b[12], b31 = b[13], b32 = b[14], b33 = b[15];
139 res[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
140 res[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
141 res[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
142 res[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
143 res[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
144 res[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
145 res[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
146 res[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
147 res[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
148 res[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
149 res[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
150 res[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
151 res[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
152 res[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
153 res[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
154 res[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
155 }
157 function m4_translation(res, x, y, z)
158 {
159 res[0] = res[5] = res[10] = res[15] = 1.0;
160 res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = res[8] = res[9] = res[11] = 0.0;
161 res[12] = x;
162 res[13] = y;
163 res[14] = z;
164 }
166 function m4_rotation(res, angle, x, y, z)
167 {
168 var sina = Math.sin(angle);
169 var cosa = Math.cos(angle);
170 var one_minus_cosa = 1.0 - cosa;
171 var nxsq = x * x;
172 var nysq = y * y;
173 var nzsq = z * z;
175 res[0] = nxsq + (1.0 - nxsq) * cosa;
176 res[4] = x * y * one_minus_cosa - z * sina;
177 res[8] = x * z * one_minus_cosa + y * sina;
178 res[1] = x * y * one_minus_cosa + z * sina;
179 res[5] = nysq + (1.0 - nysq) * cosa;
180 res[9] = y * z * one_minus_cosa - x * sina;
181 res[2] = x * z * one_minus_cosa - y * sina;
182 res[6] = y * z * one_minus_cosa + x * sina;
183 res[10] = nzsq + (1.0 - nzsq) * cosa;
185 res[3] = res[7] = res[11] = res[12] = res[13] = res[14] = 0.0;
186 res[15] = 1.0;
187 }
189 function m4_scale(res, x, y, z)
190 {
191 res[0] = x;
192 res[5] = y;
193 res[10] = z;
194 res[15] = 1.0;
195 res[1] = res[2] = res[3] = res[4] = res[6] = res[7] = 0.0;
196 res[8] = res[9] = res[11] = res[12] = res[13] = res[14] = 0.0;
197 }
199 function m4_string(mat)
200 {
201 return '[' + mat[0] + ', ' + mat[1] + ', ' + mat[2] + ', ' + mat[3] + ']\n' +
202 '['+ mat[4] + ', ' + mat[5] + ', ' + mat[6] + ', ' + mat[7] + ']\n' +
203 '['+ mat[8] + ', ' + mat[9] + ', ' + mat[10] + ', ' + mat[11] + ']\n' +
204 '['+ mat[12] + ', ' + mat[13] + ', ' + mat[14] + ', ' + mat[15] + ']\n';
206 }