summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Romanick <idr@freedesktop.org>2010-02-15 15:12:18 -0800
committerIan Romanick <idr@freedesktop.org>2010-02-15 15:12:18 -0800
commit241f99cd6bcc8e0d0899c7fc2088ced7734966ab (patch)
treea757513820d5cf9a3fb3770e4de83456bff42d1b
parenta37f465e38fc8c1f0855e09ab551efd319e8365b (diff)
Add generic routines to generate triangle strip elements for rectangular meshes
-rw-r--r--src/Makefile.am2
-rw-r--r--src/mesh.c49
-rw-r--r--src/mesh.h78
3 files changed, 128 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index b84e560..736ee5c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,7 +24,7 @@ AM_CXXFLAGS=-I../include
AM_CFLAGS=-I../include
lib_LIBRARIES = libGLU3.a
-libGLU3_a_SOURCES = matrix.c load_text.c arcball.c revolve.c
+libGLU3_a_SOURCES = matrix.c load_text.c arcball.c revolve.c mesh.c
libGLU3includedir = ${includedir}
libGLU3include_HEADERS = ../include/glu3.h ../include/glu3_scalar.h
diff --git a/src/mesh.c b/src/mesh.c
new file mode 100644
index 0000000..603570b
--- /dev/null
+++ b/src/mesh.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2010 Ian D. Romanick
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "mesh.h"
+
+void
+generate_triangle_mesh(unsigned rows, unsigned cols, unsigned width,
+ mesh_begin_cb *begin_cb,
+ mesh_index_cb *index_cb,
+ mesh_end_cb *end_cb,
+ void *data)
+{
+ unsigned i;
+ unsigned j;
+
+ for (i = 0; i < rows; i++) {
+ (*begin_cb)(data, GL_TRIANGLE_STRIP);
+
+ for (j = 0; j < cols; j++) {
+ const unsigned e0 = ((i + 0) * width) + j;
+ const unsigned e1 = ((i + 1) * width) + j;
+
+ (*index_cb)(data, e0);
+ (*index_cb)(data, e1);
+ }
+
+ (*end_cb)(data);
+ }
+}
diff --git a/src/mesh.h b/src/mesh.h
new file mode 100644
index 0000000..41cbbba
--- /dev/null
+++ b/src/mesh.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright © 2010 Ian D. Romanick
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "glu3.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+typedef void (mesh_begin_cb)(void *data, GLenum mode);
+typedef void (mesh_index_cb)(void *data, unsigned index);
+typedef void (mesh_end_cb)(void *data);
+
+/**
+ * Generate indexed drawing primitives for a rectangular mesh.
+ *
+ * Generates a sequence of \c GL_TRANGLE_STRIP primitives to draw a rectangular
+ * mesh with the specified parameters. Each triangle strip generated will be
+ * of the form:
+ *
+ * (i*width)+0, ((i+1) *width)+0, (i*width)+1, ((i+1) *width)+1, ...
+ * (i*width)+(cols-1), ((i+1) *width)+(cols-1)
+ *
+ * where \c i is the index of the row being generated, starting from 0.
+ *
+ * The triangle strips are generated in row-major order.
+ *
+ * \param rows Number of rows of primitives in the mesh. This value must
+ * be at least 1. Note that if there are \c rows of
+ * primitives, there must be at least \c rows + 1 of vertices.
+ * \param cols Number of columns of primitives in the mesh. This value
+ * must be at least 1. Note that if there are \c cols of
+ * primitives, there must be at least \c cols + 1 of vertices.
+ * \param width Width, in vertices, of the underlying mesh data. If
+ * \c width is greater than \c cols, data for a submesh within
+ * a larger primitive can be generated. If \c width is less
+ * than \c cols, the results are undefined (and probably
+ * wrong).
+ * \param begin_cb Call-back used to begin a new primitive.
+ * \param index_cb Call-back used for each index within a primitive. This
+ * will be called 2 * \c cols times between each call to
+ * \c begin_cb and \c end_cb.
+ * \param end_cb Call-back used to end a primitive.
+ * \param data User-supplied data pointer passed to the various call-back
+ * functions.
+ *
+ * \sa glDrawMeshArraysSUN
+ */
+extern void
+generate_triangle_mesh(unsigned rows, unsigned cols, unsigned width,
+ mesh_begin_cb *begin_cb,
+ mesh_index_cb *index_cb,
+ mesh_end_cb *end_cb,
+ void *data);
+
+#ifdef __cplusplus
+};
+#endif