summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-06-14 19:58:12 +0000
committerTavian Barnes <tavianator@gmail.com>2009-06-14 19:58:12 +0000
commit6c88e0028c11721499ef93a6df651df3a41150c3 (patch)
tree3f33273a350fe80fb6ce05f3ce0b7e6c54993164
parent3a8af6f1536d56fcd50a5f0a0cf264aa62a37599 (diff)
downloaddimension-6c88e0028c11721499ef93a6df651df3a41150c3.tar.xz
Use z-index coloring for ray-tracing.
-rw-r--r--libdimension/raytrace.c28
-rw-r--r--tests/raytrace.c7
2 files changed, 25 insertions, 10 deletions
diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c
index 316157a..e91dbb2 100644
--- a/libdimension/raytrace.c
+++ b/libdimension/raytrace.c
@@ -66,12 +66,15 @@ dmnsn_raytrace_scene(dmnsn_scene *scene)
static void *
dmnsn_raytrace_scene_thread(void *arg)
{
- unsigned int i, j, k;
+ unsigned int i, j, k, l;
+ double t, t_temp;
dmnsn_object *object;
dmnsn_line ray, ray_trans;
dmnsn_raytrace_thread_payload *payload = (dmnsn_raytrace_thread_payload *)arg;
dmnsn_scene *scene = payload->scene;
dmnsn_array *intersections;
+ dmnsn_color color;
+ dmnsn_sRGB sRGB;
/* Iterate through each pixel */
for (i = 0; i < scene->canvas->x; ++i) {
@@ -79,7 +82,8 @@ dmnsn_raytrace_scene_thread(void *arg)
/* Only do the pixels assigned to this thread */
if ((j*scene->canvas->x + i)%payload->n == payload->i) {
/* Set the pixel to the background color */
- dmnsn_set_pixel(scene->canvas, i, j, scene->background);
+ color = scene->background;
+ t = 0.0;
/* Get the ray corresponding to the (i,j)th pixel */
ray = (*scene->camera->ray_fn)(scene->camera, scene->canvas, i, j);
@@ -90,17 +94,23 @@ dmnsn_raytrace_scene_thread(void *arg)
/* Transform the ray according to the object */
ray_trans = dmnsn_matrix_line_mul(object->trans, ray);
- /* Test for an intersection with an object */
+ /* Test for intersections with objects */
intersections = (*object->intersections_fn)(object, ray_trans);
- if (intersections->length > 0) {
- /* Mark intersections white */
- dmnsn_set_pixel(scene->canvas, i, j,
- dmnsn_color_from_XYZ(dmnsn_whitepoint));
- dmnsn_delete_array(intersections);
- break;
+ for (l = 0; l < intersections->length; ++l) {
+ dmnsn_array_get(intersections, l, &t_temp);
+ if (t_temp < t || t == 0.0) t = t_temp;
}
dmnsn_delete_array(intersections);
}
+
+ if (t != 0.0) {
+ sRGB.R = 1.0 - (t - 2.25)/2.25;
+ sRGB.G = sRGB.R;
+ sRGB.B = sRGB.R;
+ color = dmnsn_color_from_sRGB(sRGB);
+ }
+
+ dmnsn_set_pixel(scene->canvas, i, j, color);
}
}
}
diff --git a/tests/raytrace.c b/tests/raytrace.c
index ec13782..c8c25cd 100644
--- a/tests/raytrace.c
+++ b/tests/raytrace.c
@@ -25,7 +25,7 @@
int main() {
FILE *file;
dmnsn_scene *scene;
- dmnsn_object *cube;
+ dmnsn_object *sphere, *cube;
dmnsn_sRGB sRGB;
dmnsn_color color;
dmnsn_matrix trans;
@@ -58,6 +58,10 @@ int main() {
color.filter = 0.1;
scene->background = color;
+ sphere = dmnsn_new_sphere();
+ sphere->trans = dmnsn_scale_matrix(dmnsn_vector_construct(0.8, 0.8, 0.8));
+ dmnsn_array_push(scene->objects, &sphere);
+
cube = dmnsn_new_cube();
cube->trans = dmnsn_rotation_matrix(dmnsn_vector_construct(0.75, 0.0, 0.0));
dmnsn_array_push(scene->objects, &cube);
@@ -68,6 +72,7 @@ int main() {
dmnsn_png_write_canvas(scene->canvas, file);
dmnsn_delete_cube(cube);
+ dmnsn_delete_sphere(sphere);
dmnsn_delete_perspective_camera(scene->camera);
dmnsn_delete_canvas(scene->canvas);
dmnsn_delete_scene(scene);