// Render the scene public void Render(bool debugging) { Ray ray; for (int y = 0; y < Screen.height; y++) { for (int x = 0; x < Screen.width / 2; x++) { ray = Camera.getRay(x, y); Intersection intersection = Scene.intersect(ray); Screen.pixels[x + Screen.width / 2 + y * Screen.width] = intersection.color(Scene).getColor(); if (debugging && y == 256 && x % 50 == 0) { if (intersection.ThingWeIntersectedWith != Scene.Primitives[4]) { intersection.debug(Screen); } else { ray.debug(Screen, 7, 3); } } } if (debugging) { foreach (Primitive p in Scene.Primitives) { p.debug(Screen); } Camera.debug(Screen); } } }
// Determine the colors on the scene public VPoint color(Scene scene) { if (ThingWeIntersectedWith != null) { ShadowRays = new Ray[scene.Lights.Length]; VPoint diffusion = new VPoint(); for (int i = 0; i < scene.Lights.Length; i++) { Light light = scene.Lights[i]; VPoint shadowRayDirection = (light.Location - Location); ShadowRays[i] = new Ray(Location + 0.00001f * shadowRayDirection.Normalize(), shadowRayDirection.Normalize()); ShadowRays[i].Distance = shadowRayDirection.Length; float distance = scene.intersect(ShadowRays[i]).Distance; if (distance >= shadowRayDirection.Length - 2 * 0.00001) { ShadowRays[i].Distance = distance; VPoint j = ThingWeIntersectedWith.normal(Location).Direction; if (j * Ray.Direction > 0) { j *= -1; } diffusion += light.reflectedColor(ThingWeIntersectedWith.Mat.GetColor(Location), 60 * Math.Max(0, j * ShadowRays[i].Direction.Normalize()) * (1 / (shadowRayDirection.Length * shadowRayDirection.Length))); } } diffusion = new VPoint(Math.Min(diffusion.X, 255), Math.Min(diffusion.Y, 255), Math.Min(diffusion.Z, 255)); if (ThingWeIntersectedWith.Mat.Reflects != 0 && Ray.recursion < Game.Recursion) { secondaryRay = ThingWeIntersectedWith.Reflect(Ray, Location); secondaryRay.recursion = Ray.recursion + 1; Intersection inter = scene.intersect(secondaryRay); if (inter.ThingWeIntersectedWith == scene.Primitives[0]) { secondaryRay.Distance = 3; } else { secondaryRay.Distance = inter.Distance; } return(VPoint.colorStuff(inter.color(scene), diffusion, ThingWeIntersectedWith.Mat.Reflects)); } else { return(diffusion); } } return(new VPoint()); }