// does the recursive (shadow rays & recursive/glossy rays) work Vec3f RayTracer::TraceRay(const Ray &ray, Hit &hit, int bounce_count) const { hit = Hit(); bool intersect = CastRay(ray,hit,false); Vec3f answer(args->background_color_linear); if (intersect == true) { const Material *m = hit.getMaterial(); assert (m != NULL); // rays coming from the light source are set to white, don't bother to ray trace further. if (m->getEmittedColor().Length() > 0.001) { answer = Vec3f(1,1,1); } else { // ambient light answer = args->ambient_light_linear * m->getDiffuseColor(hit.get_s(),hit.get_t()); // Shadows answer += shadows(ray, hit); // Reflections Vec3f reflectiveColor = m->getReflectiveColor(); double roughness = m->getRoughness(); if (bounce_count > 0 && reflectiveColor.Length() > MIN_COLOR_LEN) { answer += reflectiveColor * reflections(ray, hit, bounce_count, roughness); } } } return answer; }
void reflections(t_env *rt, t_vector ray, t_vector orig, int rr) { t_vector n; t_vector tmp_reflect; intersection(rt, ray, orig); rt->inter = calcul_ptinter(orig, ray, rt->t); calcul_light(rt, rt->i2); if (rt->i2 != -1) { calcul_reflec(rt, &n, &tmp_reflect, &ray); orig = rt->inter; } rt->color2.r = rt->color.r * rt->first_reflec + rt->color2.r * (1 - rt->first_reflec); rt->color2.g = rt->color.g * rt->first_reflec + rt->color2.g * (1 - rt->first_reflec); rt->color2.b = rt->color.b * rt->first_reflec + rt->color2.b * (1 - rt->first_reflec); if (rt->i2 != -1 && rr < rt->max_reflect && rt->object[rt->i2].material.shiny) reflections(rt, ray, orig, rr + 1); }