diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 18:18:19 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-06-14 18:18:19 +0000 |
commit | 5c73e21b426ed381a1ae8d1bcac6fc78e5952b8f (patch) | |
tree | 979b425b4ef93e1492b45700c4ac3f899d4fc60e /libdimension | |
parent | 5e8d0c611681d866d903a087ea238aeb5fbb5508 (diff) | |
download | dimension-5c73e21b426ed381a1ae8d1bcac6fc78e5952b8f.tar.xz |
Add a transformation matrix to objects.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/dimension/object.h | 3 | ||||
-rw-r--r-- | libdimension/object.c | 6 | ||||
-rw-r--r-- | libdimension/raytrace.c | 10 |
3 files changed, 15 insertions, 4 deletions
diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index bd660a0..589ac29 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -38,6 +38,9 @@ struct dmnsn_object { /* Generic pointer for object info */ void *ptr; + /* Transformation matrix */ + dmnsn_matrix trans; + /* Callback functions */ dmnsn_object_intersections_fn *intersections_fn; dmnsn_object_inside_fn *inside_fn; diff --git a/libdimension/object.c b/libdimension/object.c index 39064f3..75a82b6 100644 --- a/libdimension/object.c +++ b/libdimension/object.c @@ -24,7 +24,11 @@ dmnsn_object * dmnsn_new_object() { - return malloc(sizeof(dmnsn_object)); + dmnsn_object *object = malloc(sizeof(dmnsn_object)); + if (object) { + object->trans = dmnsn_identity_matrix(); + } + return object; } void diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index 469ac66..316157a 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -68,7 +68,7 @@ dmnsn_raytrace_scene_thread(void *arg) { unsigned int i, j, k; dmnsn_object *object; - dmnsn_line ray; + dmnsn_line ray, ray_trans; dmnsn_raytrace_thread_payload *payload = (dmnsn_raytrace_thread_payload *)arg; dmnsn_scene *scene = payload->scene; dmnsn_array *intersections; @@ -83,11 +83,15 @@ dmnsn_raytrace_scene_thread(void *arg) /* Get the ray corresponding to the (i,j)th pixel */ ray = (*scene->camera->ray_fn)(scene->camera, scene->canvas, i, j); - + for (k = 0; k < scene->objects->length; ++k) { dmnsn_array_get(scene->objects, k, &object); + + /* Transform the ray according to the object */ + ray_trans = dmnsn_matrix_line_mul(object->trans, ray); + /* Test for an intersection with an object */ - intersections = (*object->intersections_fn)(object, ray); + intersections = (*object->intersections_fn)(object, ray_trans); if (intersections->length > 0) { /* Mark intersections white */ dmnsn_set_pixel(scene->canvas, i, j, |