private Color shade(Intersection isect, Scene scene, double depth) { var d = isect.ray.dir; var pos = Vector.plus(Vector.times(isect.dist, d), isect.ray.start); var normal = isect.thing.normal(pos); var reflectDir = Vector.minus(d, Vector.times(2, Vector.times(Vector.dot(normal, d), normal))); var naturalColor = Color.plus(Color.background, this.getNaturalColor(isect.thing, pos, normal, reflectDir, scene)); var reflectedColor = (depth >= this.maxDepth) ? Color.grey : this.getReflectionColor(isect.thing, pos, normal, reflectDir, scene, depth); return(Color.plus(naturalColor, reflectedColor)); }
public override Intersection intersect(Ray ray) { var denom = Vector.dot(norm, ray.dir); if (denom > 0) { return(null); } else { var dist = (Vector.dot(norm, ray.start) + offset) / (-denom); return(new Intersection(this, ray, dist)); } }
Color addLight(Color col, Light light) { var ldis = Vector.minus(light.pos, pos); var livec = Vector.norm(ldis); var neatIsect = this.testRay(new Ray(pos, livec), scene); var isInShadow = (neatIsect == null) ? false : (neatIsect <= Vector.mag(ldis)); if (isInShadow) { return(col); } else { var illum = Vector.dot(livec, norm); var lcolor = (illum > 0) ? Color.scale(illum, light.color) : Color.defaultColor; var specular = Vector.dot(livec, Vector.norm(rd)); var scolor = (specular > 0) ? Color.scale(Math.Pow(specular, thing.surface.roughness), light.color) : Color.defaultColor; return(Color.plus(col, Color.plus(Color.times(thing.surface.diffuse(pos), lcolor), Color.times(thing.surface.specular(pos), scolor)))); } }
public override Intersection intersect(Ray ray) { var eo = Vector.minus(this.center, ray.start); var v = Vector.dot(eo, ray.dir); var dist = 0.0; if (v >= 0) { var disc = this.radius2 - (Vector.dot(eo, eo) - v * v); if (disc >= 0) { dist = v - Math.Sqrt(disc); } } if (dist == 0) { return(null); } else { return(new Intersection(this, ray, dist)); } }