dbf-halloween2015
diff libs/vmath/vmath.inl @ 1:c3f5c32cb210
barfed all the libraries in the source tree to make porting easier
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:36:56 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/vmath.inl Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,56 @@ 1.4 +/* 1.5 +libvmath - a vector math library 1.6 +Copyright (C) 2004-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 Lesser General Public License as published 1.10 +by 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 Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 + 1.22 +#include <stdlib.h> 1.23 + 1.24 +static inline scalar_t smoothstep(float a, float b, float x) 1.25 +{ 1.26 + if(x < a) return 0.0; 1.27 + if(x >= b) return 1.0; 1.28 + 1.29 + x = (x - a) / (b - a); 1.30 + return x * x * (3.0 - 2.0 * x); 1.31 +} 1.32 + 1.33 +/** Generates a random number in [0, range) */ 1.34 +static inline scalar_t frand(scalar_t range) 1.35 +{ 1.36 + return range * (scalar_t)rand() / (scalar_t)RAND_MAX; 1.37 +} 1.38 + 1.39 +/** Generates a random vector on the surface of a sphere */ 1.40 +static inline vec3_t sphrand(scalar_t rad) 1.41 +{ 1.42 + scalar_t u = (scalar_t)rand() / RAND_MAX; 1.43 + scalar_t v = (scalar_t)rand() / RAND_MAX; 1.44 + 1.45 + scalar_t theta = 2.0 * M_PI * u; 1.46 + scalar_t phi = acos(2.0 * v - 1.0); 1.47 + 1.48 + vec3_t res; 1.49 + res.x = rad * cos(theta) * sin(phi); 1.50 + res.y = rad * sin(theta) * sin(phi); 1.51 + res.z = rad * cos(phi); 1.52 + return res; 1.53 +} 1.54 + 1.55 +/** linear interpolation */ 1.56 +static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t) 1.57 +{ 1.58 + return a + (b - a) * t; 1.59 +}