-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac7737f
commit a1d27f7
Showing
4 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "raytracing.h" | ||
#include <math.h> | ||
|
||
Vec3 *vec3vec3_add_(Vec3 *self, Vec3 other) { | ||
self->x += other.x; | ||
self->y += other.y; | ||
self->z += other.z; | ||
return self; | ||
} | ||
|
||
Vec3 *vec3vec3_sub_(Vec3 *self, Vec3 other) { | ||
self->x -= other.x; | ||
self->y -= other.y; | ||
self->z -= other.z; | ||
return self; | ||
} | ||
|
||
Vec3 *vec3float_mul_(Vec3 *self, float other) { | ||
self->x *= other; | ||
self->y *= other; | ||
self->z *= other; | ||
return self; | ||
} | ||
|
||
Vec3 vec3vec3_add(Vec3 u, Vec3 v) { return (Vec3){u.x + v.x, u.y + v.y, u.z + v.z}; } | ||
Vec3 vec3vec3_sub(Vec3 u, Vec3 v) { return (Vec3){u.x - v.x, u.y - v.y, u.z - v.z}; } | ||
Vec3 vec3vec3_mul(Vec3 u, Vec3 v) { return (Vec3){u.x * v.x, u.y * v.y, u.z * v.z}; } | ||
Vec3 vec3float_add(Vec3 u, float v) { return (Vec3){u.x + v, u.y + v, u.z + v}; } | ||
Vec3 vec3float_sub(Vec3 u, float v) { return (Vec3){u.x - v, u.y - v, u.z - v}; } | ||
Vec3 vec3float_mul(Vec3 u, float v) { return (Vec3){u.x * v, u.y * v, u.z * v}; } | ||
|
||
float vec3_length2(Vec3 u) { return u.x * u.x + u.y * u.y + u.z * u.z; } | ||
float vec3_length(Vec3 u) { return sqrtf(vec3_length2(u)); } | ||
float vec3_dot(Vec3 u, Vec3 v) { return u.x * v.x + u.y * v.y + u.z + v.z; } | ||
Vec3 vec3_cross(Vec3 u, Vec3 v) { return (Vec3){u.y * v.z - v.y * u.z, u.z * v.x - v.z * u.x, u.x * v.y - v.x * u.y}; } | ||
Vec3 vec3_unit(Vec3 u) { return vec3float_mul(u, 1.0f / vec3_length(u)); } | ||
|
||
Vec3 ray_at(Ray ray, float t) { return vec3vec3_add(ray.origin, vec3float_mul(ray.direction, t)); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
typedef struct Vec3 { | ||
float x; | ||
float y; | ||
float z; | ||
} Vec3; | ||
|
||
// https://stackoverflow.com/a/11763277 | ||
#define _GET_MACRO(_1, _2, _3, FUNC, ...) FUNC | ||
#define vec3_add2(x, y) _Generic((y), Vec3: vec3vec3_add, float: vec3float_add)(x, y) | ||
#define vec3_add3(x, y, z) vec3_add2(vec3_add2(x, y), z) | ||
#define vec3_add(...) _GET_MACRO(__VA_ARGS__, vec3_add3, vec3_add2)(__VA_ARGS__) | ||
|
||
#define vec3_sub(x, y) _Generic((y), Vec3: vec3vec3_sub, float: vec3float_sub)(x, y) | ||
#define vec3_mul(x, y) _Generic((y), Vec3: vec3vec3_mul, float: vec3float_mul)(x, y) | ||
|
||
Vec3 *vec3vec3_add_(Vec3 *self, Vec3 other); | ||
Vec3 *vec3vec3_sub_(Vec3 *self, Vec3 other); | ||
Vec3 *vec3float_mul_(Vec3 *self, float other); | ||
|
||
Vec3 vec3vec3_add(Vec3 u, Vec3 v); | ||
Vec3 vec3vec3_sub(Vec3 u, Vec3 v); | ||
Vec3 vec3vec3_mul(Vec3 u, Vec3 v); | ||
Vec3 vec3float_add(Vec3 u, float v); | ||
Vec3 vec3float_sub(Vec3 u, float v); | ||
Vec3 vec3float_mul(Vec3 u, float v); | ||
|
||
float vec3_length2(Vec3 u); | ||
float vec3_length(Vec3 u); | ||
float vec3_dot(Vec3 u, Vec3 v); | ||
Vec3 vec3_cross(Vec3 u, Vec3 v); | ||
Vec3 vec3_unit(Vec3 u); | ||
|
||
typedef struct Ray { | ||
Vec3 origin; | ||
Vec3 direction; | ||
} Ray; | ||
|
||
Vec3 ray_at(Ray ray, float t); |