Add box hittables
This commit is contained in:
parent
990b9d3016
commit
e78b539258
47
hittable.c
47
hittable.c
@ -161,6 +161,39 @@ Hittable *hittable_create_yz_rectangle(double y0, double y1, double z0,
|
||||
return result;
|
||||
}
|
||||
|
||||
Hittable *hittable_create_box(Point3 p0, Point3 p1, const Material *material,
|
||||
Arena *arena) {
|
||||
Hittable *result = arena_alloc(arena, sizeof(Hittable));
|
||||
result->type = HITTABLE_BOX;
|
||||
result->box.min = p0;
|
||||
result->box.max = p1;
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_xy_rectangle(p0.x, p1.x, p0.y, p1.y, p0.z,
|
||||
material, arena),
|
||||
arena);
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_xy_rectangle(p0.x, p1.x, p0.y, p1.y, p1.z,
|
||||
material, arena),
|
||||
arena);
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_xz_rectangle(p0.x, p1.x, p0.z, p1.z, p0.y,
|
||||
material, arena),
|
||||
arena);
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_xz_rectangle(p0.x, p1.x, p0.z, p1.z, p1.y,
|
||||
material, arena),
|
||||
arena);
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_yz_rectangle(p0.y, p1.y, p0.z, p1.z, p0.x,
|
||||
material, arena),
|
||||
arena);
|
||||
hittable_list_add(&result->box.sides,
|
||||
hittable_create_yz_rectangle(p0.y, p1.y, p0.z, p1.z, p1.x,
|
||||
material, arena),
|
||||
arena);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool hittable_list_hit(const HittableList *list, Ray r, double t_min,
|
||||
double t_max, HitRecord *record) {
|
||||
bool hit_anything = false;
|
||||
@ -329,6 +362,11 @@ static bool yz_rectangle_hit(const YZRectangle *rectangle, Ray r, double t_min,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool box_hit(const Box *box, Ray r, double t_min, double t_max,
|
||||
HitRecord *record) {
|
||||
return hittable_list_hit(&box->sides, r, t_min, t_max, record);
|
||||
}
|
||||
|
||||
bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
||||
HitRecord *record) {
|
||||
switch (hittable->type) {
|
||||
@ -346,6 +384,8 @@ bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
||||
return xz_rectangle_hit(&hittable->xz_rectangle, r, t_min, t_max, record);
|
||||
case HITTABLE_YZ_RECTANGLE:
|
||||
return yz_rectangle_hit(&hittable->yz_rectangle, r, t_min, t_max, record);
|
||||
case HITTABLE_BOX:
|
||||
return box_hit(&hittable->box, r, t_min, t_max, record);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -437,6 +477,11 @@ static bool yz_rectangle_bounding_box(const YZRectangle *rectangle,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool box_bounding_box(const Box *box, AABB *bounding_box) {
|
||||
*bounding_box = (AABB){box->min, box->max};
|
||||
return true;
|
||||
}
|
||||
|
||||
bool hittable_bounding_box(const Hittable *hittable, double time_start,
|
||||
double time_end, AABB *bounding_box) {
|
||||
switch (hittable->type) {
|
||||
@ -456,6 +501,8 @@ bool hittable_bounding_box(const Hittable *hittable, double time_start,
|
||||
return xz_rectangle_bounding_box(&hittable->xz_rectangle, bounding_box);
|
||||
case HITTABLE_YZ_RECTANGLE:
|
||||
return yz_rectangle_bounding_box(&hittable->yz_rectangle, bounding_box);
|
||||
case HITTABLE_BOX:
|
||||
return box_bounding_box(&hittable->box, bounding_box);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
10
hittable.h
10
hittable.h
@ -30,6 +30,7 @@ typedef enum HittableType {
|
||||
HITTABLE_XY_RECTANGLE,
|
||||
HITTABLE_XZ_RECTANGLE,
|
||||
HITTABLE_YZ_RECTANGLE,
|
||||
HITTABLE_BOX,
|
||||
} HittableType;
|
||||
|
||||
typedef struct Hittable Hittable;
|
||||
@ -74,6 +75,12 @@ typedef struct YZRectangle {
|
||||
double y0, y1, z0, z1, k;
|
||||
} YZRectangle;
|
||||
|
||||
typedef struct Box {
|
||||
HittableList sides;
|
||||
Point3 min;
|
||||
Point3 max;
|
||||
} Box;
|
||||
|
||||
struct Hittable {
|
||||
HittableType type;
|
||||
union {
|
||||
@ -84,6 +91,7 @@ struct Hittable {
|
||||
XYRectangle xy_rectangle;
|
||||
XZRectangle xz_rectangle;
|
||||
YZRectangle yz_rectangle;
|
||||
Box box;
|
||||
};
|
||||
};
|
||||
|
||||
@ -105,6 +113,8 @@ Hittable *hittable_create_xz_rectangle(double x0, double x1, double z0,
|
||||
Hittable *hittable_create_yz_rectangle(double y0, double y1, double z0,
|
||||
double z1, double k,
|
||||
const Material *material, Arena *arena);
|
||||
Hittable *hittable_create_box(Point3 p0, Point3 p1, const Material *material,
|
||||
Arena *arena);
|
||||
|
||||
bool hittable_hit(const Hittable *hittable, Ray r, double t_min, double t_max,
|
||||
HitRecord *record);
|
||||
|
||||
13
main.c
13
main.c
@ -219,7 +219,18 @@ static Hittable *cornell_box(Arena *arena) {
|
||||
&world->list,
|
||||
hittable_create_xy_rectangle(0, 555, 0, 555, 555, white, arena), arena);
|
||||
|
||||
return world;
|
||||
hittable_list_add(&world->list,
|
||||
hittable_create_box((Point3){130, 0, 65},
|
||||
(Point3){295, 165, 230}, white, arena),
|
||||
arena);
|
||||
hittable_list_add(&world->list,
|
||||
hittable_create_box((Point3){265, 0, 295},
|
||||
(Point3){430, 330, 460}, white, arena),
|
||||
arena);
|
||||
|
||||
Hittable *bvh_root = hittable_create_bvh_node(
|
||||
world->list.objects, 0, world->list.size, 0.0, 1.0, arena);
|
||||
return bvh_root;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user