From 4317faa8365b5c08d9111ddd1f0a622ed9e99b52 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 7 Oct 2009 14:50:41 +0000 Subject: Add ray - bounding-box intersection test function. --- libdimension/raytrace.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'libdimension') diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index df61d6b..21ad1e8 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -229,6 +229,9 @@ dmnsn_raytrace_scene_impl(dmnsn_progress *progress, dmnsn_scene *scene, return 0; } +static double dmnsn_ray_bounding_box(dmnsn_line ray, + dmnsn_vector min, dmnsn_vector max); + /* Shoot a ray, and calculate the color, using `color' as the background */ static dmnsn_color dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, dmnsn_line ray) @@ -286,3 +289,72 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, dmnsn_line ray) return color; } + +static double +dmnsn_ray_bounding_box(dmnsn_line ray, dmnsn_vector min, dmnsn_vector max) +{ + double t = -1.0, t_temp; + dmnsn_vector p; + + if (line.n.x != 0.0) { + /* x == min.x */ + t_temp = (min.x - line.x0.x)/line.n.x; + p = dmnsn_line_point(line, t_temp); + if (p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + + /* x == max.x */ + t_temp = (max.x - line.x0.x)/line.n.x; + p = dmnsn_line_point(line, t_temp); + if (p.y >= min.y && p.y <= max.y && p.z >= min.z && p.z <= max.z + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + } + + if (line.n.y != 0.0) { + /* y == -1.0 */ + t_temp = (-1.0 - line.x0.y)/line.n.y; + p = dmnsn_line_point(line, t_temp); + if (p.x >= min.x && p.x <= max.x && p.z >= min.z && p.z <= max.z + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + + /* y == 1.0 */ + t_temp = (1.0 - line.x0.y)/line.n.y; + p = dmnsn_line_point(line, t_temp); + if (p.x >= min.x && p.x <= max.x && p.z >= min.z && p.z <= max.z + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + } + + if (line.n.z != 0.0) { + /* z == -1.0 */ + t_temp = (-1.0 - line.x0.z)/line.n.z; + p = dmnsn_line_point(line, t_temp); + if (p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + + /* z == 1.0 */ + t_temp = (1.0 - line.x0.z)/line.n.z; + p = dmnsn_line_point(line, t_temp); + if (p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y + && t_temp >= 0.0 && (t < 0.0 || t_temp < t)) + { + t = t_temp; + } + } + + return t; +} -- cgit v1.2.3