From 5c73e21b426ed381a1ae8d1bcac6fc78e5952b8f Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Jun 2009 18:18:19 +0000 Subject: Add a transformation matrix to objects. --- libdimension/dimension/object.h | 3 +++ libdimension/object.c | 6 +++++- libdimension/raytrace.c | 10 +++++++--- tests/raytrace.c | 1 + 4 files changed, 16 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, diff --git a/tests/raytrace.c b/tests/raytrace.c index de1bc89..ec13782 100644 --- a/tests/raytrace.c +++ b/tests/raytrace.c @@ -59,6 +59,7 @@ int main() { scene->background = color; cube = dmnsn_new_cube(); + cube->trans = dmnsn_rotation_matrix(dmnsn_vector_construct(0.75, 0.0, 0.0)); dmnsn_array_push(scene->objects, &cube); dmnsn_raytrace_scene(scene); -- cgit v1.2.3