summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-06-04 14:56:30 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-06-04 14:58:32 -0400
commit13779f79f41e8bce483e98faa6353c07c12563c8 (patch)
tree32e9daa27f7eb687eb60692529179fb75124f976
parent36e399810a25d5d3a41570ee0f52f8e6d1402fbc (diff)
downloaddimension-13779f79f41e8bce483e98faa6353c07c12563c8.tar.xz
triangles: Better API.
-rw-r--r--libdimension-python/dimension.pxd7
-rw-r--r--libdimension-python/dimension.pyx17
-rw-r--r--libdimension/bench/triangle.c8
-rw-r--r--libdimension/dimension/objects.h24
-rw-r--r--libdimension/tests/render.c16
-rw-r--r--libdimension/triangle.c42
6 files changed, 53 insertions, 61 deletions
diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd
index f5a723c..6579c89 100644
--- a/libdimension-python/dimension.pxd
+++ b/libdimension-python/dimension.pxd
@@ -318,11 +318,8 @@ cdef extern from "../libdimension/dimension.h":
dmnsn_object *dmnsn_new_object(dmnsn_pool *pool)
- dmnsn_object *dmnsn_new_flat_triangle(dmnsn_pool *pool, dmnsn_vector a, dmnsn_vector b, dmnsn_vector c)
- dmnsn_object *dmnsn_new_triangle(
- dmnsn_pool *pool,
- dmnsn_vector a, dmnsn_vector b, dmnsn_vector c,
- dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc)
+ dmnsn_object *dmnsn_new_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3])
+ dmnsn_object *dmnsn_new_smooth_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3], dmnsn_vector normals[3])
dmnsn_object *dmnsn_new_plane(dmnsn_pool *pool, dmnsn_vector normal)
dmnsn_object *dmnsn_new_sphere(dmnsn_pool *pool)
dmnsn_object *dmnsn_new_cube(dmnsn_pool *pool)
diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx
index d8bbb3a..d3f880c 100644
--- a/libdimension-python/dimension.pyx
+++ b/libdimension-python/dimension.pyx
@@ -1129,11 +1129,18 @@ cdef class Triangle(Object):
a_normal = cross(b - a, c - a)
b_normal = a_normal
c_normal = a_normal
- self._object = dmnsn_new_triangle(
- _get_pool(),
- Vector(a)._v, Vector(b)._v, Vector(c)._v,
- Vector(a_normal)._v, Vector(b_normal)._v, Vector(c_normal)._v
- )
+
+ cdef dmnsn_vector vertices[3]
+ vertices[0] = Vector(a)._v
+ vertices[1] = Vector(b)._v
+ vertices[2] = Vector(c)._v
+
+ cdef dmnsn_vector normals[3]
+ normals[0] = Vector(a_normal)._v
+ normals[1] = Vector(b_normal)._v
+ normals[2] = Vector(c_normal)._v
+
+ self._object = dmnsn_new_smooth_triangle(_get_pool(), vertices, normals)
Object.__init__(self, *args, **kwargs)
cdef class Plane(Object):
diff --git a/libdimension/bench/triangle.c b/libdimension/bench/triangle.c
index 39b9322..297417c 100644
--- a/libdimension/bench/triangle.c
+++ b/libdimension/bench/triangle.c
@@ -32,12 +32,12 @@ main(void)
dmnsn_pool *pool = dmnsn_new_pool();
- dmnsn_object *triangle = dmnsn_new_flat_triangle(
- pool,
+ dmnsn_vector vertices[] = {
dmnsn_new_vector(1.0, 0.0, 0.0),
dmnsn_new_vector(2.0, 2.0, 1.0),
- dmnsn_new_vector(3.0, 0.0, 2.0)
- );
+ dmnsn_new_vector(3.0, 0.0, 2.0),
+ };
+ dmnsn_object *triangle = dmnsn_new_triangle(pool, vertices);
triangle->texture = dmnsn_new_texture(pool);
triangle->texture->pigment = dmnsn_new_pigment(pool);
dmnsn_object_initialize(triangle);
diff --git a/libdimension/dimension/objects.h b/libdimension/dimension/objects.h
index eb476cf..b5d3bd7 100644
--- a/libdimension/dimension/objects.h
+++ b/libdimension/dimension/objects.h
@@ -26,29 +26,19 @@
#include <stdbool.h>
/**
- * A triangle, with normals interpolated between the points.
+ * A flat triangle, without normal interpolation.
* @param[in] pool The memory pool to allocate from.
- * @param[in] a The first corner of the triangle.
- * @param[in] b The second corner of the triangle.
- * @param[in] c The third corner of the triangle.
- * @param[in] na The normal at \p a.
- * @param[in] nb The normal at \p b.
- * @param[in] nc The normal at \p c.
+ * @param[in] vertices The corners of the triangle.
*/
-dmnsn_object *dmnsn_new_triangle(
- dmnsn_pool *pool,
- dmnsn_vector a, dmnsn_vector b, dmnsn_vector c,
- dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc
-);
+dmnsn_object *dmnsn_new_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3]);
/**
- * A flat triangle, without normal interpolation.
+ * A triangle, with normals interpolated between the points.
* @param[in] pool The memory pool to allocate from.
- * @param[in] a The first corner of the triangle.
- * @param[in] b The second corner of the triangle.
- * @param[in] c The third corner of the triangle.
+ * @param[in] vertices The corners of the triangle.
+ * @param[in] normals The normals at each corner.
*/
-dmnsn_object *dmnsn_new_flat_triangle(dmnsn_pool *pool, dmnsn_vector a, dmnsn_vector b, dmnsn_vector c);
+dmnsn_object *dmnsn_new_smooth_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3], dmnsn_vector normals[3]);
/**
* A plane.
diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c
index f12aa8b..bd0c8ca 100644
--- a/libdimension/tests/render.c
+++ b/libdimension/tests/render.c
@@ -222,9 +222,11 @@ static void
dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene)
{
dmnsn_array *strip_array = DMNSN_NEW_ARRAY(dmnsn_object *);
- dmnsn_vector a = dmnsn_zero;
- dmnsn_vector b = dmnsn_new_vector(0.0, sqrt(3.0)/2.0, 0.5);
- dmnsn_vector c = dmnsn_z;
+ dmnsn_vector vertices[] = {
+ dmnsn_zero,
+ dmnsn_new_vector(0.0, sqrt(3.0)/2.0, 0.5),
+ dmnsn_z,
+ };
dmnsn_texture *strip_textures[3] = {
dmnsn_new_texture(pool),
dmnsn_new_texture(pool),
@@ -234,13 +236,13 @@ dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene)
strip_textures[1]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange));
strip_textures[2]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_yellow));
for (unsigned int i = 0; i < 128; ++i) {
- dmnsn_object *triangle = dmnsn_new_flat_triangle(pool, a, b, c);
+ dmnsn_object *triangle = dmnsn_new_triangle(pool, vertices);
triangle->texture = strip_textures[i%3];
dmnsn_array_push(strip_array, &triangle);
- a = b;
- b = c;
- c = dmnsn_vector_add(a, dmnsn_z);
+ vertices[0] = vertices[1];
+ vertices[1] = vertices[2];
+ vertices[2] = dmnsn_vector_add(vertices[0], dmnsn_z);
}
dmnsn_object *strip = dmnsn_new_csg_union(pool, strip_array);
diff --git a/libdimension/triangle.c b/libdimension/triangle.c
index 94a6a95..8e584d8 100644
--- a/libdimension/triangle.c
+++ b/libdimension/triangle.c
@@ -64,15 +64,24 @@ dmnsn_triangle_inside_fn(const dmnsn_object *triangle, dmnsn_vector point)
return false;
}
+dmnsn_object *
+dmnsn_new_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3])
+{
+ dmnsn_vector normal = dmnsn_vector_cross(
+ dmnsn_vector_sub(vertices[1], vertices[0]),
+ dmnsn_vector_sub(vertices[2], vertices[0])
+ );
+ dmnsn_vector normals[] = { normal, normal, normal };
+ return dmnsn_new_smooth_triangle(pool, vertices, normals);
+}
+
/* Allocate a new triangle */
dmnsn_object *
-dmnsn_new_triangle(dmnsn_pool *pool,
- dmnsn_vector a, dmnsn_vector b, dmnsn_vector c,
- dmnsn_vector na, dmnsn_vector nb, dmnsn_vector nc)
+dmnsn_new_smooth_triangle(dmnsn_pool *pool, dmnsn_vector vertices[3], dmnsn_vector normals[3])
{
- na = dmnsn_vector_normalized(na);
- nb = dmnsn_vector_normalized(nb);
- nc = dmnsn_vector_normalized(nc);
+ dmnsn_vector na = dmnsn_vector_normalized(normals[0]);
+ dmnsn_vector nb = dmnsn_vector_normalized(normals[1]);
+ dmnsn_vector nc = dmnsn_vector_normalized(normals[2]);
dmnsn_triangle *triangle = DMNSN_PALLOC(pool, dmnsn_triangle);
triangle->na = na;
@@ -81,7 +90,7 @@ dmnsn_new_triangle(dmnsn_pool *pool,
dmnsn_object *object = &triangle->object;
dmnsn_init_object(pool, object);
- object->intersection_fn = dmnsn_triangle_intersection_fn;
+ object->intersection_fn = dmnsn_triangle_intersection_fn;
object->inside_fn = dmnsn_triangle_inside_fn;
object->bounding_box.min = dmnsn_zero;
object->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 0.0);
@@ -92,23 +101,10 @@ dmnsn_new_triangle(dmnsn_pool *pool,
* The new vector space has corners at <0, 1, 0>, <0, 0, 1>, and 0,
* corresponding to the basis (ab, ac, ab X ac).
*/
- dmnsn_vector ab = dmnsn_vector_sub(b, a);
- dmnsn_vector ac = dmnsn_vector_sub(c, a);
+ dmnsn_vector ab = dmnsn_vector_sub(vertices[1], vertices[0]);
+ dmnsn_vector ac = dmnsn_vector_sub(vertices[2], vertices[0]);
dmnsn_vector normal = dmnsn_vector_cross(ab, ac);
- object->intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, a);
+ object->intrinsic_trans = dmnsn_new_matrix4(ab, ac, normal, vertices[0]);
return object;
}
-
-/* Allocate a new flat triangle */
-dmnsn_object *
-dmnsn_new_flat_triangle(dmnsn_pool *pool, 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(pool, a, b, c, normal, normal, normal);
-}