summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libdimension/Makefile.am1
-rw-r--r--libdimension/cylinder.c93
-rw-r--r--libdimension/dimension/objects.h3
-rw-r--r--libdimension/sphere.c2
-rw-r--r--tests/libdimension/render.c10
5 files changed, 108 insertions, 1 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 77d3285..e437659 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -55,6 +55,7 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \
color.c \
cube.c \
csg.c \
+ cylinder.c \
diffuse.c \
dimension-impl.h \
error.c \
diff --git a/libdimension/cylinder.c b/libdimension/cylinder.c
new file mode 100644
index 0000000..fb9c12c
--- /dev/null
+++ b/libdimension/cylinder.c
@@ -0,0 +1,93 @@
+/*************************************************************************
+ * Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library is free software; you can redistribute it and/ *
+ * or modify it under the terms of the GNU Lesser General Public License *
+ * as published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this program. If not, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+#include "dimension.h"
+#include <math.h>
+
+/*
+ * Cylinder
+ */
+
+/* Cylinder callbacks */
+static bool dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder,
+ dmnsn_line line,
+ dmnsn_intersection *intersection);
+static bool dmnsn_cylinder_inside_fn(const dmnsn_object *cylinder,
+ dmnsn_vector point);
+
+/* Allocate a new cylinder object */
+dmnsn_object *
+dmnsn_new_cylinder()
+{
+ dmnsn_object *cylinder = dmnsn_new_object();
+ cylinder->intersection_fn = &dmnsn_cylinder_intersection_fn;
+ cylinder->inside_fn = &dmnsn_cylinder_inside_fn;
+ cylinder->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0);
+ cylinder->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0);
+ return cylinder;
+}
+
+/* Intersections callback for a cylinder */
+static bool
+dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder, dmnsn_line line,
+ dmnsn_intersection *intersection)
+{
+ dmnsn_line l = dmnsn_transform_line(cylinder->trans_inv, line);
+
+ /* Solve (x0 + nx*t)^2 + (z0 + nz*t)^2 == 1 */
+ double a, b, c, t;
+ a = l.n.x*l.n.x + l.n.z*l.n.z;
+ b = 2.0*(l.n.x*l.x0.x + l.n.z*l.x0.z);
+ c = l.x0.x*l.x0.x + l.x0.z*l.x0.z - 1.0;
+
+
+ if (b*b - 4.0*a*c >= 0.0) {
+ t = (-b - sqrt(b*b - 4.0*a*c))/(2.0*a);
+ dmnsn_vector p = dmnsn_line_point(l, t);
+
+ if (t < 0.0 || p.y <= -1.0 || p.y >= 1.0) {
+ t = (-b + sqrt(b*b - 4.0*a*c))/(2.0*a);
+ p = dmnsn_line_point(l, t);
+
+ if (t < 0.0 || p.y <= -1.0 || p.y >= 1.0)
+ return false;
+ }
+
+ p.y = 0;
+
+ intersection->ray = line;
+ intersection->t = t;
+ intersection->normal = dmnsn_transform_normal(cylinder->trans, p);
+ intersection->texture = cylinder->texture;
+ intersection->interior = cylinder->interior;
+ return true;
+ }
+
+ return false;
+}
+
+/* Inside callback for a cylinder */
+static bool
+dmnsn_cylinder_inside_fn(const dmnsn_object *cylinder, dmnsn_vector point)
+{
+ point = dmnsn_transform_vector(cylinder->trans_inv, point);
+ return point.x*point.x + point.z*point.z < 1
+ && point.y > -1.0 && point.y < 1.0;
+}
diff --git a/libdimension/dimension/objects.h b/libdimension/dimension/objects.h
index 9986717..4db161c 100644
--- a/libdimension/dimension/objects.h
+++ b/libdimension/dimension/objects.h
@@ -34,4 +34,7 @@ dmnsn_object *dmnsn_new_sphere(void);
/* A cube, axis-aligned, from (-1, -1, -1) to (1, 1, 1) */
dmnsn_object *dmnsn_new_cube(void);
+/* A cylinder, of radius 1, from y = -1 to y = 1 */
+dmnsn_object *dmnsn_new_cylinder(void);
+
#endif /* DIMENSION_OBJECTS_H */
diff --git a/libdimension/sphere.c b/libdimension/sphere.c
index a3ecf2d..98be4b5 100644
--- a/libdimension/sphere.c
+++ b/libdimension/sphere.c
@@ -51,7 +51,7 @@ dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_line line,
dmnsn_intersection *intersection)
{
dmnsn_line l = dmnsn_transform_line(sphere->trans_inv, line);
-
+
/* Solve (x0 + nx*t)^2 + (y0 + ny*t)^2 + (z0 + nz*t)^2 == 1 */
double a, b, c, t;
a = dmnsn_vector_dot(l.n, l.n);
diff --git a/tests/libdimension/render.c b/tests/libdimension/render.c
index 29e733e..7a55320 100644
--- a/tests/libdimension/render.c
+++ b/tests/libdimension/render.c
@@ -106,6 +106,16 @@ dmnsn_new_test_scene(void)
plane->texture->pigment = dmnsn_new_solid_pigment(dmnsn_white);
dmnsn_array_push(scene->objects, &plane);
+ dmnsn_object *cylinder = dmnsn_new_cylinder();
+ cylinder->trans =
+ dmnsn_matrix_mul(
+ dmnsn_rotation_matrix(dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0)),
+ dmnsn_scale_matrix(dmnsn_new_vector(0.1, 1.5, 0.1))
+ );
+ cylinder->texture = dmnsn_new_texture();
+ cylinder->texture->pigment = dmnsn_new_solid_pigment(dmnsn_red);
+ dmnsn_array_push(scene->objects, &cylinder);
+
return scene;
}