dungeon_crawler

annotate prototype/vmath/ray.inl @ 1:96de911d05d4

started a rough prototype
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 28 Jun 2012 06:05:50 +0300
parents
children
rev   line source
nuclear@1 1 /*
nuclear@1 2 libvmath - a vector math library
nuclear@1 3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@1 4
nuclear@1 5 This program is free software: you can redistribute it and/or modify
nuclear@1 6 it under the terms of the GNU Lesser General Public License as published
nuclear@1 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@1 8 (at your option) any later version.
nuclear@1 9
nuclear@1 10 This program is distributed in the hope that it will be useful,
nuclear@1 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@1 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@1 13 GNU Lesser General Public License for more details.
nuclear@1 14
nuclear@1 15 You should have received a copy of the GNU Lesser General Public License
nuclear@1 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@1 17 */
nuclear@1 18
nuclear@1 19 #ifdef __cplusplus
nuclear@1 20 extern "C" {
nuclear@1 21 #endif /* __cplusplus */
nuclear@1 22
nuclear@1 23 static inline ray_t ray_cons(vec3_t origin, vec3_t dir)
nuclear@1 24 {
nuclear@1 25 ray_t r;
nuclear@1 26 r.origin = origin;
nuclear@1 27 r.dir = dir;
nuclear@1 28 return r;
nuclear@1 29 }
nuclear@1 30
nuclear@1 31 #ifdef __cplusplus
nuclear@1 32 }
nuclear@1 33
nuclear@1 34 inline Ray reflect_ray(const Ray &inray, const Vector3 &norm)
nuclear@1 35 {
nuclear@1 36 Ray ray = inray;
nuclear@1 37 ray.iter--;
nuclear@1 38 ray.dir = ray.dir.reflection(norm);
nuclear@1 39 return ray;
nuclear@1 40 }
nuclear@1 41
nuclear@1 42 inline Ray refract_ray(const Ray &inray, const Vector3 &norm, scalar_t mat_ior, bool entering, scalar_t ray_mag)
nuclear@1 43 {
nuclear@1 44 Ray ray = inray;
nuclear@1 45 ray.iter--;
nuclear@1 46
nuclear@1 47 scalar_t ior = ray.calc_ior(entering, mat_ior);
nuclear@1 48
nuclear@1 49 if(entering) {
nuclear@1 50 ray.enter(mat_ior);
nuclear@1 51 } else {
nuclear@1 52 ray.leave();
nuclear@1 53 }
nuclear@1 54
nuclear@1 55 if(ray_mag < 0.0) {
nuclear@1 56 ray_mag = ray.dir.length();
nuclear@1 57 }
nuclear@1 58 ray.dir = (ray.dir / ray_mag).refraction(norm, ior) * ray_mag;
nuclear@1 59
nuclear@1 60 /* check TIR */
nuclear@1 61 if(dot_product(ray.dir, norm) > 0.0) {
nuclear@1 62 if(entering) {
nuclear@1 63 ray.leave();
nuclear@1 64 } else {
nuclear@1 65 ray.enter(mat_ior);
nuclear@1 66 }
nuclear@1 67 }
nuclear@1 68 return ray;
nuclear@1 69 }
nuclear@1 70 #endif /* __cplusplus */