static inline t_color recursive_tracer(t_params p, t_ray *ray, t_objects *sp) { t_f64 inside; t_f64 facingratio; t_f64 fresneleffect; t_vec3f refldir; t_vec3f refdir; t_color reflection; t_color refrac; t_ray rx; if ((inside = dot_vec3f(&ray->dir, &p.nhit) > 0)) p.nhit = scale_vec3f(&p.nhit, -1); facingratio = -dot_vec3f(&ray->dir, &p.nhit); fresneleffect = mix(pow(1 - facingratio, 3), 1, .1); refldir = get_reflection_dir(&p.nhit, &ray->dir); rx = reflected_ray(ray, &refldir, &p.nhit, &p.phit); reflection = trace_ray(&rx, sp, ++p.depth); if (p.sphere->transparency > 0) { refdir = compute_ideal_refractions(ray, inside, p); rx = refracted_ray(ray, &refdir, &p.nhit, &p.phit); refrac = trace_ray(&rx, sp, ++p.depth); } return (defualt_glossy_shading(&reflection, &refrac, fresneleffect, p.sphere)); }
RGBColor Transparent::shade(ShadeRec& sr) { if (ior_tex != NULL) { //get current ior float i = 0; i = ior_tex->get_color(sr).average()*1.2; set_ior(i); } RGBColor L(SV_Phong::shade(sr)); // direct illumination Vector3D wo = -sr.ray.d; Vector3D wi; RGBColor fr = reflective_brdf->sample_f(sr, wo, wi); Ray reflected_ray(sr.hit_point, wi); if (specular_btdf->tir(sr)) L += sr.w.tracer_ptr->trace_ray(reflected_ray, sr.depth + 1); else { Vector3D wt; RGBColor ft = specular_btdf->sample_f(sr,wo,wt); Ray transmitted_ray(sr.hit_point, wt); L += fr * sr.w.tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * fabs(sr.normal * wi); L += ft * sr.w.tracer_ptr->trace_ray(transmitted_ray, sr.depth + 1) * fabs(sr.normal * wt); } return (L); }
glm::vec3 rayTrace(Ray &ray, const float& t, const glm::vec3& normal, RayTracerState& state) { glm::vec3 p = ray.getOrigin() + (t*ray.getDirection()); Ray reflected_ray(p, glm::reflect(ray.getDirection(), normal)); float dotval = glm::dot(normal, -ray.getDirection()); float s = 0.7f; dotval = s*1.0f + (1.0f-s) * dotval; return state.rayTrace(reflected_ray) * dotval; }
RGBColor Reflective::path_shade(ShadeRec& sr)const { Vector3D wi; Vector3D wo = -sr.ray.d; float pdf; RGBColor f = reflective_brdf->sample_f(sr,wo,wi,pdf); float ndotwi = sr.normal * wi; Ray reflected_ray(sr.hit_point,wi); return (f * sr.w.tracer_ptr->trace_ray(reflected_ray,sr.depth+1) * ndotwi / pdf); }
RGBColor Reflective::path_shade(ShadeRec & sr){ Vec4 wo = -sr.ray.d; Vec4 wi; float pdf; RGBColor fr = reflective_brdf->sample_f(sr, wo, wi,pdf); Ray reflected_ray(sr.hit_point, wi); float ndotwi = dot(sr.normal, wi); return (fr * sr.w->tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * ndotwi /pdf); }
RGBColor Reflective::shade(ShadeRec& sr) { RGBColor L(Phong::shade(sr)); // Direct illumination Vector3D wo = -sr.ray.d; Vector3D wi; RGBColor fr = reflective_brdf->sample_f(sr, wo, wi); Ray reflected_ray(sr.hit_point, wi); L += fr * sr.w.tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * (sr.normal * wi); return L; }
RGBColor Reflective::area_light_shade(ShadeRec & sr){ RGBColor L(Phong::area_light_shade(sr)); // direct illumination Vec4 wo = -sr.ray.d; Vec4 wi; RGBColor fr = reflective_brdf->sample_f(sr, wo, wi); Ray reflected_ray(sr.hit_point, wi); L += fr * sr.w->tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * (dot(sr.normal, wi)); return (L); }
RGBColor GlossyReflector::area_light_shade(ShadeRec& sr)const { RGBColor L(Phong::area_light_shade(sr)); Vector3D wo(-sr.ray.d); Vector3D wi; float pdf; RGBColor fr(glossy_specular_brdf->sample_f(sr,wo,wi,pdf)); Ray reflected_ray(sr.hit_point,wi); L += fr * sr.w.tracer_ptr->trace_ray(reflected_ray,sr.depth+1) * (sr.normal*wi) / pdf; return L; }
RGBColor GlossyReflector::area_light_shade(ShadeRec& sr) //this is for chapter 18 page one image ad-hoc { RGBColor L(Phong::area_light_shade(sr)); // direct illumination Vector3D wo(-sr.ray.d); Vector3D wi; float pdf; RGBColor fr(glossy_specular_brdf->sample_f(sr, wo, wi, pdf)); Ray reflected_ray(sr.hit_point, wi); L += fr * sr.w.tracer_ptr->trace_ray(reflected_ray, sr.depth + 1) * (sr.normal * wi) / pdf; return (L); }
RGBColor Reflective::global_shade(ShadeRec& sr)const { RGBColor L; if( sr.depth == 0 ) L = area_light_shade(sr); Vector3D wi; Vector3D wo = -sr.ray.d; float pdf; RGBColor f = reflective_brdf->sample_f(sr,wo,wi,pdf); float ndotwi = sr.normal * wi; Ray reflected_ray(sr.hit_point,wi); L += (f * sr.w.tracer_ptr->trace_ray(reflected_ray,sr.depth+1) * ndotwi / pdf); return L; }
nex::color path_tracer::indirect_illumination(const sampler* sampler, const nex::ray* ray, const surface& surface) const { nex::vector wi; float pdf = 0.0f; nex::color f = surface.bsdf->sample(sampler->get_samples(bsdf_index)[ray->depth], surface, -ray->direction, &wi, &pdf); if ((f == nex::color::black()) || (pdf <= 0.0f)) { return nex::color::black(); } nex::ray reflected_ray(surface.position + nex::EPSILON * wi, wi, FLT_MAX, ray->depth + 1); bool emittance = (surface.bsdf->get_bxdf(BXDF_SPECULAR) != nullptr); float cos = std::abs(nex::dot(surface.normal, wi)); return radiance(sampler, &reflected_ray, emittance) * f * cos / pdf; }
RGBColor Dielectric::shade(ShadeRec& sr)const { RGBColor L(Phong::shade(sr)); Vector3D wi; Vector3D wo(-sr.ray.d); RGBColor fr = fresnel_brdf->sample_f(sr, wo, wi); // computes wi Ray reflected_ray(sr.hit_point, wi); double t; RGBColor Lr, Lt; float ndotwi = sr.normal * wi; if(fresnel_btdf->tir(sr)) { // total internal reflection if (ndotwi < 0.0) { // reflected ray is inside Lr = sr.w.tracer_ptr->trace_ray(reflected_ray, t, sr.depth + 1); L += cf_in.powc(t) * Lr; // inside filter color } else { // reflected ray is outside Lr = sr.w.tracer_ptr->trace_ray(reflected_ray, t, sr.depth + 1); // kr = 1 L += cf_out.powc(t) * Lr; // outside filter color } } else { // no total internal reflection Vector3D wt; RGBColor ft = fresnel_btdf->sample_f(sr, wo, wt); // computes wt Ray transmitted_ray(sr.hit_point, wt); float ndotwt = sr.normal * wt; if (ndotwi < 0.0) { // reflected ray is inside Lr = fr * sr.w.tracer_ptr->trace_ray(reflected_ray, t, sr.depth + 1) * fabs(ndotwi); L += cf_in.powc(t) * Lr; // inside filter color // transmitted ray is outside Lt = ft * sr.w.tracer_ptr->trace_ray(transmitted_ray, t, sr.depth + 1) * fabs(ndotwt); L += cf_out.powc(t) * Lt; // outside filter color } else { // reflected ray is outside Lr = fr * sr.w.tracer_ptr->trace_ray(reflected_ray, t, sr.depth + 1) * fabs(ndotwi); L += cf_out.powc(t) * Lr; // outside filter color // transmitted ray is inside Lt = ft * sr.w.tracer_ptr->trace_ray(transmitted_ray, t, sr.depth + 1) * fabs(ndotwt); L += cf_in.powc(t) * Lt; // inside filter color } } return (L); }