]> Pileus Git - grits/blobdiff - src/grits-opengl.c
Add support for GTK 3
[grits] / src / grits-opengl.c
index 277c11e47ac9092e17bf9a8de0d80529329fc310..2bd00822cea22ace1b4776aba045d3cf6ac2f909 100644 (file)
@@ -32,8 +32,6 @@
 #include <string.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
-#include <GL/gl.h>
-#include <GL/glu.h>
 
 #include "grits-opengl.h"
 #include "grits-util.h"
@@ -42,6 +40,8 @@
 
 // #define ROAM_DEBUG
 
+#define OVERLAY_SLICE 0.01
+
 /* Tessellation, "finding intersecting triangles" */
 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
 /* http://www.opengl.org/wiki/Alpha_Blending */
@@ -50,6 +50,7 @@
  * 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;
 };
@@ -57,18 +58,70 @@ struct RenderLevel {
 /***********
  * Helpers *
  ***********/
-static void _set_visuals(GritsOpenGL *opengl)
+static void _set_projection(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();
+
+       GtkAllocation alloc;
+       gtk_widget_get_allocation(GTK_WIDGET(opengl), &alloc);
+       double width  = alloc.width;
+       double height = alloc.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
+
+       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);
 
+       /* Lighting */
+       float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
+       glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+       /* Camera 2 */
+       glTranslatef(0, 0, -elev2rad(elev));
+       glRotatef(lat, 1, 0, 0);
+       glRotatef(-lon, 0, 1, 0);
+
+       /* Update roam view */
+       g_mutex_lock(&opengl->sphere_lock);
+       roam_sphere_update_view(opengl->sphere);
+       g_mutex_unlock(&opengl->sphere_lock);
+}
+
+static void _set_settings(GritsOpenGL *opengl)
+{
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glEnable(GL_BLEND);
+       glDisable(GL_ALPHA_TEST);
+
+       glEnable(GL_LIGHT0);
+       glEnable(GL_LIGHTING);
+
+       glEnable(GL_LINE_SMOOTH);
+
+       glDisable(GL_TEXTURE_2D);
+       glDisable(GL_COLOR_MATERIAL);
+
+       if (opengl->wireframe)
+               glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       else
+               glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+
        /* Lighting */
 #ifdef ROAM_DEBUG
        float light_ambient[]  = {0.7f, 0.7f, 0.7f, 1.0f};
@@ -77,13 +130,10 @@ static void _set_visuals(GritsOpenGL *opengl)
        float light_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
        float light_diffuse[]  = {0.8f, 0.8f, 0.8f, 1.0f};
 #endif
-       float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
-       glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-       glEnable(GL_LIGHT0);
-       glEnable(GL_LIGHTING);
 
+       /* Materials */
        float material_ambient[]  = {1.0, 1.0, 1.0, 1.0};
        float material_diffuse[]  = {1.0, 1.0, 1.0, 1.0};
        float material_specular[] = {0.0, 0.0, 0.0, 1.0};
@@ -92,20 +142,11 @@ static void _set_visuals(GritsOpenGL *opengl)
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,  material_diffuse);
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
        glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
-       glDisable(GL_TEXTURE_2D);
-       glDisable(GL_COLOR_MATERIAL);
-
-       /* Camera 2 */
-       glTranslatef(0, 0, -elev2rad(elev));
-       glRotatef(lat, 1, 0, 0);
-       glRotatef(-lon, 0, 1, 0);
 
-       glDisable(GL_ALPHA_TEST);
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glEnable(GL_BLEND);
-
-#ifndef ROAM_DEBUG
+#ifdef ROAM_DEBUG
+       glColor4f(1.0, 1.0, 1.0, 1.0);
+       glLineWidth(2);
+#else
        glCullFace(GL_BACK);
        glEnable(GL_CULL_FACE);
 
@@ -113,114 +154,244 @@ static void _set_visuals(GritsOpenGL *opengl)
        glDepthFunc(GL_LEQUAL);
        glEnable(GL_DEPTH_TEST);
 #endif
-
-       glEnable(GL_LINE_SMOOTH);
-
-       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-       //glShadeModel(GL_FLAT);
-
-       g_mutex_lock(opengl->sphere_lock);
-       roam_sphere_update_view(opengl->sphere);
-       g_mutex_unlock(opengl->sphere_lock);
 }
 
-static gboolean _foreach_object_cb(gpointer key, gpointer value, gpointer pointers)
+static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho)
 {
-       struct RenderLevel *level = value;
-       GFunc    user_func = ((gpointer*)pointers)[0];
-       gpointer user_data = ((gpointer*)pointers)[1];
-       for (GList *cur = level->unsorted.next; cur; cur = cur->next)
-               user_func(cur->data, user_data);
-       for (GList *cur = level->sorted.next;   cur; cur = cur->next)
-               user_func(cur->data, user_data);
-       return FALSE;
-}
-
-static void _foreach_object(GritsOpenGL *opengl, GFunc func, gpointer user_data)
-{
-       gpointer pointers[2] = {func, user_data};
-       g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
+       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 *
  *************/
-static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
+
+static gint run_picking(GritsOpenGL *opengl, GdkEvent *event,
+               GPtrArray *objects, GritsObject **top)
 {
-       g_debug("GritsOpenGL: on_configure");
+       /* Setup picking buffers */
+       guint buffer[100][4] = {};
+       glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
+       if (!opengl->pickmode)
+               glRenderMode(GL_SELECT);
+       glInitNames();
 
-       double width  = GTK_WIDGET(opengl)->allocation.width;
-       double height = GTK_WIDGET(opengl)->allocation.height;
+       /* 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();
+       }
 
-       /* Setup OpenGL Window */
-       glViewport(0, 0, width, height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       double ang = atan(height/FOV_DIST);
-       gluPerspective(rad2deg(ang)*2, width/height, 10, 100*EARTH_R);
+       int hits = glRenderMode(GL_RENDER);
 
-#ifndef ROAM_DEBUG
-       g_mutex_lock(opengl->sphere_lock);
-       roam_sphere_update_errors(opengl->sphere);
-       g_mutex_unlock(opengl->sphere_lock);
-#endif
+       /* 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 run_mouse_move(GritsOpenGL *opengl, GdkEventMotion *event)
+{
+       GtkAllocation alloc;
+       gtk_widget_get_allocation(GTK_WIDGET(opengl), &alloc);
+
+       gdouble gl_x   = event->x;
+       gdouble gl_y   = alloc.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: run_mouse_move - 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 gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
+static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
 {
-       g_debug("GritsOpenGL: _draw_level - level=%-4d", (int)key);
-       GritsOpenGL *opengl = user_data;
-       struct RenderLevel *level = value;
+       opengl->mouse_queue = *event;
+       grits_viewer_queue_draw(GRITS_VIEWER(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);
+               glDepthRange(OVERLAY_SLICE, 1);
+       } else {
+               /* Draw overlay in front of world */
+               glDepthRange(0, OVERLAY_SLICE);
+       }
+
+       /* Start ortho */
+       if (level->num >= GRITS_LEVEL_HUD) {
+               GtkAllocation alloc;
+               gtk_widget_get_allocation(GTK_WIDGET(opengl), &alloc);
+               glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
+               glMatrixMode(GL_MODELVIEW);  glPushMatrix(); glLoadIdentity();
+               glOrtho(0, alloc.width, alloc.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();
+       }
+
+       /* Leave depth buffer write enabled */
+       glDepthMask(TRUE);
+
        /* 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 _)
+static gboolean on_expose(GritsOpenGL *opengl, gpointer data, gpointer _)
 {
        g_debug("GritsOpenGL: on_expose - begin");
 
-       glClear(GL_COLOR_BUFFER_BIT);
+       if (opengl->pickmode)
+               return run_mouse_move(opengl, &(GdkEventMotion){});
+
+       if (opengl->mouse_queue.type != GDK_NOTHING) {
+               run_mouse_move(opengl, &opengl->mouse_queue);
+               opengl->mouse_queue.type = GDK_NOTHING;
+       }
+
+       gtk_gl_begin(GTK_WIDGET(opengl));
+
+       _set_settings(opengl);
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+#ifndef ROAM_DEBUG
+       g_mutex_lock(&opengl->sphere_lock);
+       roam_sphere_update_errors(opengl->sphere);
+       roam_sphere_split_merge(opengl->sphere);
+       g_mutex_unlock(&opengl->sphere_lock);
+#endif
 
-       _set_visuals(opengl);
 #ifdef ROAM_DEBUG
-       glColor4f(1.0, 1.0, 1.0, 1.0);
-       glLineWidth(2);
-       glDisable(GL_TEXTURE_2D);
-       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        roam_sphere_draw(opengl->sphere);
+       roam_sphere_draw_normals(opengl->sphere);
        (void)_draw_level;
-       //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);
-               glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-               roam_sphere_draw(opengl->sphere);
-               g_tree_foreach(opengl->objects, _draw_level, opengl);
-       }
-       g_mutex_unlock(opengl->objects_lock);
+       g_mutex_lock(&opengl->objects_lock);
+       g_queue_foreach(opengl->objects, _draw_level, opengl);
+       g_mutex_unlock(&opengl->objects_lock);
 #endif
 
        gtk_gl_end(GTK_WIDGET(opengl));
@@ -229,115 +400,44 @@ static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _
        return FALSE;
 }
 
-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;
-
-       /* Configure view */
-       gint viewport[4];
-       gdouble projection[16];
-       glGetIntegerv(GL_VIEWPORT, viewport);
-       glGetDoublev(GL_PROJECTION_MATRIX, projection);
-
-       glMatrixMode(GL_PROJECTION);
-       glPushMatrix();
-       glLoadIdentity();
-       gluPickMatrix(gl_x, gl_y, 2, 2, viewport);
-       glMultMatrixd(projection);
-
-       /* Prepare for picking */
-       guint buffer[100][4] = {};
-       glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
-       glRenderMode(GL_SELECT);
-       glInitNames();
-
-       /* Run picking */
-       g_mutex_lock(opengl->objects_lock);
-       _foreach_object(opengl, (GFunc)grits_object_pick_begin, opengl);
-       int hits = glRenderMode(GL_RENDER);
-       g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf",
-                       hits, gl_x, gl_y);
-       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]);
-               GritsObject *object = GRITS_OBJECT(buffer[i][3]);
-               grits_object_pick_pointer(object, gl_x, gl_y);
-       }
-       _foreach_object(opengl, (GFunc)grits_object_pick_end, NULL);
-       g_mutex_unlock(opengl->objects_lock);
-
-       /* Cleanup */
-       glMatrixMode(GL_PROJECTION);
-       glPopMatrix();
-
-       return FALSE;
-}
-
 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
 {
        g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
-                       event->keyval, event->state, GDK_plus);
+                       event->keyval, event->state, GDK_KEY_plus);
 
        guint kv = event->keyval;
        /* Testing */
-       if (kv == GDK_w) {
+       if (kv == GDK_KEY_w) {
                opengl->wireframe = !opengl->wireframe;
-               gtk_widget_queue_draw(GTK_WIDGET(opengl));
+               grits_viewer_queue_draw(GRITS_VIEWER(opengl));
+       }
+       if (kv == GDK_KEY_p) {
+               opengl->pickmode = !opengl->pickmode;
+               grits_viewer_queue_draw(GRITS_VIEWER(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);
-       else if (kv == GDK_r) roam_sphere_split_merge(opengl->sphere);
-       else if (kv == GDK_u) roam_sphere_update_errors(opengl->sphere);
-       gtk_widget_queue_draw(GTK_WIDGET(opengl));
+       else if (kv == GDK_KEY_n) roam_sphere_split_one(opengl->sphere);
+       else if (kv == GDK_KEY_p) roam_sphere_merge_one(opengl->sphere);
+       else if (kv == GDK_KEY_r) roam_sphere_split_merge(opengl->sphere);
+       else if (kv == GDK_KEY_u) roam_sphere_update_errors(opengl->sphere);
+       grits_viewer_queue_draw(GRITS_VIEWER(opengl));
 #endif
        return FALSE;
 }
 
 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
 {
-       _foreach_object(opengl, (GFunc)grits_object_event, event);
-       return FALSE;
-}
-
-static gboolean _update_errors_cb(gpointer _opengl)
-{
-       GritsOpenGL *opengl = _opengl;
-       g_mutex_lock(opengl->sphere_lock);
-       roam_sphere_update_errors(opengl->sphere);
-       g_mutex_unlock(opengl->sphere_lock);
-       opengl->ue_source = 0;
+       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 void on_view_changed(GritsOpenGL *opengl,
-               gdouble _1, gdouble _2, gdouble _3)
-{
-       g_debug("GritsOpenGL: on_view_changed");
-       _set_visuals(opengl);
-#ifndef ROAM_DEBUG
-       if (!opengl->ue_source)
-               opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30,
-                               _update_errors_cb, opengl, NULL);
-       //roam_sphere_update_errors(opengl->sphere);
-#else
-       (void)_update_errors_cb;
-#endif
-}
-
-static gboolean on_idle(GritsOpenGL *opengl)
-{
-       //g_debug("GritsOpenGL: on_idle");
-       g_mutex_lock(opengl->sphere_lock);
-       if (roam_sphere_split_merge(opengl->sphere))
-               gtk_widget_queue_draw(GTK_WIDGET(opengl));
-       g_mutex_unlock(opengl->sphere_lock);
-       return TRUE;
-}
 
 static void on_realize(GritsOpenGL *opengl, gpointer _)
 {
@@ -346,13 +446,17 @@ static void on_realize(GritsOpenGL *opengl, gpointer _)
 
        /* Connect signals and idle functions now that opengl is fully initialized */
        gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
-       g_signal_connect(opengl, "configure-event",  G_CALLBACK(on_configure),    NULL);
+       g_signal_connect(opengl, "configure-event",  G_CALLBACK(_set_projection), NULL);
+#if GTK_CHECK_VERSION(3,0,0)
+       g_signal_connect(opengl, "draw",             G_CALLBACK(on_expose),       NULL);
+#else
        g_signal_connect(opengl, "expose-event",     G_CALLBACK(on_expose),       NULL);
+#endif
 
        g_signal_connect(opengl, "key-press-event",  G_CALLBACK(on_key_press),    NULL);
 
-       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, "location-changed", G_CALLBACK(_set_projection), NULL);
+       g_signal_connect(opengl, "rotation-changed", G_CALLBACK(_set_projection), 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);
@@ -361,14 +465,6 @@ static void on_realize(GritsOpenGL *opengl, gpointer _)
        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);
-#else
-       (void)on_idle;
-       (void)_update_errors_cb;
-#endif
-
        /* Re-queue resize incase configure was triggered before realize */
        gtk_widget_queue_resize(GTK_WIDGET(opengl));
 }
@@ -414,12 +510,39 @@ 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;
+       }
+       pz = (pz-OVERLAY_SLICE) * (1.0/(1-OVERLAY_SLICE));
+       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)
 {
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
        /* TODO: get points? */
-       g_mutex_lock(opengl->sphere_lock);
+       g_mutex_lock(&opengl->sphere_lock);
        GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
                        bounds->n, bounds->s, bounds->e, bounds->w);
        for (GList *cur = triangles; cur; cur = cur->next) {
@@ -435,7 +558,7 @@ static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *boun
                }
        }
        g_list_free(triangles);
-       g_mutex_unlock(opengl->sphere_lock);
+       g_mutex_unlock(&opengl->sphere_lock);
 }
 
 static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
@@ -459,16 +582,45 @@ static void grits_opengl_clear_height_func(GritsViewer *_opengl)
                _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
 }
 
-static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
-               gint key, gboolean sort)
+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_full(level->sorted.next, g_object_unref);
+       if (level->unsorted.next)
+               g_list_free_full(level->unsorted.next, g_object_unref);
+       g_free(level);
+}
+
+static void grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
+               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) {
+       g_mutex_lock(&opengl->objects_lock);
+       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 */
@@ -479,73 +631,55 @@ static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
        if (list->next)
                list->next->prev = link;
        list->next = link;
-       g_mutex_unlock(opengl->objects_lock);
-       return link;
+       object->ref = link;
+       g_object_ref(object);
+       g_mutex_unlock(&opengl->objects_lock);
 }
 
-static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
+void grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
 {
        g_assert(GRITS_IS_OPENGL(_opengl));
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
+       if (!object->ref)
+               return;
+       g_mutex_lock(&opengl->objects_lock);
        GList *link = object->ref;
-       g_mutex_lock(opengl->objects_lock);
        /* 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);
+       object->ref = NULL;
        g_object_unref(object);
-       return object;
+       g_mutex_unlock(&opengl->objects_lock);
 }
 
 /****************
  * 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_lock = g_mutex_new();
-       opengl->sphere       = roam_sphere_new(opengl);
-       opengl->sphere_lock  = g_mutex_new();
+       opengl->objects = g_queue_new();
+       opengl->sphere  = roam_sphere_new(opengl);
+       g_mutex_init(&opengl->objects_lock);
+       g_mutex_init(&opengl->sphere_lock);
        gtk_gl_enable(GTK_WIDGET(opengl));
-       g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
+       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)
 {
        g_debug("GritsOpenGL: dispose");
        GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
-       if (opengl->sm_source[0]) {
-               g_source_remove(opengl->sm_source[0]);
-               opengl->sm_source[0] = 0;
-       }
-       if (opengl->sm_source[1]) {
-               g_source_remove(opengl->sm_source[1]);
-               opengl->sm_source[1] = 0;
-       }
-       if (opengl->ue_source) {
-               g_source_remove(opengl->ue_source);
-               opengl->ue_source = 0;
+       if (opengl->objects) {
+               GQueue *objects = opengl->objects;;
+               opengl->objects = NULL;
+               g_mutex_lock(&opengl->objects_lock);
+               g_queue_foreach(objects, _objects_free, NULL);
+               g_queue_free(objects);
+               g_mutex_unlock(&opengl->objects_lock);
        }
        G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
 }
@@ -554,9 +688,9 @@ 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_mutex_free(opengl->objects_lock);
-       g_mutex_free(opengl->sphere_lock);
+       g_mutex_clear(&opengl->objects_lock);
+       g_mutex_clear(&opengl->sphere_lock);
+       gtk_gl_disable(GTK_WIDGET(opengl));
        G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
 }
 static void grits_opengl_class_init(GritsOpenGLClass *klass)
@@ -569,6 +703,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;