#ifndef _RAY_H_ #define _RAY_H_ #include "HLSL.h" #include "Intersection.h" // First of all the Ray class stores the information required for tracing rays: origin, direction, ray parameter and the possibly hit object. // It also implements a lot of useful stuff for generating rays! class Primitive; class Ray { public: float3 origin; float3 direction; float t; Primitive * object; Ray() {} Ray(const float3 & eye, const float3 &dir) { origin = eye; direction = dir; direction = normalize(dir); } Ray(const float3 & orig, const float3 &dir, float epsilon) { direction = normalize( dir ); origin = orig + epsilon * direction; } /* compute absorption factor */ float3 absorb(Intersection const i) const { if(dot(i.n, i.v) >= 0) {return float3(1.f);} float l = length(origin - i.p); return float3(powf(M_E, -i.material->kabs[0] * l), powf(M_E, -i.material->kabs[1] * l), powf(M_E, -i.material->kabs[2] * l)); } // compute refracted direction static float3 refract(float3 l, float3 n, float sourceIOR, float targetIOR) { float cos_i = dot( n, l ); float b; if(cos_i >= 0.0f) {b = sourceIOR / targetIOR;} else {b = targetIOR / sourceIOR;} float cos_r = 1.0f - (b * b) * (1.0f - cos_i * cos_i); if(cos_r >= 0.0f) { float a; if(cos_i >= 0.0f) {a = b * cos_i - sqrtf(cos_r);} else {a = b * cos_i + sqrtf(cos_r);} float3 t = n * a - b * l; return t; } else { float3 t = n * cos_i * 2.0f - l; return t; } } // compute reflection direction static float3 reflect(float3 v, float3 n) { float t = dot(v, n); if(t > 0) {return n * 2.0f * t - v;} return float3(0.0f); } }; #endif