Commit fa74b1ad9755a8d4edc35d7f17dba5eb9c314aaf

Thomas de Grivel 2024-01-29T17:15:31

mirrors

diff --git a/Makefile b/Makefile
index c632a9d..9dc9b9c 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@ PROG = smallpt
 
 CXX_SOURCES = smallpt.cpp
 
+CXXFLAGS = -O3 -pipe # -fopenmp
+
 ${PROG}: ${CXX_SOURCES}
 	${CXX} ${CPPFLAGS} ${CXXFLAGS} ${LDFLAGS} ${CXX_SOURCES} -o ${PROG}
 
diff --git a/smallpt.cpp b/smallpt.cpp
index c5aee6b..cd78b4c 100644
--- a/smallpt.cpp
+++ b/smallpt.cpp
@@ -120,32 +120,32 @@ Sphere g_spheres[] = {
   Sphere(1e5,                           // Left
          Vec(1e5 + 1, 40.8, 81.6),
          Vec(),
-         Vec(0.75, 0.25, 0.25),
-         DIFFUSE),
+         Vec(1, 1, 1) * 0.8,
+         SPECULAR),
   Sphere(1e5,                           // Right
          Vec(-1e5 + 99, 40.8, 81.6),
          Vec(),
-         Vec(0.25, 0.25, 0.75),
-         DIFFUSE),
+         Vec(1, 1, 1) * 0.8,
+         SPECULAR),
   Sphere(1e5,                           // Back
          Vec(50, 40.8, 1e5),
          Vec(),
-         Vec(0.75, 0.75, 0.75),
-         DIFFUSE),
+         Vec(1, 1, 1) * 0.8,
+         SPECULAR),
   Sphere(1e5,                           // Front
          Vec(50, 40.8, -1e5 + 170),
          Vec(),
-         Vec(),
-         DIFFUSE),
+         Vec(1, 1, 1) * 0.8,
+         SPECULAR),
   Sphere(1e5,                           // Bottom
          Vec(50, 1e5, 81.6),
          Vec(),
-         Vec(0.75, 0.75, 0.75),
-         DIFFUSE),
+         Vec(1, 1, 1) * 0.999,
+         SPECULAR),
   Sphere(1e5,                           // Top
          Vec(50, -1e5 + 81.6, 81.6),
          Vec(),
-         Vec(0.75, 0.75, 0.75),
+         Vec(1, 1, 1) * 0.5,
          DIFFUSE),
   Sphere(16.5,                          // Mirror ball
          Vec(27, 16.5, 47),
@@ -269,23 +269,16 @@ int main (int argc, char *argv[])
   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
+  for (int y = 0; y < h; y++) {             // Loop over image rows
     fprintf(stderr, "\rRendering (%d spp) %5.2f%%",
             samples * 4,
             100.0 * y / (h - 1));
-    x = 0;
-    Xi[0] = 0;
-    Xi[1] = 0;
-    Xi[2] = static_cast<unsigned short>(y * y * y);
-    while (x < w) {                     // Loop over image columns
-      int sy = 0;
+    for (unsigned short x = 0,
+           Xi[3] = {0, 0, static_cast<unsigned short>(y * y * y)};
+           x < w; x++) {                // Loop over image columns
       int i = (h - y - 1) * w + x;
-      while (sy < 2) {                  // 2x2 subpixel rows
-        int sx = 0;
-        while (sx < 2) {                // 2x2 subpixel cols
+      for (int sy = 0; sy < 2; sy++) {          // 2x2 subpixel rows
+        for (int sx = 0; sx < 2; sx++) {        // 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);
@@ -302,13 +295,9 @@ int main (int argc, char *argv[])
           }
           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.
   fprintf(f, "P3\n%d %d\n%d\n", w, h, 255);