]> Pileus Git - grits/blob - src/plugins/env.c
6312f3b2ae7e7f9e76d0058ff720061dd2aabf02
[grits] / src / plugins / env.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:env
20  * @short_description: Environment plugin
21  *
22  * #GritsPluginEnv provides environmental information such as sky images. It can
23  * also paint a blank overlay on the surface so that other plugins can draw
24  * transparent overlays nicely.
25  */
26
27 #include <math.h>
28
29 #include <grits.h>
30
31 #ifdef HAVE_GLUT
32 #include <GL/glut.h>
33 #endif
34
35 #include "env.h"
36
37 /***********
38  * Helpers *
39  ***********/
40
41 /* Sky */
42 static void sky_expose(GritsCallback *sky, GritsOpenGL *opengl, gpointer _env)
43 {
44         GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
45         g_debug("GritsPluginEnv: expose_sky");
46
47         gdouble lat, lon, elev;
48         grits_viewer_get_location(env->viewer, &lat, &lon, &elev);
49
50         /* Misc */
51         gdouble rg   = MAX(0, 1-(elev/40000));
52         gdouble blue = MAX(0, 1-(elev/100000));
53         glClearColor(MIN(0.4,rg), MIN(0.4,rg), MIN(1,blue), 1.0f);
54         glClear(GL_COLOR_BUFFER_BIT);
55
56         /* Attempt to render an atmosphere */
57         glEnable(GL_COLOR_MATERIAL);
58         glDisable(GL_CULL_FACE);
59         glDisable(GL_LIGHTING);
60         glMatrixMode(GL_MODELVIEW);
61         glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
62         grits_viewer_center_position(env->viewer, lat, lon, -EARTH_R);
63
64         gdouble ds  = EARTH_R+elev;     // distance to self
65         gdouble da  = EARTH_R+300000;   // distance to top of atmosphere
66         gdouble dg  = EARTH_R-100000;   // distance to top of atmosphere
67         gdouble ang = acos(EARTH_R/ds); // angle to horizon
68         ang = MAX(ang,0.1);
69
70         gdouble ar  = sin(ang)*da;      // top of quad fan "atomosphere"j
71         gdouble az  = cos(ang)*da;      //
72
73         gdouble gr  = sin(ang)*dg;      // bottom of quad fan "ground"
74         gdouble gz  = cos(ang)*dg;      //
75
76         glBegin(GL_QUAD_STRIP);
77         for (gdouble i = 0; i <= 2*G_PI; i += G_PI/30) {
78                 glColor4f(0.3, 0.3, 1.0, 1.0); glVertex3f(gr*sin(i), gr*cos(i), gz);
79                 glColor4f(0.3, 0.3, 1.0, 0.0); glVertex3f(ar*sin(i), ar*cos(i), az);
80         }
81         glEnd();
82 }
83
84 /* Compass */
85 static void compass_draw_compass(gdouble scale)
86 {
87         gfloat thick = scale * 0.20;
88
89         /* Setup lighting */
90         float light_ambient[]  = {0.4f, 0.4f, 0.4f, 0.4f};
91         float light_diffuse[]  = {0.9f, 0.9f, 0.9f, 1.0f};
92         float light_position[] = {-scale*2, -scale*4, -scale*0.5, 1.0f};
93         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
94         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
95         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
96
97         /* Compass data */
98         gdouble colors[][3] = {
99                 {1, 0, 0},
100                 {1, 1, 1},
101                 {1, 1, 1},
102                 {1, 1, 1},
103         };
104         gdouble points[][3] = {
105                 {     0, -scale,      0},
106                 {     0,      0,  thick},
107                 { thick, -thick,      0},
108                 {     0,      0, -thick},
109                 {-thick, -thick,      0},
110         };
111         gint faces[][3] = {
112                 {1, 0, 2},
113                 {2, 0, 3},
114                 {3, 0, 4},
115                 {4, 0, 1},
116         };
117         gint outline[] = {
118                 2, 0, 4,
119                 1, 2, 3, 4,
120         };
121
122         /* Draw compas */
123         glEnable(GL_POLYGON_OFFSET_FILL);
124         glPolygonOffset(1, 1);
125         for (int i = 0; i < G_N_ELEMENTS(colors); i++) {
126                 gdouble *color = colors[i];
127                 glColor3dv(color);
128                 for (int j = 0; j < G_N_ELEMENTS(faces); j++) {
129                         gdouble norm[3];
130                         gdouble *v0 = points[faces[j][0]];
131                         gdouble *v1 = points[faces[j][1]];
132                         gdouble *v2 = points[faces[j][2]];
133                         crossd3(v2, v1, v0, norm);
134                         glNormal3dv(norm);
135                         glBegin(GL_TRIANGLES);
136                         glVertex3dv(v0);
137                         glVertex3dv(v1);
138                         glVertex3dv(v2);
139                         glEnd();
140                 }
141                 glRotatef(90, 0, 0, 1);
142         }
143
144         /* Draw outline */
145         glEnable(GL_POLYGON_OFFSET_LINE);
146         glDisable(GL_LIGHTING);
147         glPolygonOffset(0, 0);
148         glColor4f(0, 0, 0, 0.25);
149         glLineWidth(1);
150         for (int i = 0; i < G_N_ELEMENTS(colors); i++) {
151                 glBegin(GL_LINE_STRIP);
152                 for (int j = 0; j < G_N_ELEMENTS(outline); j++)
153                         glVertex3dv(points[outline[j]]);
154                 glEnd();
155                 glRotatef(90, 0, 0, 1);
156         }
157 }
158
159 static gboolean compass_draw_teapot(gdouble scale, GritsPluginEnv *env)
160 {
161         static int teatime = 0;
162 #ifdef HAVE_GLUT
163         static int init, argc; char *argv[] = {"", NULL};
164         if (!init) {
165                 teatime = grits_prefs_get_boolean(env->prefs, "grits/teatime", NULL);
166                 glutInit(&argc, argv);
167                 init = 1;
168                 g_message("teatime=%d", teatime);
169         }
170
171         if (teatime) {
172                 /* Setup lighting */
173                 float light_ambient[]  = {0.1f, 0.1f, 0.0f, 1.0f};
174                 float light_diffuse[]  = {0.9f, 0.9f, 0.9f, 1.0f};
175                 float light_position[] = {-50.0f, -40.0f, -80.0f, 1.0f};
176                 glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
177                 glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
178                 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
179
180                 /* Draw teapot */
181                 glRotatef(-90, 0, 0, 1);
182                 glRotatef(-90, 1, 0, 0);
183                 glColor4f(0.9, 0.9, 0.7, 1.0);
184                 glutSolidTeapot(scale * 0.60);
185         }
186 #endif
187         return teatime;
188 }
189
190 static void compass_expose(GritsCallback *compass, GritsOpenGL *opengl, gpointer _env)
191 {
192         GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
193         g_debug("GritsPluginEnv: compass_expose");
194         gdouble x, y, z;
195         grits_viewer_get_rotation(env->viewer, &x, &y, &z);
196
197         /* Setup projection */
198         gint win_width  = GTK_WIDGET(opengl)->allocation.width;
199         gint win_height = GTK_WIDGET(opengl)->allocation.height;
200         float scale     = CLAMP(MIN(win_width,win_height)/2.0 * 0.1, 40, 100);
201         float offset    = scale + 20;
202         glTranslatef(win_width - offset, offset, 0);
203
204         /* Setup state */
205         glClear(GL_DEPTH_BUFFER_BIT);
206         glEnable(GL_COLOR_MATERIAL);
207         glEnable(GL_DEPTH_TEST);
208         glDisable(GL_CULL_FACE);
209         glEnable(GL_POLYGON_SMOOTH);
210         glEnable(GL_LINE_SMOOTH);
211
212         /* Draw compass */
213         x = CLAMP(x, -66, 66);;
214         glRotatef(x, 1, 0, 0);
215         glRotatef(-z, 0, 0, 1);
216         if (!compass_draw_teapot(scale, env))
217                 compass_draw_compass(scale);
218 }
219
220 static gboolean compass_click(GritsCallback *compass, GdkEvent *evnet, GritsViewer *viewer)
221 {
222         grits_viewer_set_rotation(viewer, 0, 0, 0);
223         return TRUE;
224 }
225
226 /* Info */
227 static void info_expose(GritsCallback *compass, GritsOpenGL *opengl, gpointer _env)
228 {
229         gint win_width  = GTK_WIDGET(opengl)->allocation.width;
230         gint win_height = GTK_WIDGET(opengl)->allocation.height;
231
232         /* Create cairo  surface */
233         guint            tex     = 0;
234         const gchar     *label0  = "Location: %7.3lf°, %8.3lf°, %4.0fm";
235         const gchar     *label1  = "Cursor:   %7.3lf°, %8.3lf°, %4.0fm";
236         gdouble          width   = 300;
237         gdouble          height  = 200;
238         cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
239         cairo_t         *cairo   = cairo_create(surface);
240
241         /* Text */
242         gdouble lat, lon, elev;
243         grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
244         gchar *text0 = g_strdup_printf(label0, lat, lon, elev);
245         gchar *text1 = g_strdup_printf(label1, lat, lon, elev);
246
247         /* Draw outline */
248         cairo_set_line_width(cairo, 3);
249         cairo_set_source_rgba(cairo, 0, 0, 0, 0.75);
250         cairo_move_to(cairo, 2, 20); cairo_text_path(cairo, text0);
251         cairo_move_to(cairo, 2, 40); cairo_text_path(cairo, text1);
252         cairo_stroke(cairo);
253
254         /* Draw filler */
255         cairo_set_source_rgba(cairo, 1, 1, 1, 1);
256         cairo_move_to(cairo, 2, 20); cairo_show_text(cairo, text0);
257         cairo_move_to(cairo, 2, 40); cairo_show_text(cairo, text1);
258
259         /* Setup pango */
260         PangoLayout          *layout = pango_cairo_create_layout(cairo);
261         PangoFontDescription *font   = pango_font_description_from_string("Mono 9");
262         pango_layout_set_font_description(layout, font);
263         pango_font_description_free(font);
264         pango_layout_set_text(layout, text0, -1);
265         pango_cairo_update_layout(cairo, layout);
266         cairo_set_line_join(cairo, CAIRO_LINE_JOIN_ROUND);
267         cairo_move_to(cairo, 2, 40);
268         pango_cairo_layout_path(cairo, layout);
269         for (float w = 0.2; w <= 0.8; w+=0.2) {
270                 cairo_set_line_width(cairo, (1-w)*8);
271                 cairo_set_source_rgba(cairo, 0, 0, 0, w);
272                 cairo_stroke_preserve(cairo);
273         }
274         cairo_set_source_rgba(cairo, 1, 1, 1, 1);
275         pango_cairo_show_layout(cairo, layout);
276
277         /* Load GL texture */
278         glEnable(GL_TEXTURE_2D);
279         glGenTextures(1, &tex);
280         glBindTexture(GL_TEXTURE_2D, tex);
281         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
282         glPixelStorei(GL_PACK_ALIGNMENT, 1);
283         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height,
284                 0, GL_BGRA, GL_UNSIGNED_BYTE, cairo_image_surface_get_data(surface));
285         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
286         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
287
288         /* Draw surface */
289         glDisable(GL_LIGHTING);
290         glDisable(GL_COLOR_MATERIAL);
291         glDisable(GL_DEPTH_TEST);
292         glEnable(GL_TEXTURE_2D);
293         glBindTexture(GL_TEXTURE_2D, tex);
294         glDisable(GL_CULL_FACE);
295         glTranslatef(win_width - width, win_height - height, 0);
296         glBegin(GL_QUADS);
297         glTexCoord2f(1, 0); glVertex3f(width, 0     , 0); // 0 - 3    0
298         glTexCoord2f(1, 1); glVertex3f(width, height, 0); // 1 - |    |
299         glTexCoord2f(0, 1); glVertex3f(0    , height, 0); // 2 - |    |
300         glTexCoord2f(0, 0); glVertex3f(0    , 0     , 0); // 3 - 2----1
301         glEnd();
302 }
303
304 /***********
305  * Methods *
306  ***********/
307 /**
308  * grits_plugin_env_new:
309  * @viewer: the #GritsViewer to use for drawing
310  * @prefs:  the #GritsPrefs for storing configurations
311  *
312  * Create a new instance of the environment plugin.
313  *
314  * Returns: the new #GritsPluginEnv
315  */
316 GritsPluginEnv *grits_plugin_env_new(GritsViewer *viewer, GritsPrefs *prefs)
317 {
318         g_debug("GritsPluginEnv: new");
319         GritsPluginEnv *env = g_object_new(GRITS_TYPE_PLUGIN_ENV, NULL);
320         env->viewer = g_object_ref(viewer);
321         env->prefs  = g_object_ref(prefs);
322
323         /* Add sky */
324         GritsCallback *sky = grits_callback_new(sky_expose, env);
325         grits_viewer_add(viewer, GRITS_OBJECT(sky), GRITS_LEVEL_BACKGROUND, FALSE);
326         env->refs = g_list_prepend(env->refs, sky);
327
328         /* Add compass */
329         GritsCallback *compass = grits_callback_new(compass_expose, env);
330         grits_viewer_add(viewer, GRITS_OBJECT(compass), GRITS_LEVEL_HUD, FALSE);
331         g_signal_connect(compass, "clicked", G_CALLBACK(compass_click), viewer);
332         grits_object_set_cursor(GRITS_OBJECT(compass), GDK_CROSS);
333         env->refs = g_list_prepend(env->refs, compass);
334
335         /* Add info */
336         //GritsCallback *info = grits_callback_new(info_expose, env);
337         //grits_viewer_add(viewer, GRITS_OBJECT(info), GRITS_LEVEL_HUD, FALSE);
338         //env->refs = g_list_prepend(env->refs, info);
339         (void)info_expose;
340
341         /* Add background */
342         //GritsTile *background = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
343         //glGenTextures(1, &env->tex);
344         //background->data = &env->tex;
345         //grits_viewer_add(viewer, GRITS_OBJECT(background), GRITS_LEVEL_BACKGROUND, FALSE);
346         //env->refs = g_list_prepend(env->refs, background);
347
348         return env;
349 }
350
351
352 /****************
353  * GObject code *
354  ****************/
355 /* Plugin init */
356 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface);
357 G_DEFINE_TYPE_WITH_CODE(GritsPluginEnv, grits_plugin_env, G_TYPE_OBJECT,
358                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
359                         grits_plugin_env_plugin_init));
360 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface)
361 {
362         g_debug("GritsPluginEnv: plugin_init");
363         /* Add methods to the interface */
364 }
365 /* Class/Object init */
366 static void grits_plugin_env_init(GritsPluginEnv *env)
367 {
368         g_debug("GritsPluginEnv: init");
369         /* Set defaults */
370 }
371 static void grits_plugin_env_dispose(GObject *gobject)
372 {
373         g_debug("GritsPluginEnv: dispose");
374         GritsPluginEnv *env = GRITS_PLUGIN_ENV(gobject);
375         /* Drop references */
376         if (env->viewer) {
377                 for (GList *cur = env->refs; cur; cur = cur->next)
378                         grits_viewer_remove(env->viewer, cur->data);
379                 g_list_free_full(env->refs, g_object_unref);
380                 g_object_unref(env->viewer);
381                 g_object_unref(env->prefs);
382                 glDeleteTextures(1, &env->tex);
383                 env->viewer = NULL;
384         }
385         G_OBJECT_CLASS(grits_plugin_env_parent_class)->dispose(gobject);
386 }
387 static void grits_plugin_env_class_init(GritsPluginEnvClass *klass)
388 {
389         g_debug("GritsPluginEnv: class_init");
390         GObjectClass *gobject_class = (GObjectClass*)klass;
391         gobject_class->dispose = grits_plugin_env_dispose;
392 }