]> Pileus Git - grits/blob - src/objects/grits-poly.c
Add string parser to GritsPoly
[grits] / src / objects / grits-poly.c
1 /*
2  * Copyright (C) 2010 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-poly
20  * @short_description: Single point polys
21  *
22  * Each #GritsPoly represents a 3 dimentional polygon.
23  */
24
25 #include <config.h>
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include "grits-poly.h"
29
30 /* Drawing */
31 static void grits_poly_tess(gdouble (**points)[3])
32 {
33         //g_debug("GritsPoly: tess");
34         GLUtesselator *tess = gluNewTess();
35         gluTessCallback(tess, GLU_TESS_BEGIN,  (_GLUfuncptr)glBegin);
36         gluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)glVertex3dv);
37         gluTessCallback(tess, GLU_TESS_END,    (_GLUfuncptr)glEnd);
38         gluTessBeginPolygon(tess, NULL);
39         for (int pi = 0; points[pi]; pi++) {
40                 gluTessBeginContour(tess);
41                 for (int ci = 0; points[pi][ci][0]; ci++) {
42                         gluTessVertex(tess,
43                                 points[pi][ci],
44                                 points[pi][ci]);
45                 }
46                 gluTessEndContour(tess);
47         }
48         gluTessEndPolygon(tess);
49         gluDeleteTess(tess);
50 }
51
52 static void grits_poly_outline(gdouble (**points)[3])
53 {
54         //g_debug("GritsPoly: outline");
55         for (int pi = 0; points[pi]; pi++) {
56                 glBegin(GL_LINE_LOOP);
57                 for (int ci = 0; points[pi][ci][0]; ci++)
58                         glVertex3dv(points[pi][ci]);
59                 glEnd();
60         }
61 }
62 static gboolean grits_poly_genlist(gpointer _poly)
63 {
64         //g_debug("GritsPoly: genlist");
65         GritsPoly *poly = GRITS_POLY(_poly);
66         guint list = glGenLists(2);
67         glNewList(list+0, GL_COMPILE);
68         grits_poly_tess(poly->points);
69         glEndList();
70         glNewList(list+1, GL_COMPILE);
71         grits_poly_outline(poly->points);
72         glEndList();
73         poly->list = list;
74         return FALSE;
75 }
76
77 static void grits_poly_draw(GritsObject *_poly, GritsOpenGL *opengl)
78 {
79         //g_debug("GritsPoly: draw");
80         GritsPoly *poly = GRITS_POLY(_poly);
81
82         if (!poly->list)
83                 return;
84
85         glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT);
86         glDisable(GL_TEXTURE_2D);
87         glDisable(GL_ALPHA_TEST);
88         glDisable(GL_CULL_FACE);
89         glDisable(GL_LIGHTING);
90         if (poly->color[3]) {
91                 glColor4dv(poly->color);
92                 glCallList(poly->list+0);
93         }
94         if (poly->border[3]) {
95                 glLineWidth(poly->width);
96                 glColor4dv(poly->border);
97                 glCallList(poly->list+1);
98         }
99         glPopAttrib();
100 }
101
102 /**
103  * grits_poly_new:
104  * @points:  TODO
105  * @npoints: TODO
106  *
107  * Create a new GritsPoly which TODO.
108  *
109  * Returns: the new #GritsPoly.
110  */
111 GritsPoly *grits_poly_new(gdouble (**points)[3])
112 {
113         //g_debug("GritsPoly: new - %p", points);
114         GritsPoly *poly = g_object_new(GRITS_TYPE_POLY, NULL);
115         poly->points    = points;
116         g_idle_add(grits_poly_genlist, poly);
117         return poly;
118 }
119
120 GritsPoly *grits_poly_parse(gchar *str,
121                 gchar *poly_sep, gchar *point_sep, gchar *coord_sep)
122 {
123         /* Split and count polygons */
124         gchar **spolys = g_strsplit(str, poly_sep, -1);
125         int     npolys = g_strv_length(spolys);
126
127         GritsPoint center = {0,0,0};
128         gdouble (**polys)[3] = (gpointer)g_new0(double*, npolys+1);
129         for (int pi = 0; pi < npolys; pi++) {
130                 /* Split and count coordinates */
131                 gchar **scoords = g_strsplit(spolys[pi], point_sep, -1);
132                 int     ncoords = g_strv_length(scoords);
133
134                 /* Create binary coords */
135                 gdouble (*coords)[3] = (gpointer)g_new0(gdouble, 3*(ncoords+1));
136                 for (int ci = 0; ci < ncoords; ci++) {
137                         gdouble lat, lon;
138                         sscanf(scoords[ci], "%lf,%lf", &lat, &lon);
139                         if (ci == 0) {
140                                 center.lat  = lat;
141                                 center.lon  = lon;
142                                 center.elev = 0;
143                         }
144                         lle2xyz(lat, lon, 0,
145                                         &coords[ci][0],
146                                         &coords[ci][1],
147                                         &coords[ci][2]);
148                 }
149
150                 /* Insert coords into poly array */
151                 polys[pi] = coords;
152                 g_strfreev(scoords);
153         }
154
155         /* Create GritsPoly */
156         GritsPoly *poly = grits_poly_new(polys);
157         GRITS_OBJECT(poly)->center = center;
158         return poly;
159 }
160
161 /* GObject code */
162 G_DEFINE_TYPE(GritsPoly, grits_poly, GRITS_TYPE_OBJECT);
163 static void grits_poly_init(GritsPoly *poly)
164 {
165         poly->border[0] = 1;
166         poly->border[1] = 1;
167         poly->border[2] = 1;
168         poly->border[3] = 0.2;
169         poly->width     = 1;
170 }
171
172 static void grits_poly_finalize(GObject *_poly)
173 {
174         g_debug("GritsPoly: finalize");
175         GritsPoly *poly = GRITS_POLY(_poly);
176         (void)poly;
177         // TODO: free points
178 }
179
180 static void grits_poly_class_init(GritsPolyClass *klass)
181 {
182         g_debug("GritsPoly: class_init");
183         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
184         gobject_class->finalize = grits_poly_finalize;
185
186         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
187         object_class->draw = grits_poly_draw;
188 }