Commits

Anonymous committed f2de55c

add pixel subsampling, change scene

Comments (0)

Files changed (1)

raytrace/raytrace.c

     int    (*intersect)(Shape, Ray* );
 } Object;
 
+// TODO: use objects as lights
 typedef struct {
     Point  position;
     double intensity;
 typedef struct {
     int xres;
     int yres;
+    int sub;
     int samples;
     double fov; // radians
     double near_plane;
     return d < 0 ? 0 : d > 1 ? 1 : d;
 }
 
+// TODO: what is gamma correction correction / why the pow?
 void print_color(Color c)
 {
-    printf("%d %d %d ", (int)(clamp(c.x) * 255), (int)(clamp(c.y) * 255), (int)(clamp(c.z) * 255));
+    printf("%d %d %d ", (int)(pow(clamp(c.x),1/2.2) * 255), (int)(pow(clamp(c.y),1/2.2) * 255), (int)(pow(clamp(c.z),1/2.2) * 255));
 }
 
 int qroots(double a, double b, double c, double *r1, double *r2)
     double t = tan(c.fov / 2);
     Vector pixel;
 
-    //pixel.x = (-1 + 2 * (x + 0.5) / c.xres) * t * aspect_ratio;
-    //pixel.y = ( 1 - 2 * (y + 0.5) / c.yres) * t;
-    pixel.x = (-1 + 2 * (x + drand48()) / c.xres) * t * aspect_ratio;
-    pixel.y = ( 1 - 2 * (y + drand48()) / c.yres) * t;
+    pixel.x = (-1 + 2 * (x + drand48() / c.sub) / c.xres) * t * aspect_ratio;
+    pixel.y = ( 1 - 2 * (y + drand48() / c.sub) / c.yres) * t;
     pixel.z =  -1;
     pixel = normalize(pixel);
 
 
 void render(Camera c, Object *o, Light *l, Color bg)
 {
-    int s, x, y;
+    int s, x, y, subx, suby;
     Color color;
 
     for (y = 0; y < c.yres; y++) {
         for (x = 0; x < c.xres; x++) {
-            for (s = 0, color = BLACK; s < c.samples; s++)
-                color = add(color, trace(primary_ray(c, x, y), o, l, bg));
-            print_color(scalar_multiply(1. / c.samples, color));
+            color = BLACK;
+            for (suby = 0; suby < c.sub; suby++)
+                for (subx = 0; subx < c.sub; subx++)
+                    for (s = 0; s < c.samples; s++)
+                        color = add(color, trace(primary_ray(c, x + (double)subx / c.sub, y + (double)suby / c.sub), o, l, bg));
+            print_color(scalar_multiply(1. / (c.samples * c.sub * c.sub), color));
         }
     }
 }
 
 int main(void)
 {
-    Camera c = { 640, 480, 20, radians(80), 1, 100, { 0, 0, 0 }, { 0, 0, 1 } };
+    Camera c = { 640, 480, 2, 10, radians(45), 1, 1000, { 0, 0, 0 }, { 0, 0, 1 } };
     Object objects[] = {
-        { { .sphere   = { {   0,   0, -10 },                   2                   } }, { 0, 0, 1 }, intersect_sphere   },
-        { { .sphere   = { {   1,   1,  -9 },                   1.5                 } }, { 0, 1, 0 }, intersect_sphere   },
-        { { .disk     = { {   0,   0,   0 },                   {  0,  0,  1 } , 25 } }, { 0, 1, 1 }, intersect_disk     },
-        { { .disk     = { {   0,   0, -20 },                   {  0,  0,  1 } , 25 } }, { 0, 1, 1 }, intersect_disk     },
-        { { .disk     = { { -10,   0,   0 },                   {  1,  0,  0 } , 25 } }, { 1, 0, 0 }, intersect_disk     },
-        { { .disk     = { {  10,   0,   0 },                   { -1,  0,  0 } , 25 } }, { 1, 1, 0 }, intersect_disk     },
-        { { .disk     = { {   0,  10,   0 },                   {  0, -1,  0 } , 25 } }, { 1, 1, 1 }, intersect_disk     },
-        { { .disk     = { {   0, -10,   0 },                   {  0,  1,  0 } , 25 } }, { 1, 1, 1 }, intersect_disk     },
-        { { .disk     = { {  -1,  -1,  -8 }, normalize((Vector){ -1,  0, .5 }), .4 } }, { 1, 0, 1 }, intersect_disk     },
-        { { .triangle = { { { -1, -10, -15 }, { 1, -10, -15 }, { 0, -8, -15 } } }    }, { 1, 0, 0 }, intersect_triangle },
+        { { .sphere   = { {  -5.5,  -7, -35 }, 3                                     } }, {  .9 ,  .9 ,  .9  }, intersect_sphere   },
+        { { .sphere   = { {   4.5,  -7, -30 }, 3                                     } }, {  .9 ,  .9 ,  .9  }, intersect_sphere   },
+        { { .triangle = { { {-9  , -10, -26 }, { -7, -10, -24 }, { -7.5, -7, -25 } } } }, {  .75,  .25,  .75 }, intersect_triangle },
+        { { .triangle = { { {-6.5, -10, -27 }, { -7, -10, -24 }, { -7.5, -7, -25 } } } }, {  .75,  .25,  .75 }, intersect_triangle },
+        { { .triangle = { { {-6.5, -10, -27 }, { -9, -10, -26 }, { -7.5, -7, -25 } } } }, {  .75,  .25,  .75 }, intersect_triangle },
+        { { .disk     = { {   0  ,   0,   0 }, {  0,   0,   1 }, 50                  } }, { 0   , 0   , 0    }, intersect_disk     }, // front
+        { { .disk     = { {   0  ,   0, -40 }, {  0,   0,   1 }, 50                  } }, {  .75,  .75,  .75 }, intersect_disk     }, // back
+        { { .disk     = { { -10  ,   0,   0 }, {  1,   0,   0 }, 50                  } }, {  .75,  .25,  .25 }, intersect_disk     }, // left
+        { { .disk     = { {  10  ,   0,   0 }, { -1,   0,   0 }, 50                  } }, {  .25,  .25,  .75 }, intersect_disk     }, // right
+        { { .disk     = { {   0  ,  10,   0 }, {  0,  -1,   0 }, 50                  } }, {  .75,  .75,  .75 }, intersect_disk     }, // top
+        { { .disk     = { {   0  , -10,   0 }, {  0,   1,   0 }, 50                  } }, {  .75,  .75,  .75 }, intersect_disk     }, // bottom
         { 0 }
     };
     Light lights[] = {
-        { { -9,  2, -3 }, 200 },
-        { { -9, -2, -3 }, 100 },
+        { { 5,  5, -15 }, 300 },
         { 0 }
     };
 
     printf("P3\n%d %d\n%d\n", c.xres, c.yres, 255);
-    render(c, objects, lights, (Color){ 1, 1, 1 });
+    render(c, objects, lights, BLACK);
 
     return 0;
 }