summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-11-06 16:57:33 -0400
committerTavian Barnes <tavianator@gmail.com>2010-11-06 16:57:33 -0400
commitd7e9d913730b96a83751863ad896648488296532 (patch)
tree14d16fc53339b272fef7d33092dd5eb89da9d5e6 /libdimension
parentfd741e79c56c78de91ab3cbcbeaee28eddcee2dd (diff)
downloaddimension-d7e9d913730b96a83751863ad896648488296532.tar.xz
Add pattern framework, impelement checker pattern.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/Makefile.am5
-rw-r--r--libdimension/checker.c58
-rw-r--r--libdimension/color_map.c120
-rw-r--r--libdimension/dimension.h2
-rw-r--r--libdimension/dimension/pattern.h55
-rw-r--r--libdimension/dimension/patterns.h30
-rw-r--r--libdimension/dimension/pigments.h13
-rw-r--r--libdimension/dimension/texture.h4
-rw-r--r--libdimension/pattern.c56
-rw-r--r--libdimension/texture.c5
10 files changed, 347 insertions, 1 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 8eb4832..8558a48 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -38,6 +38,8 @@ nobase_include_HEADERS = dimension.h \
dimension/malloc.h \
dimension/object.h \
dimension/objects.h \
+ dimension/pattern.h \
+ dimension/patterns.h \
dimension/pigments.h \
dimension/png.h \
dimension/progress.h \
@@ -53,7 +55,9 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \
camera.c \
canvas.c \
canvas_pigment.c \
+ checker.c \
color.c \
+ color_map.c \
cube.c \
csg.c \
cylinder.c \
@@ -68,6 +72,7 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \
list.c \
malloc.c \
object.c \
+ pattern.c \
perspective.c \
phong.c \
plane.c \
diff --git a/libdimension/checker.c b/libdimension/checker.c
new file mode 100644
index 0000000..9c1de34
--- /dev/null
+++ b/libdimension/checker.c
@@ -0,0 +1,58 @@
+/*************************************************************************
+ * Copyright (C) 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"
+
+/*
+ * Checker pattern
+ */
+
+static double
+dmnsn_checker_pattern_fn(const dmnsn_pattern *checker, dmnsn_vector v)
+{
+ double xmod = fmod(v.x, 2.0);
+ double ymod = fmod(v.y, 2.0);
+ double zmod = fmod(v.z, 2.0);
+
+ if (xmod < -dmnsn_epsilon)
+ xmod += 2.0;
+ if (ymod < -dmnsn_epsilon)
+ ymod += 2.0;
+ if (zmod < -dmnsn_epsilon)
+ zmod += 2.0;
+
+ /* Return 0 when an even number of coordinates are in [0, 1), 1 otherwise */
+ unsigned int n = 0;
+ if (xmod >= 1.0)
+ ++n;
+ if (ymod >= 1.0)
+ ++n;
+ if (zmod >= 1.0)
+ ++n;
+ return (n%2 == 0) ? 0.0 : 1.0;
+}
+
+dmnsn_pattern *
+dmnsn_new_checker_pattern()
+{
+ dmnsn_pattern *checker = dmnsn_new_pattern();
+ checker->pattern_fn = &dmnsn_checker_pattern_fn;
+ return checker;
+}
diff --git a/libdimension/color_map.c b/libdimension/color_map.c
new file mode 100644
index 0000000..e0ddcd3
--- /dev/null
+++ b/libdimension/color_map.c
@@ -0,0 +1,120 @@
+/*************************************************************************
+ * Copyright (C) 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"
+
+/*
+ * Color-mapped patterned pigments
+ */
+
+typedef struct dmnsn_color_map_entry {
+ double n;
+ dmnsn_color color;
+} dmnsn_color_map_entry;
+
+dmnsn_color_map *
+dmnsn_new_color_map()
+{
+ return dmnsn_new_array(sizeof(dmnsn_color_map_entry));
+}
+
+void
+dmnsn_delete_color_map(dmnsn_color_map *map)
+{
+ dmnsn_delete_array(map);
+}
+
+void
+dmnsn_add_color_map_entry(dmnsn_color_map *map, double n, dmnsn_color c)
+{
+ dmnsn_color_map_entry entry = { .n = n, .color = c };
+
+ /* Sorted insertion */
+ ssize_t i;
+ dmnsn_color_map_entry *other = dmnsn_array_last(map);
+ for (i = dmnsn_array_size(map) - 1; i >= 0; --i, --other) {
+ if (other->n <= n)
+ break;
+ }
+
+ dmnsn_array_insert(map, i + 1, &entry);
+}
+
+dmnsn_color
+dmnsn_color_map_value(const dmnsn_color_map *map, double n)
+{
+ dmnsn_color_map_entry *entry = dmnsn_array_first(map);
+
+ double n1, n2 = 0.0;
+ dmnsn_color c1, c2 = entry->color;
+
+ for (; entry <= (dmnsn_color_map_entry *)dmnsn_array_last(map); ++entry) {
+ n1 = n2;
+ c1 = c2;
+
+ n2 = entry->n;
+ c2 = entry->color;
+
+ if (n < n2) {
+ return dmnsn_color_gradient(c1, c2, (n - n1)/(n2 - n1));
+ }
+ }
+
+ return c2;
+}
+
+typedef struct dmnsn_color_map_payload {
+ dmnsn_pattern *pattern;
+ dmnsn_color_map *map;
+} dmnsn_color_map_payload;
+
+static dmnsn_color
+dmnsn_color_map_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
+{
+ const dmnsn_color_map_payload *payload = pigment->ptr;
+ return dmnsn_color_map_value(payload->map,
+ dmnsn_pattern_value(payload->pattern, v));
+}
+
+static void
+dmnsn_color_map_init_fn(dmnsn_pigment *pigment)
+{
+ dmnsn_color_map_payload *payload = pigment->ptr;
+ payload->pattern->trans = dmnsn_matrix_mul(pigment->trans,
+ payload->pattern->trans);
+ dmnsn_pattern_init(payload->pattern);
+}
+
+dmnsn_pigment *
+dmnsn_new_color_map_pigment(dmnsn_pattern *pattern, dmnsn_color_map *map)
+{
+ dmnsn_pigment *pigment = dmnsn_new_pigment();
+
+ dmnsn_color_map_payload *payload
+ = dmnsn_malloc(sizeof(dmnsn_color_map_payload));
+ payload->pattern = pattern;
+ payload->map = map;
+
+ pigment->pigment_fn = &dmnsn_color_map_pigment_fn;
+ pigment->init_fn = &dmnsn_color_map_init_fn;
+ pigment->free_fn = &dmnsn_free;
+ pigment->ptr = payload;
+ return pigment;
+}
diff --git a/libdimension/dimension.h b/libdimension/dimension.h
index 5cdc5b2..985ff14 100644
--- a/libdimension/dimension.h
+++ b/libdimension/dimension.h
@@ -72,6 +72,8 @@ typedef void dmnsn_free_fn(void *ptr);
#include <dimension/canvas.h>
#include <dimension/gl.h>
#include <dimension/png.h>
+#include <dimension/pattern.h>
+#include <dimension/patterns.h>
#include <dimension/texture.h>
#include <dimension/pigments.h>
#include <dimension/finishes.h>
diff --git a/libdimension/dimension/pattern.h b/libdimension/dimension/pattern.h
new file mode 100644
index 0000000..6301372
--- /dev/null
+++ b/libdimension/dimension/pattern.h
@@ -0,0 +1,55 @@
+/*************************************************************************
+ * 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/>. *
+ *************************************************************************/
+
+/*
+ * Patterns
+ */
+
+#ifndef DIMENSION_PATTERN_H
+#define DIMENSION_PATTERN_H
+
+/* Forward-declare dmnsn_pattern */
+typedef struct dmnsn_pattern dmnsn_pattern;
+
+/* Pattern callback */
+typedef double dmnsn_pattern_fn(const dmnsn_pattern *pattern, dmnsn_vector v);
+
+/* Generic pattern */
+struct dmnsn_pattern {
+ /* Callbacks */
+ dmnsn_pattern_fn *pattern_fn;
+ dmnsn_free_fn *free_fn;
+
+ /* Transformation matrix */
+ dmnsn_matrix trans, trans_inv;
+
+ /* Generic pointer */
+ void *ptr;
+};
+
+dmnsn_pattern *dmnsn_new_pattern(void);
+void dmnsn_delete_pattern(dmnsn_pattern *pattern);
+
+void dmnsn_pattern_init(dmnsn_pattern *pattern);
+
+/* Invoke the pattern callback with the right transformation */
+double dmnsn_pattern_value(const dmnsn_pattern *pattern, dmnsn_vector v);
+
+#endif /* DIMENSION_PATTERN_H */
diff --git a/libdimension/dimension/patterns.h b/libdimension/dimension/patterns.h
new file mode 100644
index 0000000..c6dc08a
--- /dev/null
+++ b/libdimension/dimension/patterns.h
@@ -0,0 +1,30 @@
+/*************************************************************************
+ * 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/>. *
+ *************************************************************************/
+
+/*
+ * Custom patterns
+ */
+
+#ifndef DIMENSION_PATTERNS_H
+#define DIMENSION_PATTERNS_H
+
+dmnsn_pattern *dmnsn_new_checker_pattern();
+
+#endif /* DIMENSION_PATTERNS_H */
diff --git a/libdimension/dimension/pigments.h b/libdimension/dimension/pigments.h
index f985362..31c0598 100644
--- a/libdimension/dimension/pigments.h
+++ b/libdimension/dimension/pigments.h
@@ -30,4 +30,17 @@ dmnsn_pigment *dmnsn_new_solid_pigment(dmnsn_color color);
/* An image map */
dmnsn_pigment *dmnsn_new_canvas_pigment(dmnsn_canvas *canvas);
+/* Color maps */
+typedef dmnsn_array dmnsn_color_map;
+
+dmnsn_color_map *dmnsn_new_color_map();
+void dmnsn_delete_color_map(dmnsn_color_map *map);
+
+void dmnsn_add_color_map_entry(dmnsn_color_map *map, double n, dmnsn_color c);
+dmnsn_color dmnsn_color_map_value(const dmnsn_color_map *map, double n);
+
+/* Color-mapped pigments */
+dmnsn_pigment *dmnsn_new_color_map_pigment(dmnsn_pattern *pattern,
+ dmnsn_color_map *map);
+
#endif /* DIMENSION_PIGMENTS_H */
diff --git a/libdimension/dimension/texture.h b/libdimension/dimension/texture.h
index 3d37aa1..98e33b8 100644
--- a/libdimension/dimension/texture.h
+++ b/libdimension/dimension/texture.h
@@ -32,14 +32,16 @@
/* Forward-declare dmnsn_pigment */
typedef struct dmnsn_pigment dmnsn_pigment;
-/* Pigment callback */
+/* Pigment callbacks */
typedef dmnsn_color dmnsn_pigment_fn(const dmnsn_pigment *pigment,
dmnsn_vector v);
+typedef void dmnsn_pigment_init_fn(dmnsn_pigment *pigment);
/* dmnsn_pigment definition */
struct dmnsn_pigment {
/* Callbacks */
dmnsn_pigment_fn *pigment_fn;
+ dmnsn_pigment_init_fn *init_fn;
dmnsn_free_fn *free_fn;
/* Transformation matrix */
diff --git a/libdimension/pattern.c b/libdimension/pattern.c
new file mode 100644
index 0000000..0be017f
--- /dev/null
+++ b/libdimension/pattern.c
@@ -0,0 +1,56 @@
+/*************************************************************************
+ * 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"
+
+/* Allocate a dummy pattern */
+dmnsn_pattern *
+dmnsn_new_pattern(void)
+{
+ dmnsn_pattern *pattern = dmnsn_malloc(sizeof(dmnsn_pattern));
+ pattern->trans = dmnsn_identity_matrix();
+ pattern->free_fn = NULL;
+ return pattern;
+}
+
+/* Delete a pattern */
+void
+dmnsn_delete_pattern(dmnsn_pattern *pattern)
+{
+ if (pattern->free_fn) {
+ (*pattern->free_fn)(pattern->ptr);
+ }
+ dmnsn_free(pattern);
+}
+
+/* Precompute the transformation matrix inverse */
+void
+dmnsn_pattern_init(dmnsn_pattern *pattern)
+{
+ pattern->trans_inv = dmnsn_matrix_inverse(pattern->trans);
+}
+
+/* Invoke the pattern callback with the right transformation */
+double
+dmnsn_pattern_value(const dmnsn_pattern *pattern, dmnsn_vector v)
+{
+ v = dmnsn_transform_vector(pattern->trans_inv, v);
+ return (*pattern->pattern_fn)(pattern, v);
+}
diff --git a/libdimension/texture.c b/libdimension/texture.c
index 1d0f3cd..7a012b5 100644
--- a/libdimension/texture.c
+++ b/libdimension/texture.c
@@ -26,6 +26,7 @@ dmnsn_pigment *
dmnsn_new_pigment()
{
dmnsn_pigment *pigment = dmnsn_malloc(sizeof(dmnsn_pigment));
+ pigment->init_fn = NULL;
pigment->free_fn = NULL;
pigment->trans = dmnsn_identity_matrix();
return pigment;
@@ -47,6 +48,10 @@ dmnsn_delete_pigment(dmnsn_pigment *pigment)
void
dmnsn_pigment_init(dmnsn_pigment *pigment)
{
+ if (pigment->init_fn) {
+ (*pigment->init_fn)(pigment);
+ }
+
pigment->trans_inv = dmnsn_matrix_inverse(pigment->trans);
}