From 2e09068ea0c58f8a464f8584b2bb4e9f23241e95 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Mon, 21 Dec 2020 14:25:11 +0200 Subject: [PATCH] foo --- liberebus/src/erebus.h | 34 ++++++++++++++ liberebus/src/surf.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ liberebus/src/util.c | 7 +++ 3 files changed, 160 insertions(+) create mode 100644 liberebus/src/surf.c create mode 100644 liberebus/src/util.c diff --git a/liberebus/src/erebus.h b/liberebus/src/erebus.h index c439e43..6ec33e3 100644 --- a/liberebus/src/erebus.h +++ b/liberebus/src/erebus.h @@ -7,9 +7,15 @@ struct erb_node; struct erb_surf; struct erb_ray; struct erb_hit; +struct erb_aabb; typedef int (*erb_intersect_func)(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit); typedef void (*erb_sample_func)(struct erb_surf *surf, cgm_vec3 *pos); +typedef void (*erb_bbox_func)(struct erb_surf *surf, struct erb_aabb *bb); + +struct erb_aabb { + cgm_vec3 pos, sz; +}; struct erb_node { struct erb_node *par; /* parent node */ @@ -22,8 +28,14 @@ struct erb_node { struct erb_surf { struct erb_node *node; /* transformation node for this surface */ + struct erb_aabb bbox; /* local space bounding box */ + int bbox_valid; + erb_intersect_func isect; /* intersection routine */ erb_sample_func sample; /* random sample generation */ + erb_bbox_func calc_bbox; /* bounds calculation */ + + long data[1]; /* type chosen to match worst case alignment req. */ }; struct erb_ray { @@ -41,4 +53,26 @@ struct erb_hit { int erb_init(void); void erb_cleanup(void); +/* transformation nodes */ +struct erb_node *erb_node(void); +void erb_free_node(struct erb_node *n); +int erb_node_addchild(struct erb_node *n, struct erb_node *c); +int erb_node_rmchild(struct erb_node *n, struct erb_node *c); +int erb_node_addsurf(struct erb_node *n, struct erb_surf *s); +int erb_node_rmsurf(struct erb_node *n, struct erb_surf *s); + +void erb_node_setxform(struct erb_node *n, float *mat); +void erb_node_mulxform(struct erb_node *n, float *mat); +void erb_node_translate(struct erb_node *n, float x, float y, float z); +/* TODO ... more ... */ + +/* surfaces */ +struct erb_surf *erb_surface(int datasz); +void erb_free_surface(struct erb_surf *surf); +struct erb_surf *erb_surf_sphere(float rad); +struct erb_surf *erb_surf_box(float xsz, float ysz, float zsz); + +/* utility functions */ +void erb_xform_ray(struct erb_ray *ray, float *mat); + #endif /* RENDLIB_H_ */ diff --git a/liberebus/src/surf.c b/liberebus/src/surf.c new file mode 100644 index 0000000..7d8d38a --- /dev/null +++ b/liberebus/src/surf.c @@ -0,0 +1,119 @@ +#include +#include "erebus.h" + +struct sph_data { + float rad; +}; +struct box_data { + cgm_vec3 sz; +}; + +static int null_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit); +static void null_sample(struct erb_surf *surf, cgm_vec3 *pos); +static void null_bbox(struct erb_surf *surf, struct erb_aabb *bb); + +static int sph_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit); +static void sph_sample(struct erb_surf *surf, cgm_vec3 *pos); +static void sph_bbox(struct erb_surf *surf, struct erb_aabb *bb); + +static int box_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit); +static void box_sample(struct erb_surf *surf, cgm_vec3 *pos); +static void box_bbox(struct erb_surf *surf, struct erb_aabb *bb); + + +struct erb_surf *erb_surface(int datasz) +{ + struct erb_surf *surf; + + if(!(surf = calloc(1, sizeof *surf - sizeof surf->data + datasz))) { + fprintf(stderr, "failed to allocate surface\n"); + return 0; + } + surf->isect = null_isect; + surf->sample = null_sample; + surf->calc_bbox = null_bbox; + return surf; +} + +void erb_free_surface(struct erb_surf *surf) +{ + if(surf->node) { + erb_node_rmsurf(surf->node, surf); + } + free(surf); +} + +struct erb_surf *erb_surf_sphere(float rad) +{ + struct erb_surf *surf; + struct sph_data *sdata; + + if(!(surf = erb_surface(sizeof *sdata))) { + return 0; + } + surf->isect = sph_isect; + surf->sample = sph_sample; + surf->calc_bbox = sph_bbox; + sdata = (struct sph_data*)surf->data; + sdata->rad = rad; + return surf; +} + +struct erb_surf *erb_surf_box(float xsz, float ysz, float zsz) +{ + struct erb_surf *surf; + struct box_data *bdata; + + if(!(surf = erb_surface(sizeof *bdata))) { + return 0; + } + surf->isect = box_isect; + surf->sample = box_sample; + surf->calc_bbox = box_bbox; + bdata = (struct box_data*)surf->data; + cgm_vcons(&bdata->sz, xsz, ysz, zsz); + return surf; +} + + +static int null_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit) +{ + return 0; +} + +static void null_sample(struct erb_surf *surf, cgm_vec3 *pos) +{ + pos->x = pos->y = pos->z = 0; +} + +static void null_bbox(struct erb_surf *surf, struct erb_aabb *bb) +{ + bb->pos.x = bb->pos.y = bb->pos.z = 0; + bb->sz.x = bb->sz.y = bb->sz.z = 0; +} + +static int sph_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit) +{ + return 0; +} + +static void sph_sample(struct erb_surf *surf, cgm_vec3 *pos) +{ +} + +static void sph_bbox(struct erb_surf *surf, struct erb_aabb *bb) +{ +} + +static int box_isect(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit) +{ + return 0; +} + +static void box_sample(struct erb_surf *surf, cgm_vec3 *pos) +{ +} + +static void box_bbox(struct erb_surf *surf, struct erb_aabb *bb) +{ +} diff --git a/liberebus/src/util.c b/liberebus/src/util.c new file mode 100644 index 0000000..a52f35a --- /dev/null +++ b/liberebus/src/util.c @@ -0,0 +1,7 @@ +#include "erebus.h" + +void erb_xform_ray(struct erb_ray *ray, float *mat) +{ + cgm_vmul_m4v3(&ray->o, mat); + cgm_vmul_m3v3(&ray->d, mat); +} -- 1.7.10.4