Skip to content

Instantly share code, notes, and snippets.

@Vatyx
Created November 19, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vatyx/357695d8f76cf7ddb4ce5a6429f42cef to your computer and use it in GitHub Desktop.
Save Vatyx/357695d8f76cf7ddb4ce5a6429f42cef to your computer and use it in GitHub Desktop.
vec3 Raytracer::getLighting(vec3 point, vec3 norm, vec3 origin, Lighting* lighting)
{
vec3 intensity(0, 0, 0);
auto ka = lighting->ka;
auto intensityAmbient = lighting->ia;
intensity = ka * intensityAmbient;
for (vec3 pointLight : lights)
{
bool shadow = false;
Ray virRay = Ray(point + .001 * norm, pointLight - point);
for (Surface* surface : surfaces)
{
float t = 0;
if (surface->findIntersect(virRay, t))
{
if (0 < t && t < 1)
{
shadow = true;
break;
}
}
}
if (shadow)
continue;
vec3 L = pointLight - point;
L.make_unit_vector();
vec3 intensityDiffuse(1, 1, 1);
auto kd = lighting->kd;
auto ks = lighting->ks;
int n = lighting->n;
vec3 R = L - 2 * dot(L, norm) * norm;
R.make_unit_vector();
vec3 eye = point - origin;
eye.make_unit_vector();
if (dot(L, norm) > 0)
{
intensity = intensity + (kd * intensityDiffuse * dot(L, norm)); //diffuse light;
intensity = intensity + (ks * intensityDiffuse * pow(max(0.0f, dot(R, eye)), n)); //specular light;
}
}
return intensity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment