2 * Copyright (C) 2009-2010 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
35 #include "grits-marker.h"
39 * @label: a short description of the marker
41 * Create a new GritsMarker which shows the given label when drawn.
43 * Returns: the new #GritsMarker.
45 GritsMarker *grits_marker_new(const gchar *label)
47 //g_debug("GritsMarker: new - %s", label);
48 static const gdouble OUTLINE = 2;
49 static const gdouble RADIUS = 3;
50 static const gdouble WIDTH = 128;
51 static const gdouble HEIGHT = 32;
53 GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
54 marker->xoff = RADIUS+OUTLINE;
55 marker->yoff = HEIGHT-(RADIUS+OUTLINE);
56 marker->label = g_strdup(label);
57 marker->cairo = cairo_create(cairo_image_surface_create(
58 CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT));
60 cairo_select_font_face(marker->cairo, "sans-serif",
61 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
62 cairo_set_font_size(marker->cairo, 13);
65 cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
66 cairo_set_line_width(marker->cairo, OUTLINE*2);
68 cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
69 cairo_stroke(marker->cairo);
71 cairo_move_to(marker->cairo, marker->xoff+4, marker->yoff-8);
72 cairo_text_path(marker->cairo, marker->label);
73 cairo_stroke(marker->cairo);
76 cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
78 cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
79 cairo_fill(marker->cairo);
81 cairo_move_to(marker->cairo, marker->xoff+4, marker->yoff-8);
82 cairo_show_text(marker->cairo, marker->label);
85 glEnable(GL_TEXTURE_2D);
86 glGenTextures(1, &marker->tex);
87 glBindTexture(GL_TEXTURE_2D, marker->tex);
88 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
89 glPixelStorei(GL_PACK_ALIGNMENT, 1);
90 glTexImage2D(GL_TEXTURE_2D, 0, 4, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE,
91 cairo_image_surface_get_data(cairo_get_target(marker->cairo)));
92 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
93 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
99 static void grits_marker_draw(GritsObject *_marker, GritsOpenGL *opengl)
101 GritsMarker *marker = GRITS_MARKER(_marker);
102 GritsPoint *point = grits_object_center(marker);
104 grits_viewer_project(GRITS_VIEWER(opengl),
105 point->lat, point->lon, point->elev,
108 gint win_width = GTK_WIDGET(opengl)->allocation.width;
109 gint win_height = GTK_WIDGET(opengl)->allocation.height;
110 py = win_height - py;
114 //g_debug("GritsOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
116 cairo_surface_t *surface = cairo_get_target(marker->cairo);
117 gdouble width = cairo_image_surface_get_width(surface);
118 gdouble height = cairo_image_surface_get_height(surface);
120 glMatrixMode(GL_PROJECTION); glLoadIdentity();
121 glMatrixMode(GL_MODELVIEW); glLoadIdentity();
122 glOrtho(0, win_width, win_height, 0, -1, 1);
123 glTranslated(px - marker->xoff,
124 py - marker->yoff, 0);
126 glDisable(GL_LIGHTING);
127 glDisable(GL_COLOR_MATERIAL);
128 glDisable(GL_DEPTH_TEST);
129 glEnable(GL_TEXTURE_2D);
130 glBindTexture(GL_TEXTURE_2D, marker->tex);
131 glDisable(GL_CULL_FACE);
133 glTexCoord2f(1, 0); glVertex3f(width, 0 , 0);
134 glTexCoord2f(1, 1); glVertex3f(width, height, 0);
135 glTexCoord2f(0, 1); glVertex3f(0 , height, 0);
136 glTexCoord2f(0, 0); glVertex3f(0 , 0 , 0);
141 G_DEFINE_TYPE(GritsMarker, grits_marker, GRITS_TYPE_OBJECT);
142 static void grits_marker_init(GritsMarker *marker)
146 static void grits_marker_finalize(GObject *_marker)
148 //g_debug("GritsMarker: finalize - %s", marker->label);
149 GritsMarker *marker = GRITS_MARKER(_marker);
150 glDeleteTextures(1, &marker->tex);
151 cairo_surface_t *surface = cairo_get_target(marker->cairo);
152 cairo_surface_destroy(surface);
153 cairo_destroy(marker->cairo);
154 g_free(marker->label);
155 glDeleteTextures(1, &marker->tex);
158 static void grits_marker_class_init(GritsMarkerClass *klass)
160 g_debug("GritsMarker: class_init");
161 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
162 gobject_class->finalize = grits_marker_finalize;
164 GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
165 object_class->draw = grits_marker_draw;