public override Color shade(RenderContext rc, Ray ray, HitRecord hrec, int depth) { Point hp = ray.pointOn(hrec.getT()); Vector normal = hrec.getPrimitive().normal(hp); hp += 0.0001f * normal; Scene scene = rc.getScene(); int numlights = scene.numberOfLights(); Color finalc = new Color(0,0,0); for ( int i = 0; i < numlights; i++ ) { Vector ldir = new Vector(); Color lcolor = new Color(); float ldist = scene.getLight(i).getLight(ref lcolor, ref ldir, rc, hp); HitRecord shadowhr = new HitRecord(); Ray shadowray = new Ray(hp, ldir); scene.getObject().intersect(ref shadowhr, rc, shadowray); if ( shadowhr.getT() >= ldist || shadowhr.getT() < 0.01 ) { float dp = Vector.dot(normal, ldir); if( dp > 0 ) { finalc += dp*lcolor; } } } return color * ( finalc * kd + scene.getAmbient() * ka ); }
public Color renderPixel(int x, int y) { Color color; HitRecord hitRecord = new HitRecord(); Ray ray; RenderContext renderContext = new RenderContext( this.scene ); float step = 2 / (float)this.scene.getXResolution(); float xStart = -1 + ( 0.5f * step ); float yStart = (-(float)this.scene.getYResolution() * (0.5f * step)) + (0.5f * step); ray = this.scene.getCamera().generateRay( xStart + (x * step), yStart + (y * step) ); this.scene.traceRay( ray, hitRecord, renderContext ); if ( hitRecord.getT() == float.PositiveInfinity || hitRecord.getMaterial() == null || hitRecord.getPrimitive() == null ) { color = this.scene.getBackground().getColor( renderContext, ray ); } else { color = hitRecord.getMaterial().shade( renderContext, ray, hitRecord, 1 ); } return color; }
public override Color shade(RenderContext rc, Ray ray, HitRecord hrec, int depth) { Scene scene = rc.getScene(); Point hp = ray.pointOn(hrec.getT()); Vector normal = hrec.getPrimitive().normal(hp); Vector v = scene.getCamera().getEye() - hp; v.normalize(); hp += 0.0001f * normal; int numlights = scene.numberOfLights(); Color finalc = new Color(0,0,0); for(int i = 0; i < numlights; i++) { Vector ldir = new Vector(); Color lcolor = new Color(); float ldist = scene.getLight(i).getLight(ref lcolor, ref ldir, rc, hp); HitRecord shadowhr = new HitRecord(); Ray shadowray = new Ray(hp, ldir); scene.getObject().intersect(ref shadowhr, rc, shadowray); if((shadowhr.getT() >= ldist || shadowhr.getT() < 0.01)) { float dp = Vector.dot(normal, ldir) * this.kd; if(!(dp > 0)) dp = 0; Vector r = 2*(Vector.dot(ldir, normal))*normal - ldir; r.normalize(); float spec = Vector.dot(r, v); if(!(spec > 0)) spec = 0; else spec = (float)System.Math.Pow(spec, this.n); finalc += (dp*kd+spec*ks)*lcolor; } } return color*(finalc + scene.getAmbient()*ka); }