Add support for picking on orthographic projections
authorAndy Spencer <andy753421@gmail.com>
Sat, 14 Apr 2012 01:01:29 +0000 (01:01 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 14 Apr 2012 01:01:29 +0000 (01:01 +0000)
src/grits-opengl.c
src/grits-opengl.h
src/grits-test.c
src/objects/grits-marker.c
src/plugins/test.c

index 0b4100a..4da0729 100644 (file)
@@ -139,33 +139,43 @@ static void _set_visuals(GritsOpenGL *opengl)
        g_mutex_unlock(opengl->sphere_lock);
 }
 
+typedef void (*GritsLevelFunc)(GritsObject *obj, gpointer user_data, gint level, gboolean sorted);
+
 static gboolean _foreach_object_cb(gpointer key, gpointer value, gpointer pointers)
 {
        struct RenderLevel *level = value;
-       GFunc    user_func = ((gpointer*)pointers)[0];
-       gpointer user_data = ((gpointer*)pointers)[1];
+       GritsLevelFunc 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);
+               user_func(cur->data, user_data, (gint)key, FALSE);
        for (GList *cur = level->sorted.next;   cur; cur = cur->next)
-               user_func(cur->data, user_data);
+               user_func(cur->data, user_data, (gint)key, TRUE);
        return FALSE;
 }
 
-static void _foreach_object(GritsOpenGL *opengl, GFunc func, gpointer user_data)
+static void _foreach_object(GritsOpenGL *opengl, GritsLevelFunc func, gpointer user_data)
 {
        gpointer pointers[2] = {func, user_data};
        g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
 }
 
-static void _add_object(GritsObject *object, GPtrArray *array)
+static void _add_object_world(GritsObject *object, GPtrArray *array, gint level, gboolean sorted)
+{
+       if (level < GRITS_LEVEL_HUD)
+               g_ptr_array_add(array, object);
+}
+
+static void _add_object_ortho(GritsObject *object, GPtrArray *array, gint level, gboolean sorted)
 {
-       g_ptr_array_add(array, object);
+       if (level >= GRITS_LEVEL_HUD)
+               g_ptr_array_add(array, object);
 }
 
-static GPtrArray *_objects_to_array(GritsOpenGL *opengl)
+static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho)
 {
        GPtrArray *array = g_ptr_array_new();
-       _foreach_object(opengl, (GFunc)_add_object, array);
+       GritsLevelFunc func = (GritsLevelFunc)(ortho ? _add_object_ortho : _add_object_world);
+       _foreach_object(opengl, func, array);
        return array;
 }
 
@@ -186,6 +196,109 @@ static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpoi
        return FALSE;
 }
 
+static gint run_picking(GritsOpenGL *opengl, GPtrArray *objects)
+{
+       /* 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;
+       }
+
+       /* Notify objects of pointer movements */
+       for (guint i = 0; i < objects->len; i++) {
+               GritsObject *object = objects->pdata[i];
+               grits_object_set_pointer(object, 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);
+
+       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, world);
+
+       /* 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, ortho);
+
+       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 gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
 {
        GritsOpenGL *opengl = user_data;
@@ -212,6 +325,15 @@ static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
                //glDepthMask(FALSE);
        }
 
+       /* Start ortho */
+       if (lnum >= 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);
@@ -224,6 +346,12 @@ static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
        for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
                grits_object_draw(GRITS_OBJECT(cur->data), opengl);
 
+       /* End ortho */
+       if (lnum >= GRITS_LEVEL_HUD) {
+               glMatrixMode(GL_PROJECTION); glPopMatrix();
+               glMatrixMode(GL_MODELVIEW);  glPopMatrix();
+       }
+
        /* TODO: Prune empty levels */
 
        g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
@@ -235,6 +363,9 @@ static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _
 {
        g_debug("GritsOpenGL: on_expose - begin");
 
+       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);
@@ -262,72 +393,6 @@ 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);
-       GPtrArray *objects = _objects_to_array(opengl);
-       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);
-       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]);
-               guint        index  = buffer[i][3];
-               GritsObject *object = objects->pdata[index];
-               object->state.picked = TRUE;
-       }
-       for (guint i = 0; i < objects->len; i++) {
-               GritsObject *object = objects->pdata[i];
-               grits_object_set_pointer(object, object->state.picked);
-       }
-       g_ptr_array_free(objects, 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();
-
-       return FALSE;
-}
-
 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
 {
        g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
@@ -339,6 +404,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);
@@ -351,7 +420,7 @@ static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _
 
 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
 {
-       _foreach_object(opengl, (GFunc)grits_object_event, event);
+       _foreach_object(opengl, (GritsLevelFunc)grits_object_event, event);
        return FALSE;
 }
 
index 0379509..f1201df 100644 (file)
@@ -47,6 +47,7 @@ struct _GritsOpenGL {
 
        /* for testing */
        gboolean    wireframe;
+       gboolean    pickmode;
 };
 
 struct _GritsOpenGLClass {
index 54251ba..07edaf7 100644 (file)
@@ -95,7 +95,7 @@ int main(int argc, char **argv)
        load_plugin(plugins, "map",   viewer, prefs, GTK_NOTEBOOK(config));
        //load_plugin(plugins, "alert", viewer, prefs, GTK_NOTEBOOK(config));
        //load_plugin(plugins, "radar", viewer, prefs, GTK_NOTEBOOK(config));
-       //load_plugin(plugins, "test",  viewer, prefs, GTK_NOTEBOOK(config));
+       load_plugin(plugins, "test",  viewer, prefs, GTK_NOTEBOOK(config));
 
        gtk_widget_show_all(config);
        gtk_main();
index 338cf1b..e7cd4c6 100644 (file)
@@ -129,6 +129,8 @@ GritsMarker *grits_marker_new(const gchar *label)
 {
        GritsMarker *marker = g_object_new(GRITS_TYPE_MARKER, NULL);
 
+       GRITS_OBJECT(marker)->skip = GRITS_SKIP_CENTER;
+
        marker->display_mask = GRITS_MARKER_DMASK_POINT |
                               GRITS_MARKER_DMASK_LABEL;
 
@@ -250,14 +252,10 @@ static void grits_marker_draw(GritsObject *_marker, GritsOpenGL *opengl)
                                point->lat, point->lon, point->elev,
                                &px, &py, &pz);
 
-               gint win_width  = GTK_WIDGET(opengl)->allocation.width;
                gint win_height = GTK_WIDGET(opengl)->allocation.height;
                if (pz > 1)
                        return;
 
-               glMatrixMode(GL_PROJECTION); glLoadIdentity();
-               glMatrixMode(GL_MODELVIEW);  glLoadIdentity();
-               glOrtho(0, win_width, win_height, 0, 1, -1);
                glTranslated(px, win_height-py, 0);
                glRotatef(marker->angle, 0, 0, -1);
                glTranslated(-marker->xoff, -marker->yoff, 0);
index a83cd37..b5ab621 100644 (file)
@@ -77,7 +77,7 @@ static void on_marker_enter(GritsMarker *marker, GritsViewer *viewer)
 {
        g_debug("GritsPluginTest: on_marker_enter");
        GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(viewer));
-       GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
+       GdkCursor *cursor = gdk_cursor_new(GDK_HAND2);
        gdk_window_set_cursor(window, cursor);
 }
 
@@ -106,14 +106,10 @@ void _load_marker(GritsPluginTest *test)
        GRITS_OBJECT(test->marker)->center.lon  = -90.491982;
        GRITS_OBJECT(test->marker)->center.elev =   0.0;
        GRITS_OBJECT(test->marker)->lod         = EARTH_R*3;
-       grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_OVERLAY, FALSE);
-       /* These do not work on marker yet */
-       //g_signal_connect(test->marker, "enter",        G_CALLBACK(on_marker_enter),  NULL);
-       //g_signal_connect(test->marker, "leave",        G_CALLBACK(on_marker_leave),  NULL);
-       //g_signal_connect(test->marker, "button-press", G_CALLBACK(on_marker_button), NULL);
-       (void)on_marker_enter;
-       (void)on_marker_leave;
-       (void)on_marker_button;
+       grits_viewer_add(test->viewer, GRITS_OBJECT(test->marker), GRITS_LEVEL_HUD, FALSE);
+       g_signal_connect(test->marker, "enter",   G_CALLBACK(on_marker_enter),  test->viewer);
+       g_signal_connect(test->marker, "leave",   G_CALLBACK(on_marker_leave),  test->viewer);
+       g_signal_connect(test->marker, "clicked", G_CALLBACK(on_marker_button), test->viewer);
 }
 
 void _load_poly(GritsPluginTest *test)
@@ -126,10 +122,10 @@ void _load_poly(GritsPluginTest *test)
        test->poly->border[3] = 1;
        test->poly->width     = 6;
        grits_viewer_add(test->viewer, GRITS_OBJECT(test->poly),  GRITS_LEVEL_OVERLAY, FALSE);
-       g_signal_connect(test->poly, "enter",        G_CALLBACK(on_poly_enter),  NULL);
-       g_signal_connect(test->poly, "leave",        G_CALLBACK(on_poly_leave),  NULL);
-       g_signal_connect(test->poly, "button-press", G_CALLBACK(on_poly_button), NULL);
-       g_signal_connect(test->poly, "key-press",    G_CALLBACK(on_poly_key),    NULL);
+       g_signal_connect(test->poly, "enter",     G_CALLBACK(on_poly_enter),  NULL);
+       g_signal_connect(test->poly, "leave",     G_CALLBACK(on_poly_leave),  NULL);
+       g_signal_connect(test->poly, "clicked",   G_CALLBACK(on_poly_button), NULL);
+       g_signal_connect(test->poly, "key-press", G_CALLBACK(on_poly_key),    NULL);
 }
 
 void _load_line(GritsPluginTest *test)