2 * Copyright (C) 2010-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
20 * @short_description: Single point polys
22 * Each #GritsPoly represents a 3 dimentional polygon.
28 #include "grits-poly.h"
31 static void grits_poly_tess(gdouble (**points)[3])
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 for (int pi = 0; points[pi]; pi++) {
39 gluTessBeginPolygon(tess, NULL);
40 gluTessBeginContour(tess);
41 for (int ci = 0; points[pi][ci][0]; ci++) {
46 gluTessEndContour(tess);
47 gluTessEndPolygon(tess);
52 static void grits_poly_outline(gdouble (**points)[3])
54 //g_debug("GritsPoly: outline");
55 for (int pi = 0; points[pi]; pi++) {
57 for (int ci = 0; points[pi][ci][0] &&
59 points[pi][ci][2]; ci++)
60 glVertex3dv(points[pi][ci]);
65 static gboolean grits_poly_runlist(GritsPoly *poly, int i,
66 void (*render)(gdouble(**)[3]))
68 //g_debug("GritsPoly: genlist");
70 glCallList(poly->list[i]);
72 guint list = glGenLists(1);
73 glNewList(list, GL_COMPILE_AND_EXECUTE);
81 static void grits_poly_draw(GritsObject *_poly, GritsOpenGL *opengl)
83 //g_debug("GritsPoly: draw");
84 GritsPoly *poly = GRITS_POLY(_poly);
86 glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT);
87 glDisable(GL_TEXTURE_2D);
88 glDisable(GL_ALPHA_TEST);
89 glDisable(GL_CULL_FACE);
90 glDisable(GL_LIGHTING);
92 glEnable(GL_POLYGON_OFFSET_FILL);
93 glEnable(GL_POLYGON_OFFSET_LINE);
94 glEnable(GL_POLYGON_OFFSET_POINT);
97 /* Draw background farthest back */
98 glPolygonOffset(3, 3);
99 glColor4dv(poly->color);
100 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
101 grits_poly_runlist(poly, 0, grits_poly_tess);
104 glEnable(GL_POLYGON_SMOOTH);
105 glEnable(GL_LINE_SMOOTH);
106 glEnable(GL_POINT_SMOOTH);
108 if (!poly->color[3] && poly->border[3] && poly->width > 1) {
109 /* Draw line border in the middle */
112 glPointSize(poly->width*2);
113 glLineWidth(poly->width*2);
115 glPolygonOffset(2, 2);
117 glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
118 grits_poly_runlist(poly, 1, grits_poly_outline);
119 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
120 grits_poly_runlist(poly, 1, grits_poly_outline);
123 if (poly->border[3]) {
124 /* Draw border front-most */
125 glColor4dv(poly->border);
127 glPointSize(poly->width);
128 glLineWidth(poly->width);
130 glPolygonOffset(1, 1);
131 if (poly->width > 1) {
132 glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
133 grits_poly_runlist(poly, 1, grits_poly_outline);
135 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
136 grits_poly_runlist(poly, 1, grits_poly_outline);
139 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
143 static void grits_poly_pick(GritsObject *_poly, GritsOpenGL *opengl)
145 //g_debug("GritsPoly: pick");
146 GritsPoly *poly = GRITS_POLY(_poly);
147 glPushAttrib(GL_ENABLE_BIT);
148 glDisable(GL_CULL_FACE);
149 grits_poly_runlist(poly, 0, grits_poly_tess);
153 static gboolean grits_poly_delete(gpointer list)
155 glDeleteLists((guint)list, 1);
164 * Create a new GritsPoly which TODO.
166 * Returns: the new #GritsPoly.
168 GritsPoly *grits_poly_new(gdouble (**points)[3])
170 //g_debug("GritsPoly: new - %p", points);
171 GritsPoly *poly = g_object_new(GRITS_TYPE_POLY, NULL);
172 poly->points = points;
176 static void _free_points(gdouble (**points)[3])
178 for (int i = 0; points[i]; i++)
183 GritsPoly *grits_poly_parse(const gchar *str,
184 const gchar *poly_sep, const gchar *point_sep, const gchar *coord_sep)
186 /* Split and count polygons */
187 gchar **spolys = g_strsplit(str, poly_sep, -1);
188 int npolys = g_strv_length(spolys);
190 GritsBounds bounds = {-90, 90, -180, 180};
191 gdouble (**polys)[3] = (gpointer)g_new0(double*, npolys+1);
192 for (int pi = 0; pi < npolys; pi++) {
193 /* Split and count coordinates */
194 gchar **scoords = g_strsplit(spolys[pi], point_sep, -1);
195 int ncoords = g_strv_length(scoords);
197 /* Create binary coords */
198 gdouble (*coords)[3] = (gpointer)g_new0(gdouble, 3*(ncoords+1));
199 for (int ci = 0; ci < ncoords; ci++) {
201 sscanf(scoords[ci], "%lf,%lf", &lat, &lon);
202 if (lat > bounds.n) bounds.n = lat;
203 if (lat < bounds.s) bounds.s = lat;
204 if (lon > bounds.e) bounds.e = lon;
205 if (lon < bounds.w) bounds.w = lon;
212 /* Insert coords into poly array */
218 /* Create GritsPoly */
219 GritsPoly *poly = grits_poly_new(polys);
220 GRITS_OBJECT(poly)->center.lat = (bounds.n + bounds.s)/2;
221 GRITS_OBJECT(poly)->center.lon = lon_avg(bounds.e, bounds.w);
222 GRITS_OBJECT(poly)->center.elev = 0;
223 GRITS_OBJECT(poly)->skip = GRITS_SKIP_CENTER;
224 g_object_weak_ref(G_OBJECT(poly), (GWeakNotify)_free_points, polys);
229 G_DEFINE_TYPE(GritsPoly, grits_poly, GRITS_TYPE_OBJECT);
230 static void grits_poly_init(GritsPoly *poly)
235 poly->border[3] = 0.2;
237 GRITS_OBJECT(poly)->skip = GRITS_SKIP_STATE;
240 static void grits_poly_finalize(GObject *_poly)
242 //g_debug("GritsPoly: finalize");
243 GritsPoly *poly = GRITS_POLY(_poly);
244 if (poly->list[0]) g_idle_add(grits_poly_delete, (gpointer)poly->list[0]);
245 if (poly->list[1]) g_idle_add(grits_poly_delete, (gpointer)poly->list[1]);
248 static void grits_poly_class_init(GritsPolyClass *klass)
250 g_debug("GritsPoly: class_init");
251 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
252 gobject_class->finalize = grits_poly_finalize;
254 GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
255 object_class->draw = grits_poly_draw;
256 object_class->pick = grits_poly_pick;