A simple pathtracer program, capable of producing renderings of simple bodies. The program computes global illumination, by intersecting scene objects, such as luminaries and solid bodies, with idealised surface properies.
#include "./defines.hpp"
#include "./box.hpp"
#include "./vec3.hpp"
#include "./ray.hpp"
#include "./object.hpp"
#include "./material.hpp"
namespace raytracer
{
bool box::intersects(const ray& ray, double& length, vec3& normal, material& mat) const
{
double tmin, tmax, tymin, tymax, tzmin, tzmax, swap;
double bias= 1e-8;
tmin = (vmin_.x - ray.origin.x)/(ray.direction.x+bias);
tmax = (vmax_.x - ray.origin.x)/(ray.direction.x+bias);
if (tmin > tmax){ swap=tmin; tmin= tmax; tmax= swap;}
tymin = (vmin_.y - ray.origin.y)/(ray.direction.y+bias);
tymax = (vmax_.y - ray.origin.y)/(ray.direction.y+bias);
if (tymin > tymax){ swap=tymin; tymin= tymax; tymax= swap;}
if ((tmin > tymax) || (tymin > tmax)) return false;
if (tymin > tmin) tmin = tymin;
if (tymax < tmax) tmax = tymax;
tzmin = (vmin_.z - ray.origin.z)/(ray.direction.z+bias);
tzmax = (vmax_.z - ray.origin.z)/(ray.direction.z+bias);
if (tzmin > tzmax){ swap=tzmin; tzmin= tzmax; tzmax= swap;}
if ((tmin > tzmax) || (tzmin > tmax)) return false;
if (tzmin > tmin) tmin = tzmin;
if (tzmax < tmax) tmax = tzmax;
vec3 lenv= ray.direction.scale(tmin);
//calc length
length= lenv.length();
//calc normal
vec3 loc_surfp= ray.origin.add(lenv).add(this->pos_.negative());
double bias2 = 1.000001;
normal= vec3((int)(loc_surfp.x*bias2/this->halflen_.x),
(int)(loc_surfp.y*bias2/this->halflen_.y),
(int)(loc_surfp.z*bias2/this->halflen_.z)
);
mat= *this->material_;
return true;
};
}