From 7e7ec10dea751540eda7898cf83f8471185aa063 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 16 Jul 2009 18:25:02 +0000 Subject: Add support for default textures. --- libdimension/dimension/scene.h | 10 ++++++++++ libdimension/objects.c | 18 ++++++++++++------ libdimension/raytrace.c | 29 +++++++++++++++++++---------- libdimension/scene.c | 1 + 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h index aa216f4..12ad6bd 100644 --- a/libdimension/dimension/scene.h +++ b/libdimension/dimension/scene.h @@ -33,10 +33,20 @@ typedef enum { } dmnsn_quality; typedef struct { + /* World attributes */ dmnsn_color background; + dmnsn_texture *default_texture; + + /* Objects */ dmnsn_array *objects; + + /* Camera */ dmnsn_camera *camera; + + /* Canvas */ dmnsn_canvas *canvas; + + /* Rendering quality */ dmnsn_quality quality; } dmnsn_scene; diff --git a/libdimension/objects.c b/libdimension/objects.c index 3c772d1..bb5a16a 100644 --- a/libdimension/objects.c +++ b/libdimension/objects.c @@ -120,7 +120,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (-1.0 - line.x0.x)/line.n.x; p = dmnsn_line_point(line, t_temp); if (p.y >= -1.0 && p.y <= 1.0 && p.z >= -1.0 && p.z <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } @@ -128,7 +129,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (1.0 - line.x0.x)/line.n.x; p = dmnsn_line_point(line, t_temp); if (p.y >= -1.0 && p.y <= 1.0 && p.z >= -1.0 && p.z <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } } @@ -138,7 +140,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (-1.0 - line.x0.y)/line.n.y; p = dmnsn_line_point(line, t_temp); if (p.x >= -1.0 && p.x <= 1.0 && p.z >= -1.0 && p.z <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } @@ -146,7 +149,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (1.0 - line.x0.y)/line.n.y; p = dmnsn_line_point(line, t_temp); if (p.x >= -1.0 && p.x <= 1.0 && p.z >= -1.0 && p.z <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } } @@ -156,7 +160,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (-1.0 - line.x0.z)/line.n.z; p = dmnsn_line_point(line, t_temp); if (p.x >= -1.0 && p.x <= 1.0 && p.y >= -1.0 && p.y <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } @@ -164,7 +169,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) t_temp = (1.0 - line.x0.z)/line.n.z; p = dmnsn_line_point(line, t_temp); if (p.x >= -1.0 && p.x <= 1.0 && p.y >= -1.0 && p.y <= 1.0 - && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) { + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { t = t_temp; } } diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index c9217ca..470af4f 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -234,6 +234,8 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, dmnsn_line ray) dmnsn_line ray_trans; dmnsn_object *object; dmnsn_intersection *intersection = NULL, *intersection_temp; + const dmnsn_texture *texture; + const dmnsn_pigment *pigment; unsigned int i; for (i = 0; i < dmnsn_array_size(scene->objects); ++i) { @@ -245,6 +247,7 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, dmnsn_line ray) /* Test for intersections with objects */ intersection_temp = (*object->intersection_fn)(object, ray_trans); + /* Find the closest intersection to the camera */ if (intersection_temp && (!intersection || intersection_temp->t < intersection->t)) { dmnsn_delete_intersection(intersection); @@ -253,23 +256,29 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, dmnsn_line ray) } if (intersection) { + /* Default to black if we have no texture/pigment */ + color = dmnsn_black; + if (scene->quality >= DMNSN_RENDER_PIGMENT) { - if (intersection->texture) { - if (intersection->texture->pigment) { - color = (*intersection->texture->pigment->pigment_fn)( - intersection->texture->pigment, + /* Use the default texture if given a NULL texture */ + texture = intersection->texture ? intersection->texture + : scene->default_texture; + + if (texture) { + /* Use the default pigment if given a NULL pigment */ + pigment = texture->pigment ? texture->pigment + : scene->default_texture->pigment; + + if (pigment) { + color = (*pigment->pigment_fn)( + pigment, dmnsn_line_point(intersection->ray, intersection->t) ); - } else { - color = dmnsn_black; } - } else { - color = dmnsn_black; } - } else { - color = dmnsn_white; } + /* Delete the intersection */ dmnsn_delete_intersection(intersection); } diff --git a/libdimension/scene.c b/libdimension/scene.c index 7e24403..59a1eb9 100644 --- a/libdimension/scene.c +++ b/libdimension/scene.c @@ -27,6 +27,7 @@ dmnsn_new_scene() { dmnsn_scene *scene = malloc(sizeof(dmnsn_scene)); if (scene) { + scene->default_texture = NULL; scene->objects = dmnsn_new_array(sizeof(dmnsn_object*)); scene->quality = DMNSN_RENDER_FULL; } -- cgit v1.2.3