diff options
-rw-r--r-- | dimension/realize.c | 2 | ||||
-rw-r--r-- | libdimension/dimension/object.h | 2 | ||||
-rw-r--r-- | libdimension/kD_splay_tree.c | 25 | ||||
-rw-r--r-- | tests/libdimension/tests.c | 8 |
4 files changed, 16 insertions, 21 deletions
diff --git a/dimension/realize.c b/dimension/realize.c index b5c4225..bb977a6 100644 --- a/dimension/realize.c +++ b/dimension/realize.c @@ -113,7 +113,6 @@ dmnsn_realize_box(dmnsn_astnode astnode) (x2.z + x1.z)/2.0)), box->trans ); - box->trans = dmnsn_matrix_inverse(box->trans); return box; } @@ -151,7 +150,6 @@ dmnsn_realize_sphere(dmnsn_astnode astnode) sphere->trans = dmnsn_scale_matrix(dmnsn_vector_construct(r, r, r)); sphere->trans = dmnsn_matrix_mul(dmnsn_translation_matrix(x0), sphere->trans); - sphere->trans = dmnsn_matrix_inverse(sphere->trans); return sphere; } diff --git a/libdimension/dimension/object.h b/libdimension/dimension/object.h index fd0eba1..0f53406 100644 --- a/libdimension/dimension/object.h +++ b/libdimension/dimension/object.h @@ -56,7 +56,7 @@ struct dmnsn_object { dmnsn_texture *texture; /* Transformation matrix */ - dmnsn_matrix trans; + dmnsn_matrix trans, trans_inv; /* Bounding box */ dmnsn_vector min, max; diff --git a/libdimension/kD_splay_tree.c b/libdimension/kD_splay_tree.c index 44fae7b..5c6289d 100644 --- a/libdimension/kD_splay_tree.c +++ b/libdimension/kD_splay_tree.c @@ -98,10 +98,11 @@ static void dmnsn_kD_splay_node_swallow(dmnsn_kD_splay_node *node, void dmnsn_kD_splay_insert(dmnsn_kD_splay_tree *tree, dmnsn_object *object) { - dmnsn_vector corner; - dmnsn_matrix trans_inv; dmnsn_kD_splay_node *node = dmnsn_new_kD_splay_node(), *parent = tree->root; + /* Store the inverse of the transformation matrix */ + object->trans_inv = dmnsn_matrix_inverse(object->trans); + node->contains = NULL; node->container = NULL; node->parent = NULL; @@ -110,36 +111,36 @@ dmnsn_kD_splay_insert(dmnsn_kD_splay_tree *tree, dmnsn_object *object) /* Calculate the new bounding box by finding the minimum coordinate of the transformed corners of the object's original bounding box */ - trans_inv = dmnsn_matrix_inverse(object->trans); - node->min = dmnsn_matrix_vector_mul(trans_inv, object->min); + node->min = dmnsn_matrix_vector_mul(object->trans, object->min); node->max = node->min; + dmnsn_vector corner; corner = dmnsn_vector_construct(object->min.x, object->min.y, object->max.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->min.x, object->max.y, object->min.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->min.x, object->max.y, object->max.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->max.x, object->min.y, object->min.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->max.x, object->min.y, object->max.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->max.x, object->max.y, object->min.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); corner = dmnsn_vector_construct(object->max.x, object->max.y, object->max.z); - corner = dmnsn_matrix_vector_mul(trans_inv, corner); + corner = dmnsn_matrix_vector_mul(object->trans, corner); dmnsn_kD_splay_node_swallow(node, corner, corner); /* Now insert the node */ @@ -337,7 +338,7 @@ dmnsn_kD_splay_search_recursive(dmnsn_kD_splay_node *node, dmnsn_line ray, || dmnsn_ray_box_intersection(ray, node->min, node->max, t)) { /* Transform the ray according to the object */ - ray_trans = dmnsn_matrix_line_mul(node->object->trans, ray); + ray_trans = dmnsn_matrix_line_mul(node->object->trans_inv, ray); if (dmnsn_box_contains(ray_trans.x0, node->object->min, node->object->max) || dmnsn_ray_box_intersection(ray_trans, node->object->min, diff --git a/tests/libdimension/tests.c b/tests/libdimension/tests.c index 1257219..87cbb7b 100644 --- a/tests/libdimension/tests.c +++ b/tests/libdimension/tests.c @@ -86,9 +86,7 @@ dmnsn_new_default_scene() return NULL; } - sphere->trans = dmnsn_matrix_inverse( - dmnsn_scale_matrix(dmnsn_vector_construct(1.25, 1.25, 1.25)) - ); + sphere->trans = dmnsn_scale_matrix(dmnsn_vector_construct(1.25, 1.25, 1.25)); dmnsn_object *cube = dmnsn_new_cube(); if (!cube) { @@ -109,9 +107,7 @@ dmnsn_new_default_scene() return NULL; } - cube->trans = dmnsn_matrix_inverse( - dmnsn_rotation_matrix(dmnsn_vector_construct(0.75, 0.0, 0.0)) - ); + cube->trans = dmnsn_rotation_matrix(dmnsn_vector_construct(0.75, 0.0, 0.0)); /* Now make a light */ |