dungeon_crawler
view 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 source
1 #include "ray.h"
2 #include "vector.h"
4 scalar_t Ray::env_ior = 1.0;
6 Ray::Ray() {
7 ior = env_ior;
8 energy = 1.0;
9 time = 0;
10 iter = 0;
11 }
13 Ray::Ray(const Vector3 &origin, const Vector3 &dir) {
14 this->origin = origin;
15 this->dir = dir;
16 ior = env_ior;
17 energy = 1.0;
18 time = 0;
19 iter = 0;
20 }
22 void Ray::transform(const Matrix4x4 &xform) {
23 Matrix4x4 upper;
24 Vector3 dir;
26 upper = xform;
27 upper[0][3] = upper[1][3] = upper[2][3] = upper[3][0] = upper[3][1] = upper[3][2] = 0.0;
28 upper[3][3] = 1.0;
30 dir = (this->dir - origin).transformed(upper);
31 origin.transform(xform);
32 this->dir = dir + origin;
33 }
35 Ray Ray::transformed(const Matrix4x4 &xform) const {
36 Ray foo = *this;
37 foo.transform(xform);
38 return foo;
39 }
41 void Ray::enter(scalar_t new_ior) {
42 ior = new_ior;
43 ior_stack.push(ior);
44 }
46 void Ray::leave() {
47 if(ior_stack.empty()) {
48 //std::cerr << "empty ior stack?\n";
49 return;
50 }
51 ior_stack.pop();
52 ior = ior_stack.empty() ? env_ior : ior_stack.top();
53 }
55 scalar_t Ray::calc_ior(bool entering, scalar_t mat_ior) const
56 {
57 scalar_t from_ior = this->ior;
59 if(entering) {
60 return from_ior / mat_ior;
61 }
63 Ray tmp = *this;
64 tmp.leave();
65 return from_ior / tmp.ior;
66 }