diff options
author | Julian T <julian@jtle.dk> | 2020-03-02 18:57:28 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-03-02 18:58:17 +0100 |
commit | 1a849a6b3479a58f1101abadfe1995f4098f5354 (patch) | |
tree | 158a21deabfb2cd6250dca82e1219b13baeaf05a | |
parent | 9aefb74ee094399e2ffffb1ed9935fbead18d73c (diff) |
Added makefile and removed debug printing
-rw-r--r-- | Makefile | 38 | ||||
-rw-r--r-- | main.c | 17 | ||||
-rw-r--r-- | ray.c | 33 | ||||
-rw-r--r-- | scene.h | 1 | ||||
-rw-r--r-- | test.png | bin | 70902 -> 70902 bytes |
5 files changed, 51 insertions, 38 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..96789b8 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +CC=gcc + +# Enable debugging +CFLAGS=-ggdb + +# We need math +LDFLAGS=-lm + +# Output and build options +BINARY=raytrace +BUILD_DIR=build + +# Input files +c_files=$(wildcard *.c) +OBJ=$(patsubst %.c, $(BUILD_DIR)/%.o, $(c_files)) + +# Build rules +# $@ is the %.o file and $^ is the %.c file +$(BUILD_DIR)/%.o: %.c + mkdir -p $(dir $@) + $(CC) -c -o $@ $^ $(CFLAGS) + +# $@ becomes left part thus linked +$(BINARY): $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +.PHONY: run show clean + +# This will also generate the image +run: $(BINARY) + ./$(BINARY) | convert - test.png + +show: run + xdg-open test.png + +clean: + rm -f $(OBJ) $(BINARY) + rmdir $(BUILD_DIR) @@ -10,9 +10,6 @@ #define TESTW 1000 #define TESTH 1000 -color_t back = {255, 255, 255}; -int print = 0; - int main() { //printf("Starting\n"); @@ -20,6 +17,9 @@ int main() s.objects = NULL; s.lights = NULL; color_set(&s.ambient, 25, 25, 25); + // Currently havin issues with white background + // color_set(&s.back, 255, 255, 255); + color_set(&s.back, 0, 0, 0); material_t m; vector_set(&m.color, 1, 1, 1); @@ -41,7 +41,7 @@ int main() mpl.defuse = 1; mpl.specular = 0.0; mpl.shine = 50; - mpl.reflective = 0; + mpl.reflective = 0.0; vector_set(&s.view.position, 0, 14, 8); vector_set(&s.view.target, 0, 0, 0); @@ -61,19 +61,12 @@ int main() pgm_write_header(stdout, TESTW, TESTH); for (int y = TESTH; y; y--) { for (int x = TESTW; x; x--) { - if (0 && y <= 500) { - printf("Lets go"); - print = 1; - } color_t *c = ray_trace(&s, x, y); if (c) { pgm_write_pixel(stdout, c); - } else { - pgm_write_pixel(stdout, &back); - } + } free(c); - print = 0; } } @@ -5,8 +5,6 @@ #include "ray.h" -extern int print; - // https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection // http://viclw17.github.io/2018/07/16/raytracing-ray-sphere-intersection/ // https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection @@ -29,9 +27,6 @@ COORD_T ray_intersect_sphere(sphere_t *s, ray_t *ray, bool skip_dist) return -1; } if (skip_dist) { - if (print) { - printf("sph dist: skip\n"); - } return 1; } @@ -56,9 +51,6 @@ COORD_T ray_intersect_sphere(sphere_t *s, ray_t *ray, bool skip_dist) if (x0 < ZERO_APROX) { return -1; } - if (print) { - printf("sph dist: %f\n", x0); - } return x0; } @@ -72,15 +64,9 @@ COORD_T ray_intersect_plane(plane_t *p, ray_t *ray, bool skip_dist) // Take care of rounding errors if (nr < ZERO_APROX && nr > -ZERO_APROX) { - if (print) { - printf("Ohh no"); - } return -1; } if (skip_dist) { - if (print) { - printf("pdist: skip\n"); - } return 1; } @@ -90,9 +76,6 @@ COORD_T ray_intersect_plane(plane_t *p, ray_t *ray, bool skip_dist) vector_sub(&tmp, &tmp, ray->start); COORD_T t = vector_dot(&tmp, p->norm) / nr; - if (print) { - printf("pdist: %f\n", t); - } return t; } @@ -120,9 +103,6 @@ object_t *ray_cast(space_t *s, ray_t *r, COORD_T *dist_ret, bool chk, COORD_T ch while (o) { COORD_T d = ray_intersect(o, r, false); - if (print) - printf("Distance: %f\n", d); - if (d > ZERO_APROX) { if (chk && chk_dist > d) { if (dist_ret) { @@ -215,7 +195,8 @@ int ray_trace_recur(space_t *s, color_t *dest, ray_t *ray, unsigned hop, COORD_T object_t *o = ray_cast(s, ray, &dist, false, 0); if (!o) { - return 1; + color_add(&c, &c, &s->back); + goto exit; } vector_t rdir, rstart; @@ -242,8 +223,12 @@ int ray_trace_recur(space_t *s, color_t *dest, ray_t *ray, unsigned hop, COORD_T ray_trace_recur(s, &c, &r, hop+1, o->m->reflective); } + // Scale by the objects own color. color_scale_vector(&c, &c, &o->m->color); + +exit: + // Add it to the result color_scale(&c, &c, scale); color_add(dest, dest, &c); @@ -262,11 +247,7 @@ color_t *ray_trace(space_t *s, unsigned int x, unsigned int y) color_t *c = color_set(NULL, s->ambient.r, s->ambient.g, s->ambient.b); // Run the recursive ray trace - int status = ray_trace_recur(s, c, &r, 0, 1); - if (status) { - free(c); - return NULL; - } + ray_trace_recur(s, c, &r, 0, 1); return c; } @@ -61,6 +61,7 @@ typedef struct { light_t *lights; color_t ambient; + color_t back; } space_t; object_t *add_sphere(space_t *s, vector_t *c, COORD_T r, material_t *m); Binary files differ |