working on the transformation node system
[erebus2020] / liberebus / src / erebus.h
index 6ec33e3..758e4d3 100644 (file)
@@ -3,24 +3,55 @@
 
 #include <cgmath/cgmath.h>
 
+struct erb_rend;
 struct erb_node;
 struct erb_surf;
 struct erb_ray;
 struct erb_hit;
 struct erb_aabb;
 
+struct erb_rect {
+       int x, y, w, h;
+};
+
 typedef int (*erb_intersect_func)(struct erb_surf *surf, struct erb_ray *ray, struct erb_hit *hit);
+typedef void (*erb_hitgeom_func)(struct erb_surf *surf, 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);
 
+/* job (block/frame) completion callback type */
+typedef void (*erb_done_func)(unsigned int job, struct erb_rect *rect, void *cls);
+
 struct erb_aabb {
        cgm_vec3 pos, sz;
 };
 
+struct erb_key {
+       long tm;
+       float val;
+};
+
+enum {
+       ERB_TRK_X, ERB_TRK_Y, ERB_TRK_Z,                                /* position (x,y,z) */
+       ERB_TRK_QX, ERB_TRK_QY, ERB_TRK_QZ, ERB_TRK_QW, /* rotation quat (xi+yj+zk+w) */
+       ERB_TRK_SX, ERB_TRK_SY, ERB_TRK_SZ,                             /* scaling (x,y,z) */
+       ERB_NUM_TRACKS
+};
+enum erb_interp { ERB_LINEAR, ERB_STEP, ERB_CUBIC };
+enum erb_extrap { ERB_CLAMP, ERB_EXTEND, ERB_REPEAT };
+
+struct erb_track {
+       enum erb_interp interp; /* interpolation mode */
+       enum erb_extrap extrap; /* extrapolation mode */
+       struct erb_key *keys;   /* dynamic array of keyframes */
+       int num_keys, max_keys; /* max_keys: allocated size, num_keys: used */
+};
+
 struct erb_node {
        struct erb_node *par;                                           /* parent node */
        struct erb_node *clist;                                         /* child nodes */
        struct erb_surf *surflist;                                      /* surfaces in this node */
+       struct erb_track track[ERB_NUM_TRACKS];         /* PRS keyframe tracks */
        float xform[16], inv_xform[16];                         /* global transformation */
        float node_xform[16], inv_node_xform[16];       /* local transformation */
 };
@@ -32,6 +63,7 @@ struct erb_surf {
        int bbox_valid;
 
        erb_intersect_func isect;       /* intersection routine */
+       erb_hitgeom_func hitgeom;       /* calculate derived values of the erb_hit structure */
        erb_sample_func sample;         /* random sample generation */
        erb_bbox_func calc_bbox;        /* bounds calculation */
 
@@ -47,11 +79,37 @@ struct erb_ray {
 
 struct erb_hit {
        float t, err;
+       cgm_vec3 pos, lpos;             /* global and local space hit positions */
        struct erb_surf *surf;
+       float total_dist;
+
+       /* derived by calc_hit */
+       cgm_vec3 norm, tang;
+       float u, v;
 };
 
-int erb_init(void);
-void erb_cleanup(void);
+struct erb_rend *erb_create(void);
+void erb_destroy(struct erb_rend *erb);
+
+int erb_allocframe(struct erb_rend *erb, int width, int height);
+float *erb_getframe(struct erb_rend *erb);
+
+void erb_setfov(struct erb_rend *erb, float vfov_deg);
+
+/* clears the framebuffer and sample counters to begin rendering a new frame */
+void erb_begin(struct erb_rend *erb);
+/* finalizes the frame, averaging samples (optional) */
+float *erb_end(struct erb_rend *erb);
+
+void erb_set_done_callback(struct erb_rend *erb, erb_done_func donecb, void *cls);
+
+void erb_queue_frame(struct erb_rend *erb, unsigned int job_id);
+void erb_queue_block(struct erb_rend *erb, unsigned int job_id, int x, int y,
+               int width, int height);
+void erb_wait(struct erb_rend *erb);
+
+void erb_primary_ray(struct erb_rend *erb, struct erb_ray *ray, int sample);
+void erb_sample_ray(struct erb_rend *erb, struct erb_ray *ray, float *col);
 
 /* transformation nodes */
 struct erb_node *erb_node(void);
@@ -61,10 +119,22 @@ 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);
 
+/* sets the transformation matrix and also calculates the inverse */
+void erb_node_pos(struct erb_node *n, long tm, float x, float y, float z);
+void erb_node_rot(struct erb_node *n, long tm, float qx, float qy, float qz, float qw);
+void erb_node_rot_axis(struct erb_node *n, long tm, float angle_deg, float x, float y, float z);
+void erb_node_scale(struct erb_node *n, long tm, float sx, float sy, float sz);
+/* calculates transformation matrices for time tm by keyframe interpolation */
+void erb_node_update(struct erb_node *n, long tm);
+/* sets transformation, ignoring animation keyframes */
 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 ... */
+
+void erb_node_eval_pos(struct erb_node *n, long tm, cgm_vec3 *pos);
+void erb_node_eval_rot(struct erb_node *n, long tm, cgm_quat *rot);
+void erb_node_eval_scale(struct erb_node *n, long tm, cgm_vec3 *scale);
+
+void erb_node_eval_matrix(struct erb_node *n, long tm, float *mat);                    /* mat[16] */
+void erb_node_eval_node_matrix(struct erb_node *n, long tm, float *mat);       /* mat[16] */
 
 /* surfaces */
 struct erb_surf *erb_surface(int datasz);
@@ -73,6 +143,6 @@ 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);
+void erb_xform_ray(struct erb_ray *inray, float *mat, cgm_vec3 *org, cgm_vec3 *dir);
 
 #endif /* RENDLIB_H_ */