Simplify the Hittable API
We need to disable strict aliasing, but it is weird anyways: https://blog.regehr.org/archives/1307
This commit is contained in:
parent
e397ee9ad9
commit
f6278ce63e
2
build.sh
2
build.sh
@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
CC=${CC:-"gcc"}
|
CC=${CC:-"gcc"}
|
||||||
CFLAGS=${CFLAGS:-"-Wall -Wextra -std=gnu18 -g -fsanitize=address"}
|
CFLAGS=${CFLAGS:-"-Wall -Wextra -std=gnu18 -fno-strict-aliasing -g"}
|
||||||
LDFLAGS=${LDFLAGS:-"-lm"}
|
LDFLAGS=${LDFLAGS:-"-lm"}
|
||||||
MAIN_FILE=${MAIN_FILE:-"main.c"}
|
MAIN_FILE=${MAIN_FILE:-"main.c"}
|
||||||
|
|
||||||
|
|||||||
21
hittable.c
21
hittable.c
@ -15,25 +15,22 @@ bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
|||||||
HitRecord *record) {
|
HitRecord *record) {
|
||||||
switch (hittable->type) {
|
switch (hittable->type) {
|
||||||
case HITTABLE_LIST:
|
case HITTABLE_LIST:
|
||||||
return hittable_list_hit(hittable->data, r, t_min, t_max, record);
|
return hittable_list_hit((const HittableList *)hittable, r, t_min, t_max,
|
||||||
|
record);
|
||||||
case HITTABLE_SPHERE:
|
case HITTABLE_SPHERE:
|
||||||
return sphere_hit(hittable->data, r, t_min, t_max, record);
|
return sphere_hit((const Sphere *)hittable, r, t_min, t_max, record);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hittable make_hittable_list(const HittableList *list) {
|
|
||||||
return (Hittable){.type = HITTABLE_LIST, .data = list};
|
|
||||||
}
|
|
||||||
|
|
||||||
static void hittable_list_grow(HittableList *list, size_t n) {
|
static void hittable_list_grow(HittableList *list, size_t n) {
|
||||||
if (list->objects) {
|
if (list->objects) {
|
||||||
list->objects = realloc(list->objects, n);
|
list->objects = realloc(list->objects, n * sizeof(Hittable *));
|
||||||
if (!list->objects)
|
if (!list->objects)
|
||||||
abort();
|
abort();
|
||||||
list->capacity = n;
|
list->capacity = n;
|
||||||
} else {
|
} else {
|
||||||
list->objects = malloc(n * sizeof(Hittable));
|
list->objects = malloc(n * sizeof(Hittable *));
|
||||||
if (!list->objects)
|
if (!list->objects)
|
||||||
abort();
|
abort();
|
||||||
list->capacity = n;
|
list->capacity = n;
|
||||||
@ -41,7 +38,7 @@ static void hittable_list_grow(HittableList *list, size_t n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hittable_list_add(HittableList *list, Hittable hittable) {
|
void hittable_list_add(HittableList *list, const Hittable *hittable) {
|
||||||
if (list->capacity == list->size)
|
if (list->capacity == list->size)
|
||||||
hittable_list_grow(list, list->capacity == 0 ? 16 : list->capacity);
|
hittable_list_grow(list, list->capacity == 0 ? 16 : list->capacity);
|
||||||
list->objects[list->size++] = hittable;
|
list->objects[list->size++] = hittable;
|
||||||
@ -53,7 +50,7 @@ bool hittable_list_hit(const HittableList *list, Ray r, double t_min,
|
|||||||
double closest_so_far = t_max;
|
double closest_so_far = t_max;
|
||||||
|
|
||||||
for (size_t i = 0; i < list->size; ++i) {
|
for (size_t i = 0; i < list->size; ++i) {
|
||||||
if (hittable_hit(&list->objects[i], r, t_min, closest_so_far, record)) {
|
if (hittable_hit(list->objects[i], r, t_min, closest_so_far, record)) {
|
||||||
hit_anything = true;
|
hit_anything = true;
|
||||||
closest_so_far = record->t;
|
closest_so_far = record->t;
|
||||||
}
|
}
|
||||||
@ -67,10 +64,6 @@ void hittable_list_free(HittableList *list) {
|
|||||||
list->objects = 0;
|
list->objects = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hittable make_hittable_sphere(const Sphere *sphere) {
|
|
||||||
return (Hittable){.type = HITTABLE_SPHERE, sphere};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool sphere_hit(const Sphere *sphere, Ray r, double t_min, double t_max,
|
bool sphere_hit(const Sphere *sphere, Ray r, double t_min, double t_max,
|
||||||
HitRecord *record) {
|
HitRecord *record) {
|
||||||
Vec3 oc = point3_sub(r.origin, sphere->center);
|
Vec3 oc = point3_sub(r.origin, sphere->center);
|
||||||
|
|||||||
13
hittable.h
13
hittable.h
@ -24,32 +24,29 @@ typedef enum HittableType {
|
|||||||
|
|
||||||
typedef struct Hittable {
|
typedef struct Hittable {
|
||||||
HittableType type;
|
HittableType type;
|
||||||
const void *data;
|
|
||||||
} Hittable;
|
} Hittable;
|
||||||
|
|
||||||
bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
||||||
HitRecord *record);
|
HitRecord *record);
|
||||||
|
|
||||||
typedef struct HittableList {
|
typedef struct HittableList {
|
||||||
Hittable *objects;
|
HittableType type;
|
||||||
|
const Hittable **objects;
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
} HittableList;
|
} HittableList;
|
||||||
|
|
||||||
Hittable make_hittable_list(const HittableList *list);
|
void hittable_list_add(HittableList *list, const Hittable *hittable);
|
||||||
|
|
||||||
void hittable_list_add(HittableList *list, Hittable hittable);
|
|
||||||
bool hittable_list_hit(const HittableList *list, Ray r, double t_min,
|
bool hittable_list_hit(const HittableList *list, Ray r, double t_min,
|
||||||
double t_max, HitRecord *record);
|
double t_max, HitRecord *record);
|
||||||
void hittable_list_free(HittableList *list);
|
void hittable_list_free(HittableList *list);
|
||||||
|
|
||||||
typedef struct Sphere {
|
typedef struct Sphere {
|
||||||
|
HittableType type;
|
||||||
Point3 center;
|
Point3 center;
|
||||||
double radius;
|
double radius;
|
||||||
} Sphere;
|
} Sphere;
|
||||||
|
|
||||||
Hittable make_hittable_sphere(const Sphere *sphere);
|
|
||||||
|
|
||||||
bool sphere_hit(const Sphere *sphere, Ray r, double t_min, double t_max,
|
bool sphere_hit(const Sphere *sphere, Ray r, double t_min, double t_max,
|
||||||
HitRecord *record);
|
HitRecord *record);
|
||||||
|
|
||||||
|
|||||||
32
main.c
32
main.c
@ -11,12 +11,12 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
Color ray_color(Ray r, Hittable world, int depth) {
|
Color ray_color(Ray r, const Hittable *world, int depth) {
|
||||||
if (depth <= 0)
|
if (depth <= 0)
|
||||||
return (Color){0, 0, 0};
|
return (Color){0, 0, 0};
|
||||||
|
|
||||||
HitRecord record;
|
HitRecord record;
|
||||||
if (hittable_hit(&world, r, 0.001, DBL_MAX, &record)) {
|
if (hittable_hit(world, r, 0.001, DBL_MAX, &record)) {
|
||||||
Point3 target = point3_add(
|
Point3 target = point3_add(
|
||||||
record.p, vec3_add(record.normal, vec3_random_unit_vector()));
|
record.p, vec3_add(record.normal, vec3_random_unit_vector()));
|
||||||
return color_mul(0.5,
|
return color_mul(0.5,
|
||||||
@ -39,16 +39,19 @@ int main(void) {
|
|||||||
const int max_depth = 50;
|
const int max_depth = 50;
|
||||||
|
|
||||||
/* World */
|
/* World */
|
||||||
HittableList object_list = {0};
|
HittableList world = {.type = HITTABLE_LIST};
|
||||||
hittable_list_add(&object_list, make_hittable_sphere(&(Sphere){
|
Sphere sphere1 = {
|
||||||
.center = (Point3){0, 0, -1},
|
.type = HITTABLE_SPHERE,
|
||||||
.radius = 0.5,
|
.center = (Point3){0, 0, -1},
|
||||||
}));
|
.radius = 0.5,
|
||||||
hittable_list_add(&object_list, make_hittable_sphere(&(Sphere){
|
};
|
||||||
.center = (Point3){0, -100.5, -1},
|
Sphere sphere2 = {
|
||||||
.radius = 100,
|
.type = HITTABLE_SPHERE,
|
||||||
}));
|
.center = (Point3){0, -100.5, -1},
|
||||||
Hittable world = make_hittable_list(&object_list);
|
.radius = 100,
|
||||||
|
};
|
||||||
|
hittable_list_add(&world, (const Hittable *)&sphere1);
|
||||||
|
hittable_list_add(&world, (const Hittable *)&sphere2);
|
||||||
|
|
||||||
/* Camera */
|
/* Camera */
|
||||||
Camera camera;
|
Camera camera;
|
||||||
@ -64,7 +67,8 @@ int main(void) {
|
|||||||
double u = (i + random_double()) / (image_width - 1);
|
double u = (i + random_double()) / (image_width - 1);
|
||||||
double v = (j + random_double()) / (image_height - 1);
|
double v = (j + random_double()) / (image_height - 1);
|
||||||
Ray r = camera_get_ray(&camera, u, v);
|
Ray r = camera_get_ray(&camera, u, v);
|
||||||
pixel_color = color_add(pixel_color, ray_color(r, world, max_depth));
|
pixel_color = color_add(
|
||||||
|
pixel_color, ray_color(r, (const Hittable *)&world, max_depth));
|
||||||
}
|
}
|
||||||
color_write(stdout, pixel_color, samples_per_pixel);
|
color_write(stdout, pixel_color, samples_per_pixel);
|
||||||
}
|
}
|
||||||
@ -72,7 +76,7 @@ int main(void) {
|
|||||||
|
|
||||||
fprintf(stderr, "\nDone.\n");
|
fprintf(stderr, "\nDone.\n");
|
||||||
|
|
||||||
hittable_list_free(&object_list);
|
hittable_list_free(&world);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user