Commit 053e0afb1e590c7e8230809882c15501bd5db29a

Thomas de Grivel 2024-01-28T20:12:31

make code readable

diff --git a/smallpt.cpp b/smallpt.cpp
index da985ab..aee3c5c 100644
--- a/smallpt.cpp
+++ b/smallpt.cpp
@@ -213,25 +213,54 @@ int main (int argc, char *argv[])
   int h = 768;
   int samples = argc == 2 ? atoi(argv[1]) / 4 : 1;
   Ray camera(Vec(50,52,295.6), Vec(0,-0.042612,-1).normalize());
-  Vec cx=Vec(w*.5135/h), cy=(cx%camera.direction).norm()*.5135, r, *c=new Vec[w*h];
-#pragma omp parallel for schedule(dynamic, 1) private(r)       // OpenMP
-  for (int y=0; y<h; y++){                       // Loop over image rows
-    fprintf(stderr,"\rRendering (%d spp) %5.2f%%",samples*4,100.*y/(h-1));
-    for (unsigned short x=0, Xi[3]={0,0,static_cast<unsigned short>(y*y*y)}; x<w; x++)   // Loop cols
-      for (int sy=0, i=(h-y-1)*w+x; sy<2; sy++)     // 2x2 subpixel rows
-        for (int sx=0; sx<2; sx++, r=Vec()){        // 2x2 subpixel cols
-          for (int s=0; s<samples; s++){
-            F r1=2*erand48(Xi), dx=r1<1 ? Fsqrt(r1)-1: 1-Fsqrt(2-r1);
-            F r2=2*erand48(Xi), dy=r2<1 ? Fsqrt(r2)-1: 1-Fsqrt(2-r2);
-            Vec d = cx*( ( (sx+.5 + dx)/2 + x)/w - .5) +
-              cy*( ( (sy+.5 + dy)/2 + y)/h - .5) + camera.direction;
-            r = r + radiance(Ray(camera.origin + d*140,d.norm()),0,Xi)*(1./samples);
-          } // Camera rays are pushed ^^^^^ forward to start in interior
-          c[i] = c[i] + Vec(clamp(r.x),clamp(r.y),clamp(r.z))*.25;
+  Vec cx = Vec(w * .5135 / h);
+  Vec cy = (cx % camera.direction).normalize() * .5135;
+  Vec r;
+  Vec *c = new Vec[w * h];
+#pragma omp parallel for schedule(dynamic, 1) private(r)    // OpenMP
+  unsigned short x;
+  unsigned short Xi[3];
+  int y = 0;
+  while (y < h) {                       // Loop over image rows
+    fprintf(stderr, "\rRendering (%d spp) %5.2f%%",
+            samples * 4,
+            100.0 * y / (h - 1));
+    x = 0;
+    *Xi = (unsigned short [])
+      {0, 0, static_cast<unsigned short>(y * y * y)};
+    while (x < w) {                     // Loop over image columns
+      int sy = 0;
+      int i = (h - y - 1) * w + x;
+      while (sy < 2) {                  // 2x2 subpixel rows
+        int sx = 0;
+        while (sx < 2) {                // 2x2 subpixel cols
+          for (int s = 0; s < samples; s++){
+            F r1 = 2 * erand48(Xi);
+            F dx = r1 < 1 ? Fsqrt(r1) - 1 : 1 - Fsqrt(2 - r1);
+            F r2 = 2 * erand48(Xi);
+            F dy = r2 < 1 ? Fsqrt(r2)-1 : 1 - Fsqrt(2 - r2);
+            Vec d = cx * (((sx + 0.5 + dx) / 2 + x) / w - 0.5) +
+              cy * (((sy + 0.5 + dy) / 2 + y) / h - 0.5) +
+              camera.direction;
+            // Camera rays are pushed forward to start in interior
+            r = r +
+              radiance(Ray(camera.origin + d * 140,
+                           d.normalize()), 0, Xi) *
+              (1.0 / samples);
+          }
+          c[i] = c[i] + Vec(clamp(r.x), clamp(r.y), clamp(r.z)) * 0.25;
+          r = Vec();
+          sx++;
         }
+        sy++;
+      }
+      x++;
+    }
+    y++;
   }
-  FILE *f = fopen("image.ppm", "w");         // Write image to PPM file.
+  FILE *f = fopen("image.ppm", "w");    // Write image to PPM file.
   fprintf(f, "P3\n%d %d\n%d\n", w, h, 255);
-  for (int i=0; i<w*h; i++)
-    fprintf(f,"%d %d %d ", toInt(c[i].x), toInt(c[i].y), toInt(c[i].z));
+  for (int i = 0; i < w * h; i++)
+    fprintf(f, "%d %d %d ", toInt(c[i].x), toInt(c[i].y),
+            toInt(c[i].z));
 }