Add vectors, points and colors
This commit is contained in:
parent
e13b93fac0
commit
14fa4b6c9a
2
build.sh
2
build.sh
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
CC=${CC:-"gcc"}
|
CC=${CC:-"gcc"}
|
||||||
CFLAGS=${CFLAGS:-"-Wall -Wextra -std=c18 -g"}
|
CFLAGS=${CFLAGS:-"-Wall -Wextra -std=c18 -g"}
|
||||||
LDFLAGS=${LDFLAGS:-""}
|
LDFLAGS=${LDFLAGS:-"-lm"}
|
||||||
MAIN_FILE=${MAIN_FILE:-"main.c"}
|
MAIN_FILE=${MAIN_FILE:-"main.c"}
|
||||||
|
|
||||||
# Write out compile_flags.txt for clangd
|
# Write out compile_flags.txt for clangd
|
||||||
|
|||||||
7
color.c
Normal file
7
color.c
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include "color.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
12
color.h
Normal file
12
color.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef INCLUDED_COLOR_H
|
||||||
|
#define INCLUDED_COLOR_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct color {
|
||||||
|
double r, g, b;
|
||||||
|
} color;
|
||||||
|
|
||||||
|
void color_write(FILE *out, color c);
|
||||||
|
|
||||||
|
#endif /* INCLUDED_COLOR_H */
|
||||||
21
main.c
21
main.c
@ -1,5 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "color.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const int image_width = 256;
|
const int image_width = 256;
|
||||||
const int image_height = 256;
|
const int image_height = 256;
|
||||||
@ -9,15 +11,12 @@ int main(void) {
|
|||||||
for (int j = image_height - 1; j >= 0; --j) {
|
for (int j = image_height - 1; j >= 0; --j) {
|
||||||
fprintf(stderr, "\rScanlines remaining: %d ", j);
|
fprintf(stderr, "\rScanlines remaining: %d ", j);
|
||||||
for (int i = 0; i < image_width; ++i) {
|
for (int i = 0; i < image_width; ++i) {
|
||||||
double r = (double)(i) / (image_width - 1);
|
color pixel_color = {
|
||||||
double g = (double)(j) / (image_height - 1);
|
.r = (double)(i) / (image_width - 1),
|
||||||
double b = 0.25;
|
.g = (double)(j) / (image_height - 1),
|
||||||
|
.b = 0.25,
|
||||||
int ir = (int)(255.999 * r);
|
};
|
||||||
int ig = (int)(255.999 * g);
|
color_write(stdout, pixel_color);
|
||||||
int ib = (int)(255.999 * b);
|
|
||||||
|
|
||||||
printf("%d %d %d\n", ir, ig, ib);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,3 +24,7 @@ int main(void) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "color.c"
|
||||||
|
#include "point3.c"
|
||||||
|
#include "vec3.c"
|
||||||
|
|||||||
9
point3.c
Normal file
9
point3.c
Normal file
@ -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};
|
||||||
|
}
|
||||||
13
point3.h
Normal file
13
point3.h
Normal file
@ -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 */
|
||||||
35
vec3.c
Normal file
35
vec3.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include "vec3.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
22
vec3.h
Normal file
22
vec3.h
Normal file
@ -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 */
|
||||||
Loading…
x
Reference in New Issue
Block a user