]> Pileus Git - grits/blob - src/objects/grits-marker.c
507afd3ad60a762fe1ee7d299b1a441be3af6e1e
[grits] / src / objects / grits-marker.c
1 /*
2  * Copyright (C) 2009-2011 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-marker
20  * @short_description: Single point markers
21  *
22  * Each #GritsMarker represents some point on the earth with some form of
23  * content. Commonly this is used to mark geographic features such as cities or
24  * states.
25  * 
26  * While markers represent a place in three dimensions somewhere on, below, or
27  * above the surface of the earth, they are drawn in 2 dimensions so that they
28  * look normal and readable by the user. Due to this, GritsObjects should almost
29  * always be added to the GRITS_LEVEL_OVERLAY level so they are drawn "above" the
30  * actual earth.
31  */
32
33 #include <config.h>
34 #include <math.h>
35 #include "gtkgl.h"
36 #include "grits-marker.h"
37
38 static void render_all(GritsMarker *marker);
39 static void render_point(GritsMarker *marker);
40 static void render_label(GritsMarker *marker);
41 static void render_icon(GritsMarker *marker);
42
43 /**
44  * grits_marker_new:
45  * @label: a short description of the marker
46  *
47  * Create a new GritsMarker which shows the given label when drawn.
48  *
49  * Returns: the new #GritsMarker.
50  */
51 GritsMarker *grits_marker_new(const gchar *label)
52 {
53         GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
54
55         marker->display_mask = MARKER_DMASK_POINT | MARKER_DMASK_LABEL;
56
57         //g_debug("GritsMarker: new - %s", label);
58         /* specify size of point and size of surface */
59         marker->outline =   2;
60         marker->radius  =   3;
61         marker->width   = 128;
62         marker->height  =  32;
63         marker->icon_width  = marker->radius * 2;
64         marker->icon_height = marker->radius * 3;
65
66         marker->xoff  = marker->radius+marker->outline;
67         marker->yoff  = marker->height-(marker->radius+marker->outline);
68         marker->cairo = cairo_create(cairo_image_surface_create(
69                         CAIRO_FORMAT_ARGB32, marker->width, marker->height));
70
71         marker->label = g_strdup(label);
72
73         render_all(marker);
74
75         return marker;
76 }
77
78 /*
79  * Create a new marker with a label, point, icon (png), or any
80  * combination of the above.
81  * label: The label to display if MARKER_DMASK_LABEL is set
82  * filename: The filename of the icon
83  * angle: The angle to rotate the icon (0 is north)
84  * flip: Whether to flip the image so that it's never upside down.
85  *       Useful for non-symmetric icons which have an "up".
86  * display_mask: A bitmask which specifies which items to display.
87  */
88 GritsMarker *grits_marker_icon_new(const gchar *label, const gchar *filename,
89     guint angle, gboolean flip, guint display_mask)
90 {
91         GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
92
93         marker->label = g_strdup(label);
94         marker->angle = angle;
95         marker->flip = flip;
96         marker->display_mask = display_mask;
97
98         if (display_mask & MARKER_DMASK_ICON) {
99                 g_assert(filename != NULL);
100                 g_debug("GritsMarker: marker_icon_new - marker image %s",
101                         filename);
102                 marker->icon_img = cairo_image_surface_create_from_png(filename);
103                 if (cairo_surface_status(marker->icon_img)) {
104                         g_warning("GritsMarker: marker_icon_new - "
105                                   "error reading file %s", filename);
106                         /* convert it to a point, better than nothing */
107                         marker->display_mask &= ~MARKER_DMASK_ICON;
108                         marker->display_mask |= MARKER_DMASK_POINT;
109                         marker->icon_width = 4;
110                         marker->icon_height = 8;
111
112                 } else {
113                         marker->icon_width =
114                             cairo_image_surface_get_width(marker->icon_img);
115                         marker->icon_height =
116                             cairo_image_surface_get_height(marker->icon_img);
117                 }
118         } else {
119                 marker->icon_img = NULL;
120                 marker->icon_width = 4; /* room for the point if there is one */
121                 marker->icon_height = 8;
122         }
123         g_debug("GritsMarker: marker_icon_new - width = %d, height = %d",
124                 marker->icon_width, marker->icon_height);
125
126         marker->outline =   2;
127         marker->radius  =   3;
128         /* this is the surface size, a guess really */
129         marker->width   = marker->icon_width  + 128;
130         marker->height  = marker->icon_height + 64;
131
132         marker->xoff  = marker->width/2;
133         marker->yoff  = marker->height/2;
134         marker->cairo = cairo_create(cairo_image_surface_create(
135                         CAIRO_FORMAT_ARGB32, marker->width, marker->height));
136         /* clear the surface just in case */
137         cairo_set_operator(marker->cairo, CAIRO_OPERATOR_SOURCE);
138         //cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.3); // debug
139         cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.0);
140         cairo_paint(marker->cairo);
141         cairo_set_operator(marker->cairo, CAIRO_OPERATOR_OVER);
142
143         render_all(marker);
144
145         if (marker->icon_img) {
146                 cairo_surface_destroy(marker->icon_img);
147         }
148
149         return marker;
150 }
151
152 static void render_all(GritsMarker *marker)
153 {
154         g_assert(marker);
155         if (marker->display_mask & MARKER_DMASK_ICON) {
156                 render_icon(marker);
157         }
158         if (marker->display_mask & MARKER_DMASK_POINT) {
159                 render_point(marker);
160         }
161         if (marker->display_mask & MARKER_DMASK_LABEL) {
162                 render_label(marker);
163         }
164
165         /* Load GL texture */
166         glEnable(GL_TEXTURE_2D);
167         glGenTextures(1, &marker->tex);
168         glBindTexture(GL_TEXTURE_2D, marker->tex);
169         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
170         glPixelStorei(GL_PACK_ALIGNMENT, 1);
171         glTexImage2D(GL_TEXTURE_2D, 0, 4, marker->width, marker->height,
172                 0, GL_BGRA, GL_UNSIGNED_BYTE,
173                 cairo_image_surface_get_data(cairo_get_target(marker->cairo)));
174         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
175         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
176 }
177
178
179 static void render_point(GritsMarker *marker)
180 {
181         /* Draw outline */
182         cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
183         cairo_set_line_width(marker->cairo, marker->outline*2);
184
185         cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
186                   0, 2*G_PI);
187         cairo_stroke(marker->cairo);
188
189         /* Draw filler */
190         cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
191
192         cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
193                   0, 2*G_PI);
194         cairo_fill(marker->cairo);
195 }
196
197 static void render_label(GritsMarker *marker)
198 {
199         g_assert(marker->label);
200
201         cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
202         cairo_select_font_face(marker->cairo, "sans-serif",
203                         CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
204         cairo_set_font_size(marker->cairo, 13);
205         cairo_move_to(marker->cairo, marker->xoff + (marker->icon_width  / 2),
206                                      marker->yoff - (marker->icon_height / 2));
207         cairo_text_path(marker->cairo, marker->label);
208         cairo_stroke(marker->cairo);
209
210         /* Draw filler */
211         cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
212         cairo_move_to(marker->cairo, marker->xoff + (marker->icon_width  / 2),
213                                      marker->yoff - (marker->icon_height / 2));
214         cairo_show_text(marker->cairo, marker->label);
215 }
216
217 static void render_icon(GritsMarker *marker)
218 {
219         g_assert(marker->icon_img != NULL);
220
221         /* This code assumes the icon is an image pointing toward 0 degrees
222         * (ie. north/up).  If marker->flip is set, then it will rotate the
223         * icon appropriately then reflect it across the vertical axis so
224         * it's never upside down.
225         */
226         gdouble flip = 1.0;
227         gdouble angle = marker->angle % 360;
228         if (marker->flip && (angle < 360 && angle > 180)) {
229                 /* if icon rotates to the left half it will be upside down */
230                 flip = -1.0; /* flip horizontally */
231         }
232
233         cairo_save(marker->cairo);
234
235         /* move to marker location */
236         cairo_translate(marker->cairo, marker->xoff, marker->yoff);
237
238         /* perform rotation and flip in one transformation */
239         gdouble C = cos(angle*(M_PI/180.0));
240         gdouble S = sin(angle*(M_PI/180.0));
241         gdouble fx = flip; 
242         gdouble fy = 1.0;
243         gdouble tx = 0.0;
244         gdouble ty = 0.0;
245         cairo_matrix_t matrix;
246         cairo_matrix_init(&matrix,
247                 fx*C, fx*S,
248                 -S*fy, C*fy,
249                 C*tx*(1-fx)-S*ty*(fy-1)+tx-C*tx+S*ty,
250                 S*tx*(1-fx)+C*ty*(fy-1)+ty-S*tx-C*ty);
251         cairo_transform(marker->cairo, &matrix);
252
253         /* center image */
254         cairo_translate(marker->cairo, -marker->icon_width/2,
255                                        -marker->icon_height/2);
256
257         cairo_set_source_surface(marker->cairo, marker->icon_img, 0, 0);
258
259         cairo_paint(marker->cairo);
260         cairo_restore(marker->cairo);
261 }
262
263 /* Drawing */
264 static void grits_marker_draw(GritsObject *_marker, GritsOpenGL *opengl)
265 {
266         GritsMarker *marker = GRITS_MARKER(_marker);
267         GritsPoint  *point  = grits_object_center(marker);
268         gdouble px, py, pz;
269         grits_viewer_project(GRITS_VIEWER(opengl),
270                         point->lat, point->lon, point->elev,
271                         &px, &py, &pz);
272
273         gint win_width  = GTK_WIDGET(opengl)->allocation.width;
274         gint win_height = GTK_WIDGET(opengl)->allocation.height;
275         py = win_height - py;
276         if (pz > 1)
277                 return;
278
279         //g_debug("GritsOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
280
281         cairo_surface_t *surface = cairo_get_target(marker->cairo);
282         gdouble width  = cairo_image_surface_get_width(surface);
283         gdouble height = cairo_image_surface_get_height(surface);
284
285         glMatrixMode(GL_PROJECTION); glLoadIdentity();
286         glMatrixMode(GL_MODELVIEW);  glLoadIdentity();
287         glOrtho(0, win_width, win_height, 0, -1, 1);
288         glTranslated(px - marker->xoff,
289                      py - marker->yoff, 0);
290
291         glDisable(GL_LIGHTING);
292         glDisable(GL_COLOR_MATERIAL);
293         glDisable(GL_DEPTH_TEST);
294         glEnable(GL_TEXTURE_2D);
295         glBindTexture(GL_TEXTURE_2D, marker->tex);
296         glDisable(GL_CULL_FACE);
297         glBegin(GL_QUADS);
298         glTexCoord2f(1, 0); glVertex3f(width, 0     , 0);
299         glTexCoord2f(1, 1); glVertex3f(width, height, 0);
300         glTexCoord2f(0, 1); glVertex3f(0    , height, 0);
301         glTexCoord2f(0, 0); glVertex3f(0    , 0     , 0);
302         glEnd();
303 }
304
305
306 /* GObject code */
307 G_DEFINE_TYPE(GritsMarker, grits_marker, GRITS_TYPE_OBJECT);
308 static void grits_marker_init(GritsMarker *marker)
309 {
310 }
311
312 static void grits_marker_finalize(GObject *_marker)
313 {
314         //g_debug("GritsMarker: finalize - %s", marker->label);
315         GritsMarker *marker = GRITS_MARKER(_marker);
316         glDeleteTextures(1, &marker->tex);
317         cairo_surface_t *surface = cairo_get_target(marker->cairo);
318         cairo_surface_destroy(surface);
319         cairo_destroy(marker->cairo);
320         g_free(marker->label);
321         glDeleteTextures(1, &marker->tex);
322 }
323
324 static void grits_marker_class_init(GritsMarkerClass *klass)
325 {
326         g_debug("GritsMarker: class_init");
327         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
328         gobject_class->finalize = grits_marker_finalize;
329
330         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
331         object_class->draw = grits_marker_draw;
332 }