diff --git a/Skeleton.cpp b/Skeleton.cpp index 8b6014218c03039ef021fe594800c7010de96b02..90d6d9f116b202ebe482e7522a0ee1ae4497dcbb 100644 --- a/Skeleton.cpp +++ b/Skeleton.cpp @@ -196,6 +196,15 @@ struct Light { position = _position; Le = _Le; } + + vec3 getDirection(const vec3& from) const { + return normalize(position - from); + } + + float getDistance(const vec3& point) const { + vec3 d = point - position; + return sqrtf(dot(d, d)); + } }; float rnd() { return (float)rand() / RAND_MAX; } @@ -257,11 +266,11 @@ public: return bestHit; } - bool shadowIntersect(Ray ray) { // for directional lights + bool shadowIntersect(Ray ray, Light* light) { // for directional lights for (Intersectable * object : objects) { Hit hit = object->intersect(ray); - if (hit.t < 0) - if(hit.t > length(hit.position - ray.start)) + if (hit.t > 0) + if(light->getDistance(ray.start) > hit.t) return true; } return false; @@ -281,12 +290,13 @@ public: for (Light *light : lights) { Ray shadowRay(hit.position + hit.normal * epsilon, light->position); float cosTheta = dot(hit.normal, light->position); - if (cosTheta > 0 && !shadowIntersect(shadowRay)) { // shadow computation - outRadiance = outRadiance + light->Le * hit.material->kd * cosTheta; - vec3 halfway = normalize(-ray.dir + light->position); + if (cosTheta > 0 && !shadowIntersect(shadowRay, light)) { // shadow computation + vec3 le = light->Le / powf(light->getDistance(hit.position), 2); + outRadiance = outRadiance + le * hit.material->kd * cosTheta; + vec3 halfway = normalize(-ray.dir + light->getDirection(hit.position)); float cosDelta = dot(hit.normal, halfway); - if (cosDelta > 0) outRadiance = outRadiance + light->Le * hit.material->ks * - powf(cosDelta, hit.material->shininess); + if (cosDelta > 0) + outRadiance = outRadiance + le * hit.material->ks * powf(cosDelta, hit.material->shininess); } } }