Skip to content

Commit

Permalink
refactor and add docs to calcColor
Browse files Browse the repository at this point in the history
  • Loading branch information
MeirKlemp committed May 11, 2021
1 parent 1b46714 commit ae9ace6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/renderer/BasicRayTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,33 @@ public Color traceRay(Ray ray) {
return _scene.background;
}

/**
* Helper function for calculating the color at a given collision.
* @param geoPoint the point of the collision
* @param ray the ray from the camera
* @return the color at the collision
*/
private Color calcColor(GeoPoint geoPoint, Ray ray) {
Point3D p = geoPoint.point;
Vector n = geoPoint.geometry.getNormal(p);
double kd = geoPoint.geometry.getMaterial().kD;
double ks = geoPoint.geometry.getMaterial().kS;
double nShininess = geoPoint.geometry.getMaterial().nShininess;
Vector v = ray.getDir();

Color lightsColor = Color.BLACK;
for (LightSource ls : _scene.lights) {
double ln = ls.getL(p).dotProduct(n);
double vn = ray.getDir().dotProduct(n);
Vector l = ls.getL(p);
double ln = l.dotProduct(n);
double vn = v.dotProduct(n);
if (ln * vn < 0) {
continue;
}
Vector r = ls.getL(p).subtract(n.scale(2 * ln)).normalize();
double kd = geoPoint.geometry.getMaterial().kD;
double ks = geoPoint.geometry.getMaterial().kS;
double nShininess = geoPoint.geometry.getMaterial().nShininess;
double vr = ray.getDir().dotProduct(r);
double sumLightsEffects = kd * Math.abs(ln) + ks * Math.pow(Math.max(0, -vr),nShininess);
lightsColor = lightsColor.add(ls.getIntensity(p).scale(sumLightsEffects));

Vector r = l.subtract(n.scale(2 * ln)).normalize();
double vr = v.dotProduct(r);
double lightEffect = kd * Math.abs(ln) + ks * Math.pow(Math.max(0, -vr),nShininess);
lightsColor = lightsColor.add(ls.getIntensity(p).scale(lightEffect));
}
Color emissionColor = geoPoint.geometry.getEmission();
Color ambientLightColor = _scene.ambientLight.getIntensity();
Expand Down

0 comments on commit ae9ace6

Please sign in to comment.