public override ISect intersect(Ray currentRay) { ISect record = null; float a = currentRay.Direction.Dot(currentRay.Direction); float b = (currentRay.Origin - Position).Dot(currentRay.Direction) * 2.0f; float c = (currentRay.Origin - Position).Dot(currentRay.Origin - Position) - (float)Math.Pow(radius,2.0); float det = (float)Math.Pow(b, 2) - 4 * a * c; if (det < 0) return null; float t1 = (-b + (float)Math.Sqrt(det)) / (2 * a); float t2 = (-b - (float)Math.Sqrt(det)) / (2 * a); if (t1 < 0 && t2 < 0) return record; record = new ISect(); record.Thing = this; record.Ray = currentRay; if (t1 < 0) record.Dist = t2; else if (t2 < 0) record.Dist = t1; else record.Dist = Math.Min(t1, t2); return record; }
public static Vec3 GetPosition(Ray currentRay, float t) { return currentRay.Direction * t + currentRay.Origin; }
public void castRays() { /* Ray[] Rays = new Ray[width*height]; for(int y = 0; y<height; y++) for(int x = 0; x<width; x++) Rays[y*width+x] = new Ray(){ Origin = Camera.Origin, Direction = Camera.Direction + new Vec3(x/width-.5f,y/height-.5f,0) }; foreach (Ray currentRay in Rays) { var intersections = defaultScene.things .Select(thing => thing.intersect(currentRay)) .Where(t => t != null) .OrderBy(t=>t) .FirstOrDefault(); } */ Ray CurrentRay, reflectedRay; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { //Create Ray CurrentRay = new Ray() { Origin = Camera.Origin, Direction = (new Vec3(x / ((float)width) - .5f, y / ((float)height) - .5f, 0) - Camera.Origin).Normalize() }; //Intersect Ray var intersection = defaultScene.things .Select(thing => thing.intersect(CurrentRay)) .Where(t => t != null) .OrderBy(t => t.Dist) .FirstOrDefault(); var pixelColor = new Color(0, 0, 0); var reflection = 0.0f; for (int r = 0; r <= 5; r++) { if (intersection == null) break; pixelColor = pixelColor * reflection + (Color.phong(intersection, defaultScene) * (1 - reflection)); reflection = intersection.Thing.reflect; reflectedRay = new Ray() { Direction = intersection.Position() - intersection.Thing.Position, Origin = intersection.Position() + ((intersection.Position() - intersection.Thing.Position) * .01f) }; intersection = defaultScene.things .Select(thing => thing.intersect(reflectedRay)) .Where(t => t != null) .OrderBy(t => t.Dist) .FirstOrDefault(); } //Color Pixel //Buffer.Add(Color.phong(intersections, defaultScene) ?? new Color()); setPixel(x, y, Color.swap(pixelColor ?? new Color())); } return; }
public virtual Color phong(Ray currentRay, float t) { return new Color(0,0,0); }
public virtual ISect intersect(Ray currentRay) { return null; }
public override Color phong(Ray currentRay, float t) { return base.phong(currentRay, t); }