bool triangle::intersect(const point &p, const ray &r, point &i) { point i_proj; matrix inv; float t = 0.0f; float alpha = 0.0f, beta = 0.0f; t = r.get_dir()*this->normal_; if (t == 0.0f) { return this->OBJECT_NO_INTERSECTION; } t = (this->plan_offset_ + vector(p)*this->normal_) / t; i = p - r.get_dir()*t; inv = this->basis_.inverse(); i_proj = vector(i-this->vertices_[0]); i_proj = prod(i_proj, inv); alpha = i_proj[0]; beta = i_proj[1]; if ( (0.0f <= alpha && alpha <= 1.0f) && (0.0f <= beta && beta <= 1.0f) && (alpha+beta <= 1.0f) ) { return this->OBJECT_INTERSECTION; } else { return this->OBJECT_NO_INTERSECTION; } }
ray triangle::reflect(const ray &r) { ray reflect_ray(r.get_dir() + 2*(r.get_dir()*this->normal_)*this->normal_); return reflect_ray; }