#include #include "typedefs.h" #include "funcdefs.h" #include "globalvar.h" int shade(t_3d *pos, t_3d *ray, t_3d *nrm, t_object *obj, t_color *color) { int lnum; double k, bright, spec, diffuse; t_surface *surf; t_3d refl, ltray; /* Compute ambient light */ surf = &surface[obj->surfnum]; color->r += surf->ar; color->g += surf->ag; color->b += surf->ab; /* Calculate reflected ray */ k = -2.0 * dotp(ray, nrm); refl.x = k * nrm->x + ray->x; refl.y = k * nrm->y + ray->y; refl.z = k * nrm->z + ray->z; for (lnum = 0; lnum < nlight; lnum++) { lightray(lnum, pos, <ray); /* Get ray to light */ /* Compute diffuse light */ diffuse = dotp(nrm, <ray); if (diffuse > 0) { /* point of intersection faces light */ bright = brightness(obj->id, lnum, pos, <ray); diffuse *= bright; color->r += surf->dr * diffuse; color->g += surf->dg * diffuse; color->b += surf->db * diffuse; /* Compute specular component */ spec = dotp(&refl, <ray); if (spec > 0) { spec = bright * pow(spec, surf->coef); color->r += surf->sr * spec; color->g += surf->sg * spec; color->b += surf->sb * spec; } } } return 0; }