dbf-halloween2015

view libs/vmath/vmath.inl @ 3:c37fe5d8a4ed

windows port
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:04:28 +0200
parents
children
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-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 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 */
19 #include <stdlib.h>
21 static inline scalar_t smoothstep(float a, float b, float x)
22 {
23 if(x < a) return 0.0;
24 if(x >= b) return 1.0;
26 x = (x - a) / (b - a);
27 return x * x * (3.0 - 2.0 * x);
28 }
30 /** Generates a random number in [0, range) */
31 static inline scalar_t frand(scalar_t range)
32 {
33 return range * (scalar_t)rand() / (scalar_t)RAND_MAX;
34 }
36 /** Generates a random vector on the surface of a sphere */
37 static inline vec3_t sphrand(scalar_t rad)
38 {
39 scalar_t u = (scalar_t)rand() / RAND_MAX;
40 scalar_t v = (scalar_t)rand() / RAND_MAX;
42 scalar_t theta = 2.0 * M_PI * u;
43 scalar_t phi = acos(2.0 * v - 1.0);
45 vec3_t res;
46 res.x = rad * cos(theta) * sin(phi);
47 res.y = rad * sin(theta) * sin(phi);
48 res.z = rad * cos(phi);
49 return res;
50 }
52 /** linear interpolation */
53 static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t)
54 {
55 return a + (b - a) * t;
56 }