From 00997fa54c64b53618255b1e292d3db4d18b86af Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 16 Jul 2009 01:16:16 +0000 Subject: New dmnsn_intersection* type for ray-object intersections. --- libdimension/objects.c | 104 ++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 41 deletions(-) (limited to 'libdimension/objects.c') diff --git a/libdimension/objects.c b/libdimension/objects.c index 32d3fdd..780c06b 100644 --- a/libdimension/objects.c +++ b/libdimension/objects.c @@ -27,8 +27,10 @@ */ /* Sphere object callbacks */ -static dmnsn_array *dmnsn_sphere_intersections_fn(const dmnsn_object *sphere, - dmnsn_line line); + +static dmnsn_intersection * +dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_line line); + static int dmnsn_sphere_inside_fn(const dmnsn_object *sphere, dmnsn_vector point); @@ -38,18 +40,18 @@ dmnsn_new_sphere() { dmnsn_object *sphere = dmnsn_new_object(); if (sphere) { - sphere->intersections_fn = &dmnsn_sphere_intersections_fn; - sphere->inside_fn = &dmnsn_sphere_inside_fn; + sphere->intersection_fn = &dmnsn_sphere_intersection_fn; + sphere->inside_fn = &dmnsn_sphere_inside_fn; } return sphere; } -/* Return a list of insersections of `line' with a sphere */ -static dmnsn_array * -dmnsn_sphere_intersections_fn(const dmnsn_object *sphere, dmnsn_line line) +/* Returns the closest intersection of `line' with `sphere' */ +static dmnsn_intersection * +dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_line line) { - double a, b, c, t[2]; - dmnsn_array *array = dmnsn_new_array(sizeof(double)); + double a, b, c, t; + dmnsn_intersection *intersection = NULL; /* Solve (x0 + nx*t)^2 + (y0 + ny*t)^2 + (z0 + nz*t)^2 == 1 */ @@ -58,13 +60,20 @@ dmnsn_sphere_intersections_fn(const dmnsn_object *sphere, dmnsn_line line) c = line.x0.x*line.x0.x + line.x0.y*line.x0.y + line.x0.z*line.x0.z - 1.0; if (b*b - 4.0*a*c >= 0) { - t[0] = (-b + sqrt(b*b - 4.0*a*c))/(2*a); - t[1] = (-b - sqrt(b*b - 4.0*a*c))/(2*a); - dmnsn_array_set(array, 0, &t[0]); - dmnsn_array_set(array, 1, &t[1]); + t = (-b - sqrt(b*b - 4.0*a*c))/(2*a); + if (t < 0.0) { + t = (-b + sqrt(b*b - 4.0*a*c))/(2*a); + } + + if (t >= 0.0) { + intersection = dmnsn_new_intersection(); + intersection->ray = line; + intersection->t = t; + intersection->texture = sphere->texture; + } } - return array; + return intersection; } /* Return whether a point is inside a sphere (x**2 + y**2 + z**2 < 1.0) */ @@ -79,8 +88,8 @@ dmnsn_sphere_inside_fn(const dmnsn_object *sphere, dmnsn_vector point) */ /* Cube callbacks */ -static dmnsn_array *dmnsn_cube_intersections_fn(const dmnsn_object *cube, - dmnsn_line line); +static dmnsn_intersection *dmnsn_cube_intersection_fn(const dmnsn_object *cube, + dmnsn_line line); static int dmnsn_cube_inside_fn(const dmnsn_object *cube, dmnsn_vector point); @@ -90,71 +99,84 @@ dmnsn_new_cube() { dmnsn_object *cube = dmnsn_new_object(); if (cube) { - cube->intersections_fn = &dmnsn_cube_intersections_fn; + cube->intersection_fn = &dmnsn_cube_intersection_fn; cube->inside_fn = &dmnsn_cube_inside_fn; } return cube; } /* Intersections callback for a cube */ -static dmnsn_array * -dmnsn_cube_intersections_fn(const dmnsn_object *cube, dmnsn_line line) +static dmnsn_intersection * +dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_line line) { - double t; + double t = -1.0, t_temp; dmnsn_vector p; - dmnsn_array *array = dmnsn_new_array(sizeof(double)); + dmnsn_intersection *intersection = NULL; /* Six ray-plane intersection tests (x, y, z) = +/- 1.0 */ if (line.n.x != 0.0) { /* x = -1.0 */ - t = (-1.0 - line.x0.x)/line.n.x; - p = dmnsn_line_point(line, t); - if (p.y >= -1.0 && p.y <= 1.0 && p.z >= -1.0 && p.z <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } /* x = 1.0 */ - t = (1.0 - line.x0.x)/line.n.x; + t_temp = (1.0 - line.x0.x)/line.n.x; p = dmnsn_line_point(line, t); - if (p.y >= -1.0 && p.y <= 1.0 && p.z >= -1.0 && p.z <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } } if (line.n.y != 0.0) { /* y = -1.0 */ - t = (-1.0 - line.x0.y)/line.n.y; + t_temp = (-1.0 - line.x0.y)/line.n.y; p = dmnsn_line_point(line, t); - if (p.x >= -1.0 && p.x <= 1.0 && p.z >= -1.0 && p.z <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } /* y = 1.0 */ - t = (1.0 - line.x0.y)/line.n.y; + t_temp = (1.0 - line.x0.y)/line.n.y; p = dmnsn_line_point(line, t); - if (p.x >= -1.0 && p.x <= 1.0 && p.z >= -1.0 && p.z <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } } if (line.n.z != 0.0) { /* z = -1.0 */ - t = (-1.0 - line.x0.z)/line.n.z; + t_temp = (-1.0 - line.x0.z)/line.n.z; p = dmnsn_line_point(line, t); - if (p.x >= -1.0 && p.x <= 1.0 && p.y >= -1.0 && p.y <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } /* z = 1.0 */ - t = (1.0 - line.x0.z)/line.n.z; + t_temp = (1.0 - line.x0.z)/line.n.z; p = dmnsn_line_point(line, t); - if (p.x >= -1.0 && p.x <= 1.0 && p.y >= -1.0 && p.y <= 1.0) { - dmnsn_array_push(array, &t); + 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 = t_temp; } } - return array; + if (t >= 0.0) { + intersection = dmnsn_new_intersection(); + intersection->ray = line; + intersection->t = t; + intersection->texture = cube->texture; + } + + return intersection; } /* Inside callback for a cube */ -- cgit v1.2.3