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