From 0ef8e84c082a80354b502b169b6c79fccffa4405 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:22:11 -0500 Subject: Make a headless entry point for benchmarking purposes --- ray.c | 112 ++++++++++++++++-------------------------------------------------- 1 file changed, 26 insertions(+), 86 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index d80125c..018ace1 100644 --- a/ray.c +++ b/ray.c @@ -1,16 +1,13 @@ +#include "ray.h" + #include #include #include #include #include -#include -#include -#include #include "3dmath.h" -#define WIDTH 1000 -#define HEIGHT 1000 #define BUFFER_SIZE (WIDTH * HEIGHT * 4) #define LENGTH(array) (sizeof(array) / sizeof(array[0])) @@ -30,8 +27,11 @@ typedef struct { float diffuse[3]; } Light; -static unsigned char threaded = 0; -static unsigned char buffer[BUFFER_SIZE]; +typedef struct { + unsigned char* buffer; + long line; +} ThreadArg; + static Object objects[] = { {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, 0, .8}}, {.position={0, 1.414, -3}, .radius=1, .diffuse={0, .8, .8}}, @@ -93,23 +93,35 @@ trace_line(int l, unsigned char *buf) { static void * thread(void *arg) { - long line = (long) arg; + ThreadArg* thread_arg = arg; - trace_line(line, buffer + line * 4 * WIDTH); + trace_line(thread_arg->line, thread_arg->buffer + thread_arg->line * 4 * WIDTH); - pthread_exit(NULL); + return NULL; } -static void -trace_scene(unsigned char *buf) { +void +trace_scene(float time, unsigned char *buf, int threaded) { + + objects[0].position[0] = 1.5 * cos(time); + objects[0].position[1] = 1.5 * sin(time); + objects[1].position[0] = 1.5 * cos(time + 1/3. * TAU); + objects[1].position[1] = 1.5 * sin(time + 1/3. * TAU); + objects[3].position[0] = 1.5 * cos(time + 2/3. * TAU); + objects[3].position[1] = 1.5 * sin(time + 2/3. * TAU); + objects[2].position[2] = -3 + 2 * sin(time * 2); if(threaded) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + ThreadArg thread_args[HEIGHT]; pthread_t threads[HEIGHT]; for(long i = 0; i < HEIGHT; ++i) { - int ret = pthread_create(&threads[i], &attr, thread, (void *)i); + thread_args[i].line = i; + thread_args[i].buffer = buf; + + int ret = pthread_create(&threads[i], &attr, thread, &thread_args[i]); if(ret) { fprintf(stderr, "pthread_create(): %d\n", ret); @@ -122,78 +134,6 @@ trace_scene(unsigned char *buf) { pthread_join(threads[i], &status); } else { for(int i = 0; i < HEIGHT; ++i) - trace_line(i, buffer + i * 4 * WIDTH); - } -} - -static void -display(void) { - static int count = 0; - ++count; - if(count > 10000) - exit(0); - float time = (float)glutGet(GLUT_ELAPSED_TIME) / 1000; - - objects[0].position[0] = 1.5 * cos(time); - objects[0].position[1] = 1.5 * sin(time); - objects[1].position[0] = 1.5 * cos(time + 1/3. * TAU); - objects[1].position[1] = 1.5 * sin(time + 1/3. * TAU); - objects[3].position[0] = 1.5 * cos(time + 2/3. * TAU); - objects[3].position[1] = 1.5 * sin(time + 2/3. * TAU); - objects[2].position[2] = -3 + 2 * sin(time * 2); - - trace_scene(buffer); - glDrawPixels(WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, buffer); - glutSwapBuffers(); -} - -static void -reshape(int w, int h) { - glViewport(0, 0, w, h); -} - -static void -keyboard(unsigned char key, int x, int y) { - switch(key) { - case 27: - exit(EXIT_SUCCESS); - break; - case 't': - if(threaded) - threaded = 0; - else - threaded = 1; - break; + trace_line(i, buf + i * 4 * WIDTH); } } - -static int -init(int argc, char **argv, int w, int h) { - glutInit(&argc, argv); - - glutInitWindowPosition(0, 0); - glutInitWindowSize(w, h); - glutInitDisplayMode(GLUT_RGB); - glutCreateWindow(argv[0]); - - glDepthMask(0); - glDisable(GL_DEPTH_TEST); - glDisable(GL_BLEND); - - return 0; -} - -int -main(int argc, char **argv) { - if (init(argc, argv, WIDTH, HEIGHT)) - return EXIT_FAILURE; - - glutDisplayFunc(display); - glutIdleFunc(display); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyboard); - - glutMainLoop(); - - return EXIT_SUCCESS; -} -- cgit v1.2.3 From 3d422f2b3d311f1be3658ff1f44d818a99561a66 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:43:17 -0500 Subject: Minor style changes --- ray.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 018ace1..45597df 100644 --- a/ray.c +++ b/ray.c @@ -102,7 +102,6 @@ thread(void *arg) { void trace_scene(float time, unsigned char *buf, int threaded) { - objects[0].position[0] = 1.5 * cos(time); objects[0].position[1] = 1.5 * sin(time); objects[1].position[0] = 1.5 * cos(time + 1/3. * TAU); @@ -110,6 +109,7 @@ trace_scene(float time, unsigned char *buf, int threaded) { objects[3].position[0] = 1.5 * cos(time + 2/3. * TAU); objects[3].position[1] = 1.5 * sin(time + 2/3. * TAU); objects[2].position[2] = -3 + 2 * sin(time * 2); + if(threaded) { pthread_attr_t attr; pthread_attr_init(&attr); -- cgit v1.2.3 From 04ee2de37c1f80d098172fc7bc4d42ea1797f128 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 14:54:39 -0500 Subject: ray.c: Precalculate per-pixel ray directions This saves 16ms/frame on yelena. --- ray.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 45597df..6304b36 100644 --- a/ray.c +++ b/ray.c @@ -32,6 +32,9 @@ typedef struct { long line; } ThreadArg; +static float trace_vectors[HEIGHT][WIDTH][3]; +static int trace_vectors_initialized; + static Object objects[] = { {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, 0, .8}}, {.position={0, 1.414, -3}, .radius=1, .diffuse={0, .8, .8}}, @@ -44,7 +47,7 @@ static Light lights[] = { }; static void -trace(float s[3], float d[3], float pixel[3], int n) { +trace(float s[3], const float d[3], float pixel[3], int n) { int i, j, k, m; float l[3], r[3], t, y[3]; @@ -71,23 +74,16 @@ trace_line(int l, unsigned char *buf) { static float s[3] = {0, 0, 0}; float y = l - HEIGHT / 2; - for(int i = 0; i < 4 * WIDTH; i += 4) { - float x = (i / 4) - WIDTH / 2; - + for(int i = 0; i < WIDTH; ++i) { float pixel[3]; memset(pixel, '\0', sizeof(pixel)); - float d[3]; - d[0] = x / (WIDTH / 2); - d[1] = y / (HEIGHT / 2) * ((float)HEIGHT / (float)WIDTH); - d[2] = -1; - - normalize(d); + const float* d = trace_vectors[l][i]; trace(s, d, pixel, 1); for(int j = 0; j < 3; ++j) - buf[i + j] = MIN(255 * pixel[j], 255); + buf[i * 4 + j] = MIN(255 * pixel[j], 255); } } @@ -100,8 +96,25 @@ thread(void *arg) { return NULL; } +static void +initialize_trace_vectors(void) { + for(int y = 0; y < HEIGHT; ++y) { + for(int x = 0; x < WIDTH; ++x) { + float* d = trace_vectors[y][x]; + d[0] = ((float)x / WIDTH - 0.5f) * 2.0f; + d[1] = ((float)y / HEIGHT - 0.5f) * 2.0f * ((float)HEIGHT / WIDTH); + d[2] = -1; + normalize(d); + } + } + trace_vectors_initialized = 1; +} + void trace_scene(float time, unsigned char *buf, int threaded) { + if (!trace_vectors_initialized) + initialize_trace_vectors(); + objects[0].position[0] = 1.5 * cos(time); objects[0].position[1] = 1.5 * sin(time); objects[1].position[0] = 1.5 * cos(time + 1/3. * TAU); -- cgit v1.2.3 From 1dca69e545ab815850230a377bc346bb5dc33839 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:18:32 -0500 Subject: ray.c: Skip normalizing if light has negative contribution This saves about 3ms/frame on yelena. --- ray.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 6304b36..1125d19 100644 --- a/ray.c +++ b/ray.c @@ -59,9 +59,12 @@ trace(float s[3], const float d[3], float pixel[3], int n) { for(i = 0; i < 3; ++i) l[i] = lights[m].position[i] - y[i]; - normalize(l); - for(k = 0; k < 3; ++k) - pixel[k] += lights[m].diffuse[k] * objects[j].diffuse[k] * (MAX(dot(l, r), 0)) / (1 << n); + float lr_dot = dot(l, r); + if (lr_dot > 0) { + float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); + for(k = 0; k < 3; ++k) + pixel[k] += lights[m].diffuse[k] * objects[j].diffuse[k] * scale; + } trace(y, r, pixel, n + 1); } -- cgit v1.2.3 From ce39f5c767ae25ed884aa24120e340d154442342 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:39:32 -0500 Subject: ray.c: Simplify trace_line This doesn't save any time. --- ray.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 1125d19..edbb37a 100644 --- a/ray.c +++ b/ray.c @@ -74,19 +74,16 @@ trace(float s[3], const float d[3], float pixel[3], int n) { static void trace_line(int l, unsigned char *buf) { - static float s[3] = {0, 0, 0}; - float y = l - HEIGHT / 2; + static const float s[3] = {0, 0, 0}; - for(int i = 0; i < WIDTH; ++i) { - float pixel[3]; - memset(pixel, '\0', sizeof(pixel)); + for(int i = 0; i < WIDTH; ++i, buf += 4) { + float pixel[3] = { 0, 0, 0 }; - const float* d = trace_vectors[l][i]; + trace(s, trace_vectors[l][i], pixel, 1); - trace(s, d, pixel, 1); - - for(int j = 0; j < 3; ++j) - buf[i * 4 + j] = MIN(255 * pixel[j], 255); + buf[0] = MIN(pixel[0], 1.0f) * 255; + buf[1] = MIN(pixel[1], 1.0f) * 255; + buf[2] = MIN(pixel[2], 1.0f) * 255; } } -- cgit v1.2.3 From 64a24eff5995c44d21e42d0bc654739030eca76c Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 15:50:45 -0500 Subject: ray.c: Don't look for self in reflection on self --- ray.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index edbb37a..96a605a 100644 --- a/ray.c +++ b/ray.c @@ -47,11 +47,13 @@ static Light lights[] = { }; static void -trace(float s[3], const float d[3], float pixel[3], int n) { +trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { int i, j, k, m; float l[3], r[3], t, y[3]; for(j = 0; j < LENGTH(objects); ++j) { + if ((1 << j) & mask) continue; + t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius); if(t > 0) { @@ -66,7 +68,7 @@ trace(float s[3], const float d[3], float pixel[3], int n) { pixel[k] += lights[m].diffuse[k] * objects[j].diffuse[k] * scale; } - trace(y, r, pixel, n + 1); + trace(y, r, pixel, n + 1, (1 << j)); } } } @@ -79,7 +81,7 @@ trace_line(int l, unsigned char *buf) { for(int i = 0; i < WIDTH; ++i, buf += 4) { float pixel[3] = { 0, 0, 0 }; - trace(s, trace_vectors[l][i], pixel, 1); + trace(s, trace_vectors[l][i], pixel, 1, 0); buf[0] = MIN(pixel[0], 1.0f) * 255; buf[1] = MIN(pixel[1], 1.0f) * 255; -- cgit v1.2.3 From a9fd902610a81d0bcbfde0fbc7377d839a41ffa2 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 16:20:36 -0500 Subject: ray.c: Use __builtin_expect to mark sphere collisions as unlikely This saves 10ms/frame on yelena. --- ray.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 96a605a..64f8810 100644 --- a/ray.c +++ b/ray.c @@ -16,6 +16,14 @@ #define TAU 6.28318531 +#if __GNUC__ >= 3 +# define unlikely(cond) __builtin_expect ((cond), 0) +# define likely(cond) __builtin_expect ((cond), 1) +#else +# define unlikely(cond) (cond) +# define likely(cond) (cond) +#endif + typedef struct { float position[3]; float radius; @@ -56,7 +64,7 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius); - if(t > 0) { + if(unlikely(t > 0)) { for(m = 0; m < LENGTH(lights); ++m) { for(i = 0; i < 3; ++i) l[i] = lights[m].position[i] - y[i]; -- cgit v1.2.3 From 656717d3126949955b9a451539f4476e84e308e6 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 16:34:50 -0500 Subject: ray.c: Create only one thread per core --- ray.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 64f8810..5e8243e 100644 --- a/ray.c +++ b/ray.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "3dmath.h" @@ -36,8 +37,10 @@ typedef struct { } Light; typedef struct { + pthread_mutex_t mutex; + unsigned char* buffer; - long line; + long next_line; } ThreadArg; static float trace_vectors[HEIGHT][WIDTH][3]; @@ -101,7 +104,16 @@ static void * thread(void *arg) { ThreadArg* thread_arg = arg; - trace_line(thread_arg->line, thread_arg->buffer + thread_arg->line * 4 * WIDTH); + for (;;) { + pthread_mutex_lock(&thread_arg->mutex); + if (thread_arg->next_line == HEIGHT) break; + long line = thread_arg->next_line++; + pthread_mutex_unlock(&thread_arg->mutex); + + trace_line(line, thread_arg->buffer + line * 4 * WIDTH); + } + + pthread_mutex_unlock(&thread_arg->mutex); return NULL; } @@ -134,27 +146,24 @@ trace_scene(float time, unsigned char *buf, int threaded) { objects[2].position[2] = -3 + 2 * sin(time * 2); if(threaded) { - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - - ThreadArg thread_args[HEIGHT]; - pthread_t threads[HEIGHT]; - for(long i = 0; i < HEIGHT; ++i) { - thread_args[i].line = i; - thread_args[i].buffer = buf; - - int ret = pthread_create(&threads[i], &attr, thread, &thread_args[i]); - - if(ret) { - fprintf(stderr, "pthread_create(): %d\n", ret); - exit(EXIT_FAILURE); - } + ThreadArg arg; + memset(&arg, 0, sizeof(arg)); + pthread_mutex_init(&arg.mutex, NULL); + arg.buffer = buf; + + int num_threads = sysconf(_SC_NPROCESSORS_CONF) - 1; + pthread_t* threads = NULL; + if (num_threads > 0) { + threads = calloc(sizeof(*threads), num_threads); + + for (int i = 0; i < num_threads; ++i) + pthread_create(&threads[i], NULL, thread, &arg); } - void *status; - for(long i = 0; i < HEIGHT; ++i) - pthread_join(threads[i], &status); + thread(&arg); + + for(int i = 0; i < num_threads; ++i) + pthread_join(threads[i], NULL); } else { for(int i = 0; i < HEIGHT; ++i) trace_line(i, buf + i * 4 * WIDTH); -- cgit v1.2.3 From c1c1559007f5557ebfc2a19899224ee9df2f9447 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 17:21:23 -0500 Subject: ray.c: Calculate reflection only once per hit This saves 10ms on yelena. --- ray.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 5e8243e..8a236bd 100644 --- a/ray.c +++ b/ray.c @@ -78,9 +78,9 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma for(k = 0; k < 3; ++k) pixel[k] += lights[m].diffuse[k] * objects[j].diffuse[k] * scale; } - - trace(y, r, pixel, n + 1, (1 << j)); } + + trace(y, r, pixel, n + 1, (1 << j)); } } } -- cgit v1.2.3 From 983bbf379fb22856987a8a9c38226605b7f40d1e Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 17:24:29 -0500 Subject: ray.c: Calculate lighting only for nearest hit This loses 5ms/frame on yelena. --- ray.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 8a236bd..218f20e 100644 --- a/ray.c +++ b/ray.c @@ -59,30 +59,45 @@ static Light lights[] = { static void trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { - int i, j, k, m; - float l[3], r[3], t, y[3]; + float nearest = HUGE_VAL; + int nearest_object = -1; + float nearest_y[3]; + float nearest_r[3]; + + for(int j = 0; j < LENGTH(objects); ++j) { + float r[3], t, y[3]; - for(j = 0; j < LENGTH(objects); ++j) { if ((1 << j) & mask) continue; t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius); - if(unlikely(t > 0)) { - for(m = 0; m < LENGTH(lights); ++m) { - for(i = 0; i < 3; ++i) - l[i] = lights[m].position[i] - y[i]; + if(likely(t <= 0)) + continue; + + if (t < nearest) { + nearest = t; + nearest_object = j; + memcpy(nearest_y, y, sizeof(nearest_y)); + memcpy(nearest_r, r, sizeof(nearest_y)); + } + } + + if (nearest_object == -1) return; - float lr_dot = dot(l, r); - if (lr_dot > 0) { - float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); - for(k = 0; k < 3; ++k) - pixel[k] += lights[m].diffuse[k] * objects[j].diffuse[k] * scale; - } - } + for(int m = 0; m < LENGTH(lights); ++m) { + float l[3]; + for(int i = 0; i < 3; ++i) + l[i] = lights[m].position[i] - nearest_y[i]; - trace(y, r, pixel, n + 1, (1 << j)); + float lr_dot = dot(l, nearest_r); + if (lr_dot > 0) { + float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); + for(int k = 0; k < 3; ++k) + pixel[k] += lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; } } + + trace(nearest_y, nearest_r, pixel, n + 1, (1 << nearest_object)); } static void -- cgit v1.2.3 From 292d95a35b9d1268492f47c44580ec006f670bdb Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 17:31:37 -0500 Subject: ray.c: Halve the FOV This makes the spheres look more spherical. --- ray.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 218f20e..7672779 100644 --- a/ray.c +++ b/ray.c @@ -102,7 +102,7 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma static void trace_line(int l, unsigned char *buf) { - static const float s[3] = {0, 0, 0}; + static const float s[3] = {0, 0, 8}; for(int i = 0; i < WIDTH; ++i, buf += 4) { float pixel[3] = { 0, 0, 0 }; @@ -138,8 +138,8 @@ initialize_trace_vectors(void) { for(int y = 0; y < HEIGHT; ++y) { for(int x = 0; x < WIDTH; ++x) { float* d = trace_vectors[y][x]; - d[0] = ((float)x / WIDTH - 0.5f) * 2.0f; - d[1] = ((float)y / HEIGHT - 0.5f) * 2.0f * ((float)HEIGHT / WIDTH); + d[0] = ((float)x / WIDTH - 0.5f) * 0.5f; + d[1] = ((float)y / HEIGHT - 0.5f) * 0.5f * ((float)HEIGHT / WIDTH); d[2] = -1; normalize(d); } -- cgit v1.2.3 From 5eb4b4bb4e44eec2015ed4ec2bf966e1b0b0f85b Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 17:45:04 -0500 Subject: ray.c: Add missing free --- ray.c | 1 + 1 file changed, 1 insertion(+) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 7672779..2470777 100644 --- a/ray.c +++ b/ray.c @@ -179,6 +179,7 @@ trace_scene(float time, unsigned char *buf, int threaded) { for(int i = 0; i < num_threads; ++i) pthread_join(threads[i], NULL); + free(threads); } else { for(int i = 0; i < HEIGHT; ++i) trace_line(i, buf + i * 4 * WIDTH); -- cgit v1.2.3 From a64a0186cdeee9189bd4db1303f82908cfe279a4 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 21:30:55 -0500 Subject: ray.c: Prevent lights from subtracting color in the shade Also add some ambient light. --- ray.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 2470777..483dc0c 100644 --- a/ray.c +++ b/ray.c @@ -56,6 +56,7 @@ static Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, {.position={0, 30, -4}, .diffuse={1, 1, 1}} }; +static float ambient[3] = {0.2, 0.1, 0.1}; static void trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { @@ -92,8 +93,11 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma float lr_dot = dot(l, nearest_r); if (lr_dot > 0) { float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); - for(int k = 0; k < 3; ++k) - pixel[k] += lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; + for(int k = 0; k < 3; ++k) { + float diffuse = lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; + if (diffuse < 0.0f) diffuse = 0.0f; + pixel[k] += diffuse + ambient[k] * objects[nearest_object].diffuse[k]; + } } } -- cgit v1.2.3 From 6cee50ceb0d153622b89fb813060419f5985857d Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 21:32:20 -0500 Subject: Subtract the central sphere, and update the animation to match --- ray.c | 58 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 483dc0c..12b565e 100644 --- a/ray.c +++ b/ray.c @@ -29,6 +29,7 @@ typedef struct { float position[3]; float radius; float diffuse[3]; + int subtract; } Object; typedef struct { @@ -47,10 +48,10 @@ static float trace_vectors[HEIGHT][WIDTH][3]; static int trace_vectors_initialized; static Object objects[] = { - {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, 0, .8}}, - {.position={0, 1.414, -3}, .radius=1, .diffuse={0, .8, .8}}, - {.position={0, 0, -3}, .radius=.25, .diffuse={.8, .8, .8}}, - {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, 0}} + {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, 0, .8}, .subtract=0}, + {.position={0, 1.414, -3}, .radius=1, .diffuse={0, .8, .8}, .subtract=0}, + {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .subtract=1}, + {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, 0}, .subtract=0} }; static Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, @@ -60,27 +61,42 @@ static float ambient[3] = {0.2, 0.1, 0.1}; static void trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { + // Reflections in concave objects can go really deep, so we need to limit + // the recursion depth. + if (n > 6) return; + float nearest = HUGE_VAL; int nearest_object = -1; float nearest_y[3]; float nearest_r[3]; - for(int j = 0; j < LENGTH(objects); ++j) { + for(size_t j = 0; j < LENGTH(objects); ++j) { float r[3], t, y[3]; - if ((1 << j) & mask) continue; + if (((1 << j) & mask) || objects[j].subtract) continue; - t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius); + t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius, 0); - if(likely(t <= 0)) + if(likely(t <= 0) || t > nearest) continue; - if (t < nearest) { - nearest = t; - nearest_object = j; - memcpy(nearest_y, y, sizeof(nearest_y)); - memcpy(nearest_r, r, sizeof(nearest_y)); + size_t k; + for (k = 0; k < LENGTH(objects); ++k) { + if (!objects[k].subtract) continue; + if (POW2(y[0] - objects[k].position[0]) + POW2(y[1] - objects[k].position[1]) + POW2(y[2] - objects[k].position[2]) > POW2(objects[k].radius)) continue; + + t = sphere_intersect(y, r, s, d, objects[k].position, objects[k].radius, 1); + + break; } + + if(likely(t <= 0) || t > nearest) + continue; + + nearest = t; + nearest_object = j; + memcpy(nearest_y, y, sizeof(nearest_y)); + memcpy(nearest_r, r, sizeof(nearest_y)); } if (nearest_object == -1) return; @@ -101,7 +117,7 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma } } - trace(nearest_y, nearest_r, pixel, n + 1, (1 << nearest_object)); + trace(nearest_y, nearest_r, pixel, n + 1, 1 << nearest_object); } static void @@ -156,13 +172,13 @@ trace_scene(float time, unsigned char *buf, int threaded) { if (!trace_vectors_initialized) initialize_trace_vectors(); - objects[0].position[0] = 1.5 * cos(time); - objects[0].position[1] = 1.5 * sin(time); - objects[1].position[0] = 1.5 * cos(time + 1/3. * TAU); - objects[1].position[1] = 1.5 * sin(time + 1/3. * TAU); - objects[3].position[0] = 1.5 * cos(time + 2/3. * TAU); - objects[3].position[1] = 1.5 * sin(time + 2/3. * TAU); - objects[2].position[2] = -3 + 2 * sin(time * 2); + objects[0].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time); + objects[0].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time); + objects[1].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time + 1/3. * TAU); + objects[1].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time + 1/3. * TAU); + objects[3].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time + 2/3. * TAU); + objects[3].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time + 2/3. * TAU); + objects[2].position[2] = -3 + 0.5 * sin(time * 2.0); if(threaded) { ThreadArg arg; -- cgit v1.2.3 From e9de20a9047ae351d66a4aedc5be53c5ebf61d66 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 22:07:18 -0500 Subject: ray.c: Add specular color multiplier, and fix some shading bugs --- ray.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 12b565e..b9c5f09 100644 --- a/ray.c +++ b/ray.c @@ -29,6 +29,7 @@ typedef struct { float position[3]; float radius; float diffuse[3]; + float specular[3]; int subtract; } Object; @@ -48,16 +49,16 @@ static float trace_vectors[HEIGHT][WIDTH][3]; static int trace_vectors_initialized; static Object objects[] = { - {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, 0, .8}, .subtract=0}, - {.position={0, 1.414, -3}, .radius=1, .diffuse={0, .8, .8}, .subtract=0}, - {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .subtract=1}, - {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, 0}, .subtract=0} + {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, .0, .8}, .specular={.8, .8, .8}, .subtract=0}, + {.position={0, 1.414, -3}, .radius=1, .diffuse={.0, .8, .8}, .specular={.8, .8, .8}, .subtract=0}, + {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .specular={.8, .8, .8}, .subtract=1}, + {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, .0}, .specular={.8, .8, .8}, .subtract=0} }; -static Light lights[] = { +static const Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, {.position={0, 30, -4}, .diffuse={1, 1, 1}} }; -static float ambient[3] = {0.2, 0.1, 0.1}; +static const float ambient[3] = {0.2, 0.1, 0.1}; static void trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { @@ -101,23 +102,27 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma if (nearest_object == -1) return; + trace(nearest_y, nearest_r, pixel, n + 1, 1 << nearest_object); + + for (int k = 0; k < 3; ++k) + pixel[k] = pixel[k] * objects[nearest_object].specular[k] + ambient[k] * objects[nearest_object].diffuse[k]; + for(int m = 0; m < LENGTH(lights); ++m) { float l[3]; for(int i = 0; i < 3; ++i) l[i] = lights[m].position[i] - nearest_y[i]; float lr_dot = dot(l, nearest_r); - if (lr_dot > 0) { - float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); - for(int k = 0; k < 3; ++k) { - float diffuse = lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; - if (diffuse < 0.0f) diffuse = 0.0f; - pixel[k] += diffuse + ambient[k] * objects[nearest_object].diffuse[k]; - } - } - } + if (lr_dot <= 0) continue; - trace(nearest_y, nearest_r, pixel, n + 1, 1 << nearest_object); + float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); + // The cutoff at 0.05 is for artistic reasons; 0.0 would be more + // realistic. + if (scale <= 0.05) continue; + + for(int k = 0; k < 3; ++k) + pixel[k] += lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; + } } static void -- cgit v1.2.3 From e11fb53429bc923a9b757bba307c6c8f6f0bf7a9 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 22:17:50 -0500 Subject: ray.c: Allow self-reflections, now that we have concave objects Also update the animation a bit. --- ray.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index b9c5f09..d74492a 100644 --- a/ray.c +++ b/ray.c @@ -49,10 +49,10 @@ static float trace_vectors[HEIGHT][WIDTH][3]; static int trace_vectors_initialized; static Object objects[] = { - {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, .0, .8}, .specular={.8, .8, .8}, .subtract=0}, - {.position={0, 1.414, -3}, .radius=1, .diffuse={.0, .8, .8}, .specular={.8, .8, .8}, .subtract=0}, - {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .specular={.8, .8, .8}, .subtract=1}, - {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, .0}, .specular={.8, .8, .8}, .subtract=0} + {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, .0, .8}, .specular={.7, .6, .7}, .subtract=0}, + {.position={0, 1.414, -3}, .radius=1, .diffuse={.0, .8, .8}, .specular={.6, .7, .7}, .subtract=0}, + {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .specular={.7, .7, .7}, .subtract=1}, + {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, .0}, .specular={.7, .7, .6}, .subtract=0} }; static const Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, @@ -61,7 +61,7 @@ static const Light lights[] = { static const float ambient[3] = {0.2, 0.1, 0.1}; static void -trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { +trace(const float s[3], const float d[3], float pixel[3], int n) { // Reflections in concave objects can go really deep, so we need to limit // the recursion depth. if (n > 6) return; @@ -74,7 +74,7 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma for(size_t j = 0; j < LENGTH(objects); ++j) { float r[3], t, y[3]; - if (((1 << j) & mask) || objects[j].subtract) continue; + if (objects[j].subtract) continue; t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius, 0); @@ -102,7 +102,7 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma if (nearest_object == -1) return; - trace(nearest_y, nearest_r, pixel, n + 1, 1 << nearest_object); + trace(nearest_y, nearest_r, pixel, n + 1); for (int k = 0; k < 3; ++k) pixel[k] = pixel[k] * objects[nearest_object].specular[k] + ambient[k] * objects[nearest_object].diffuse[k]; @@ -132,7 +132,7 @@ trace_line(int l, unsigned char *buf) { for(int i = 0; i < WIDTH; ++i, buf += 4) { float pixel[3] = { 0, 0, 0 }; - trace(s, trace_vectors[l][i], pixel, 1, 0); + trace(s, trace_vectors[l][i], pixel, 1); buf[0] = MIN(pixel[0], 1.0f) * 255; buf[1] = MIN(pixel[1], 1.0f) * 255; @@ -177,13 +177,13 @@ trace_scene(float time, unsigned char *buf, int threaded) { if (!trace_vectors_initialized) initialize_trace_vectors(); - objects[0].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time); - objects[0].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time); - objects[1].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time + 1/3. * TAU); - objects[1].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time + 1/3. * TAU); - objects[3].position[0] = (1.5 + 0.35 * sin(1.1 * time)) * cos(0.5 * time + 2/3. * TAU); - objects[3].position[1] = (1.5 + 0.35 * sin(1.1 * time)) * sin(0.5 * time + 2/3. * TAU); - objects[2].position[2] = -3 + 0.5 * sin(time * 2.0); + objects[0].position[0] = (1.5 + 0.35 * sin(1.1 * time + 0.0)) * cos(0.5 * time); + objects[0].position[1] = (1.5 + 0.35 * sin(1.1 * time + 2.5)) * sin(0.5 * time); + objects[1].position[0] = (1.5 + 0.35 * sin(1.1 * time + 2.0)) * cos(0.5 * time + 1/3. * TAU); + objects[1].position[1] = (1.5 + 0.35 * sin(1.1 * time + 1.5)) * sin(0.5 * time + 1/3. * TAU); + objects[3].position[0] = (1.5 + 0.35 * sin(1.1 * time + 1.0)) * cos(0.5 * time + 2/3. * TAU); + objects[3].position[1] = (1.5 + 0.35 * sin(1.1 * time + 0.5)) * sin(0.5 * time + 2/3. * TAU); + objects[2].position[2] = -3 + 0.2 * sin(time * 1.2); if(threaded) { ThreadArg arg; -- cgit v1.2.3 From eefec7a70c96e20e3920fb2adea6c4da77c26170 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Fri, 12 Dec 2014 15:41:38 -0800 Subject: Render to window dimensions --- ray.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index d74492a..4450eba 100644 --- a/ray.c +++ b/ray.c @@ -9,8 +9,6 @@ #include "3dmath.h" -#define BUFFER_SIZE (WIDTH * HEIGHT * 4) - #define LENGTH(array) (sizeof(array) / sizeof(array[0])) #define MAX(x, y) (x > y ? x : y) #define MIN(x, y) (x < y ? x : y) @@ -41,12 +39,13 @@ typedef struct { typedef struct { pthread_mutex_t mutex; + int width, height; unsigned char* buffer; long next_line; } ThreadArg; -static float trace_vectors[HEIGHT][WIDTH][3]; -static int trace_vectors_initialized; +static float* trace_vectors; +static int trace_vectors_width, trace_vectors_height; static Object objects[] = { {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, .0, .8}, .specular={.7, .6, .7}, .subtract=0}, @@ -126,13 +125,13 @@ trace(const float s[3], const float d[3], float pixel[3], int n) { } static void -trace_line(int l, unsigned char *buf) { +trace_line(int l, int width, unsigned char *buf) { static const float s[3] = {0, 0, 8}; - for(int i = 0; i < WIDTH; ++i, buf += 4) { + for(int i = 0; i < width; ++i, buf += 4) { float pixel[3] = { 0, 0, 0 }; - trace(s, trace_vectors[l][i], pixel, 1); + trace(s, &trace_vectors[(l * width + i) * 3], pixel, 1); buf[0] = MIN(pixel[0], 1.0f) * 255; buf[1] = MIN(pixel[1], 1.0f) * 255; @@ -146,11 +145,11 @@ thread(void *arg) { for (;;) { pthread_mutex_lock(&thread_arg->mutex); - if (thread_arg->next_line == HEIGHT) break; + if (thread_arg->next_line == thread_arg->height) break; long line = thread_arg->next_line++; pthread_mutex_unlock(&thread_arg->mutex); - trace_line(line, thread_arg->buffer + line * 4 * WIDTH); + trace_line(line, thread_arg->width, thread_arg->buffer + line * 4 * thread_arg->width); } pthread_mutex_unlock(&thread_arg->mutex); @@ -159,23 +158,29 @@ thread(void *arg) { } static void -initialize_trace_vectors(void) { - for(int y = 0; y < HEIGHT; ++y) { - for(int x = 0; x < WIDTH; ++x) { - float* d = trace_vectors[y][x]; - d[0] = ((float)x / WIDTH - 0.5f) * 0.5f; - d[1] = ((float)y / HEIGHT - 0.5f) * 0.5f * ((float)HEIGHT / WIDTH); +initialize_trace_vectors(int width, int height) { + trace_vectors = calloc(width * height, 3 * sizeof(float)); + trace_vectors_width = width; + trace_vectors_height = height; + for(int y = 0; y < height; ++y) { + for(int x = 0; x < width; ++x) { + float* d = &trace_vectors[(y * width + x) * 3]; + d[0] = ((float)x / width - 0.5f) * 0.5f * ((float)width / height); + d[1] = ((float)y / height - 0.5f) * 0.5f; d[2] = -1; normalize(d); } } - trace_vectors_initialized = 1; } void -trace_scene(float time, unsigned char *buf, int threaded) { - if (!trace_vectors_initialized) - initialize_trace_vectors(); +trace_scene(float time, int width, int height, unsigned char *buf, int threaded) { + if (trace_vectors && (trace_vectors_width != width || trace_vectors_height != height)) { + free(trace_vectors); + trace_vectors = 0; + } + if (!trace_vectors) + initialize_trace_vectors(width, height); objects[0].position[0] = (1.5 + 0.35 * sin(1.1 * time + 0.0)) * cos(0.5 * time); objects[0].position[1] = (1.5 + 0.35 * sin(1.1 * time + 2.5)) * sin(0.5 * time); @@ -188,6 +193,8 @@ trace_scene(float time, unsigned char *buf, int threaded) { if(threaded) { ThreadArg arg; memset(&arg, 0, sizeof(arg)); + arg.width = width; + arg.height = height; pthread_mutex_init(&arg.mutex, NULL); arg.buffer = buf; @@ -206,7 +213,7 @@ trace_scene(float time, unsigned char *buf, int threaded) { pthread_join(threads[i], NULL); free(threads); } else { - for(int i = 0; i < HEIGHT; ++i) - trace_line(i, buf + i * 4 * WIDTH); + for(int i = 0; i < height; ++i) + trace_line(i, width, buf + i * 4 * width); } } -- cgit v1.2.3 From e108aca4afc2f5dc06216b0cf2e0bc3500cad21a Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Fri, 12 Dec 2014 15:49:24 -0800 Subject: Re-add center sphere --- ray.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 4450eba..d1840c2 100644 --- a/ray.c +++ b/ray.c @@ -51,7 +51,8 @@ static Object objects[] = { {.position={-1.414, -1, -3}, .radius=1, .diffuse={.8, .0, .8}, .specular={.7, .6, .7}, .subtract=0}, {.position={0, 1.414, -3}, .radius=1, .diffuse={.0, .8, .8}, .specular={.6, .7, .7}, .subtract=0}, {.position={0, 0, -3}, .radius=1.5, .diffuse={.8, .8, .8}, .specular={.7, .7, .7}, .subtract=1}, - {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, .0}, .specular={.7, .7, .6}, .subtract=0} + {.position={1.414, -1, -3}, .radius=1, .diffuse={.8, .8, .0}, .specular={.7, .7, .6}, .subtract=0}, + {.position={0, 0, -3}, .radius=1.1, .diffuse={.9, .9, .9}, .specular={.9, .9, .9}, .subtract=2} }; static const Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, @@ -73,25 +74,27 @@ trace(const float s[3], const float d[3], float pixel[3], int n) { for(size_t j = 0; j < LENGTH(objects); ++j) { float r[3], t, y[3]; - if (objects[j].subtract) continue; + if (objects[j].subtract == 1) continue; t = sphere_intersect(y, r, s, d, objects[j].position, objects[j].radius, 0); if(likely(t <= 0) || t > nearest) continue; - size_t k; - for (k = 0; k < LENGTH(objects); ++k) { - if (!objects[k].subtract) continue; - if (POW2(y[0] - objects[k].position[0]) + POW2(y[1] - objects[k].position[1]) + POW2(y[2] - objects[k].position[2]) > POW2(objects[k].radius)) continue; + if (objects[j].subtract == 0) { + size_t k; + for (k = 0; k < LENGTH(objects); ++k) { + if (!objects[k].subtract) continue; + if (POW2(y[0] - objects[k].position[0]) + POW2(y[1] - objects[k].position[1]) + POW2(y[2] - objects[k].position[2]) > POW2(objects[k].radius)) continue; - t = sphere_intersect(y, r, s, d, objects[k].position, objects[k].radius, 1); + t = sphere_intersect(y, r, s, d, objects[k].position, objects[k].radius, 1); - break; - } + break; + } - if(likely(t <= 0) || t > nearest) - continue; + if(likely(t <= 0) || t > nearest) + continue; + } nearest = t; nearest_object = j; @@ -189,6 +192,7 @@ trace_scene(float time, int width, int height, unsigned char *buf, int threaded) objects[3].position[0] = (1.5 + 0.35 * sin(1.1 * time + 1.0)) * cos(0.5 * time + 2/3. * TAU); objects[3].position[1] = (1.5 + 0.35 * sin(1.1 * time + 0.5)) * sin(0.5 * time + 2/3. * TAU); objects[2].position[2] = -3 + 0.2 * sin(time * 1.2); + memcpy(objects[4].position, objects[2].position, sizeof(objects[4].position)); if(threaded) { ThreadArg arg; -- cgit v1.2.3