diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-12-17 16:43:19 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-12-17 17:49:00 -0500 |
commit | 4dde640671a8278ff6c41ef21f03ba351828009c (patch) | |
tree | 4ab145084a16a6af07ff53d606c40464f8cabec0 /libdimension/triangle.c | |
parent | 65b1d1998f560c03c7cc204b0ee4f5c9c4f517e0 (diff) | |
download | dimension-4dde640671a8278ff6c41ef21f03ba351828009c.tar.xz |
Make triangles smooth.
Diffstat (limited to 'libdimension/triangle.c')
-rw-r--r-- | libdimension/triangle.c | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/libdimension/triangle.c b/libdimension/triangle.c index f224aa9..a176414 100644 --- a/libdimension/triangle.c +++ b/libdimension/triangle.c @@ -26,7 +26,7 @@ #include "dimension.h" typedef struct dmnsn_triangle_payload { - dmnsn_vector a, ab, ac, normal; + dmnsn_vector a, ab, ac, na, nab, nac, normal; } dmnsn_triangle_payload; /** Triangle intersection callback. */ @@ -43,7 +43,13 @@ dmnsn_triangle_intersection_fn(const dmnsn_object *triangle, dmnsn_line l, double v = -dmnsn_vector_dot(l.n, dmnsn_vector_cross(payload->ab, ax0))/den; if (t >= 0.0 && u >= 0.0 && v >= 0.0 && u + v <= 1.0) { intersection->t = t; - intersection->normal = payload->normal; + intersection->normal = dmnsn_vector_add( + payload->na, + dmnsn_vector_add( + dmnsn_vector_mul(u, payload->nab), + dmnsn_vector_mul(v, payload->nac) + ) + ); return true; } @@ -59,17 +65,24 @@ dmnsn_triangle_inside_fn(const dmnsn_object *triangle, dmnsn_vector point) /* Allocate a new triangle */ dmnsn_object * -dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) +dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c, + dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc) { - dmnsn_object *triangle = dmnsn_new_object(); + na = dmnsn_vector_normalized(na); + nb = dmnsn_vector_normalized(nb); + nc = dmnsn_vector_normalized(nc); dmnsn_triangle_payload *payload = dmnsn_malloc(sizeof(dmnsn_triangle_payload)); payload->a = a; + payload->na = na; payload->ab = dmnsn_vector_sub(b, a); + payload->nab = dmnsn_vector_sub(nb, na); payload->ac = dmnsn_vector_sub(c, a); + payload->nac = dmnsn_vector_sub(nc, na); payload->normal = dmnsn_vector_cross(payload->ab, payload->ac); + dmnsn_object *triangle = dmnsn_new_object(); triangle->ptr = payload; triangle->intersection_fn = dmnsn_triangle_intersection_fn; triangle->inside_fn = dmnsn_triangle_inside_fn; @@ -78,3 +91,16 @@ dmnsn_new_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) triangle->bounding_box.max = dmnsn_vector_max(dmnsn_vector_max(a, b), c); return triangle; } + +/* Allocate a new flat triangle */ +dmnsn_object * +dmnsn_new_flat_triangle(dmnsn_vector a, dmnsn_vector b, dmnsn_vector c) +{ + /* Flat triangles are just smooth triangles with identical normals at all + verticies */ + dmnsn_vector normal = dmnsn_vector_cross( + dmnsn_vector_sub(b, a), + dmnsn_vector_sub(c, a) + ); + return dmnsn_new_triangle(a, b, c, normal, normal, normal); +} |