diff options
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | pgm.c | 26 | ||||
-rw-r--r-- | pgm.h | 4 | ||||
-rw-r--r-- | ray.c | 6 | ||||
-rw-r--r-- | test.png | bin | 70902 -> 55642 bytes | |||
-rw-r--r-- | viewpoint.c | 2 | ||||
-rw-r--r-- | viewpoint.h | 2 |
7 files changed, 23 insertions, 21 deletions
@@ -16,7 +16,7 @@ int main() space_t s; s.objects = NULL; s.lights = NULL; - color_set(&s.ambient, 25, 25, 25); + color_set(&s.ambient, 0.09, 0.09, 0.09); // Currently havin issues with white background // color_set(&s.back, 255, 255, 255); color_set(&s.back, 0, 0, 0); @@ -55,7 +55,7 @@ int main() add_sphere(&s, vector_set(NULL, 8, 8, 3), 2, &m2); add_plane(&s, vector_set(NULL, 0, 0, 2), vector_set(NULL, 0, 0, 1), &mpl); //add_plane(&s, vector_set(NULL, 0, -20, 0), vector_set(NULL, 0, 1, 0), &mpl); - add_light(&s, vector_set(NULL, 20, 10, 30), color_set(NULL, 255, 255, 255), color_set(NULL, 150, 150, 150)); + add_light(&s, vector_set(NULL, 20, 10, 30), color_set(NULL, 1, 1, 1), color_set(NULL, 0.5, 0.5, 0.5)); //add_light(&s, vector_set(NULL, 0, 10, 20), color_set(NULL, 255, 255, 255), color_set(NULL, 150, 150, 150)); pgm_write_header(stdout, TESTW, TESTH); @@ -3,20 +3,22 @@ #include <stdio.h> #include <stdlib.h> +#define COLOR_MAX 255 + int pgm_write_header(FILE *fp, unsigned int w, unsigned int h) { - return fprintf(fp, "P3\n%d %d\n255\n", w, h); + return fprintf(fp, "P3\n%d %d\n%d\n", w, h, COLOR_MAX); } int pgm_write_pixel(FILE *fp, color_t *c) { - return fprintf(fp, "%d %d %d\n", c->r, c->g, c->b); + return fprintf(fp, "%.0lf %.0lf %.0lf\n", c->r * COLOR_MAX, c->g * COLOR_MAX, c->b * COLOR_MAX); } color_t *color_set(color_t *c, uint8_t r, uint8_t g, uint8_t b) { if (!c) { - c = (color_t *) malloc(sizeof(c)); + c = (color_t *) malloc(sizeof(color_t)); } c->r = r; @@ -32,32 +34,32 @@ color_t *color_add(color_t *dest, color_t *a, color_t *b) dest = (color_t *) malloc(sizeof(dest)); } - unsigned int tmp = a->r + b->r; - dest->r = tmp > 255 ? 255 : tmp; + COORD_T tmp = a->r + b->r; + dest->r = tmp > 1 ? 1 : tmp; tmp = a->g + b->g; - dest->g = tmp > 255 ? 255 : tmp; + dest->g = tmp > 1 ? 1 : tmp; tmp = a->b + b->b; - dest->b = tmp > 255 ? 255 : tmp; + dest->b = tmp > 1 ? 1 : tmp; return dest; } -color_t *color_scale(color_t *dest, color_t *a, float b) +color_t *color_scale(color_t *dest, color_t *a, COORD_T b) { if (!dest) { dest = (color_t *) malloc(sizeof(dest)); } - unsigned int tmp = a->r * b; - dest->r = tmp > 255 ? 255 : tmp; + COORD_T tmp = a->r * b; + dest->r = tmp > 1 ? 1 : tmp; tmp = a->g * b; - dest->g = tmp > 255 ? 255 : tmp; + dest->g = tmp > 1 ? 1 : tmp; tmp = a->b * b; - dest->b = tmp > 255 ? 255 : tmp; + dest->b = tmp > 1 ? 1 : tmp; return dest; } @@ -6,11 +6,11 @@ #include "vector.h" typedef struct { - uint8_t r, g, b; + COORD_T r, g, b; } color_t; color_t *color_set(color_t *c, uint8_t r, uint8_t g, uint8_t b); -color_t *color_scale(color_t *dest, color_t *a, float b); +color_t *color_scale(color_t *dest, color_t *a, COORD_T b); color_t *color_add(color_t *dest, color_t *a, color_t *b); color_t *color_scale_vector(color_t *dest, color_t *a, vector_t *v); @@ -237,15 +237,15 @@ exit: color_t *ray_trace(space_t *s, unsigned int x, unsigned int y) { + // Init return color. Will be accumilated with all the detected light. + color_t *c = color_set(NULL, s->ambient.r, s->ambient.g, s->ambient.b); + // Setup primary ray ray_t r; r.start = &s->view.position; r.direction = vector_copy(NULL, NULL); viewpoint_ray(&s->view, r.direction, x, y); - // Init return color. Will be accumilated with all the detected light. - color_t *c = color_set(NULL, s->ambient.r, s->ambient.g, s->ambient.b); - // Run the recursive ray trace ray_trace_recur(s, c, &r, 0, 1); diff --git a/viewpoint.c b/viewpoint.c index 7786c12..e8d2a1e 100644 --- a/viewpoint.c +++ b/viewpoint.c @@ -47,7 +47,7 @@ void viewpoint_init(viewpoint_t *view) // Calculate ray for viewport w, h // https://en.wikipedia.org/wiki/Ray_tracing_(graphics) -void viewpoint_ray(viewpoint_t *view, vector_t *r, unsigned x, unsigned y) +void viewpoint_ray(viewpoint_t *view, vector_t *r, COORD_T x, COORD_T y) { // Calculate ray vector vector_copy(r, &view->blc); diff --git a/viewpoint.h b/viewpoint.h index 6e5178c..7abde6c 100644 --- a/viewpoint.h +++ b/viewpoint.h @@ -24,5 +24,5 @@ typedef struct { // Todo handle initial setup void viewpoint_init(viewpoint_t *view); -void viewpoint_ray(viewpoint_t *view, vector_t *r, unsigned x, unsigned y); +void viewpoint_ray(viewpoint_t *view, vector_t *r, COORD_T x, COORD_T y); #endif |