Browse Source

distfuncs

alistair
alistair 4 years ago
parent
commit
ad61b8f435
  1. 1
      Makefile
  2. 1
      colours.c
  3. 110
      distfuncs.c
  4. 3
      main.c
  5. 4
      vect.c
  6. 12
      vect.h

1
Makefile

@ -16,6 +16,5 @@ distfuncs.o: distfuncs.c distfuncs.h @@ -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

1
colours.c

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
#include "colours.h"
int c_round(double a) {
return a - (int)a > 0.5 ? (int)a + 1 : (int)a;
}

110
distfuncs.c

@ -3,11 +3,11 @@ @@ -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) { @@ -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) { @@ -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;
}

3
main.c

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
#include <SDL2/SDL_thread.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <stdlib.h>
int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events
int exitnow = 0;
@ -58,7 +59,7 @@ int input_loop(void *ptr) { @@ -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;

4
vect.c

@ -342,7 +342,7 @@ vec_max(const struct vec *v) @@ -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) @@ -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;

12
vect.h

@ -4,9 +4,19 @@ @@ -4,9 +4,19 @@
#include <stdlib.h>
#include <stdio.h>
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);

Loading…
Cancel
Save