raytracer-c/main/sphere_shadow.c
2020-08-21 00:13:11 -04:00

53 lines
1.8 KiB
C

#include "canvas.h"
#include "exceptions.h"
#include "ray.h"
#include <stdio.h>
#include <sphere.h>
int main(void) {
CEXCEPTION_T e;
Try {
double wall_z = 10;
double wall_size = 7;
unsigned int canvas_pixels = 500;
double pixel_size = wall_size / canvas_pixels;
double half = wall_size / 2;
CANVAS_Canvas* canvas = CANVAS_new(canvas_pixels, canvas_pixels);
TUPLES_Color red = TUPLES_color(1, 0, 0);
TUPLES_Point ray_origin = TUPLES_point(0, 0, -5);
SPHERE_Sphere* sphere = SPHERE_new();
for (uint y=0; y < canvas_pixels - 1; y++) {
double world_y = half - pixel_size * y;
for (uint x=0; x < canvas_pixels - 1; x++) {
double world_x = -half + pixel_size * x;
TUPLES_Point position = TUPLES_point(world_x, world_y, wall_z);
TUPLES_Vector v_tmp = TUPLES_normalize(TUPLES_subtract(ray_origin, position));
RAY_Ray r;
RAY_init_from_tuples(&r, position, v_tmp);
RAY_Intersections* xs = SPHERE_intersect(sphere, &r, NULL);
if (RAY_hit(xs)) {
CANVAS_write_pixel(canvas, x, y, red);
}
RAY_delete_intersections(xs);
RAY_destroy(&r);
}
}
char *filename = "sphere_shadow.ppm";
printf("Writing file %s...", filename);
CANVAS_write_to_file(canvas, filename);
printf("done\n");
SPHERE_delete(sphere);
CANVAS_delete(canvas);
} Catch(e) {
if (e == E_MALLOC_FAILED)
printf("Malloc failed. Exiting\n");
else if (e == E_FILE_FAILED)
printf("Failed to open test.ppm\n");
else
printf("Unknown exception %i occurred\n", e);
}
return 0;
}