@@ -101,6 +101,7 @@ Color rayColor(Ray r, HitTable world, int depth){
101101 return skyColor (r );
102102 }
103103
104+ // Old version of imageRender, kept for benchmarking purposes and for futures references. Do not use.
104105 void imageRender (HitTable scene , int spp , String filename ) {
105106 try (FileOutputStream file = new FileOutputStream (filename );
106107 PrintStream filePrint = new PrintStream (file ) ) {
@@ -141,18 +142,24 @@ void imageRenderParallel(HitTable scene, int spp, String filename) {
141142
142143 for (long y =0 ; y < image_height ; ++y ) {
143144 System .out .print ("\r " + "Scanlines remaining: " +(image_height -y ));
144- for (long x =0 ; x < image_width ; ++x ) {
145+ long finalY = y ;
146+ IntStream .range (0 , image_width ).parallel ().forEach (x -> {
145147 Color pixelColor = new Color (0 );
146-
147- long finalX = x ;
148- long finalY = y ;
149- IntStream .range (0 , spp ).parallel ().forEach (s -> {
150- double u = (finalX + randomDouble ()) / (image_width -1 );
151- double v = (finalY + randomDouble ()) / (image_height -1 );
148+ for (int s = 0 ; s < spp ; ++s ) {
149+ double u = (x + randomDouble ()) / (image_width - 1 );
150+ double v = (finalY + randomDouble ()) / (image_height - 1 );
152151 pixelColor .equalAdd (rayColor (camera .get_ray (u , v ), scene , depth ));
153- });
154- image .setRGB ((int )x , (int )y , writeAwtColor (pixelColor , spp ).getRGB ());
155- }
152+ }
153+ // pixelColor = pixelColor.multiply(1.0 / spp);
154+
155+ RayResult result = new RayResult ();
156+ int index = (int ) (finalY * image_width + x );
157+ result .color = pixelColor ;
158+ result .index = index ;
159+ synchronized (image ) {
160+ image .setRGB (index % image_width , index / image_width , writeAwtColor (result .color , spp ).getRGB ());
161+ }
162+ });
156163 }
157164 try {
158165 ImageIO .write (image , "png" , new java .io .File (filename ));
0 commit comments