Color Lambert::shade(const Ray& ray, const IntersectionInfo& info) { Color diffuse = texture ? texture->sample(info) : this->color; Vector v1 = info.normal; Vector v2 = lightPos - info.ip; v2.normalize(); double lambertCoeff = dot(v1, v2); return ambientLight * diffuse + diffuse * lambertCoeff * getLightContrib(info); }
Color Phong::shade(const Ray& ray, const IntersectionInfo& info) { Color diffuse = texture ? texture->sample(info) : this->color; Color result(0, 0, 0); for (auto& light: scene.lights) { int N = 1; Color sum (0, 0, 0); for (int i = 0; i < 1; i++) { Vector lightPos; Color lightColor; light->getNthSample(i, info.ip, lightPos, lightColor); Vector v2 = info.ip - lightPos; // from light towards the intersection point Vector v1 = faceforward(ray.dir, info.normal); // orient so that surface points to the light v2.normalize(); double lambertCoeff = dot(v1, -v2); Color fromLight = getLightContrib(info, lightPos, lightColor); Vector r = reflect(v2, v1); Vector toCamera = -ray.dir; double cosGamma = dot(toCamera, r); double phongCoeff; if (cosGamma > 0) phongCoeff = pow(cosGamma, specularExponent); else phongCoeff = 0; sum += diffuse * lambertCoeff * fromLight + (phongCoeff * specularMultiplier * fromLight); } result += sum / N; } result += scene.settings.ambientLight * diffuse; return result; }
Color Phong::shade(const Ray& ray, const IntersectionInfo& info) { Color diffuse = texture ? texture->sample(info) : this->color; Vector v1 = info.normal; Vector v2 = lightPos - info.ip; v2.normalize(); double lambertCoeff = dot(v1, v2); double fromLight = getLightContrib(info); Vector r = reflect(info.ip - lightPos, info.normal); Vector toCamera = -ray.dir; double cosGamma = dot(toCamera, r); double phongCoeff; if (cosGamma > 0) phongCoeff = pow(cosGamma, specularExponent); else phongCoeff = 0; return ambientLight * diffuse + diffuse * lambertCoeff * fromLight + Color(1, 1, 1) * (phongCoeff * specularMultiplier * fromLight); }
Color Lambert::shade(const Ray& ray, const IntersectionInfo& info) { Color diffuse = texture ? texture->sample(info) : this->color; Color result(0, 0, 0); for (auto& light: scene.lights) { int N = light->getNumSamples(); Color sum (0, 0, 0); for (int i = 0; i < 1; i++) { Vector lightPos; Color lightColor; light->getNthSample(i, info.ip, lightPos, lightColor); Vector v2 = info.ip - lightPos; // from light towards the intersection point Vector v1 = faceforward(ray.dir, info.normal); // orient so that surface points to the light v2.normalize(); double lambertCoeff = dot(v1, -v2); sum += diffuse * lambertCoeff * getLightContrib(info, lightPos, lightColor); } result += sum / N; } result += scene.settings.ambientLight * diffuse; return result; }