/*======== double get_illumination() ========== Inputs: double *l double *n int cp double *constants Returns: illumination (0 - 255) Sum of ambient, diffuse, and specular components of illumination ====================*/ color get_illumination(vector light, vector normal, double *cp, color ca, struct constants *cons){ color c; double ambient, diffuse, specular; int val; ambient = get_ambient(ca.red, cons -> r[Ka]); diffuse = get_diffuse(light, normal, cp[0], cons -> r[Kd]); specular = get_specular(light, normal, cp[0], cons -> r[Ks]); val = ambient + diffuse + specular; c.red = val <= 255 ? val : 255; ambient = get_ambient(ca.green, cons -> g[Ka]); diffuse = get_diffuse(light, normal, cp[1], cons -> g[Kd]); specular = get_specular(light, normal, cp[1], cons -> g[Ks]); val = ambient + diffuse + specular; c.green = val <= 255 ? val : 255; ambient = get_ambient(ca.blue, cons -> b[Ka]); diffuse = get_diffuse(light, normal, cp[2], cons -> b[Kd]); specular = get_specular(light, normal, cp[2], cons -> b[Ks]); val = ambient + diffuse + specular; c.blue = val <= 255 ? val : 255; if (cons -> red || cons -> green || cons -> blue){ c.red *= cons -> red; c.green *= cons -> green; c.blue *= cons -> blue; } return c; }
vector3f get_ray_trace(ray &r, kdtree &kdtree, int depth) { if (depth >= max_depth) { return vector3f(0.0, 0.0, 0.0); } vector3f origin_cp = r.origin; primitive *pri = get_intersecting_primitive(kdtree, r); if (pri == NULL) { return background; } else { double *amb = pri->get_material().amb; double *dif = pri->get_material().dif; double *spc = pri->get_material().spc; // ambient calculation vector3f ambient = get_ambient(amb); // specular calculation (no transparency) vector3f specular = get_specular(spc, pri, r, kdtree, depth); // diffuse calculation double spc_alpha = 1; vector3f diffuse = get_diffuse(dif, pri, r, kdtree, depth, spc_alpha); return rgb_trim(ambient + specular * spc_alpha + diffuse); } }
static int get_spot(t_ray *ray, t_ray *light, t_spot *spot) { int color; color = get_diffuse(ray, light, spot); color = color_add(color, get_specular(ray, light, spot)); return (color); }