diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2018-03-19 21:39:09 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2018-03-19 22:02:08 -0700 |
commit | b5bc962e834992eeba2abdd10f6e37ee2ba20295 (patch) | |
tree | 11bf58a64208806817c0fcab481588086297ce6c /src/libs/ray/ray_3f.h | |
parent | 3b9a4861d6937a66b03791b5b497e47c52189a7d (diff) |
ray: libize raytracer core, introduces src/libs
This is the first step of breaking out all the core rendering stuffs
into reusable libraries and making modules purely compositional,
consumers of various included rendering/effects libraries.
Expect multiple modules leveraging libray for a variety of scenes and
such. Also expect compositions mixing the various libraries for more
interesting visuals.
Diffstat (limited to 'src/libs/ray/ray_3f.h')
-rw-r--r-- | src/libs/ray/ray_3f.h | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/libs/ray/ray_3f.h b/src/libs/ray/ray_3f.h new file mode 100644 index 0000000..2c04a5a --- /dev/null +++ b/src/libs/ray/ray_3f.h @@ -0,0 +1,154 @@ +#ifndef _RAY_3F_H +#define _RAY_3F_H + +#include <math.h> + +typedef struct ray_3f_t { + float x, y, z; +} ray_3f_t; + + +/* return the result of (a + b) */ +static inline ray_3f_t ray_3f_add(const ray_3f_t *a, const ray_3f_t *b) +{ + ray_3f_t res = { + .x = a->x + b->x, + .y = a->y + b->y, + .z = a->z + b->z, + }; + + return res; +} + + +/* return the result of (a - b) */ +static inline ray_3f_t ray_3f_sub(const ray_3f_t *a, const ray_3f_t *b) +{ + ray_3f_t res = { + .x = a->x - b->x, + .y = a->y - b->y, + .z = a->z - b->z, + }; + + return res; +} + + +/* return the result of (-v) */ +static inline ray_3f_t ray_3f_negate(const ray_3f_t *v) +{ + ray_3f_t res = { + .x = -v->x, + .y = -v->y, + .z = -v->z, + }; + + return res; +} + + +/* return the result of (a * b) */ +static inline ray_3f_t ray_3f_mult(const ray_3f_t *a, const ray_3f_t *b) +{ + ray_3f_t res = { + .x = a->x * b->x, + .y = a->y * b->y, + .z = a->z * b->z, + }; + + return res; +} + + +/* return the result of (v * scalar) */ +static inline ray_3f_t ray_3f_mult_scalar(const ray_3f_t *v, float scalar) +{ + ray_3f_t res = { + .x = v->x * scalar, + .y = v->y * scalar, + .z = v->z * scalar, + }; + + return res; +} + + +/* return the result of (uv / scalar) */ +static inline ray_3f_t ray_3f_div_scalar(const ray_3f_t *v, float scalar) +{ + ray_3f_t res = { + .x = v->x / scalar, + .y = v->y / scalar, + .z = v->z / scalar, + }; + + return res; +} + + +/* return the result of (a . b) */ +static inline float ray_3f_dot(const ray_3f_t *a, const ray_3f_t *b) +{ + return a->x * b->x + a->y * b->y + a->z * b->z; +} + + +/* return the length of the supplied vector */ +static inline float ray_3f_length(const ray_3f_t *v) +{ + return sqrtf(ray_3f_dot(v, v)); +} + + +/* return the normalized form of the supplied vector */ +static inline ray_3f_t ray_3f_normalize(const ray_3f_t *v) +{ + return ray_3f_mult_scalar(v, 1.0f / ray_3f_length(v)); +} + + +/* return the distance between two arbitrary points */ +static inline float ray_3f_distance(const ray_3f_t *a, const ray_3f_t *b) +{ + ray_3f_t delta = ray_3f_sub(a, b); + + return ray_3f_length(&delta); +} + + +/* return the cross product of two unit vectors */ +static inline ray_3f_t ray_3f_cross(const ray_3f_t *a, const ray_3f_t *b) +{ + ray_3f_t product; + + product.x = a->y * b->z - a->z * b->y; + product.y = a->z * b->x - a->x * b->z; + product.z = a->x * b->y - a->y * b->x; + + return product; +} + + +/* return the linearly interpolated vector between the two vectors at point alpha (0-1.0) */ +static inline ray_3f_t ray_3f_lerp(const ray_3f_t *a, const ray_3f_t *b, float alpha) +{ + ray_3f_t lerp_a, lerp_b; + + lerp_a = ray_3f_mult_scalar(a, 1.0f - alpha); + lerp_b = ray_3f_mult_scalar(b, alpha); + + return ray_3f_add(&lerp_a, &lerp_b); +} + + +/* return the normalized linearly interpolated vector between the two vectors at point alpha (0-1.0) */ +static inline ray_3f_t ray_3f_nlerp(const ray_3f_t *a, const ray_3f_t *b, float alpha) +{ + ray_3f_t lerp; + + lerp = ray_3f_lerp(a, b, alpha); + + return ray_3f_normalize(&lerp); +} + +#endif |