]> Pileus Git - grits/commitdiff
Add GritsLine
authorAndy Spencer <andy753421@gmail.com>
Tue, 3 Jan 2012 07:32:34 +0000 (07:32 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 3 Jan 2012 07:32:34 +0000 (07:32 +0000)
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
src/objects/Makefile.am
src/objects/grits-line.c [new file with mode: 0644]
src/objects/grits-line.h [new file with mode: 0644]
src/plugins/test.c
src/plugins/test.h

index 523d87c9c3802efc58c0d03776e28ccfaa554f89..540120c24d8cd1377c122ce0f746d492fbdbbf71 100644 (file)
@@ -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>
index dc6286195d989132763a26b4ea56e16fbcf360ed..119b0a4d633cd95bbfce3752116be4b354dd367b 100644 (file)
@@ -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 (file)
index 0000000..a59795c
--- /dev/null
@@ -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 (file)
index 0000000..10a06c9
--- /dev/null
@@ -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
index d0315bd7f6e9552b65002213fce49d17f9486772..81082f4aab7d53b353a84659f48166a51ba6fc70 100644 (file)
@@ -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;
 }
 
index 54e214f6314f5ecf83151e08e03e8263cdc62494..7e980e6c88e3bdd2089ccc1012d50db84dc820fd 100644 (file)
@@ -37,6 +37,7 @@ struct _GritsPluginTest {
        GritsViewer *viewer;
        GritsMarker *marker;
        GritsPoly   *poly;
+       GritsLine   *line;
 };
 
 struct _GritsPluginTestClass {