From ad61b8f435571cbde78ddfbc567c9f487077cecf Mon Sep 17 00:00:00 2001 From: alistair Date: Sat, 17 Oct 2020 17:37:50 +1000 Subject: [PATCH] distfuncs --- Makefile | 1 - colours.c | 1 - distfuncs.c | 110 +++++++++++++++++++++++++++++++++++++++++----------- main.c | 3 +- vect.c | 4 +- vect.h | 12 +++++- 6 files changed, 103 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index bd77e3f..c21beba 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,5 @@ distfuncs.o: distfuncs.c distfuncs.h camera.o: camera.c gcc $(LINKS) -c camera.c -o camera.o - clean: rm *.o blackpink diff --git a/colours.c b/colours.c index d4aef96..9fb62a5 100644 --- a/colours.c +++ b/colours.c @@ -1,6 +1,5 @@ #include "colours.h" - int c_round(double a) { return a - (int)a > 0.5 ? (int)a + 1 : (int)a; } diff --git a/distfuncs.c b/distfuncs.c index 819cabb..b13dda8 100644 --- a/distfuncs.c +++ b/distfuncs.c @@ -3,11 +3,11 @@ #include "vect.h" double sdf_sphere(struct vec *x) { - const double r = 1.4; + static const double r = 1.4; struct vec *v = copy_vec(x); v->elements[2] -= 5; - v->elements[1] += (SDL_GetTicks()/1000.0) - 5; +// v->elements[1] += (SDL_GetTicks()/1000.0) - 5; double res = magnitude_vec(v) - r; free_vec(v); @@ -18,38 +18,103 @@ double sdf_sphere(struct vec *x) { return res; } +double sdf_hplane(struct vec *x) { + static const double h = -5; + return -(x->e->y) - h ; +} + +double clamp(double val, double min, double max) { + if (val < min) + return min; + if (val > max) + return max; + return val; +} + +/* +double sdf_cone(struct vec *x) { + double height = 3; + struct vec *v = copy_vec(x); + double cx = 1; + double cy = 1; + static struct vec *cone = NULL; + static struct vec *w, *a, *b; + + if (!cone) { + cone = scalar_multiply_vec_ip(new_vec2(cx/cy, -1), height); + } + struct vec *temp; + + double cone2 = + + struct vec * temp = new_vec2(v->e->x, v->e->z); + double aaa = magnitude_vec(temp); + free_vec(temp); + w = new_vec2(aaa, v->e->y); + + double t2 = clamp((dot_product_vec(w, v) / dot_product_vec(cone, cone)), 0.0, 1.0); + temp = scalar_multiply_vec(cone, t2); + a = subtract_vec(w, temp); + free_vec(temp); + + temp = new_vec2(clamp(w->e->x / cone->e->x, 0.0, 1.0), 1.0); + scalar_multiply_vec_ip(temp, ) + + + + + + double result = -1; + free_vec(v); + return result; +} + */ + +double is_pos(double a) { + if (a > 0) { + return a; + } + return 0; +} + +double sdf_fat_vert_line(struct vec *x) { + static const double h = 10; + static const double r = 1; + + struct vec *v = copy_vec(x); + v->elements[2] -= 50; + + v->e->y -= clamp(v->e->y, 0.0, h); + double val = magnitude_vec(v) - r; + free_vec(v); + return val; +} /* https://www.alanzucconi.com/2016/07/01/signed-distance-functions/#part3 */ /* http://mercury.sexy/hg_sdf/ */ -double sdf_box(struct vec *v) { - const double s = 10; +double sdf_box(struct vec *x) { + + struct vec *v = copy_vec(x); + v->elements[2] -= 50; + do_on_vec_ip(v, fabs); - /* the NEGATED box shape */ static struct vec * box_shape = NULL; - struct vec * radius_vec = NULL; if (!box_shape) { - box_shape = new_vec(v->dimension); - for (int i = 0; i < box_shape->dimension; i++) { - box_shape->elements[i] = 1; - } - scalar_multiply_vec_ip(box_shape, -1); - radius_vec= new_vec_of(v->dimension, -s);; + box_shape = new_vec_of(v->dimension, -0.5); } + + do_on_vec_ip(v, is_pos); - struct vec * p = add_vec_ip(copy_vec(v), box_shape); // v - box_shape - do_on_vec_ip(p, fabs); - add_vec_ip(p, radius_vec); + double result = magnitude_vec(v); - double result = vec_max(p); - free_vec(p); + free_vec(v); return result; } - -struct colour simple_col(struct ray *ray) { - struct colour c = {.r = 255, .g = 255, .b = 255, .a = 255, .sp=CS_RGB}; +struct colour yeet(struct ray *ray) { + struct colour c = {.r = 255, .g = 0, .b = 255, .a = 255, .sp=CS_RGB}; return (c); } @@ -59,8 +124,9 @@ struct object new_sphere(double radius) { struct vec * v = new_vec4(0,0,5,0); s.sol.pos = *v; s.sol.op = B_ADD; - s.col = simple_col; - s.sol.dist = sdf_sphere; + s.col = yeet; + s.sol.dist = sdf_fat_vert_line; return s; } + diff --git a/main.c b/main.c index 8ac68b5..d9bbe9d 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include #include #include +#include int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events int exitnow = 0; @@ -58,7 +59,7 @@ int input_loop(void *ptr) { switch (event.type) { case SDL_QUIT: exitnow = 1; - *(int *)0 = 1; + // *(int *)0 = 1; // segfault break; case SDL_KEYDOWN: keyboardstate[event.key.keysym.scancode] = 1; diff --git a/vect.c b/vect.c index 64758e9..f7217b9 100644 --- a/vect.c +++ b/vect.c @@ -342,7 +342,7 @@ vec_max(const struct vec *v) for (int i = 0; i < v->dimension; i++) { if (i > max) - max = i; + max = v->elements[i]; } return max; @@ -355,7 +355,7 @@ vec_min(const struct vec *v) for (int i = 0; i < v->dimension; i++) { if (i < min) - min = i; + min = v->elements[i]; } return min; diff --git a/vect.h b/vect.h index b4ed5e4..e001d0d 100644 --- a/vect.h +++ b/vect.h @@ -4,9 +4,19 @@ #include #include +struct yeetVec { + double x; + double y; + double z; + double w; +}; + struct vec { int dimension; - double *elements; + union { + double *elements; + struct yeetVec *e; + }; }; struct vec* new_vec(int num_dimensions);