2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
20 * @short_description: Environment plugin
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.
42 static void sky_expose(GritsCallback *sky, GritsOpenGL *opengl, gpointer _env)
44 GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
45 g_debug("GritsPluginEnv: expose_sky");
47 gdouble lat, lon, elev;
48 grits_viewer_get_location(env->viewer, &lat, &lon, &elev);
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);
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);
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
70 gdouble ar = sin(ang)*da; // top of quad fan "atomosphere"j
71 gdouble az = cos(ang)*da; //
73 gdouble gr = sin(ang)*dg; // bottom of quad fan "ground"
74 gdouble gz = cos(ang)*dg; //
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);
85 static void compass_draw_compass(gdouble scale)
87 gfloat thick = scale * 0.20;
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);
98 gdouble colors[][3] = {
104 gdouble points[][3] = {
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];
128 for (int j = 0; j < G_N_ELEMENTS(faces); j++) {
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);
135 glBegin(GL_TRIANGLES);
141 glRotatef(90, 0, 0, 1);
145 glEnable(GL_POLYGON_OFFSET_LINE);
146 glDisable(GL_LIGHTING);
147 glPolygonOffset(0, 0);
148 glColor4f(0, 0, 0, 0.25);
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]]);
155 glRotatef(90, 0, 0, 1);
159 static gboolean compass_draw_teapot(gdouble scale, GritsPluginEnv *env)
161 static int teatime = 0;
163 static int init, argc; char *argv[] = {"", NULL};
165 teatime = grits_prefs_get_boolean(env->prefs, "grits/teatime", NULL);
166 glutInit(&argc, argv);
168 g_message("teatime=%d", teatime);
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);
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);
190 static void compass_expose(GritsCallback *compass, GritsOpenGL *opengl, gpointer _env)
192 GritsPluginEnv *env = GRITS_PLUGIN_ENV(_env);
193 g_debug("GritsPluginEnv: compass_expose");
195 grits_viewer_get_rotation(env->viewer, &x, &y, &z);
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);
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);
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);
220 static gboolean compass_click(GritsCallback *compass, GdkEvent *evnet, GritsViewer *viewer)
222 grits_viewer_set_rotation(viewer, 0, 0, 0);
230 * grits_plugin_env_new:
231 * @viewer: the #GritsViewer to use for drawing
232 * @prefs: the #GritsPrefs for storing configurations
234 * Create a new instance of the environment plugin.
236 * Returns: the new #GritsPluginEnv
238 GritsPluginEnv *grits_plugin_env_new(GritsViewer *viewer, GritsPrefs *prefs)
240 g_debug("GritsPluginEnv: new");
241 GritsPluginEnv *env = g_object_new(GRITS_TYPE_PLUGIN_ENV, NULL);
242 env->viewer = g_object_ref(viewer);
243 env->prefs = g_object_ref(prefs);
246 GritsCallback *sky = grits_callback_new(sky_expose, env);
247 grits_viewer_add(viewer, GRITS_OBJECT(sky), GRITS_LEVEL_BACKGROUND, FALSE);
248 env->refs = g_list_prepend(env->refs, sky);
251 GritsCallback *compass = grits_callback_new(compass_expose, env);
252 grits_viewer_add(viewer, GRITS_OBJECT(compass), GRITS_LEVEL_HUD, FALSE);
253 g_signal_connect(compass, "clicked", G_CALLBACK(compass_click), viewer);
254 grits_object_set_cursor(GRITS_OBJECT(compass), GDK_CROSS);
255 env->refs = g_list_prepend(env->refs, compass);
258 //GritsTile *background = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
259 //glGenTextures(1, &env->tex);
260 //background->data = &env->tex;
261 //grits_viewer_add(viewer, GRITS_OBJECT(background), GRITS_LEVEL_BACKGROUND, FALSE);
262 //env->refs = g_list_prepend(env->refs, background);
272 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface);
273 G_DEFINE_TYPE_WITH_CODE(GritsPluginEnv, grits_plugin_env, G_TYPE_OBJECT,
274 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
275 grits_plugin_env_plugin_init));
276 static void grits_plugin_env_plugin_init(GritsPluginInterface *iface)
278 g_debug("GritsPluginEnv: plugin_init");
279 /* Add methods to the interface */
281 /* Class/Object init */
282 static void grits_plugin_env_init(GritsPluginEnv *env)
284 g_debug("GritsPluginEnv: init");
287 static void grits_plugin_env_dispose(GObject *gobject)
289 g_debug("GritsPluginEnv: dispose");
290 GritsPluginEnv *env = GRITS_PLUGIN_ENV(gobject);
291 /* Drop references */
293 for (GList *cur = env->refs; cur; cur = cur->next)
294 grits_viewer_remove(env->viewer, cur->data);
295 g_list_free(env->refs);
296 g_object_unref(env->viewer);
297 glDeleteTextures(1, &env->tex);
300 G_OBJECT_CLASS(grits_plugin_env_parent_class)->dispose(gobject);
302 static void grits_plugin_env_class_init(GritsPluginEnvClass *klass)
304 g_debug("GritsPluginEnv: class_init");
305 GObjectClass *gobject_class = (GObjectClass*)klass;
306 gobject_class->dispose = grits_plugin_env_dispose;