e6e067b338375a16fb8e4728694ec38dcfe380a9
[erebus2020] / liberebus / src / erebus.h
1 #ifndef RENDLIB_H_
2 #define RENDLIB_H_
3
4 #include <cgmath/cgmath.h>
5
6 struct erb_rend;
7 struct erb_node;
8 struct erb_surf;
9 struct erb_ray;
10 struct erb_hit;
11 struct erb_aabb;
12
13 struct erb_rect {
14         int x, y, w, h;
15 };
16
17 typedef int (*erb_intersect_func)(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit);
18 typedef void (*erb_hitgeom_func)(struct erb_surf *surf, struct erb_hit *hit);
19 typedef void (*erb_sample_func)(struct erb_surf *surf, cgm_vec3 *pos);
20 typedef void (*erb_bbox_func)(struct erb_surf *surf, struct erb_aabb *bb);
21
22 /* job (block/frame) completion callback type */
23 typedef void (*erb_done_func)(unsigned int job, struct erb_rect *rect, void *cls);
24
25 struct erb_aabb {
26         cgm_vec3 pos, sz;
27 };
28
29 struct erb_node {
30         struct erb_node *par;                                           /* parent node */
31         struct erb_node *clist;                                         /* child nodes */
32         struct erb_surf *surflist;                                      /* surfaces in this node */
33         float xform[16], inv_xform[16];                         /* global transformation */
34         float node_xform[16], inv_node_xform[16];       /* local transformation */
35 };
36
37 struct erb_surf {
38         struct erb_node *node;          /* transformation node for this surface */
39
40         struct erb_aabb bbox;           /* local space bounding box */
41         int bbox_valid;
42
43         erb_intersect_func isect;       /* intersection routine */
44         erb_hitgeom_func hitgeom;       /* calculate derived values of the erb_hit structure */
45         erb_sample_func sample;         /* random sample generation */
46         erb_bbox_func calc_bbox;        /* bounds calculation */
47
48         long data[1];                   /* type chosen to match worst case alignment req. */
49 };
50
51 struct erb_ray {
52         cgm_vec3 o, d;          /* origin and direction */
53         float tmin, tmax;       /* segment bounds */
54         float ior;                      /* IOR of the medium through which this ray travels */
55         float total_dist;       /* travel distance accumulator */
56 };
57
58 struct erb_hit {
59         float t, err;
60         cgm_vec3 pos, lpos;             /* global and local space hit positions */
61         struct erb_surf *surf;
62         float total_dist;
63
64         /* derived by calc_hit */
65         cgm_vec3 norm, tang;
66         float u, v;
67 };
68
69 struct erb_rend *erb_create(void);
70 void erb_destroy(struct erb_rend *erb);
71
72 int erb_allocframe(struct erb_rend *erb, int width, int height);
73 float *erb_getframe(struct erb_rend *erb);
74
75 /* clears the framebuffer and sample counters to begin rendering a new frame */
76 void erb_begin(struct erb_rend *erb);
77 /* finalizes the frame, averaging samples (optional) */
78 float *erb_end(struct erb_rend *erb);
79
80 void erb_set_done_callback(struct erb_rend *erb, erb_done_func donecb, void *cls);
81
82 void erb_queue_frame(struct erb_rend *erb, unsigned int job_id);
83 void erb_queue_block(struct erb_rend *erb, unsigned int job_id, int x, int y,
84                 int width, int height);
85 void erb_wait(struct erb_rend *erb);
86
87 void erb_primary_ray(struct erb_rend *erb, struct erb_ray *ray, int sample);
88 void erb_sample_ray(struct erb_rend *erb, struct erb_ray *ray, float *col);
89
90 /* transformation nodes */
91 struct erb_node *erb_node(void);
92 void erb_free_node(struct erb_node *n);
93 int erb_node_addchild(struct erb_node *n, struct erb_node *c);
94 int erb_node_rmchild(struct erb_node *n, struct erb_node *c);
95 int erb_node_addsurf(struct erb_node *n, struct erb_surf *s);
96 int erb_node_rmsurf(struct erb_node *n, struct erb_surf *s);
97
98 /* sets the transformation matrix and also calculates the inverse */
99 void erb_node_setxform(struct erb_node *n, float *mat);
100
101 /* surfaces */
102 struct erb_surf *erb_surface(int datasz);
103 void erb_free_surface(struct erb_surf *surf);
104 struct erb_surf *erb_surf_sphere(float rad);
105 struct erb_surf *erb_surf_box(float xsz, float ysz, float zsz);
106
107 /* utility functions */
108 void erb_xform_ray(struct erb_ray *inray, float *mat, cgm_vec3 *org, cgm_vec3 *dir);
109
110 #endif  /* RENDLIB_H_ */