Skip to content
Snippets Groups Projects
Verified Commit 3c63b83f authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

actually positional light

parent 4d091853
No related branches found
No related tags found
No related merge requests found
...@@ -196,6 +196,15 @@ struct Light { ...@@ -196,6 +196,15 @@ struct Light {
position = _position; position = _position;
Le = _Le; 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; } float rnd() { return (float)rand() / RAND_MAX; }
...@@ -257,11 +266,11 @@ public: ...@@ -257,11 +266,11 @@ public:
return bestHit; return bestHit;
} }
bool shadowIntersect(Ray ray) { // for directional lights bool shadowIntersect(Ray ray, Light* light) { // for directional lights
for (Intersectable * object : objects) { for (Intersectable * object : objects) {
Hit hit = object->intersect(ray); Hit hit = object->intersect(ray);
if (hit.t < 0) if (hit.t > 0)
if(hit.t > length(hit.position - ray.start)) if(light->getDistance(ray.start) > hit.t)
return true; return true;
} }
return false; return false;
...@@ -281,12 +290,13 @@ public: ...@@ -281,12 +290,13 @@ public:
for (Light *light : lights) { for (Light *light : lights) {
Ray shadowRay(hit.position + hit.normal * epsilon, light->position); Ray shadowRay(hit.position + hit.normal * epsilon, light->position);
float cosTheta = dot(hit.normal, light->position); float cosTheta = dot(hit.normal, light->position);
if (cosTheta > 0 && !shadowIntersect(shadowRay)) { // shadow computation if (cosTheta > 0 && !shadowIntersect(shadowRay, light)) { // shadow computation
outRadiance = outRadiance + light->Le * hit.material->kd * cosTheta; vec3 le = light->Le / powf(light->getDistance(hit.position), 2);
vec3 halfway = normalize(-ray.dir + light->position); outRadiance = outRadiance + le * hit.material->kd * cosTheta;
vec3 halfway = normalize(-ray.dir + light->getDirection(hit.position));
float cosDelta = dot(hit.normal, halfway); float cosDelta = dot(hit.normal, halfway);
if (cosDelta > 0) outRadiance = outRadiance + light->Le * hit.material->ks * if (cosDelta > 0)
powf(cosDelta, hit.material->shininess); outRadiance = outRadiance + le * hit.material->ks * powf(cosDelta, hit.material->shininess);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment