]> Pileus Git - grits/blob - src/plugins/env.c
Add compass rose
[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 #include <GL/glut.h>
31
32 #include "env.h"
33
34 /***********
35  * Helpers *
36  ***********/
37 static void expose_sky(GritsCallback *sky, GritsOpenGL *opengl, gpointer _env)
38 {
39         GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
40         g_debug("GritsPluginEnv: expose_sky");
41
42         gdouble lat, lon, elev;
43         grits_viewer_get_location(env->viewer, &lat, &lon, &elev);
44
45         /* Misc */
46         gdouble rg   = MAX(0, 1-(elev/40000));
47         gdouble blue = MAX(0, 1-(elev/100000));
48         glClearColor(MIN(0.4,rg), MIN(0.4,rg), MIN(1,blue), 1.0f);
49         glClear(GL_COLOR_BUFFER_BIT);
50
51         /* Attempt to render an atmosphere */
52         glEnable(GL_COLOR_MATERIAL);
53         glDisable(GL_CULL_FACE);
54         glDisable(GL_LIGHTING);
55         glMatrixMode(GL_MODELVIEW);
56         glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
57         grits_viewer_center_position(env->viewer, lat, lon, -EARTH_R);
58
59         gdouble ds  = EARTH_R+elev;     // distance to self
60         gdouble da  = EARTH_R+300000;   // distance to top of atmosphere
61         gdouble dg  = EARTH_R-100000;   // distance to top of atmosphere
62         gdouble ang = acos(EARTH_R/ds); // angle to horizon
63         ang = MAX(ang,0.1);
64
65         gdouble ar  = sin(ang)*da;      // top of quad fan "atomosphere"j
66         gdouble az  = cos(ang)*da;      //
67
68         gdouble gr  = sin(ang)*dg;      // bottom of quad fan "ground"
69         gdouble gz  = cos(ang)*dg;      //
70
71         glBegin(GL_QUAD_STRIP);
72         for (gdouble i = 0; i <= 2*G_PI; i += G_PI/30) {
73                 glColor4f(0.3, 0.3, 1.0, 1.0); glVertex3f(gr*sin(i), gr*cos(i), gz);
74                 glColor4f(0.3, 0.3, 1.0, 0.0); glVertex3f(ar*sin(i), ar*cos(i), az);
75         }
76         glEnd();
77 }
78
79 static void expose_hud(GritsCallback *hud, GritsOpenGL *opengl, gpointer _env)
80 {
81         GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
82         g_debug("GritsPluginEnv: expose_hud");
83         gdouble x, y, z;
84         grits_viewer_get_rotation(env->viewer, &x, &y, &z);
85
86         /* Setup projection */
87         gint win_width  = GTK_WIDGET(opengl)->allocation.width;
88         gint win_height = GTK_WIDGET(opengl)->allocation.height;
89         float scale     = MIN(win_width, win_height) / 10.0;
90         float offset    = scale+20;
91         glTranslatef(offset, offset, 0);
92
93         /* Setup lighting */
94         float light_ambient[]  = {0.1f, 0.1f, 0.0f, 1.0f};
95         float light_diffuse[]  = {0.9f, 0.9f, 0.9f, 1.0f};
96         float light_position[] = {-50.0f, -40.0f, -80.0f, 1.0f};
97         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
98         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
99         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
100
101         /* Setup state */
102         glClear(GL_DEPTH_BUFFER_BIT);
103         glEnable(GL_COLOR_MATERIAL);
104         glEnable(GL_DEPTH_TEST);
105         glDisable(GL_CULL_FACE);
106
107         /* Draw compas */
108         glRotatef(x, 1, 0, 0);
109         glRotatef(-z, 0, 0, 1);
110         glRotatef(-90, 0, 0, 1);
111         glRotatef(-90, 1, 0, 0);
112         glColor4f(0.9, 0.9, 0.7, 1.0);
113         glutSolidTeapot(scale * 0.5);
114 }
115
116 static void click_hud(GritsCallback *hud, GritsViewer *viewer)
117 {
118         grits_viewer_set_rotation(viewer, 0, 0, 0);
119 }
120
121 /***********
122  * Methods *
123  ***********/
124 /**
125  * grits_plugin_env_new:
126  * @viewer: the #GritsViewer to use for drawing
127  * @prefs:  the #GritsPrefs for storing configurations
128  *
129  * Create a new instance of the environment plugin.
130  *
131  * Returns: the new #GritsPluginEnv
132  */
133 GritsPluginEnv *grits_plugin_env_new(GritsViewer *viewer, GritsPrefs *prefs)
134 {
135         g_debug("GritsPluginEnv: new");
136         GritsPluginEnv *env = g_object_new(GRITS_TYPE_PLUGIN_ENV, NULL);
137         env->viewer = g_object_ref(viewer);
138
139         /* Add sky */
140         GritsCallback *sky = grits_callback_new(expose_sky, env);
141         GritsCallback *hud = grits_callback_new(expose_hud, env);
142         grits_viewer_add(viewer, GRITS_OBJECT(sky), GRITS_LEVEL_BACKGROUND, FALSE);
143         grits_viewer_add(viewer, GRITS_OBJECT(hud), GRITS_LEVEL_HUD, FALSE);
144         g_signal_connect(hud, "clicked", G_CALLBACK(click_hud), viewer);
145         env->refs = g_list_prepend(env->refs, sky);
146         env->refs = g_list_prepend(env->refs, hud);
147
148         /* Add background */
149         //GritsTile *background = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
150         //glGenTextures(1, &env->tex);
151         //background->data = &env->tex;
152         //grits_viewer_add(viewer, GRITS_OBJECT(background), GRITS_LEVEL_BACKGROUND, FALSE);
153         //env->refs = g_list_prepend(env->refs, background);
154
155         return env;
156 }
157
158
159 /****************
160  * GObject code *
161  ****************/
162 /* Plugin init */
163 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface);
164 G_DEFINE_TYPE_WITH_CODE(GritsPluginEnv, grits_plugin_env, G_TYPE_OBJECT,
165                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
166                         grits_plugin_env_plugin_init));
167 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface)
168 {
169         g_debug("GritsPluginEnv: plugin_init");
170         /* Add methods to the interface */
171 }
172 /* Class/Object init */
173 static void grits_plugin_env_init(GritsPluginEnv *env)
174 {
175         g_debug("GritsPluginEnv: init");
176         /* Set defaults */
177 }
178 static void grits_plugin_env_dispose(GObject *gobject)
179 {
180         g_debug("GritsPluginEnv: dispose");
181         GritsPluginEnv *env = GRITS_PLUGIN_ENV(gobject);
182         /* Drop references */
183         if (env->viewer) {
184                 for (GList *cur = env->refs; cur; cur = cur->next)
185                         grits_viewer_remove(env->viewer, cur->data);
186                 g_list_free(env->refs);
187                 g_object_unref(env->viewer);
188                 glDeleteTextures(1, &env->tex);
189                 env->viewer = NULL;
190         }
191         G_OBJECT_CLASS(grits_plugin_env_parent_class)->dispose(gobject);
192 }
193 static void grits_plugin_env_class_init(GritsPluginEnvClass *klass)
194 {
195         g_debug("GritsPluginEnv: class_init");
196         int argc = 1; char *argv[] = {"", NULL};
197         glutInit(&argc, argv);
198         GObjectClass *gobject_class = (GObjectClass*)klass;
199         gobject_class->dispose = grits_plugin_env_dispose;
200 }