diff --git a/build.sh b/build.sh index 8848b68..08854f0 100755 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ CC=${CC:-"gcc"} CFLAGS=${CFLAGS:-"-Wall -Wextra -std=c18 -g"} -LDFLAGS=${LDFLAGS:-""} +LDFLAGS=${LDFLAGS:-"-lm"} MAIN_FILE=${MAIN_FILE:-"main.c"} # Write out compile_flags.txt for clangd diff --git a/color.c b/color.c new file mode 100644 index 0000000..60d97bc --- /dev/null +++ b/color.c @@ -0,0 +1,7 @@ +#include "color.h" +#include + +void color_write(FILE *out, color c) { + fprintf(out, "%d %d %d\n", (int)(255.999 * c.r), (int)(255.999 * c.g), + (int)(255.999 * c.b)); +} diff --git a/color.h b/color.h new file mode 100644 index 0000000..15a4199 --- /dev/null +++ b/color.h @@ -0,0 +1,12 @@ +#ifndef INCLUDED_COLOR_H +#define INCLUDED_COLOR_H + +#include + +typedef struct color { + double r, g, b; +} color; + +void color_write(FILE *out, color c); + +#endif /* INCLUDED_COLOR_H */ diff --git a/main.c b/main.c index 7c21cb5..ee8553e 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,7 @@ #include +#include "color.h" + int main(void) { const int image_width = 256; const int image_height = 256; @@ -9,15 +11,12 @@ int main(void) { for (int j = image_height - 1; j >= 0; --j) { fprintf(stderr, "\rScanlines remaining: %d ", j); for (int i = 0; i < image_width; ++i) { - double r = (double)(i) / (image_width - 1); - double g = (double)(j) / (image_height - 1); - double b = 0.25; - - int ir = (int)(255.999 * r); - int ig = (int)(255.999 * g); - int ib = (int)(255.999 * b); - - printf("%d %d %d\n", ir, ig, ib); + color pixel_color = { + .r = (double)(i) / (image_width - 1), + .g = (double)(j) / (image_height - 1), + .b = 0.25, + }; + color_write(stdout, pixel_color); } } @@ -25,3 +24,7 @@ int main(void) { return 0; } + +#include "color.c" +#include "point3.c" +#include "vec3.c" diff --git a/point3.c b/point3.c new file mode 100644 index 0000000..ec218b6 --- /dev/null +++ b/point3.c @@ -0,0 +1,9 @@ +#include "point3.h" + +point3 point3_add(point3 p, vec3 v) { + return (point3){p.x + v.x, p.y + v.y, p.z + v.z}; +} + +vec3 point3_sub(point3 p1, point3 p2) { + return (vec3){p1.x - p2.x, p1.y - p2.y, p1.z - p2.z}; +} diff --git a/point3.h b/point3.h new file mode 100644 index 0000000..158a0e4 --- /dev/null +++ b/point3.h @@ -0,0 +1,13 @@ +#ifndef INCLUDED_POINT3_H +#define INCLUDED_POINT3_H + +#include "vec3.h" + +typedef struct point3 { + double x, y, z; +} point3; + +point3 point3_add(point3 p, vec3 v); +vec3 point3_sub(point3 p1, point3 p2); + +#endif /* INCLUDED_POINT3_H */ diff --git a/vec3.c b/vec3.c new file mode 100644 index 0000000..3586c22 --- /dev/null +++ b/vec3.c @@ -0,0 +1,35 @@ +#include "vec3.h" + +#include + +vec3 vec3_neg(vec3 v) { return (vec3){-v.x, -v.y, -v.z}; } + +vec3 vec3_add(vec3 v1, vec3 v2) { + return (vec3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z}; +} + +vec3 vec3_sub(vec3 v1, vec3 v2) { + return (vec3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z}; +} + +vec3 vec3_mul(double c, vec3 v) { return (vec3){c * v.x, c * v.y, c * v.z}; } + +vec3 vec3_div(vec3 v, double c) { return (vec3){v.x / c, v.y / c, v.z / c}; } + +double vec3_length(vec3 v) { return sqrt(vec3_length2(v)); } + +double vec3_length2(vec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; } + +vec3 vec3_normalize(vec3 v) { return vec3_div(v, vec3_length(v)); } + +double vec3_dot(vec3 v1, vec3 v2) { + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; +} + +vec3 vec3_cross(vec3 v1, vec3 v2) { + return (vec3){ + v1.y * v2.z - v1.z * v2.y, + v1.z * v2.x - v1.x * v2.z, + v1.x * v2.y - v1.y * v2.x, + }; +} diff --git a/vec3.h b/vec3.h new file mode 100644 index 0000000..88393fc --- /dev/null +++ b/vec3.h @@ -0,0 +1,22 @@ +#ifndef INCLUDED_VEC3_H +#define INCLUDED_VEC3_H + +typedef struct vec3 { + double x, y, z; +} vec3; + +vec3 vec3_neg(vec3 v); +vec3 vec3_add(vec3 v1, vec3 v2); +vec3 vec3_sub(vec3 v1, vec3 v2); +vec3 vec3_mul(double c, vec3 v); +vec3 vec3_div(vec3 v, double c); + +double vec3_length(vec3 v); +double vec3_length2(vec3 v); + +vec3 vec3_normalize(vec3 v); + +double vec3_dot(vec3 v1, vec3 v2); +vec3 vec3_cross(vec3 v1, vec3 v2); + +#endif /* INCLUDED_VEC3_H */