70 lines
1.8 KiB
C
70 lines
1.8 KiB
C
#include "canvas.h"
|
|
#include "exceptions.h"
|
|
#include "ray.h"
|
|
#include "sphere.h"
|
|
|
|
#include <unity.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
CEXCEPTION_T global_exception;
|
|
|
|
void setUp(void) {}
|
|
|
|
void tearDown(void) {}
|
|
|
|
void test_sphere_with_shadow(void) {
|
|
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_new_color(1, 0, 0);
|
|
|
|
TUPLES_Point* ray_origin = TUPLES_new_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_init_point(&position, world_x, world_y, wall_z);
|
|
TUPLES_Vector v_tmp;
|
|
TUPLES_subtract(&v_tmp, ray_origin, &position);
|
|
TUPLES_normalize(&v_tmp);
|
|
RAY_Ray r;
|
|
RAY_init_from_tuples(&r, &position, &v_tmp);
|
|
RAY_Intersections* xs = RAY_new_intersections();
|
|
SPHERE_local_intersect(xs, sphere, &r);
|
|
if (RAY_hit(xs)) {
|
|
CANVAS_write_pixel(canvas, x, y, red);
|
|
}
|
|
RAY_delete_intersections(xs);
|
|
TUPLES_destroy_all(&position, &v_tmp);
|
|
RAY_destroy(&r);
|
|
}
|
|
}
|
|
SPHERE_delete(sphere);
|
|
CANVAS_delete(canvas);
|
|
TUPLES_delete_all(red, ray_origin);
|
|
} Catch(global_exception) {
|
|
if (global_exception == E_MALLOC_FAILED) {
|
|
printf("Malloc failed. Exiting\n");
|
|
TEST_FAIL();
|
|
} else {
|
|
printf("Unknown exception %i occurred\n", global_exception);
|
|
TEST_FAIL();
|
|
}
|
|
}
|
|
TEST_PASS();
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_sphere_with_shadow);
|
|
return UNITY_END();
|
|
}
|