3dphotoshoot

view libs/vmath/vmath.c @ 10:c71c477521ca

converting to GLES2 and C++
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 31 May 2015 00:40:26 +0300
parents
children
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-2015 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 Lesser General Public License as published
7 by 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdlib.h>
19 #include <math.h>
20 #include "vmath.h"
22 #if defined(__APPLE__) && !defined(TARGET_IPHONE)
23 #include <xmmintrin.h>
25 void enable_fpexcept(void)
26 {
27 unsigned int bits;
28 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
29 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~bits);
30 }
32 void disable_fpexcept(void)
33 {
34 unsigned int bits;
35 bits = _MM_MASK_INVALID | _MM_MASK_DIV_ZERO | _MM_MASK_OVERFLOW | _MM_MASK_UNDERFLOW;
36 _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | bits);
37 }
39 #elif defined(__GNUC__) && !defined(TARGET_IPHONE) && !defined(__MINGW32__)
40 #define __USE_GNU
41 #include <fenv.h>
43 void enable_fpexcept(void)
44 {
45 feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
46 }
48 void disable_fpexcept(void)
49 {
50 fedisableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
51 }
53 #elif defined(_MSC_VER) || defined(__MINGW32__)
54 #include <float.h>
56 #if defined(__MINGW32__) && !defined(_EM_OVERFLOW)
57 /* if gcc's float.h gets precedence, the mingw MSVC includes won't be declared */
58 #define _MCW_EM 0x8001f
59 #define _EM_INVALID 0x10
60 #define _EM_ZERODIVIDE 0x08
61 #define _EM_OVERFLOW 0x04
62 unsigned int __cdecl _clearfp(void);
63 unsigned int __cdecl _controlfp(unsigned int, unsigned int);
64 #endif
66 void enable_fpexcept(void)
67 {
68 _clearfp();
69 _controlfp(_controlfp(0, 0) & ~(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
70 }
72 void disable_fpexcept(void)
73 {
74 _clearfp();
75 _controlfp(_controlfp(0, 0) | (_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW), _MCW_EM);
76 }
77 #else
78 void enable_fpexcept(void) {}
79 void disable_fpexcept(void) {}
80 #endif
83 /** Numerical calculation of integrals using simpson's rule */
84 scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples)
85 {
86 int i;
87 scalar_t h = (high - low) / (scalar_t)samples;
88 scalar_t sum = 0.0;
90 for(i=0; i<samples+1; i++) {
91 scalar_t y = f((scalar_t)i * h + low);
92 sum += ((!i || i == samples) ? y : ((i % 2) ? 4.0 * y : 2.0 * y)) * (h / 3.0);
93 }
94 return sum;
95 }
97 /** Gaussuan function */
98 scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev)
99 {
100 scalar_t exponent = -SQ(x - mean) / (2.0 * SQ(sdev));
101 return 1.0 - -pow(M_E, exponent) / (sdev * sqrt(TWO_PI));
102 }
105 /** b-spline approximation */
106 scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
107 {
108 vec4_t tmp;
109 scalar_t tsq = t * t;
111 static mat4_t bspline_mat = {
112 {-1, 3, -3, 1},
113 {3, -6, 3, 0},
114 {-3, 0, 3, 0},
115 {1, 4, 1, 0}
116 };
118 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), bspline_mat), 1.0 / 6.0);
119 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
120 }
122 /** Catmull-rom spline interpolation */
123 scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
124 {
125 vec4_t tmp;
126 scalar_t tsq = t * t;
128 static mat4_t crspline_mat = {
129 {-1, 3, -3, 1},
130 {2, -5, 4, -1},
131 {-1, 0, 1, 0},
132 {0, 2, 0, 0}
133 };
135 tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), crspline_mat), 0.5);
136 return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
137 }
139 /** Bezier interpolation */
140 scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
141 {
142 scalar_t omt, omt3, t3, f;
143 t3 = t * t * t;
144 omt = 1.0f - t;
145 omt3 = omt * omt * omt;
146 f = 3 * t * omt;
148 return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
149 }
151 /* ---- Ken Perlin's implementation of noise ---- */
153 #define B 0x100
154 #define BM 0xff
155 #define N 0x1000
156 #define NP 12 /* 2^N */
157 #define NM 0xfff
159 #define s_curve(t) (t * t * (3.0f - 2.0f * t))
161 #define setup(elem, b0, b1, r0, r1) \
162 do { \
163 scalar_t t = elem + N; \
164 b0 = ((int)t) & BM; \
165 b1 = (b0 + 1) & BM; \
166 r0 = t - (int)t; \
167 r1 = r0 - 1.0f; \
168 } while(0)
171 static int perm[B + B + 2]; /* permuted index from g_n onto themselves */
172 static vec3_t grad3[B + B + 2]; /* 3D random gradients */
173 static vec2_t grad2[B + B + 2]; /* 2D random gradients */
174 static scalar_t grad1[B + B + 2]; /* 1D random ... slopes */
175 static int tables_valid;
177 static void init_noise()
178 {
179 int i;
181 /* calculate random gradients */
182 for(i=0; i<B; i++) {
183 perm[i] = i; /* .. and initialize permutation mapping to identity */
185 grad1[i] = (scalar_t)((rand() % (B + B)) - B) / B;
187 grad2[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
188 grad2[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
189 grad2[i] = v2_normalize(grad2[i]);
191 grad3[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
192 grad3[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
193 grad3[i].z = (scalar_t)((rand() % (B + B)) - B) / B;
194 grad3[i] = v3_normalize(grad3[i]);
195 }
197 /* permute indices by swapping them randomly */
198 for(i=0; i<B; i++) {
199 int rand_idx = rand() % B;
201 int tmp = perm[i];
202 perm[i] = perm[rand_idx];
203 perm[rand_idx] = tmp;
204 }
206 /* fill up the rest of the arrays by duplicating the existing gradients */
207 /* and permutations */
208 for(i=0; i<B+2; i++) {
209 perm[B + i] = perm[i];
210 grad1[B + i] = grad1[i];
211 grad2[B + i] = grad2[i];
212 grad3[B + i] = grad3[i];
213 }
214 }
216 scalar_t noise1(scalar_t x)
217 {
218 int bx0, bx1;
219 scalar_t rx0, rx1, sx, u, v;
221 if(!tables_valid) {
222 init_noise();
223 tables_valid = 1;
224 }
226 setup(x, bx0, bx1, rx0, rx1);
227 sx = s_curve(rx0);
228 u = rx0 * grad1[perm[bx0]];
229 v = rx1 * grad1[perm[bx1]];
231 return lerp(u, v, sx);
232 }
234 scalar_t noise2(scalar_t x, scalar_t y)
235 {
236 int i, j, b00, b10, b01, b11;
237 int bx0, bx1, by0, by1;
238 scalar_t rx0, rx1, ry0, ry1;
239 scalar_t sx, sy, u, v, a, b;
241 if(!tables_valid) {
242 init_noise();
243 tables_valid = 1;
244 }
246 setup(x, bx0, bx1, rx0, rx1);
247 setup(y, by0, by1, ry0, ry1);
249 i = perm[bx0];
250 j = perm[bx1];
252 b00 = perm[i + by0];
253 b10 = perm[j + by0];
254 b01 = perm[i + by1];
255 b11 = perm[j + by1];
257 /* calculate hermite inteprolating factors */
258 sx = s_curve(rx0);
259 sy = s_curve(ry0);
261 /* interpolate along the left edge */
262 u = v2_dot(grad2[b00], v2_cons(rx0, ry0));
263 v = v2_dot(grad2[b10], v2_cons(rx1, ry0));
264 a = lerp(u, v, sx);
266 /* interpolate along the right edge */
267 u = v2_dot(grad2[b01], v2_cons(rx0, ry1));
268 v = v2_dot(grad2[b11], v2_cons(rx1, ry1));
269 b = lerp(u, v, sx);
271 /* interpolate between them */
272 return lerp(a, b, sy);
273 }
275 scalar_t noise3(scalar_t x, scalar_t y, scalar_t z)
276 {
277 int i, j;
278 int bx0, bx1, by0, by1, bz0, bz1;
279 int b00, b10, b01, b11;
280 scalar_t rx0, rx1, ry0, ry1, rz0, rz1;
281 scalar_t sx, sy, sz;
282 scalar_t u, v, a, b, c, d;
284 if(!tables_valid) {
285 init_noise();
286 tables_valid = 1;
287 }
289 setup(x, bx0, bx1, rx0, rx1);
290 setup(y, by0, by1, ry0, ry1);
291 setup(z, bz0, bz1, rz0, rz1);
293 i = perm[bx0];
294 j = perm[bx1];
296 b00 = perm[i + by0];
297 b10 = perm[j + by0];
298 b01 = perm[i + by1];
299 b11 = perm[j + by1];
301 /* calculate hermite interpolating factors */
302 sx = s_curve(rx0);
303 sy = s_curve(ry0);
304 sz = s_curve(rz0);
306 /* interpolate along the top slice of the cell */
307 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz0));
308 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz0));
309 a = lerp(u, v, sx);
311 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz0));
312 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz0));
313 b = lerp(u, v, sx);
315 c = lerp(a, b, sy);
317 /* interpolate along the bottom slice of the cell */
318 u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz1));
319 v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz1));
320 a = lerp(u, v, sx);
322 u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz1));
323 v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz1));
324 b = lerp(u, v, sx);
326 d = lerp(a, b, sy);
328 /* interpolate between slices */
329 return lerp(c, d, sz);
330 }
332 scalar_t fbm1(scalar_t x, int octaves)
333 {
334 int i;
335 scalar_t res = 0.0f, freq = 1.0f;
336 for(i=0; i<octaves; i++) {
337 res += noise1(x * freq) / freq;
338 freq *= 2.0f;
339 }
340 return res;
341 }
343 scalar_t fbm2(scalar_t x, scalar_t y, int octaves)
344 {
345 int i;
346 scalar_t res = 0.0f, freq = 1.0f;
347 for(i=0; i<octaves; i++) {
348 res += noise2(x * freq, y * freq) / freq;
349 freq *= 2.0f;
350 }
351 return res;
352 }
354 scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves)
355 {
356 int i;
357 scalar_t res = 0.0f, freq = 1.0f;
358 for(i=0; i<octaves; i++) {
359 res += noise3(x * freq, y * freq, z * freq) / freq;
360 freq *= 2.0f;
361 }
362 return res;
363 }
365 scalar_t turbulence1(scalar_t x, int octaves)
366 {
367 int i;
368 scalar_t res = 0.0f, freq = 1.0f;
369 for(i=0; i<octaves; i++) {
370 res += fabs(noise1(x * freq) / freq);
371 freq *= 2.0f;
372 }
373 return res;
374 }
376 scalar_t turbulence2(scalar_t x, scalar_t y, int octaves)
377 {
378 int i;
379 scalar_t res = 0.0f, freq = 1.0f;
380 for(i=0; i<octaves; i++) {
381 res += fabs(noise2(x * freq, y * freq) / freq);
382 freq *= 2.0f;
383 }
384 return res;
385 }
387 scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves)
388 {
389 int i;
390 scalar_t res = 0.0f, freq = 1.0f;
391 for(i=0; i<octaves; i++) {
392 res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
393 freq *= 2.0f;
394 }
395 return res;
396 }