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