]> Pileus Git - grits/blobdiff - src/grits-opengl.c
Change level storage from GTree to GQueue
[grits] / src / grits-opengl.c
index f4523d5ea3f3b3fb984f74eec6757793e343ae1a..1aec412131f6e30d938963a2ff9901ba25adcafe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@
  * algorithm for updating surface mesh the planet. The only thing GritsOpenGL
  * can actually render on it's own is a wireframe of a sphere.
  *
- * GritsOpenGL relies on #GtkGlExt and requires (at least) OpenGL 2.0.
+ * GritsOpenGL requires (at least) OpenGL 2.0.
  */
 
 #include <config.h>
 #include <string.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
-#include <gtk/gtkgl.h>
-#include <GL/gl.h>
-#include <GL/glu.h>
 
 #include "grits-opengl.h"
 #include "grits-util.h"
+#include "gtkgl.h"
 #include "roam.h"
 
 // #define ROAM_DEBUG
 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
 /* http://www.opengl.org/wiki/Alpha_Blending */
 
+/* The unsorted/sroted GLists are blank head nodes,
+ * This way us we can remove objects from the level just by fixing up links
+ * I.e. we don't need to do a lookup to remove an object if we have its GList */
+struct RenderLevel {
+       gint  num;
+       GList unsorted;
+       GList sorted;
+};
+
 /***********
  * Helpers *
  ***********/
 static void _set_visuals(GritsOpenGL *opengl)
 {
+       double lat, lon, elev, rx, ry, rz;
+       grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
+       grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
+
+       /* Set projection and clipping planes */
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+
+       double width  = GTK_WIDGET(opengl)->allocation.width;
+       double height = GTK_WIDGET(opengl)->allocation.height;
+       double ang    = atan((height/2)/FOV_DIST)*2;
+       double atmos  = 100000;
+       double near   = MAX(elev*0.75 - atmos, 50); // View 100km of atmosphere
+       double far    = elev + 2*EARTH_R + atmos;   // on both sides of the earth
+
+       grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
+       glViewport(0, 0, width, height);
+       gluPerspective(rad2deg(ang), width/height, near, far);
+
+       /* Setup camera and lighting */
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
        /* Camera 1 */
-       double lat, lon, elev, rx, ry, rz;
-       grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
-       grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
        glRotatef(rx, 1, 0, 0);
        glRotatef(rz, 0, 0, 1);
 
@@ -116,32 +140,30 @@ static void _set_visuals(GritsOpenGL *opengl)
        g_mutex_unlock(opengl->sphere_lock);
 }
 
+static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho)
+{
+       GPtrArray *array = g_ptr_array_new();
+       for (GList *i = opengl->objects->head; i; i = i->next) {
+               struct RenderLevel *level = i->data;
+               if ((ortho == TRUE  && level->num <  GRITS_LEVEL_HUD) ||
+                   (ortho == FALSE && level->num >= GRITS_LEVEL_HUD))
+                       continue;
+               for (GList *j = level->unsorted.next; j; j = j->next)
+                       g_ptr_array_add(array, j->data);
+               for (GList *j = level->sorted.next;   j; j = j->next)
+                       g_ptr_array_add(array, j->data);
+       }
+       return array;
+}
 
 /*************
  * Callbacks *
  *************/
-/* The unsorted/sroted GLists are blank head nodes,
- * This way us we can remove objects from the level just by fixing up links
- * I.e. we don't need to do a lookup to remove an object if we have its GList */
-struct RenderLevel {
-       GList unsorted;
-       GList sorted;
-};
-
 static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
 {
        g_debug("GritsOpenGL: on_configure");
 
-       double width  = GTK_WIDGET(opengl)->allocation.width;
-       double height = GTK_WIDGET(opengl)->allocation.height;
-
-       /* Setup OpenGL Window */
-       glViewport(0, 0, width, height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       double ang = atan(height/FOV_DIST);
-       gluPerspective(rad2deg(ang)*2, width/height, 1000, 10*EARTH_R);
-
+       _set_visuals(opengl);
 #ifndef ROAM_DEBUG
        g_mutex_lock(opengl->sphere_lock);
        roam_sphere_update_errors(opengl->sphere);
@@ -151,39 +173,190 @@ static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpoi
        return FALSE;
 }
 
-static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
+static gint run_picking(GritsOpenGL *opengl, GdkEvent *event,
+               GPtrArray *objects, GritsObject **top)
 {
-       g_debug("GritsOpenGL: _draw_level - level=%-4d", (int)key);
-       GritsOpenGL *opengl = user_data;
-       struct RenderLevel *level = value;
+       /* Setup picking buffers */
+       guint buffer[100][4] = {};
+       glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
+       if (!opengl->pickmode)
+               glRenderMode(GL_SELECT);
+       glInitNames();
+
+       /* Render/pick objects */
+       for (guint i = 0; i < objects->len; i++) {
+               glPushName(i);
+               GritsObject *object = objects->pdata[i];
+               object->state.picked = FALSE;
+               grits_object_pick(object, opengl);
+               glPopName();
+       }
+
+       int hits = glRenderMode(GL_RENDER);
+
+       /* Process hits */
+       for (int i = 0; i < hits; i++) {
+               //g_debug("\tHit: %d",     i);
+               //g_debug("\t\tcount: %d", buffer[i][0]);
+               //g_debug("\t\tz1:    %f", (float)buffer[i][1]/0x7fffffff);
+               //g_debug("\t\tz2:    %f", (float)buffer[i][2]/0x7fffffff);
+               //g_debug("\t\tname:  %p", (gpointer)buffer[i][3]);
+               guint        index  = buffer[i][3];
+               GritsObject *object = objects->pdata[index];
+               object->state.picked = TRUE;
+               *top = object;
+       }
+
+       /* Notify objects of pointer movements */
+       for (guint i = 0; i < objects->len; i++) {
+               GritsObject *object = objects->pdata[i];
+               grits_object_set_pointer(object, event, object->state.picked);
+       }
+
+       return hits;
+}
+
+static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
+{
+       gdouble height = GTK_WIDGET(opengl)->allocation.height;
+       gdouble gl_x   = event->x;
+       gdouble gl_y   = height - event->y;
+       gdouble delta  = opengl->pickmode ? 200 : 2;
+
+       if (opengl->pickmode) {
+               gtk_gl_begin(GTK_WIDGET(opengl));
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       }
+
+       /* Save matricies */
+       gdouble projection[16];
+       gint    viewport[4]; // x=0,y=0,w,h
+       glGetDoublev(GL_PROJECTION_MATRIX, projection);
+       glGetIntegerv(GL_VIEWPORT, viewport);
+       glMatrixMode(GL_MODELVIEW);  glPushMatrix();
+       glMatrixMode(GL_PROJECTION); glPushMatrix();
+
+       g_mutex_lock(opengl->objects_lock);
+
+       GritsObject *top = NULL;
+       GPtrArray *ortho = _objects_to_array(opengl, TRUE);
+       GPtrArray *world = _objects_to_array(opengl, FALSE);
+
+       /* Run perspective picking */
+       glMatrixMode(GL_PROJECTION); glLoadIdentity();
+       gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
+       glMultMatrixd(projection);
+       gint world_hits = run_picking(opengl, (GdkEvent*)event, world, &top);
+
+       /* Run ortho picking */
+       glMatrixMode(GL_PROJECTION); glLoadIdentity();
+       gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
+       glMatrixMode(GL_MODELVIEW);  glLoadIdentity();
+       glOrtho(0, viewport[2], viewport[3], 0, 1000, -1000);
+       gint ortho_hits = run_picking(opengl, (GdkEvent*)event, ortho, &top);
+
+       /* Update cursor */
+       static GdkCursor *cursor = NULL;
+       static GdkWindow *window = NULL;
+       if (!window || !cursor) {
+               cursor = gdk_cursor_new(GDK_FLEUR);
+               window = gtk_widget_get_window(GTK_WIDGET(opengl));
+       }
+       GdkCursor *topcursor = top && top->cursor ? top->cursor : cursor;
+       gdk_window_set_cursor(window, topcursor);
+
+       g_debug("GritsOpenGL: on_motion_notify - hits=%d/%d,%d/%d ev=%.0lf,%.0lf",
+                       world_hits, world->len, ortho_hits, ortho->len, gl_x, gl_y);
+
+       g_ptr_array_free(world, TRUE);
+       g_ptr_array_free(ortho, TRUE);
+
+       g_mutex_unlock(opengl->objects_lock);
+
+
+       /* Test unproject */
+       //gdouble lat, lon, elev;
+       //grits_viewer_unproject(GRITS_VIEWER(opengl),
+       //              gl_x, gl_y, -1, &lat, &lon, &elev);
+
+       /* Cleanup */
+       glMatrixMode(GL_PROJECTION); glPopMatrix();
+       glMatrixMode(GL_MODELVIEW);  glPopMatrix();
+
+       if (opengl->pickmode)
+               gtk_gl_end(GTK_WIDGET(opengl));
+
+       return FALSE;
+}
+
+static void _draw_level(gpointer _level, gpointer _opengl)
+{
+       GritsOpenGL *opengl = _opengl;
+       struct RenderLevel *level = _level;
+
+       g_debug("GritsOpenGL: _draw_level - level=%-4d", level->num);
        int nsorted = 0, nunsorted = 0;
        GList *cur = NULL;
 
-       /* Draw opaque objects without sorting */
-       glDepthMask(TRUE);
-       glClear(GL_DEPTH_BUFFER_BIT);
+       /* Configure individual levels */
+       if (level->num < GRITS_LEVEL_WORLD) {
+               /* Disable depth for background levels */
+               glDepthMask(FALSE);
+               glDisable(GL_ALPHA_TEST);
+       } else if (level->num < GRITS_LEVEL_OVERLAY) {
+               /* Enable depth and alpha for world levels */
+               glEnable(GL_ALPHA_TEST);
+               glAlphaFunc(GL_GREATER, 0.1);
+               glDepthMask(TRUE);
+       } else {
+               /* Disable depth for Overlay/HUD levels */
+               // This causes rendering glitches not sure why..
+               //glDepthMask(FALSE);
+       }
+
+       /* Start ortho */
+       if (level->num >= GRITS_LEVEL_HUD) {
+               glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
+               glMatrixMode(GL_MODELVIEW);  glPushMatrix(); glLoadIdentity();
+               gint win_width  = GTK_WIDGET(opengl)->allocation.width;
+               gint win_height = GTK_WIDGET(opengl)->allocation.height;
+               glOrtho(0, win_width, win_height, 0, 1000, -1000);
+       }
+
+       /* Draw unsorted objects without depth testing,
+        * these are polygons, etc, rather than physical objects */
+       glDisable(GL_DEPTH_TEST);
        for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
                grits_object_draw(GRITS_OBJECT(cur->data), opengl);
 
-       /* Freeze depth buffer and draw transparent objects sorted */
-       /* TODO: sorting */
-       //glDepthMask(FALSE);
-       glAlphaFunc(GL_GREATER, 0.1);
+       /* Draw sorted objects using depth testing
+        * These are things that are actually part of the world */
+       glEnable(GL_DEPTH_TEST);
        for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
                grits_object_draw(GRITS_OBJECT(cur->data), opengl);
 
+       /* End ortho */
+       if (level->num >= GRITS_LEVEL_HUD) {
+               glMatrixMode(GL_PROJECTION); glPopMatrix();
+               glMatrixMode(GL_MODELVIEW);  glPopMatrix();
+       }
+
        /* TODO: Prune empty levels */
 
        g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
                        nunsorted, nsorted);
-       return FALSE;
 }
 
 static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
 {
        g_debug("GritsOpenGL: on_expose - begin");
 
-       glClear(GL_COLOR_BUFFER_BIT);
+       if (opengl->pickmode)
+               return on_motion_notify(opengl, (GdkEventMotion*)event, NULL);
+
+       gtk_gl_begin(GTK_WIDGET(opengl));
+
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        _set_visuals(opengl);
 #ifdef ROAM_DEBUG
@@ -196,17 +369,13 @@ static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _
        //roam_sphere_draw_normals(opengl->sphere);
 #else
        g_mutex_lock(opengl->objects_lock);
-       g_tree_foreach(opengl->objects, _draw_level, opengl);
-       if (opengl->wireframe) {
-               glClear(GL_DEPTH_BUFFER_BIT);
+       if (opengl->wireframe)
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-               roam_sphere_draw(opengl->sphere);
-       }
+       g_queue_foreach(opengl->objects, _draw_level, opengl);
        g_mutex_unlock(opengl->objects_lock);
 #endif
 
-       GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
-       gdk_gl_drawable_swap_buffers(gldrawable);
+       gtk_gl_end(GTK_WIDGET(opengl));
 
        g_debug("GritsOpenGL: on_expose - end\n");
        return FALSE;
@@ -223,6 +392,10 @@ static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _
                opengl->wireframe = !opengl->wireframe;
                gtk_widget_queue_draw(GTK_WIDGET(opengl));
        }
+       if (kv == GDK_p) {
+               opengl->pickmode = !opengl->pickmode;
+               gtk_widget_queue_draw(GTK_WIDGET(opengl));
+       }
 #ifdef ROAM_DEBUG
        else if (kv == GDK_n) roam_sphere_split_one(opengl->sphere);
        else if (kv == GDK_p) roam_sphere_merge_one(opengl->sphere);
@@ -233,6 +406,20 @@ static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _
        return FALSE;
 }
 
+static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
+{
+       for (GList *i = opengl->objects->tail; i; i = i->prev) {
+               struct RenderLevel *level = i->data;
+               for (GList *j = level->unsorted.next; j; j = j->next)
+                       if (grits_object_event(j->data, event))
+                               return TRUE;
+               for (GList *j = level->sorted.next;   j; j = j->next)
+                       if (grits_object_event(j->data, event))
+                               return TRUE;
+       }
+       return FALSE;
+}
+
 static gboolean _update_errors_cb(gpointer _opengl)
 {
        GritsOpenGL *opengl = _opengl;
@@ -270,12 +457,7 @@ static gboolean on_idle(GritsOpenGL *opengl)
 static void on_realize(GritsOpenGL *opengl, gpointer _)
 {
        g_debug("GritsOpenGL: on_realize");
-
-       /* Start OpenGL */
-       GdkGLContext   *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(opengl));
-       GdkGLDrawable  *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
-       if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
-               g_assert_not_reached();
+       gtk_gl_begin(GTK_WIDGET(opengl));
 
        /* Connect signals and idle functions now that opengl is fully initialized */
        gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
@@ -287,6 +469,13 @@ static void on_realize(GritsOpenGL *opengl, gpointer _)
        g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
        g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
 
+       g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
+       g_signal_connect_after(opengl, "key-press-event",      G_CALLBACK(on_chained_event), NULL);
+       g_signal_connect_after(opengl, "key-release-event",    G_CALLBACK(on_chained_event), NULL);
+       g_signal_connect_after(opengl, "button-press-event",   G_CALLBACK(on_chained_event), NULL);
+       g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
+       g_signal_connect_after(opengl, "motion-notify-event",  G_CALLBACK(on_chained_event), NULL);
+
 #ifndef ROAM_DEBUG
        opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33,  (GSourceFunc)on_idle, opengl, NULL);
        opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
@@ -340,6 +529,32 @@ static void grits_opengl_project(GritsViewer *_opengl,
                px, py, pz);
 }
 
+static void grits_opengl_unproject(GritsViewer *_opengl,
+               gdouble px, gdouble py, gdouble pz,
+               gdouble *lat, gdouble *lon, gdouble *elev)
+{
+       GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
+       if (!opengl->sphere->view)
+               return;
+       gdouble x, y, z;
+       if (pz < 0) {
+               gfloat tmp = 0;
+               glReadPixels(px, py, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tmp);
+               pz = tmp;
+       }
+       gluUnProject(px, py, pz,
+               opengl->sphere->view->model,
+               opengl->sphere->view->proj,
+               opengl->sphere->view->view,
+               &x, &y, &z);
+       xyz2lle(x, y, z, lat, lon, elev);
+       //g_message("GritsOpenGL: unproject - "
+       //              "%4.0lf,%4.0lf,(%5.3lf) -> "
+       //              "%8.0lf,%8.0lf,%8.0lf -> "
+       //              "%6.2lf,%7.2lf,%4.0lf",
+       //      px, py, pz, x, y, z, *lat, *lon, *elev);
+}
+
 static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
                RoamHeightFunc height_func, gpointer user_data, gboolean update)
 {
@@ -385,16 +600,45 @@ static void grits_opengl_clear_height_func(GritsViewer *_opengl)
                _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
 }
 
+static gint _objects_find(gconstpointer a, gconstpointer b)
+{
+       const struct RenderLevel *level = a;
+       const gint *key = b;
+       return level->num == *key ? 0 : 1;
+}
+
+static gint _objects_sort(gconstpointer _a, gconstpointer _b, gpointer _)
+{
+       const struct RenderLevel *a = _a;
+       const struct RenderLevel *b = _b;
+       return a->num < b->num ? -1 :
+              a->num > b->num ?  1 : 0;
+}
+
+static void _objects_free(gpointer value, gpointer _)
+{
+       struct RenderLevel *level = value;
+       if (level->sorted.next)
+               g_list_free(level->sorted.next);
+       if (level->unsorted.next)
+               g_list_free(level->unsorted.next);
+       g_free(level);
+}
+
 static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
-               gint key, gboolean sort)
+               gint num, gboolean sort)
 {
        g_assert(GRITS_IS_OPENGL(_opengl));
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
        g_mutex_lock(opengl->objects_lock);
-       struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)key);
-       if (!level) {
+       struct RenderLevel *level = NULL;
+       GList *tmp = g_queue_find_custom(opengl->objects, &num, _objects_find);
+       if (tmp) {
+               level = tmp->data;
+       } else {
                level = g_new0(struct RenderLevel, 1);
-               g_tree_insert(opengl->objects, (gpointer)key, level);
+               level->num = num;
+               g_queue_insert_sorted(opengl->objects, level, _objects_sort, NULL);
        }
        GList *list = sort ? &level->sorted : &level->unsorted;
        /* Put the link in the list */
@@ -409,64 +653,38 @@ static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
        return link;
 }
 
-static GritsObject *grits_opengl_remove(GritsViewer *_opengl, gpointer _link)
+static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
 {
        g_assert(GRITS_IS_OPENGL(_opengl));
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
+       GList *link = object->ref;
        g_mutex_lock(opengl->objects_lock);
-       GList *link = _link;
-       GritsObject *object = link->data;
        /* Just unlink and free it, link->prev is assured */
        link->prev->next = link->next;
        if (link->next)
                link->next->prev = link->prev;
+       g_mutex_unlock(opengl->objects_lock);
+       object->ref    = NULL;
+       object->viewer = NULL;
        g_free(link);
        g_object_unref(object);
-       g_mutex_unlock(opengl->objects_lock);
        return object;
 }
 
 /****************
  * GObject code *
  ****************/
-static int _objects_cmp(gconstpointer _a, gconstpointer _b, gpointer _)
-{
-       gint a = (int)_a, b = (int)_b;
-       return a < b ? -1 :
-              a > b ?  1 : 0;
-}
-static void _objects_free(gpointer value)
-{
-       struct RenderLevel *level = value;
-       if (level->sorted.next)
-               g_list_free(level->sorted.next);
-       if (level->unsorted.next)
-               g_list_free(level->unsorted.next);
-       g_free(level);
-}
-
 G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
 static void grits_opengl_init(GritsOpenGL *opengl)
 {
        g_debug("GritsOpenGL: init");
-       opengl->objects      = g_tree_new_full(_objects_cmp, NULL, NULL, _objects_free);
+       opengl->objects      = g_queue_new();
        opengl->objects_lock = g_mutex_new();
        opengl->sphere       = roam_sphere_new(opengl);
        opengl->sphere_lock  = g_mutex_new();
-
-       /* Set OpenGL before "realize" */
-       GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
-                       GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
-                       GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
-       if (!glconfig)
-               g_error("Failed to create glconfig");
-       if (!gtk_widget_set_gl_capability(GTK_WIDGET(opengl),
-                               glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
-               g_error("GL lacks required capabilities");
-       g_object_unref(glconfig);
-
-       /* Finish OpenGL init after it's realized */
-       g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
+       gtk_gl_enable(GTK_WIDGET(opengl));
+       gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
+       g_signal_connect(opengl, "map", G_CALLBACK(on_realize), NULL);
 }
 static void grits_opengl_dispose(GObject *_opengl)
 {
@@ -491,7 +709,8 @@ static void grits_opengl_finalize(GObject *_opengl)
        g_debug("GritsOpenGL: finalize");
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
        roam_sphere_free(opengl->sphere);
-       g_tree_destroy(opengl->objects);
+       g_queue_foreach(opengl->objects, _objects_free, NULL);
+       g_queue_free(opengl->objects);
        g_mutex_free(opengl->objects_lock);
        g_mutex_free(opengl->sphere_lock);
        G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
@@ -506,6 +725,7 @@ static void grits_opengl_class_init(GritsOpenGLClass *klass)
        GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
        viewer_class->center_position   = grits_opengl_center_position;
        viewer_class->project           = grits_opengl_project;
+       viewer_class->unproject         = grits_opengl_unproject;
        viewer_class->clear_height_func = grits_opengl_clear_height_func;
        viewer_class->set_height_func   = grits_opengl_set_height_func;
        viewer_class->add               = grits_opengl_add;