X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fgrits-opengl.c;h=2bd00822cea22ace1b4776aba045d3cf6ac2f909;hp=b2dca5eebab5922e832625d7dc6e85a45cd3369c;hb=5c17e7d79895982036139dcffb5c17cefb1d4d9f;hpb=536c4ae84ecb423286423bd62bce106dc4a0072d diff --git a/src/grits-opengl.c b/src/grits-opengl.c index b2dca5e..2bd0082 100644 --- a/src/grits-opengl.c +++ b/src/grits-opengl.c @@ -40,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 */ @@ -56,7 +58,7 @@ 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); @@ -66,14 +68,15 @@ static void _set_visuals(GritsOpenGL *opengl) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - double width = GTK_WIDGET(opengl)->allocation.width; - double height = GTK_WIDGET(opengl)->allocation.height; + 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 - grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev); glViewport(0, 0, width, height); gluPerspective(rad2deg(ang), width/height, near, far); @@ -85,6 +88,40 @@ static void _set_visuals(GritsOpenGL *opengl) 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}; @@ -93,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}; @@ -108,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); @@ -129,15 +154,6 @@ 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 GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho) @@ -159,19 +175,6 @@ static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho) /************* * Callbacks * *************/ -static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _) -{ - g_debug("GritsOpenGL: on_configure"); - - _set_visuals(opengl); -#ifndef ROAM_DEBUG - g_mutex_lock(&opengl->sphere_lock); - roam_sphere_update_errors(opengl->sphere); - g_mutex_unlock(&opengl->sphere_lock); -#endif - - return FALSE; -} static gint run_picking(GritsOpenGL *opengl, GdkEvent *event, GPtrArray *objects, GritsObject **top) @@ -218,9 +221,11 @@ static gint run_picking(GritsOpenGL *opengl, GdkEvent *event, static gboolean run_mouse_move(GritsOpenGL *opengl, GdkEventMotion *event) { - gdouble height = GTK_WIDGET(opengl)->allocation.height; + GtkAllocation alloc; + gtk_widget_get_allocation(GTK_WIDGET(opengl), &alloc); + gdouble gl_x = event->x; - gdouble gl_y = height - event->y; + gdouble gl_y = alloc.height - event->y; gdouble delta = opengl->pickmode ? 200 : 2; if (opengl->pickmode) { @@ -273,7 +278,6 @@ static gboolean run_mouse_move(GritsOpenGL *opengl, GdkEventMotion *event) g_mutex_unlock(&opengl->objects_lock); - /* Test unproject */ //gdouble lat, lon, elev; //grits_viewer_unproject(GRITS_VIEWER(opengl), @@ -292,7 +296,7 @@ static gboolean run_mouse_move(GritsOpenGL *opengl, GdkEventMotion *event) static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _) { opengl->mouse_queue = *event; - gtk_widget_queue_draw(GTK_WIDGET(opengl)); + grits_viewer_queue_draw(GRITS_VIEWER(opengl)); return FALSE; } @@ -314,18 +318,19 @@ static void _draw_level(gpointer _level, gpointer _opengl) /* Enable depth and alpha for world levels */ glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.1); + glDepthRange(OVERLAY_SLICE, 1); } else { - /* Disable depth for Overlay/HUD levels */ - glDepthMask(FALSE); + /* 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(); - 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); + glOrtho(0, alloc.width, alloc.height, 0, 1000, -1000); } /* Draw unsorted objects without depth testing, @@ -355,12 +360,12 @@ static void _draw_level(gpointer _level, gpointer _opengl) nunsorted, nsorted); } -static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _) +static gboolean on_expose(GritsOpenGL *opengl, gpointer data, gpointer _) { g_debug("GritsOpenGL: on_expose - begin"); if (opengl->pickmode) - return run_mouse_move(opengl, (GdkEventMotion*)event); + return run_mouse_move(opengl, &(GdkEventMotion){}); if (opengl->mouse_queue.type != GDK_NOTHING) { run_mouse_move(opengl, &opengl->mouse_queue); @@ -369,21 +374,22 @@ static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _ gtk_gl_begin(GTK_WIDGET(opengl)); + _set_settings(opengl); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - _set_visuals(opengl); +#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 + #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); - if (opengl->wireframe) - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); g_queue_foreach(opengl->objects, _draw_level, opengl); g_mutex_unlock(&opengl->objects_lock); #endif @@ -397,24 +403,24 @@ static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _ 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_p) { + if (kv == GDK_KEY_p) { opengl->pickmode = !opengl->pickmode; - gtk_widget_queue_draw(GTK_WIDGET(opengl)); + 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; } @@ -433,40 +439,6 @@ static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer 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; - 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 _) { g_debug("GritsOpenGL: on_realize"); @@ -474,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); @@ -489,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)); } @@ -555,6 +523,7 @@ static void grits_opengl_unproject(GritsViewer *_opengl, 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, @@ -632,13 +601,13 @@ static void _objects_free(gpointer value, gpointer _) { struct RenderLevel *level = value; if (level->sorted.next) - g_list_free(level->sorted.next); + g_list_free_full(level->sorted.next, g_object_unref); if (level->unsorted.next) - g_list_free(level->unsorted.next); + g_list_free_full(level->unsorted.next, g_object_unref); g_free(level); } -static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object, +static void grits_opengl_add(GritsViewer *_opengl, GritsObject *object, gint num, gboolean sort) { g_assert(GRITS_IS_OPENGL(_opengl)); @@ -662,26 +631,27 @@ static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object, if (list->next) list->next->prev = link; list->next = link; + object->ref = link; + g_object_ref(object); g_mutex_unlock(&opengl->objects_lock); - return link; } -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); - GList *link = object->ref; + if (!object->ref) + return; g_mutex_lock(&opengl->objects_lock); + GList *link = object->ref; /* 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); } /**************** @@ -703,17 +673,13 @@ 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); } @@ -722,10 +688,9 @@ static void grits_opengl_finalize(GObject *_opengl) g_debug("GritsOpenGL: finalize"); GritsOpenGL *opengl = GRITS_OPENGL(_opengl); roam_sphere_free(opengl->sphere); - g_queue_foreach(opengl->objects, _objects_free, NULL); - g_queue_free(opengl->objects); 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)