summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-10-07 14:50:41 +0000
committerTavian Barnes <tavianator@gmail.com>2009-10-07 14:50:41 +0000
commit4317faa8365b5c08d9111ddd1f0a622ed9e99b52 (patch)
tree8793f640e6f28d6274e5e92b5f52e9476011f4d5 /libdimension
parent5f46ca9570f887e057bec38c7430dbdd54f7a85b (diff)
downloaddimension-4317faa8365b5c08d9111ddd1f0a622ed9e99b52.tar.xz
Add ray - bounding-box intersection test function.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/raytrace.c72
1 files changed, 72 insertions, 0 deletions
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;
+}