public static Intersection intersect(Ray ray, Model model) { Intersection result = new Intersection(false); int matchCount = 0; for (DrawedObject obj : model.getObjects()) { if (obj instanceof TriangleObject) { Intersection intersection = getIntersection(ray, (TriangleObject) obj, model); final boolean match = intersection.isMatch(); if (match) { ++matchCount; } if (match && intersection.getDistance() < result.getDistance()) { result = intersection; } } else if (obj instanceof SphereObject) { Intersection intersection = getIntersection(ray, (SphereObject) obj, model); final boolean match = intersection.isMatch(); if (match) { ++matchCount; } if (match && intersection.getDistance() < result.getDistance()) { result = intersection; } } } result.setIntersectionCount(matchCount); return result; }
public static BufferedImage render(Model model) { Camera cam = getCamera(model); BufferedImage result = new BufferedImage(model.getW(), model.getH(), BufferedImage.TYPE_INT_RGB); IntStream xs = IntStream.range(0, model.getW()).parallel(); Date startTime = new Date(); xs.forEach( x -> { IntStream ys = IntStream.range(0, model.getH()); ys.forEach( y -> { Ray ray = rayThruPixel(cam, x, y); Intersection hit = intersect(ray, model); result.setRGB(x, y, findColor(hit, cam, model)); }); }); Date endTime = new Date(); final float time = endTime.getTime() - startTime.getTime(); System.out.println("Executing time: " + time / 1000); return result; }
private static Camera getCamera(Model model) { Camera result = new Camera(); result.setFrom(model.getFrom()); result.setTo(model.getTo()); result.setUp(model.getUp()); result.setFov(model.getFov()); result.setWidth(model.getW()); result.setHeight(model.getH()); result.init(); return result; }