2 * Copyright (C) 2009-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/>.
19 * SECTION:grits-marker
20 * @short_description: Single point markers
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
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
36 #include "grits-marker.h"
38 /* Texture setup functions */
39 static void render_point(GritsMarker *marker)
42 cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
43 cairo_set_line_width(marker->cairo, marker->outline*2);
45 cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
47 cairo_stroke(marker->cairo);
50 cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
52 cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
54 cairo_fill(marker->cairo);
57 static void render_label(GritsMarker *marker)
59 g_assert(marker->label);
61 cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
62 cairo_select_font_face(marker->cairo, "sans-serif",
63 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
64 cairo_set_font_size(marker->cairo, 13);
65 cairo_move_to(marker->cairo, marker->xoff + (marker->icon_width / 2),
66 marker->yoff - (marker->icon_height / 2));
67 cairo_text_path(marker->cairo, marker->label);
68 cairo_stroke(marker->cairo);
71 cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
72 cairo_move_to(marker->cairo, marker->xoff + (marker->icon_width / 2),
73 marker->yoff - (marker->icon_height / 2));
74 cairo_show_text(marker->cairo, marker->label);
77 static void render_icon(GritsMarker *marker)
79 g_assert(marker->icon_img != NULL);
81 /* This code assumes the icon is an image pointing toward 0 degrees
82 * (ie. north/up). If marker->flip is set, then it will rotate the
83 * icon appropriately then reflect it across the vertical axis so
84 * it's never upside down. */
86 gdouble angle = marker->angle % 360;
87 if (marker->flip && (angle < 360 && angle > 180)) {
88 /* if icon rotates to the left half it will be upside down */
89 flip = -1.0; /* flip horizontally */
92 cairo_save(marker->cairo);
94 /* move to marker location */
95 cairo_translate(marker->cairo, marker->xoff, marker->yoff);
97 /* perform rotation and flip in one transformation */
98 gdouble C = cos(angle*(M_PI/180.0));
99 gdouble S = sin(angle*(M_PI/180.0));
104 cairo_matrix_t matrix;
105 cairo_matrix_init(&matrix,
108 C*tx*(1-fx)-S*ty*(fy-1)+tx-C*tx+S*ty,
109 S*tx*(1-fx)+C*ty*(fy-1)+ty-S*tx-C*ty);
110 cairo_transform(marker->cairo, &matrix);
113 cairo_translate(marker->cairo, -marker->icon_width/2,
114 -marker->icon_height/2);
116 cairo_set_source_surface(marker->cairo, marker->icon_img, 0, 0);
118 cairo_paint(marker->cairo);
119 cairo_restore(marker->cairo);
122 static void render_all(GritsMarker *marker)
125 if (marker->display_mask & MARKER_DMASK_ICON)
128 if (marker->display_mask & MARKER_DMASK_POINT)
129 render_point(marker);
131 if (marker->display_mask & MARKER_DMASK_LABEL)
132 render_label(marker);
134 /* Load GL texture */
135 glEnable(GL_TEXTURE_2D);
136 glGenTextures(1, &marker->tex);
137 glBindTexture(GL_TEXTURE_2D, marker->tex);
138 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
139 glPixelStorei(GL_PACK_ALIGNMENT, 1);
140 glTexImage2D(GL_TEXTURE_2D, 0, 4, marker->width, marker->height,
141 0, GL_BGRA, GL_UNSIGNED_BYTE,
142 cairo_image_surface_get_data(cairo_get_target(marker->cairo)));
143 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
144 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
151 * @label: a short description of the marker
153 * Create a new GritsMarker which shows the given label when drawn.
155 * Returns: the new #GritsMarker.
157 GritsMarker *grits_marker_new(const gchar *label)
159 GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
161 marker->display_mask = MARKER_DMASK_POINT | MARKER_DMASK_LABEL;
163 //g_debug("GritsMarker: new - %s", label);
164 /* specify size of point and size of surface */
169 marker->icon_width = marker->radius * 2;
170 marker->icon_height = marker->radius * 3;
172 marker->xoff = marker->radius+marker->outline;
173 marker->yoff = marker->height-(marker->radius+marker->outline);
174 marker->cairo = cairo_create(cairo_image_surface_create(
175 CAIRO_FORMAT_ARGB32, marker->width, marker->height));
177 marker->label = g_strdup(label);
185 * grits_marker_icon_new:
186 * @label: The label to display if MARKER_DMASK_LABEL is set
187 * @filename: The filename of the icon
188 * @angle: The angle to rotate the icon (0 is north)
189 * @flip: Whether to flip the image so that it's never upside down.
190 * Useful for non-symmetric icons which have an "up".
191 * @display_mask: A bitmask which specifies which items to display.
193 * Create a new marker with a label, point, icon (png), or any
194 * combination of the above.
196 * Returns: the new #GritsMarker
198 GritsMarker *grits_marker_icon_new(const gchar *label, const gchar *filename,
199 guint angle, gboolean flip, guint display_mask)
201 GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
203 marker->label = g_strdup(label);
204 marker->angle = angle;
206 marker->display_mask = display_mask;
208 if (display_mask & MARKER_DMASK_ICON) {
209 g_assert(filename != NULL);
210 g_debug("GritsMarker: marker_icon_new - marker image %s",
212 marker->icon_img = cairo_image_surface_create_from_png(filename);
213 if (cairo_surface_status(marker->icon_img)) {
214 g_warning("GritsMarker: marker_icon_new - "
215 "error reading file %s", filename);
216 /* convert it to a point, better than nothing */
217 marker->display_mask &= ~MARKER_DMASK_ICON;
218 marker->display_mask |= MARKER_DMASK_POINT;
219 marker->icon_width = 4;
220 marker->icon_height = 8;
224 cairo_image_surface_get_width(marker->icon_img);
225 marker->icon_height =
226 cairo_image_surface_get_height(marker->icon_img);
229 marker->icon_img = NULL;
230 marker->icon_width = 4; /* room for the point if there is one */
231 marker->icon_height = 8;
233 g_debug("GritsMarker: marker_icon_new - width = %d, height = %d",
234 marker->icon_width, marker->icon_height);
238 /* this is the surface size, a guess really */
239 marker->width = marker->icon_width + 128;
240 marker->height = marker->icon_height + 64;
242 marker->xoff = marker->width/2;
243 marker->yoff = marker->height/2;
244 marker->cairo = cairo_create(cairo_image_surface_create(
245 CAIRO_FORMAT_ARGB32, marker->width, marker->height));
246 /* clear the surface just in case */
247 cairo_set_operator(marker->cairo, CAIRO_OPERATOR_SOURCE);
248 //cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.3); // debug
249 cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.0);
250 cairo_paint(marker->cairo);
251 cairo_set_operator(marker->cairo, CAIRO_OPERATOR_OVER);
255 if (marker->icon_img)
256 cairo_surface_destroy(marker->icon_img);
263 static void grits_marker_draw(GritsObject *_marker, GritsOpenGL *opengl)
265 GritsMarker *marker = GRITS_MARKER(_marker);
266 GritsPoint *point = grits_object_center(marker);
268 grits_viewer_project(GRITS_VIEWER(opengl),
269 point->lat, point->lon, point->elev,
272 gint win_width = GTK_WIDGET(opengl)->allocation.width;
273 gint win_height = GTK_WIDGET(opengl)->allocation.height;
274 py = win_height - py;
278 //g_debug("GritsOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
280 cairo_surface_t *surface = cairo_get_target(marker->cairo);
281 gdouble width = cairo_image_surface_get_width(surface);
282 gdouble height = cairo_image_surface_get_height(surface);
284 glMatrixMode(GL_PROJECTION); glLoadIdentity();
285 glMatrixMode(GL_MODELVIEW); glLoadIdentity();
286 glOrtho(0, win_width, win_height, 0, -1, 1);
287 glTranslated(px - marker->xoff,
288 py - marker->yoff, 0);
290 glDisable(GL_LIGHTING);
291 glDisable(GL_COLOR_MATERIAL);
292 glDisable(GL_DEPTH_TEST);
293 glEnable(GL_TEXTURE_2D);
294 glBindTexture(GL_TEXTURE_2D, marker->tex);
295 glDisable(GL_CULL_FACE);
297 glTexCoord2f(1, 0); glVertex3f(width, 0 , 0);
298 glTexCoord2f(1, 1); glVertex3f(width, height, 0);
299 glTexCoord2f(0, 1); glVertex3f(0 , height, 0);
300 glTexCoord2f(0, 0); glVertex3f(0 , 0 , 0);
306 G_DEFINE_TYPE(GritsMarker, grits_marker, GRITS_TYPE_OBJECT);
307 static void grits_marker_init(GritsMarker *marker)
311 static void grits_marker_finalize(GObject *_marker)
313 //g_debug("GritsMarker: finalize - %s", marker->label);
314 GritsMarker *marker = GRITS_MARKER(_marker);
315 glDeleteTextures(1, &marker->tex);
316 cairo_surface_t *surface = cairo_get_target(marker->cairo);
317 cairo_surface_destroy(surface);
318 cairo_destroy(marker->cairo);
319 g_free(marker->label);
320 glDeleteTextures(1, &marker->tex);
323 static void grits_marker_class_init(GritsMarkerClass *klass)
325 g_debug("GritsMarker: class_init");
326 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
327 gobject_class->finalize = grits_marker_finalize;
329 GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
330 object_class->draw = grits_marker_draw;