]> Pileus Git - grits/blob - src/objects/grits-marker.c
libgis -> grits: Fix build after rename
[grits] / src / objects / grits-marker.c
1 /*
2  * Copyright (C) 2009-2010 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:gis-marker
20  * @short_description: Single point markers
21  *
22  * Each #GisMarker 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, GisObjects should almost
29  * always be added to the GIS_LEVEL_OVERLAY level so they are drawn "above" the
30  * actual earth.
31  */
32
33 #include <config.h>
34 #include <GL/gl.h>
35 #include "grits-marker.h"
36
37 /**
38  * gis_marker_new:
39  * @label: a short description of the marker
40  *
41  * Create a new GisMarker which shows the given label when drawn.
42  *
43  * Returns: the new #GisMarker.
44  */
45 GisMarker *gis_marker_new(const gchar *label)
46 {
47         //g_debug("GisMarker: 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;
52
53         GisMarker *marker = g_object_new(GIS_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));
59
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);
63
64         /* Draw outline */
65         cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
66         cairo_set_line_width(marker->cairo, OUTLINE*2);
67
68         cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
69         cairo_stroke(marker->cairo);
70
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);
74
75         /* Draw filler */
76         cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
77
78         cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
79         cairo_fill(marker->cairo);
80
81         cairo_move_to(marker->cairo, marker->xoff+4, marker->yoff-8);
82         cairo_show_text(marker->cairo, marker->label);
83
84         /* Load GL texture */
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);
94
95         return marker;
96 }
97
98 /* Drawing */
99 static void gis_marker_draw(GisObject *_marker, GisOpenGL *opengl)
100 {
101         GisMarker *marker = GIS_MARKER(_marker);
102         GisPoint *point = gis_object_center(marker);
103         gdouble px, py, pz;
104         gis_viewer_project(GIS_VIEWER(opengl),
105                         point->lat, point->lon, point->elev,
106                         &px, &py, &pz);
107
108         gint win_width  = GTK_WIDGET(opengl)->allocation.width;
109         gint win_height = GTK_WIDGET(opengl)->allocation.height;
110         py = win_height - py;
111         if (pz > 1)
112                 return;
113
114         //g_debug("GisOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
115
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);
119
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);
125
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);
132         glBegin(GL_QUADS);
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);
137         glEnd();
138 }
139
140 /* GObject code */
141 G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
142 static void gis_marker_init(GisMarker *marker)
143 {
144 }
145
146 static void gis_marker_finalize(GObject *_marker)
147 {
148         //g_debug("GisMarker: finalize - %s", marker->label);
149         GisMarker *marker = GIS_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 }
156
157 static void gis_marker_class_init(GisMarkerClass *klass)
158 {
159         g_debug("GisMarker: class_init");
160         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
161         gobject_class->finalize = gis_marker_finalize;
162
163         GisObjectClass *object_class = GIS_OBJECT_CLASS(klass);
164         object_class->draw = gis_marker_draw;
165 }