[grits] Add GritsLine



commit ae0f47ffeff87c31aa126934bcb08b68309c2a5f
Author: Andy Spencer <andy753421 gmail com>
Date:   Tue Jan 3 07:32:34 2012 +0000

    Add GritsLine
    
    GritsLine is similar to GritsPoly, but does not draw a filled polygon.
    As such it does not need to do any tessellation.
    
    Currently it draws the points directly instead of using a display list.
    This should make it easier to updated the line at runtime without
    needing to create a new object.

 src/grits.h              |    1 +
 src/objects/Makefile.am  |    2 +
 src/objects/grits-line.c |  130 ++++++++++++++++++++++++++++++++++++++++++++++
 src/objects/grits-line.h |   55 +++++++++++++++++++
 src/plugins/test.c       |   16 ++++++
 src/plugins/test.h       |    1 +
 6 files changed, 205 insertions(+), 0 deletions(-)
---
diff --git a/src/grits.h b/src/grits.h
index 523d87c..540120c 100644
--- a/src/grits.h
+++ b/src/grits.h
@@ -36,6 +36,7 @@
 #include <objects/grits-callback.h>
 #include <objects/grits-volume.h>
 #include <objects/grits-poly.h>
+#include <objects/grits-line.h>
 
 /* Plugins */
 #include <grits-plugin.h>
diff --git a/src/objects/Makefile.am b/src/objects/Makefile.am
index dc62861..119b0a4 100644
--- a/src/objects/Makefile.am
+++ b/src/objects/Makefile.am
@@ -13,6 +13,7 @@ grits_objects_include_HEADERS = \
 	grits-tile.h     \
 	grits-volume.h   \
 	grits-poly.h     \
+	grits-line.h     \
 	marching.h
 
 noinst_LTLIBRARIES = libgrits-objects.la
@@ -23,6 +24,7 @@ libgrits_objects_la_SOURCES = \
 	grits-tile.c     grits-tile.h     \
 	grits-volume.c   grits-volume.h   \
 	grits-poly.c     grits-poly.h     \
+	grits-line.c     grits-line.h     \
 	marching.c       marching.h
 libgrits_objects_la_LDFLAGS = -static
 
diff --git a/src/objects/grits-line.c b/src/objects/grits-line.c
new file mode 100644
index 0000000..a59795c
--- /dev/null
+++ b/src/objects/grits-line.c
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421 gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * SECTION:grits-line
+ * @short_description: Single point lines
+ *
+ * Each #GritsLine represents a 3 dimentional line.
+ */
+
+#include <config.h>
+#include "gtkgl.h"
+#include "grits-line.h"
+
+/* Drawing */
+static void grits_line_trace(guint mode, gdouble (**points)[3])
+{
+	//g_debug("GritsLine: outline");
+	for (int pi = 0; points[pi]; pi++) {
+		glBegin(mode);
+	 	for (int ci = 0; points[pi][ci][0] &&
+	 	                 points[pi][ci][1] &&
+	 	                 points[pi][ci][2]; ci++)
+			glVertex3dv(points[pi][ci]);
+		glEnd();
+	}
+}
+
+static void grits_line_draw(GritsObject *_poly, GritsOpenGL *opengl)
+{
+	//g_debug("GritsLine: draw");
+	GritsLine *line = GRITS_LINE(_poly);
+
+	glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT |
+			GL_POINT_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
+
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_CULL_FACE);
+	glDisable(GL_LIGHTING);
+
+	glEnable(GL_LINE_SMOOTH);
+	glEnable(GL_POINT_SMOOTH);
+
+	glColor4dv(line->color);
+
+	glPointSize(line->width);
+	glLineWidth(line->width);
+
+	if (line->width > 1)
+		grits_line_trace(GL_POINTS, line->points);
+	grits_line_trace(GL_LINE_STRIP, line->points);
+
+	glPopAttrib();
+}
+
+/**
+ * grits_line_new:
+ * @points:  TODO
+ * @npoints: TODO
+ *
+ * Create a new GritsLine which TODO.
+ *
+ * Returns: the new #GritsLine.
+ */
+GritsLine *grits_line_new(gdouble (**points)[3])
+{
+	//g_debug("GritsLine: new - %p", points);
+	GritsLine *line = g_object_new(GRITS_TYPE_LINE, NULL);
+	line->points    = points;
+	return line;
+}
+
+GritsLine *grits_line_parse(const gchar *str,
+		const gchar *line_sep, const gchar *point_sep, const gchar *coord_sep)
+{
+	GritsPoint center;
+	gdouble (**lines)[3] = parse_points(str,
+			line_sep, point_sep, coord_sep, NULL, &center);
+
+	GritsLine *line = grits_line_new(lines);
+	GRITS_OBJECT(line)->center = center;
+	GRITS_OBJECT(line)->skip   = GRITS_SKIP_CENTER;
+	g_object_weak_ref(G_OBJECT(line), (GWeakNotify)free_points, lines);
+	return line;
+}
+
+/* GObject code */
+G_DEFINE_TYPE(GritsLine, grits_line, GRITS_TYPE_OBJECT);
+static void grits_line_init(GritsLine *line)
+{
+	line->color[0] = 1;
+	line->color[1] = 1;
+	line->color[2] = 1;
+	line->color[3] = 0.2;
+	line->width    = 1;
+	GRITS_OBJECT(line)->skip = GRITS_SKIP_STATE;
+}
+
+static void grits_line_finalize(GObject *_line)
+{
+	//g_debug("GritsLine: finalize");
+	GritsLine *line = GRITS_LINE(_line);
+	(void)line;
+}
+
+static void grits_line_class_init(GritsLineClass *klass)
+{
+	g_debug("GritsLine: class_init");
+	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+	gobject_class->finalize = grits_line_finalize;
+
+	GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
+	object_class->draw = grits_line_draw;
+	//object_class->pick = grits_line_pick;
+}
diff --git a/src/objects/grits-line.h b/src/objects/grits-line.h
new file mode 100644
index 0000000..10a06c9
--- /dev/null
+++ b/src/objects/grits-line.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2012 Andy Spencer <andy753421 gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GRITS_LINE_H__
+#define __GRITS_LINE_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <cairo.h>
+#include <objects/grits-object.h>
+
+/* GritsLine */
+#define GRITS_TYPE_LINE            (grits_line_get_type())
+#define GRITS_LINE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),   GRITS_TYPE_LINE, GritsLine))
+#define GRITS_IS_LINE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),   GRITS_TYPE_LINE))
+#define GRITS_LINE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST   ((klass), GRITS_TYPE_LINE, GritsLineClass))
+#define GRITS_IS_LINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE   ((klass), GRITS_TYPE_LINE))
+#define GRITS_LINE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),   GRITS_TYPE_LINE, GritsLineClass))
+
+typedef struct _GritsLine      GritsLine;
+typedef struct _GritsLineClass GritsLineClass;
+
+struct _GritsLine {
+	GritsObject  parent_instance;
+	gdouble   (**points)[3];
+	gdouble      color[4];
+	gdouble      width;
+};
+
+struct _GritsLineClass {
+	GritsObjectClass parent_class;
+};
+
+GType grits_line_get_type(void);
+
+GritsLine *grits_line_new(gdouble (**points)[3]);
+
+GritsLine *grits_line_parse(const gchar *str,
+		const gchar *line_sep, const gchar *point_sep, const gchar *coord_sep);
+
+#endif
diff --git a/src/plugins/test.c b/src/plugins/test.c
index d0315bd..81082f4 100644
--- a/src/plugins/test.c
+++ b/src/plugins/test.c
@@ -130,6 +130,21 @@ void _load_poly(GritsPluginTest *test)
 	g_signal_connect(test->poly, "key-press",    G_CALLBACK(on_poly_key),    NULL);
 }
 
+void _load_line(GritsPluginTest *test)
+{
+	test->line = grits_line_parse("30,-80 30,-120 50,-120 50,-80", "\t", " ", ",");
+	test->line->color[0]  = 1;
+	test->line->color[1]  = 0;
+	test->line->color[2]  = 0;
+	test->line->color[3]  = 1;
+	test->line->width     = 8;
+	grits_viewer_add(test->viewer, GRITS_OBJECT(test->line),  GRITS_LEVEL_OVERLAY, TRUE);
+	g_signal_connect(test->line, "enter",        G_CALLBACK(on_poly_enter),  NULL);
+	g_signal_connect(test->line, "leave",        G_CALLBACK(on_poly_leave),  NULL);
+	g_signal_connect(test->line, "button-press", G_CALLBACK(on_poly_button), NULL);
+	g_signal_connect(test->line, "key-press",    G_CALLBACK(on_poly_key),    NULL);
+}
+
 /**
  * grits_plugin_test_new:
  * @viewer: the #GritsViewer to use for drawing
@@ -145,6 +160,7 @@ GritsPluginTest *grits_plugin_test_new(GritsViewer *viewer)
 	test->viewer = g_object_ref(viewer);
 	_load_marker(test);
 	_load_poly(test);
+	_load_line(test);
 	return test;
 }
 
diff --git a/src/plugins/test.h b/src/plugins/test.h
index 54e214f..7e980e6 100644
--- a/src/plugins/test.h
+++ b/src/plugins/test.h
@@ -37,6 +37,7 @@ struct _GritsPluginTest {
 	GritsViewer *viewer;
 	GritsMarker *marker;
 	GritsPoly   *poly;
+	GritsLine   *line;
 };
 
 struct _GritsPluginTestClass {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]