dungeon_crawler
diff prototype/vmath/vmath.inl @ 1:96de911d05d4
started a rough prototype
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 28 Jun 2012 06:05:50 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/vmath/vmath.inl Thu Jun 28 06:05:50 2012 +0300 1.3 @@ -0,0 +1,47 @@ 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 +/** Generates a random number in [0, range) */ 1.25 +static inline scalar_t frand(scalar_t range) 1.26 +{ 1.27 + return range * (scalar_t)rand() / (scalar_t)RAND_MAX; 1.28 +} 1.29 + 1.30 +/** Generates a random vector on the surface of a sphere */ 1.31 +static inline vec3_t sphrand(scalar_t rad) 1.32 +{ 1.33 + scalar_t u = (scalar_t)rand() / RAND_MAX; 1.34 + scalar_t v = (scalar_t)rand() / RAND_MAX; 1.35 + 1.36 + scalar_t theta = 2.0 * M_PI * u; 1.37 + scalar_t phi = acos(2.0 * v - 1.0); 1.38 + 1.39 + vec3_t res; 1.40 + res.x = rad * cos(theta) * sin(phi); 1.41 + res.y = rad * sin(theta) * sin(phi); 1.42 + res.z = rad * cos(phi); 1.43 + return res; 1.44 +} 1.45 + 1.46 +/** linear interpolation */ 1.47 +static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t) 1.48 +{ 1.49 + return a + (b - a) * t; 1.50 +}