]> Pileus Git - grits/blob - src/objects/grits-line.c
Add GritsLine
[grits] / src / objects / grits-line.c
1 /*
2  * Copyright (C) 2012 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:grits-line
20  * @short_description: Single point lines
21  *
22  * Each #GritsLine represents a 3 dimentional line.
23  */
24
25 #include <config.h>
26 #include "gtkgl.h"
27 #include "grits-line.h"
28
29 /* Drawing */
30 static void grits_line_trace(guint mode, gdouble (**points)[3])
31 {
32         //g_debug("GritsLine: outline");
33         for (int pi = 0; points[pi]; pi++) {
34                 glBegin(mode);
35                 for (int ci = 0; points[pi][ci][0] &&
36                                  points[pi][ci][1] &&
37                                  points[pi][ci][2]; ci++)
38                         glVertex3dv(points[pi][ci]);
39                 glEnd();
40         }
41 }
42
43 static void grits_line_draw(GritsObject *_poly, GritsOpenGL *opengl)
44 {
45         //g_debug("GritsLine: draw");
46         GritsLine *line = GRITS_LINE(_poly);
47
48         glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT |
49                         GL_POINT_BIT | GL_LINE_BIT | GL_POLYGON_BIT);
50
51         glDisable(GL_TEXTURE_2D);
52         glDisable(GL_ALPHA_TEST);
53         glDisable(GL_CULL_FACE);
54         glDisable(GL_LIGHTING);
55
56         glEnable(GL_LINE_SMOOTH);
57         glEnable(GL_POINT_SMOOTH);
58
59         glColor4dv(line->color);
60
61         glPointSize(line->width);
62         glLineWidth(line->width);
63
64         if (line->width > 1)
65                 grits_line_trace(GL_POINTS, line->points);
66         grits_line_trace(GL_LINE_STRIP, line->points);
67
68         glPopAttrib();
69 }
70
71 /**
72  * grits_line_new:
73  * @points:  TODO
74  * @npoints: TODO
75  *
76  * Create a new GritsLine which TODO.
77  *
78  * Returns: the new #GritsLine.
79  */
80 GritsLine *grits_line_new(gdouble (**points)[3])
81 {
82         //g_debug("GritsLine: new - %p", points);
83         GritsLine *line = g_object_new(GRITS_TYPE_LINE, NULL);
84         line->points    = points;
85         return line;
86 }
87
88 GritsLine *grits_line_parse(const gchar *str,
89                 const gchar *line_sep, const gchar *point_sep, const gchar *coord_sep)
90 {
91         GritsPoint center;
92         gdouble (**lines)[3] = parse_points(str,
93                         line_sep, point_sep, coord_sep, NULL, &center);
94
95         GritsLine *line = grits_line_new(lines);
96         GRITS_OBJECT(line)->center = center;
97         GRITS_OBJECT(line)->skip   = GRITS_SKIP_CENTER;
98         g_object_weak_ref(G_OBJECT(line), (GWeakNotify)free_points, lines);
99         return line;
100 }
101
102 /* GObject code */
103 G_DEFINE_TYPE(GritsLine, grits_line, GRITS_TYPE_OBJECT);
104 static void grits_line_init(GritsLine *line)
105 {
106         line->color[0] = 1;
107         line->color[1] = 1;
108         line->color[2] = 1;
109         line->color[3] = 0.2;
110         line->width    = 1;
111         GRITS_OBJECT(line)->skip = GRITS_SKIP_STATE;
112 }
113
114 static void grits_line_finalize(GObject *_line)
115 {
116         //g_debug("GritsLine: finalize");
117         GritsLine *line = GRITS_LINE(_line);
118         (void)line;
119 }
120
121 static void grits_line_class_init(GritsLineClass *klass)
122 {
123         g_debug("GritsLine: class_init");
124         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
125         gobject_class->finalize = grits_line_finalize;
126
127         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
128         object_class->draw = grits_line_draw;
129         //object_class->pick = grits_line_pick;
130 }