From 53e4d9a56c1f0fe5c5c7a61de3b249cd05e36c46 Mon Sep 17 00:00:00 2001 From: Julian T Date: Mon, 23 Mar 2020 19:12:02 +0100 Subject: Renders a cool animation --- main.c | 98 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 42 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 82b376c..7402c3c 100644 --- a/main.c +++ b/main.c @@ -8,12 +8,12 @@ #include "scene.h" #include "pgm.h" -#define TESTW 1000 -#define TESTH 1000 +#define TESTW 512 +#define TESTH 512 #define PERCENTSTEP (TESTH / 100) -#define WORKERS 4 +#define WORKERS 8 void *worker_func(void *arg); @@ -38,15 +38,15 @@ COORD_T ray_rand(void *seed) int main() { container_init(cont, 5, 4, 1); - + // Init space_t space_t *s = container_prepare_space(cont);; - + // Set space options color_set(&s->ambient, 0.09, 0.09, 0.09); color_set(&s->back, 0.8, 0.8, 0.8); color_set(&s->env_color, 0.13, 0.13, 0.13); - s->env_samples = 16; + s->env_samples = 256; // Set viewpoint options vector_set(&s->view.position, 0, 16, 6); @@ -83,8 +83,6 @@ int main() mpl->shine = 50; mpl->reflective = 0.0; - viewpoint_init(&s->view); - object_t *o = add_object(cont, TYPE_SPHERE); vector_set(&o->sph.center, 0, 4, 7); o->sph.radius = 5; @@ -114,51 +112,67 @@ int main() vector_set(&l->pos, 20, 10, 30); color_set(&l->defuse, 0.3, 0.3, 0.3); color_set(&l->specular, 0.5, 0.5, 0.5); + color_t *image = malloc( sizeof(color_t) * TESTW * TESTH); - pgm_write_header(stdout, TESTW, TESTH); + char filename[20]; + COORD_T r = 16; + for (int angle = 0; angle < 360; angle += 4) { + COORD_T rad = (COORD_T)angle * (3.14159265 / 180); + // Calculate x and y for camera + vector_set(&s->view.position, r * cos( rad ), r * sin( rad ), 6); + printf("rad: %lf, x: %lf, y: %lf\n", rad, s->view.position.x, s->view.position.y); + viewpoint_init(&s->view); - // Create image array. Not as memory efficient but much simpler when multiprocessing - color_t *image = malloc( sizeof(color_t) * TESTW * TESTH); - if (!image) { - fprintf(stderr, "Could not allocate image array"); - exit(1); - } + - if (pthread_mutex_init(&percentlock, NULL)) { - fprintf(stderr, "Could not percent lock\n"); - exit(1); - } + sprintf(filename, "img%d.ppm", angle); + FILE *file = fopen(filename, "w"); + pgm_write_header(file, TESTW, TESTH); - // Hire the workers - pthread_t workers[WORKERS]; - for (int i = 0; i < WORKERS; i++) { - office_t *office = malloc(sizeof(office_t)); - office->id = i; - office->image = image; - - // Start them and show them their office(chunk in the array in this case :-D) - int rc = pthread_create(&workers[i], NULL, worker_func, office); - if (rc) { - fprintf(stderr, "Could not create worker %d\nsorry\n", i); + // Create image array. Not as memory efficient but much simpler when multiprocessing + if (!image) { + fprintf(stderr, "Could not allocate image array"); exit(1); } - } - // Wait for the threads to finish and print as we go - for (int i = 0; i < WORKERS; i++) { - pthread_join(workers[i], NULL); - - } + if (pthread_mutex_init(&percentlock, NULL)) { + fprintf(stderr, "Could not percent lock\n"); + exit(1); + } - // Print the stuff the worker was responsable for. - for (int y = 0; y < TESTH; y++) { - for(int x = 0; x < TESTW; x++) { - pgm_write_pixel(stdout, &image[ y * TESTW + x]); + // Hire the workers + pthread_t workers[WORKERS]; + for (int i = 0; i < WORKERS; i++) { + office_t *office = malloc(sizeof(office_t)); + office->id = i; + office->image = image; + + // Start them and show them their office(chunk in the array in this case :-D) + int rc = pthread_create(&workers[i], NULL, worker_func, office); + if (rc) { + fprintf(stderr, "Could not create worker %d\nsorry\n", i); + exit(1); + } } + + // Wait for the threads to finish and print as we go + for (int i = 0; i < WORKERS; i++) { + pthread_join(workers[i], NULL); + + } + + // Print the stuff the worker was responsable for. + for (int y = 0; y < TESTH; y++) { + for(int x = 0; x < TESTW; x++) { + pgm_write_pixel(file, &image[ y * TESTW + x]); + } + } + fflush(file); + fclose(file); } free(image); - + } void *worker_func(void *arg) { @@ -180,7 +194,7 @@ void *worker_func(void *arg) { if (y % PERCENTSTEP == 0) { // Unlock the thingy pthread_mutex_lock(&percentlock); - fprintf(stderr, "%d%\n", percent++); + //fprintf(stderr, "%d%\n", percent++); pthread_mutex_unlock(&percentlock); } } -- cgit v1.2.3