diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-07-16 01:16:16 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-07-16 01:16:16 +0000 |
commit | 00997fa54c64b53618255b1e292d3db4d18b86af (patch) | |
tree | 45ffdb6c3b604fef398f6c01d8d78aa48e3231d6 /libdimension | |
parent | 8f01c5394dcce8f5d4e7102dacfcdea9d1f7b021 (diff) | |
download | dimension-00997fa54c64b53618255b1e292d3db4d18b86af.tar.xz |
New dmnsn_intersection* type for ray-object intersections.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/object.h | 26 | ||||
-rw-r--r-- | libdimension/objects.c | 104 | ||||
-rw-r--r-- | libdimension/raytrace.c | 9 |
3 files changed, 86 insertions, 53 deletions
diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index 901b366..fefb061 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -25,12 +25,27 @@ #ifndef DIMENSION_OBJECT_H #define DIMENSION_OBJECT_H +/* A type to represent a ray-object intersection */ +typedef struct { + /* The ray and point which intersected */ + dmnsn_line ray; + double t; + + /* The texture at the intersection point */ + const dmnsn_texture *texture; +} dmnsn_intersection; + +dmnsn_intersection *dmnsn_new_intersection(); +void dmnsn_delete_intersection(dmnsn_intersection *intersection); + /* Forward-declare dmnsn_object */ typedef struct dmnsn_object dmnsn_object; /* Object callback types */ -typedef dmnsn_array *dmnsn_object_intersections_fn(const dmnsn_object *object, - dmnsn_line line); + +typedef dmnsn_intersection * +dmnsn_object_intersection_fn(const dmnsn_object *object, dmnsn_line line); + typedef int dmnsn_object_inside_fn(const dmnsn_object *object, dmnsn_vector point); @@ -43,9 +58,9 @@ struct dmnsn_object { dmnsn_matrix trans; /* Callback functions */ - dmnsn_object_intersections_fn *intersections_fn; - dmnsn_object_inside_fn *inside_fn; - dmnsn_free_fn *free_fn; + dmnsn_object_intersection_fn *intersection_fn; + dmnsn_object_inside_fn *inside_fn; + dmnsn_free_fn *free_fn; /* Generic pointer for object info */ void *ptr; @@ -53,7 +68,6 @@ struct dmnsn_object { /* Allocate a dummy object */ dmnsn_object *dmnsn_new_object(); - /* Free an object */ void dmnsn_delete_object(dmnsn_object *object); 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 */ diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 03dcbab..75e857c 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -230,7 +230,7 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, { dmnsn_line ray, ray_trans; dmnsn_object *object; - dmnsn_array *intersections; + dmnsn_intersection *intersection; unsigned int i; /* Get the ray corresponding to the (x,y)'th pixel */ @@ -243,14 +243,11 @@ dmnsn_raytrace_shoot(dmnsn_scene *scene, dmnsn_color color, ray_trans = dmnsn_matrix_line_mul(object->trans, ray); /* Test for intersections with objects */ - intersections = (*object->intersections_fn)(object, ray_trans); + intersection = (*object->intersection_fn)(object, ray_trans); - if (dmnsn_array_size(intersections) > 0) { + if (intersection) { color = dmnsn_color_from_XYZ(dmnsn_whitepoint); } - - /* Delete the intersections array */ - dmnsn_delete_array(intersections); } return color; |