/** * The renderImage method renders the entire scene. * * @param scene The scene to be rendered */ public void renderImage(Scene scene) { // Get the output image Image image = scene.getImage(); Camera cam = scene.getCamera(); // Set the camera aspect ratio to match output image int width = image.getWidth(); int height = image.getHeight(); // Timing counters long startTime = System.currentTimeMillis(); // Do some basic setup Ray ray = new Ray(); Colord rayColor = new Colord(); int total = height * width; int counter = 0; int lastShownPercent = 0; double exposure = scene.getExposure(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { rayColor.setZero(); cam.getRay(ray, (x + 0.5) / width, (y + 0.5) / height); shadeRay(rayColor, scene, ray); rayColor.mul(exposure); image.setPixelColor(rayColor, x, y); counter++; if ((int) (100.0 * counter / total) != lastShownPercent) { lastShownPercent = (int) (100.0 * counter / total); System.out.println(lastShownPercent + "%"); } } } // Output time long totalTime = (System.currentTimeMillis() - startTime); System.out.println("Done. Total rendering time: " + (totalTime / 1000.0) + " seconds"); }
/** * This method returns the color along a single ray in outColor. * * @param outColor output space * @param scene the scene * @param ray the ray to shade */ public static void shadeRay(Colord outColor, Scene scene, Ray ray) { // TODO#A2: Compute the color of the intersection point. // 1) Find the first intersection of "ray" with the scene. // Record intersection in intersectionRecord. If it doesn't hit anything, // just return the scene's background color. // 2) Get the shader from the intersection record. // 3) Call the shader's shade() method to set the color for this ray. IntersectionRecord outRecord = new IntersectionRecord(); boolean itInterescts = scene.getFirstIntersection(outRecord, ray); if (!itInterescts) { outColor.set(scene.getBackColor()); } else { outRecord.surface.getShader().shade(outColor, scene, ray, outRecord); } }