]> Pileus Git - grits/blob - src/objects/grits-marker.c
Delay load the textures for label markers
[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 /* Texture setup functions */
39 static void render_point(GritsMarker *marker)
40 {
41         /* Draw outline */
42         cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
43         cairo_set_line_width(marker->cairo, marker->outline*2);
44
45         cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
46                   0, 2*G_PI);
47         cairo_stroke(marker->cairo);
48
49         /* Draw filler */
50         cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
51
52         cairo_arc(marker->cairo, marker->xoff, marker->yoff, marker->radius,
53                   0, 2*G_PI);
54         cairo_fill(marker->cairo);
55 }
56
57 static void render_label(GritsMarker *marker)
58 {
59         g_assert(marker->label);
60
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);
69
70         /* Draw filler */
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);
75 }
76
77 static void render_icon(GritsMarker *marker)
78 {
79         g_assert(marker->icon_img != NULL);
80
81         /* move to marker location */
82         cairo_translate(marker->cairo, marker->xoff, marker->yoff);
83
84         /* center image */
85         cairo_translate(marker->cairo, -marker->icon_width/2,
86                                        -marker->icon_height/2);
87
88         cairo_set_source_surface(marker->cairo, marker->icon_img, 0, 0);
89
90         cairo_paint(marker->cairo);
91 }
92
93 static void render_all(GritsMarker *marker)
94 {
95         g_assert(marker);
96         if (marker->display_mask & GRITS_MARKER_DMASK_ICON)
97                 render_icon(marker);
98
99         if (marker->display_mask & GRITS_MARKER_DMASK_POINT)
100                 render_point(marker);
101
102         if (marker->display_mask & GRITS_MARKER_DMASK_LABEL)
103                 render_label(marker);
104
105         /* Load GL texture */
106         glEnable(GL_TEXTURE_2D);
107         glGenTextures(1, &marker->tex);
108         glBindTexture(GL_TEXTURE_2D, marker->tex);
109         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110         glPixelStorei(GL_PACK_ALIGNMENT, 1);
111         glTexImage2D(GL_TEXTURE_2D, 0, 4, marker->width, marker->height,
112                 0, GL_BGRA, GL_UNSIGNED_BYTE,
113                 cairo_image_surface_get_data(cairo_get_target(marker->cairo)));
114         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
115         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116 }
117
118
119 /* Constructors */
120 /**
121  * grits_marker_new:
122  * @label: a short description of the marker
123  *
124  * Create a new GritsMarker which shows the given label when drawn.
125  *
126  * Returns: the new #GritsMarker.
127  */
128 GritsMarker *grits_marker_new(const gchar *label)
129 {
130         GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
131
132         marker->display_mask = GRITS_MARKER_DMASK_POINT |
133                                GRITS_MARKER_DMASK_LABEL;
134
135         //g_debug("GritsMarker: new - %s", label);
136         /* specify size of point and size of surface */
137         marker->outline =   2;
138         marker->radius  =   3;
139         marker->width   = 128;
140         marker->height  =  32;
141         marker->icon_width  = marker->radius * 2;
142         marker->icon_height = marker->radius * 3;
143
144         marker->xoff  = marker->radius+marker->outline;
145         marker->yoff  = marker->height-(marker->radius+marker->outline);
146         marker->cairo = cairo_create(cairo_image_surface_create(
147                         CAIRO_FORMAT_ARGB32, marker->width, marker->height));
148
149         marker->label = g_strdup(label);
150
151         return marker;
152 }
153
154 /**
155  * grits_marker_icon_new:
156  * @label:        The label to display if GRITS_MARKER_DMASK_LABEL is set
157  * @filename:     The filename of the icon
158  * @angle:        The angle to rotate the icon (0 is north)
159  * @flip:         Whether to flip the image so that it's never upside down.
160  *                Useful for non-symmetric icons which have an "up".
161  * @display_mask: A bitmask which specifies which items to display.
162  *
163  * Create a new marker with a label, point, icon (png), or any
164  * combination of the above.
165  *
166  * Returns: the new #GritsMarker
167  */
168 GritsMarker *grits_marker_icon_new(const gchar *label, const gchar *filename,
169     guint angle, gboolean flip, guint display_mask)
170 {
171         GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
172
173         marker->label = g_strdup(label);
174         marker->angle = angle;
175         marker->flip = flip;
176         marker->display_mask = display_mask;
177
178         if (display_mask & GRITS_MARKER_DMASK_ICON) {
179                 g_assert(filename != NULL);
180                 g_debug("GritsMarker: marker_icon_new - marker image %s",
181                         filename);
182                 marker->icon_img = cairo_image_surface_create_from_png(filename);
183                 if (cairo_surface_status(marker->icon_img)) {
184                         g_warning("GritsMarker: marker_icon_new - "
185                                   "error reading file %s", filename);
186                         /* convert it to a point, better than nothing */
187                         marker->display_mask &= ~GRITS_MARKER_DMASK_ICON;
188                         marker->display_mask |=  GRITS_MARKER_DMASK_POINT;
189                         marker->icon_width = 4;
190                         marker->icon_height = 8;
191
192                 } else {
193                         marker->icon_width =
194                             cairo_image_surface_get_width(marker->icon_img);
195                         marker->icon_height =
196                             cairo_image_surface_get_height(marker->icon_img);
197                 }
198         } else {
199                 marker->icon_img = NULL;
200                 marker->icon_width = 4; /* room for the point if there is one */
201                 marker->icon_height = 8;
202         }
203         g_debug("GritsMarker: marker_icon_new - width = %d, height = %d",
204                 marker->icon_width, marker->icon_height);
205
206         marker->outline =   2;
207         marker->radius  =   3;
208
209         /* this is the surface size, a guess really */
210         marker->width   = marker->icon_width  + 128;
211         marker->height  = marker->icon_height + 64;
212
213         marker->xoff  = marker->width/2;
214         marker->yoff  = marker->height/2;
215         marker->cairo = cairo_create(cairo_image_surface_create(
216                         CAIRO_FORMAT_ARGB32, marker->width, marker->height));
217
218         /* clear the surface just in case */
219         cairo_set_operator(marker->cairo, CAIRO_OPERATOR_SOURCE);
220         //cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.3); // debug
221         cairo_set_source_rgba(marker->cairo, 1.0, 0.0, 0.0, 0.0);
222         cairo_paint(marker->cairo);
223         cairo_set_operator(marker->cairo, CAIRO_OPERATOR_OVER);
224
225         render_all(marker);
226
227         if (marker->icon_img)
228                 cairo_surface_destroy(marker->icon_img);
229
230         return marker;
231 }
232
233
234 /* Drawing */
235 static void grits_marker_draw(GritsObject *_marker, GritsOpenGL *opengl)
236 {
237         GritsMarker *marker = GRITS_MARKER(_marker);
238         GritsPoint  *point  = grits_object_center(marker);
239
240         cairo_surface_t *surface = cairo_get_target(marker->cairo);
241         gdouble width  = cairo_image_surface_get_width(surface);
242         gdouble height = cairo_image_surface_get_height(surface);
243
244         if (!marker->tex)
245                 render_all(marker);
246
247         if (marker->ortho) {
248                 gdouble px, py, pz;
249                 grits_viewer_project(GRITS_VIEWER(opengl),
250                                 point->lat, point->lon, point->elev,
251                                 &px, &py, &pz);
252
253                 gint win_width  = GTK_WIDGET(opengl)->allocation.width;
254                 gint win_height = GTK_WIDGET(opengl)->allocation.height;
255                 if (pz > 1)
256                         return;
257
258                 glMatrixMode(GL_PROJECTION); glLoadIdentity();
259                 glMatrixMode(GL_MODELVIEW);  glLoadIdentity();
260                 glOrtho(0, win_width, win_height, 0, 1, -1);
261                 glTranslated(px, win_height-py, 0);
262                 glRotatef(marker->angle, 0, 0, -1);
263                 glTranslated(-marker->xoff, -marker->yoff, 0);
264         } else {
265                 gdouble scale, eye[3], pos[3] =
266                         {point->lat, point->lon, point->elev};
267                 grits_viewer_get_location(GRITS_VIEWER(opengl),
268                        &eye[0], &eye[1], &eye[2]);
269                 lle2xyz(eye[0],  eye[1],  eye[2],
270                        &eye[0], &eye[1], &eye[2]);
271                 lle2xyz(pos[0],  pos[1],  pos[2],
272                        &pos[0], &pos[1], &pos[2]);
273                 scale = MPPX(distd(eye, pos));
274
275                 glRotatef(marker->angle, 0, 0, -1);
276                 glRotatef(180, 1, 0, 0);
277                 glScalef(scale, scale, 1);
278                 glTranslated(-marker->xoff, -marker->yoff, 0);
279         }
280
281         //g_debug("GritsOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
282
283         glDisable(GL_LIGHTING);
284         glDisable(GL_COLOR_MATERIAL);
285         glDisable(GL_DEPTH_TEST);
286         glEnable(GL_TEXTURE_2D);
287         glBindTexture(GL_TEXTURE_2D, marker->tex);
288         glDisable(GL_CULL_FACE);
289         glBegin(GL_QUADS);
290         glTexCoord2f(1, 0); glVertex3f(width, 0     , 0); // 0 - 3    0 
291         glTexCoord2f(1, 1); glVertex3f(width, height, 0); // 1 - |    | 
292         glTexCoord2f(0, 1); glVertex3f(0    , height, 0); // 2 - |    | 
293         glTexCoord2f(0, 0); glVertex3f(0    , 0     , 0); // 3 - 2----1 
294         glEnd();
295 }
296
297
298 /* GObject code */
299 G_DEFINE_TYPE(GritsMarker, grits_marker, GRITS_TYPE_OBJECT);
300 static void grits_marker_init(GritsMarker *marker)
301 {
302         marker->ortho = TRUE;
303 }
304
305 static void grits_marker_finalize(GObject *_marker)
306 {
307         //g_debug("GritsMarker: finalize - %s", marker->label);
308         GritsMarker *marker = GRITS_MARKER(_marker);
309         if (marker->tex)
310                 glDeleteTextures(1, &marker->tex);
311         if (marker->cairo) {
312                 cairo_surface_t *surface = cairo_get_target(marker->cairo);
313                 cairo_surface_destroy(surface);
314                 cairo_destroy(marker->cairo);
315         }
316         g_free(marker->label);
317 }
318
319 static void grits_marker_class_init(GritsMarkerClass *klass)
320 {
321         g_debug("GritsMarker: class_init");
322         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
323         gobject_class->finalize = grits_marker_finalize;
324
325         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
326         object_class->draw = grits_marker_draw;
327 }