dungeon_crawler

annotate prototype/vmath/ray.cc @ 54:995191474cc0

writting the stream audio player
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Sep 2012 05:22:43 +0300
parents
children
rev   line source
nuclear@1 1 #include "ray.h"
nuclear@1 2 #include "vector.h"
nuclear@1 3
nuclear@1 4 scalar_t Ray::env_ior = 1.0;
nuclear@1 5
nuclear@1 6 Ray::Ray() {
nuclear@1 7 ior = env_ior;
nuclear@1 8 energy = 1.0;
nuclear@1 9 time = 0;
nuclear@1 10 iter = 0;
nuclear@1 11 }
nuclear@1 12
nuclear@1 13 Ray::Ray(const Vector3 &origin, const Vector3 &dir) {
nuclear@1 14 this->origin = origin;
nuclear@1 15 this->dir = dir;
nuclear@1 16 ior = env_ior;
nuclear@1 17 energy = 1.0;
nuclear@1 18 time = 0;
nuclear@1 19 iter = 0;
nuclear@1 20 }
nuclear@1 21
nuclear@1 22 void Ray::transform(const Matrix4x4 &xform) {
nuclear@1 23 Matrix4x4 upper;
nuclear@1 24 Vector3 dir;
nuclear@1 25
nuclear@1 26 upper = xform;
nuclear@1 27 upper[0][3] = upper[1][3] = upper[2][3] = upper[3][0] = upper[3][1] = upper[3][2] = 0.0;
nuclear@1 28 upper[3][3] = 1.0;
nuclear@1 29
nuclear@1 30 dir = (this->dir - origin).transformed(upper);
nuclear@1 31 origin.transform(xform);
nuclear@1 32 this->dir = dir + origin;
nuclear@1 33 }
nuclear@1 34
nuclear@1 35 Ray Ray::transformed(const Matrix4x4 &xform) const {
nuclear@1 36 Ray foo = *this;
nuclear@1 37 foo.transform(xform);
nuclear@1 38 return foo;
nuclear@1 39 }
nuclear@1 40
nuclear@1 41 void Ray::enter(scalar_t new_ior) {
nuclear@1 42 ior = new_ior;
nuclear@1 43 ior_stack.push(ior);
nuclear@1 44 }
nuclear@1 45
nuclear@1 46 void Ray::leave() {
nuclear@1 47 if(ior_stack.empty()) {
nuclear@1 48 //std::cerr << "empty ior stack?\n";
nuclear@1 49 return;
nuclear@1 50 }
nuclear@1 51 ior_stack.pop();
nuclear@1 52 ior = ior_stack.empty() ? env_ior : ior_stack.top();
nuclear@1 53 }
nuclear@1 54
nuclear@1 55 scalar_t Ray::calc_ior(bool entering, scalar_t mat_ior) const
nuclear@1 56 {
nuclear@1 57 scalar_t from_ior = this->ior;
nuclear@1 58
nuclear@1 59 if(entering) {
nuclear@1 60 return from_ior / mat_ior;
nuclear@1 61 }
nuclear@1 62
nuclear@1 63 Ray tmp = *this;
nuclear@1 64 tmp.leave();
nuclear@1 65 return from_ior / tmp.ior;
nuclear@1 66 }