foo
[erebus2020] / liberebus / src / erebus.h
1 #ifndef RENDLIB_H_
2 #define RENDLIB_H_
3
4 #include <cgmath/cgmath.h>
5
6 struct erb_node;
7 struct erb_surf;
8 struct erb_ray;
9 struct erb_hit;
10 struct erb_aabb;
11
12 typedef int (*erb_intersect_func)(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit);
13 typedef void (*erb_sample_func)(struct erb_surf *surf, cgm_vec3 *pos);
14 typedef void (*erb_bbox_func)(struct erb_surf *surf, struct erb_aabb *bb);
15
16 struct erb_aabb {
17         cgm_vec3 pos, sz;
18 };
19
20 struct erb_node {
21         struct erb_node *par;                                           /* parent node */
22         struct erb_node *clist;                                         /* child nodes */
23         struct erb_surf *surflist;                                      /* surfaces in this node */
24         float xform[16], inv_xform[16];                         /* global transformation */
25         float node_xform[16], inv_node_xform[16];       /* local transformation */
26 };
27
28 struct erb_surf {
29         struct erb_node *node;          /* transformation node for this surface */
30
31         struct erb_aabb bbox;           /* local space bounding box */
32         int bbox_valid;
33
34         erb_intersect_func isect;       /* intersection routine */
35         erb_sample_func sample;         /* random sample generation */
36         erb_bbox_func calc_bbox;        /* bounds calculation */
37
38         long data[1];                   /* type chosen to match worst case alignment req. */
39 };
40
41 struct erb_ray {
42         cgm_vec3 o, d;          /* origin and direction */
43         float tmin, tmax;       /* segment bounds */
44         float ior;                      /* IOR of the medium through which this ray travels */
45         float total_dist;       /* travel distance accumulator */
46 };
47
48 struct erb_hit {
49         float t, err;
50         struct erb_surf *surf;
51 };
52
53 int erb_init(void);
54 void erb_cleanup(void);
55
56 /* transformation nodes */
57 struct erb_node *erb_node(void);
58 void erb_free_node(struct erb_node *n);
59 int erb_node_addchild(struct erb_node *n, struct erb_node *c);
60 int erb_node_rmchild(struct erb_node *n, struct erb_node *c);
61 int erb_node_addsurf(struct erb_node *n, struct erb_surf *s);
62 int erb_node_rmsurf(struct erb_node *n, struct erb_surf *s);
63
64 void erb_node_setxform(struct erb_node *n, float *mat);
65 void erb_node_mulxform(struct erb_node *n, float *mat);
66 void erb_node_translate(struct erb_node *n, float x, float y, float z);
67 /* TODO ... more ... */
68
69 /* surfaces */
70 struct erb_surf *erb_surface(int datasz);
71 void erb_free_surface(struct erb_surf *surf);
72 struct erb_surf *erb_surf_sphere(float rad);
73 struct erb_surf *erb_surf_box(float xsz, float ysz, float zsz);
74
75 /* utility functions */
76 void erb_xform_ray(struct erb_ray *ray, float *mat);
77
78 #endif  /* RENDLIB_H_ */