dungeon_crawler
diff prototype/vmath/ray.cc @ 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/ray.cc Thu Jun 28 06:05:50 2012 +0300 1.3 @@ -0,0 +1,66 @@ 1.4 +#include "ray.h" 1.5 +#include "vector.h" 1.6 + 1.7 +scalar_t Ray::env_ior = 1.0; 1.8 + 1.9 +Ray::Ray() { 1.10 + ior = env_ior; 1.11 + energy = 1.0; 1.12 + time = 0; 1.13 + iter = 0; 1.14 +} 1.15 + 1.16 +Ray::Ray(const Vector3 &origin, const Vector3 &dir) { 1.17 + this->origin = origin; 1.18 + this->dir = dir; 1.19 + ior = env_ior; 1.20 + energy = 1.0; 1.21 + time = 0; 1.22 + iter = 0; 1.23 +} 1.24 + 1.25 +void Ray::transform(const Matrix4x4 &xform) { 1.26 + Matrix4x4 upper; 1.27 + Vector3 dir; 1.28 + 1.29 + upper = xform; 1.30 + upper[0][3] = upper[1][3] = upper[2][3] = upper[3][0] = upper[3][1] = upper[3][2] = 0.0; 1.31 + upper[3][3] = 1.0; 1.32 + 1.33 + dir = (this->dir - origin).transformed(upper); 1.34 + origin.transform(xform); 1.35 + this->dir = dir + origin; 1.36 +} 1.37 + 1.38 +Ray Ray::transformed(const Matrix4x4 &xform) const { 1.39 + Ray foo = *this; 1.40 + foo.transform(xform); 1.41 + return foo; 1.42 +} 1.43 + 1.44 +void Ray::enter(scalar_t new_ior) { 1.45 + ior = new_ior; 1.46 + ior_stack.push(ior); 1.47 +} 1.48 + 1.49 +void Ray::leave() { 1.50 + if(ior_stack.empty()) { 1.51 + //std::cerr << "empty ior stack?\n"; 1.52 + return; 1.53 + } 1.54 + ior_stack.pop(); 1.55 + ior = ior_stack.empty() ? env_ior : ior_stack.top(); 1.56 +} 1.57 + 1.58 +scalar_t Ray::calc_ior(bool entering, scalar_t mat_ior) const 1.59 +{ 1.60 + scalar_t from_ior = this->ior; 1.61 + 1.62 + if(entering) { 1.63 + return from_ior / mat_ior; 1.64 + } 1.65 + 1.66 + Ray tmp = *this; 1.67 + tmp.leave(); 1.68 + return from_ior / tmp.ior; 1.69 +}