X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fgis-opengl.c;h=8b711426e123c7e76c60bc0b8d4c01af0de3662d;hp=08babe50b8617e77365cf45df64e91d5a42160b5;hb=2be8bbde8f465947d364b28c5e156631caed6d25;hpb=bd716715f1d13a8df514fcfa53fd82aebdfda770 diff --git a/src/gis-opengl.c b/src/gis-opengl.c index 08babe5..8b71142 100644 --- a/src/gis-opengl.c +++ b/src/gis-opengl.c @@ -15,9 +15,17 @@ * along with this program. If not, see . */ -/* Tessellation, "finding intersecting triangles" */ -/* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */ -/* http://www.opengl.org/wiki/Alpha_Blending */ +/** + * SECTION:gis-opengl + * @short_description: OpenGL based virtual globe + * + * #GisOpenGL is the core rendering engine used by libgis. Theoretically other + * renderers could be writte, but they have not been. GisOpenGL uses the ROAM + * algorithm for updating surface mesh the planet. The only thing GisOpenGL can + * actually render on it's own is a wireframe of a sphere. + * + * GisOpenGL relies on #GtkGlExt and requires (at least) OpenGL 2.0. + */ #include #include @@ -33,14 +41,16 @@ #include "roam.h" #include "objects/gis-object.h" +#include "objects/gis-tile.h" #include "objects/gis-marker.h" #include "objects/gis-callback.h" -#define FOV_DIST 2000.0 -#define MPPX(dist) (4*dist/FOV_DIST) - // #define ROAM_DEBUG +/* Tessellation, "finding intersecting triangles" */ +/* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */ +/* http://www.opengl.org/wiki/Alpha_Blending */ + /*********** * Helpers * ***********/ @@ -106,24 +116,35 @@ static void _set_visuals(GisOpenGL *opengl) 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); } /******************** * Object handleing * ********************/ -static void _draw_tile(GisOpenGL *opengl, GisTile *tile) +static void _draw_tile(GisOpenGL *opengl, GisTile *tile, GList *triangles) { if (!tile || !tile->data) return; - GList *triangles = roam_sphere_get_intersect(opengl->sphere, FALSE, - tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); if (!triangles) g_warning("GisOpenGL: _draw_tiles - No triangles to draw: edges=%f,%f,%f,%f", tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); //g_message("drawing %4d triangles for tile edges=%7.2f,%7.2f,%7.2f,%7.2f", // g_list_length(triangles), tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); + gdouble n = tile->edge.n; + gdouble s = tile->edge.s; + gdouble e = tile->edge.e; + gdouble w = tile->edge.w; + + gdouble londist = e - w; + gdouble latdist = n - s; + + gdouble xscale = tile->coords.e - tile->coords.w; + gdouble yscale = tile->coords.s - tile->coords.n; + for (GList *cur = triangles; cur; cur = cur->next) { RoamTriangle *tri = cur->data; @@ -136,14 +157,6 @@ static void _draw_tile(GisOpenGL *opengl, GisTile *tile) if (lon[2] > 90) lon[2] -= 360; } - gdouble n = tile->edge.n; - gdouble s = tile->edge.s; - gdouble e = tile->edge.e; - gdouble w = tile->edge.w; - - gdouble londist = e - w; - gdouble latdist = n - s; - gdouble xy[3][2] = { {(lon[0]-w)/londist, 1-(lat[0]-s)/latdist}, {(lon[1]-w)/londist, 1-(lat[1]-s)/latdist}, @@ -168,41 +181,77 @@ static void _draw_tile(GisOpenGL *opengl, GisTile *tile) if (lat[1] == 90 || lat[1] == -90) xy[1][0] = 0.5; if (lat[2] == 90 || lat[2] == -90) xy[2][0] = 0.5; + /* Scale to tile coords */ + for (int i = 0; i < 3; i++) { + xy[i][0] = tile->coords.w + xy[i][0]*xscale; + xy[i][1] = tile->coords.n + xy[i][1]*yscale; + } + glEnable(GL_TEXTURE_2D); + glEnable(GL_POLYGON_OFFSET_FILL); glBindTexture(GL_TEXTURE_2D, *(guint*)tile->data); + glPolygonOffset(0, -tile->zindex); glBegin(GL_TRIANGLES); glNormal3dv(tri->p.r->norm); glTexCoord2dv(xy[0]); glVertex3dv((double*)tri->p.r); glNormal3dv(tri->p.m->norm); glTexCoord2dv(xy[1]); glVertex3dv((double*)tri->p.m); glNormal3dv(tri->p.l->norm); glTexCoord2dv(xy[2]); glVertex3dv((double*)tri->p.l); glEnd(); } - g_list_free(triangles); } static void _draw_tiles(GisOpenGL *opengl, GisTile *tile) { /* Only draw children if possible */ - gboolean has_children = TRUE; + gboolean has_children = FALSE; GisTile *child; gis_tile_foreach(tile, child) - if (!child || !child->data) - has_children = FALSE; - if (has_children) - /* Only draw children */ - gis_tile_foreach(tile, child) - _draw_tiles(opengl, child); - else - /* No children, draw this tile */ - _draw_tile(opengl, tile); + if (child && child->data) + has_children = TRUE; + + GList *triangles = NULL; + if (has_children) { + /* TODO: simplify this */ + const gdouble rows = G_N_ELEMENTS(tile->children); + const gdouble cols = G_N_ELEMENTS(tile->children[0]); + const gdouble lat_dist = tile->edge.n - tile->edge.s; + const gdouble lon_dist = tile->edge.e - tile->edge.w; + const gdouble lat_step = lat_dist / rows; + const gdouble lon_step = lon_dist / cols; + int row, col; + gis_tile_foreach_index(tile, row, col) { + GisTile *child = tile->children[row][col]; + if (child && child->data) { + _draw_tiles(opengl, child); + } else { + const gdouble n = tile->edge.n-(lat_step*(row+0)); + const gdouble s = tile->edge.n-(lat_step*(row+1)); + const gdouble e = tile->edge.w+(lon_step*(col+1)); + const gdouble w = tile->edge.w+(lon_step*(col+0)); + GList *these = roam_sphere_get_intersect(opengl->sphere, + FALSE, n, s, e, w); + triangles = g_list_concat(triangles, these); + } + } + } else { + triangles = roam_sphere_get_intersect(opengl->sphere, FALSE, + tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); + } + if (triangles) + _draw_tile(opengl, tile, triangles); + g_list_free(triangles); } static void _draw_marker(GisOpenGL *opengl, GisMarker *marker) { - GisPoint *point = gis_object_center(GIS_OBJECT(marker)); + GisPoint *point = gis_object_center(marker); gdouble px, py, pz; gis_viewer_project(GIS_VIEWER(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; + py = win_height - py; if (pz > 1) return; @@ -214,8 +263,7 @@ static void _draw_marker(GisOpenGL *opengl, GisMarker *marker) glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glOrtho(0, GTK_WIDGET(opengl)->allocation.width, - 0, GTK_WIDGET(opengl)->allocation.height, -1, 1); + glOrtho(0, win_width, win_height, 0, -1, 1); glTranslated(px - marker->xoff, py - marker->yoff, 0); @@ -224,11 +272,12 @@ static void _draw_marker(GisOpenGL *opengl, GisMarker *marker) glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, marker->tex); + glDisable(GL_CULL_FACE); glBegin(GL_QUADS); - glTexCoord2f(1, 1); glVertex3f(width, 0 , 0); - glTexCoord2f(1, 0); glVertex3f(width, height, 0); - glTexCoord2f(0, 0); glVertex3f(0 , height, 0); - glTexCoord2f(0, 1); glVertex3f(0 , 0 , 0); + glTexCoord2f(1, 0); glVertex3f(width, 0 , 0); + glTexCoord2f(1, 1); glVertex3f(width, height, 0); + glTexCoord2f(0, 1); glVertex3f(0 , height, 0); + glTexCoord2f(0, 0); glVertex3f(0 , 0 , 0); glEnd(); } @@ -240,16 +289,29 @@ static void _draw_callback(GisOpenGL *opengl, GisCallback *callback) static void _draw_object(GisOpenGL *opengl, GisObject *object) { //g_debug("GisOpenGL: draw_object"); + /* Skip hidden objects */ + if (object->hidden) + return; + /* Skip out of range objects */ if (object->lod > 0) { + /* LOD test */ gdouble eye[3], obj[3]; gis_viewer_get_location(GIS_VIEWER(opengl), &eye[0], &eye[1], &eye[2]); + gdouble elev = eye[2]; lle2xyz(eye[0], eye[1], eye[2], &eye[0], &eye[1], &eye[2]); lle2xyz(object->center.lat, object->center.lon, object->center.elev, &obj[0], &obj[1], &obj[2]); gdouble dist = distd(obj, eye); if (object->lod < dist) return; + + /* Horizon testing */ + gdouble c = EARTH_R+elev; + gdouble a = EARTH_R; + gdouble horizon = sqrt(c*c - a*a); + if (dist > horizon) + return; } /* Draw */ @@ -261,7 +323,11 @@ static void _draw_object(GisOpenGL *opengl, GisObject *object) } else if (GIS_IS_CALLBACK(object)) { _draw_callback(opengl, GIS_CALLBACK(object)); } else if (GIS_IS_TILE(object)) { + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + g_mutex_lock(opengl->sphere_lock); _draw_tiles(opengl, GIS_TILE(object)); + g_mutex_unlock(opengl->sphere_lock); } glPopAttrib(); glMatrixMode(GL_PROJECTION); glPopMatrix(); @@ -270,7 +336,7 @@ static void _draw_object(GisOpenGL *opengl, GisObject *object) static void _load_object(GisOpenGL *opengl, GisObject *object) { - g_debug("GisOpenGL: load_object"); + //g_debug("GisOpenGL: load_object"); if (GIS_IS_MARKER(object)) { GisMarker *marker = GIS_MARKER(object); cairo_surface_t *surface = cairo_get_target(marker->cairo); @@ -287,13 +353,13 @@ static void _load_object(GisOpenGL *opengl, GisObject *object) cairo_image_surface_get_data(surface)); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - g_debug("load_texture: %d", marker->tex); + //g_debug("load_texture: %d", marker->tex); } } static void _unload_object(GisOpenGL *opengl, GisObject *object) { - g_debug("GisOpenGL: unload_object"); + //g_debug("GisOpenGL: unload_object"); if (GIS_IS_MARKER(object)) { GisMarker *marker = GIS_MARKER(object); glDeleteTextures(1, &marker->tex); @@ -312,21 +378,6 @@ struct RenderLevel { GList sorted; }; -static void on_realize(GisOpenGL *opengl, gpointer _) -{ - g_debug("GisOpenGL: on_realize"); - - 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(); - - _set_visuals(opengl); - g_mutex_lock(opengl->sphere_lock); - roam_sphere_update_errors(opengl->sphere); - g_mutex_unlock(opengl->sphere_lock); -} - static gboolean on_configure(GisOpenGL *opengl, GdkEventConfigure *event, gpointer _) { g_debug("GisOpenGL: on_configure"); @@ -339,7 +390,7 @@ static gboolean on_configure(GisOpenGL *opengl, GdkEventConfigure *event, gpoint glMatrixMode(GL_PROJECTION); glLoadIdentity(); double ang = atan(height/FOV_DIST); - gluPerspective(rad2deg(ang)*2, width/height, 1, 10*EARTH_R); + gluPerspective(rad2deg(ang)*2, width/height, 1000, 10*EARTH_R); #ifndef ROAM_DEBUG g_mutex_lock(opengl->sphere_lock); @@ -350,6 +401,19 @@ static gboolean on_configure(GisOpenGL *opengl, GdkEventConfigure *event, gpoint return FALSE; } +static void on_realize(GisOpenGL *opengl, gpointer _) +{ + g_debug("GisOpenGL: on_realize"); + + 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(); + + _set_visuals(opengl); + on_configure(opengl, NULL, NULL); +} + static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data) { g_debug("GisOpenGL: _draw_level - level=%-4d", (int)key); @@ -392,7 +456,9 @@ static gboolean on_expose(GisOpenGL *opengl, GdkEventExpose *event, gpointer _) roam_sphere_draw(opengl->sphere); //roam_sphere_draw_normals(opengl->sphere); #else + g_mutex_lock(opengl->objects_lock); g_tree_foreach(opengl->objects, _draw_level, opengl); + g_mutex_unlock(opengl->objects_lock); if (opengl->wireframe) { glClear(GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -413,7 +479,6 @@ static gboolean on_key_press(GisOpenGL *opengl, GdkEventKey *event, gpointer _) event->keyval, event->state, GDK_plus); guint kv = event->keyval; - gdk_threads_leave(); /* Testing */ if (kv == GDK_w) { opengl->wireframe = !opengl->wireframe; @@ -424,17 +489,18 @@ static gboolean on_key_press(GisOpenGL *opengl, GdkEventKey *event, gpointer _) 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); - gdk_threads_enter(); gtk_widget_queue_draw(GTK_WIDGET(opengl)); -#else - gdk_threads_enter(); #endif return FALSE; } -static gboolean _update_errors_cb(gpointer sphere) +static gboolean _update_errors_cb(gpointer _opengl) { - roam_sphere_update_errors(sphere); + GisOpenGL *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(GisOpenGL *opengl, @@ -443,8 +509,9 @@ static void on_view_changed(GisOpenGL *opengl, g_debug("GisOpenGL: on_view_changed"); _set_visuals(opengl); #ifndef ROAM_DEBUG - opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30, - _update_errors_cb, opengl->sphere, NULL); + 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); #endif } @@ -452,12 +519,10 @@ static void on_view_changed(GisOpenGL *opengl, static gboolean on_idle(GisOpenGL *opengl) { //g_debug("GisOpenGL: on_idle"); - gdk_threads_enter(); 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); - gdk_threads_leave(); return TRUE; } @@ -465,6 +530,15 @@ static gboolean on_idle(GisOpenGL *opengl) /********************* * GisViewer methods * *********************/ +/** + * gis_opengl_new: + * @plugins: the plugins store to use + * @prefs: the preferences object to use + * + * Create a new OpenGL renderer. + * + * Returns: the new #GisOpenGL + */ GisViewer *gis_opengl_new(GisPlugins *plugins, GisPrefs *prefs) { g_debug("GisOpenGL: new"); @@ -475,7 +549,6 @@ GisViewer *gis_opengl_new(GisPlugins *plugins, GisPrefs *prefs) static void gis_opengl_center_position(GisViewer *_opengl, gdouble lat, gdouble lon, gdouble elev) { - GisOpenGL *opengl = GIS_OPENGL(_opengl); glRotatef(lon, 0, 1, 0); glRotatef(-lat, 1, 0, 0); glTranslatef(0, 0, elev2rad(elev)); @@ -495,22 +568,20 @@ static void gis_opengl_project(GisViewer *_opengl, px, py, pz); } -static void gis_opengl_set_height_func(GisViewer *_opengl, GisTile *tile, +static void gis_opengl_set_height_func(GisViewer *_opengl, GisBounds *bounds, RoamHeightFunc height_func, gpointer user_data, gboolean update) { GisOpenGL *opengl = GIS_OPENGL(_opengl); - if (!tile) - return; /* TODO: get points? */ g_mutex_lock(opengl->sphere_lock); GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE, - tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); + bounds->n, bounds->s, bounds->e, bounds->w); for (GList *cur = triangles; cur; cur = cur->next) { RoamTriangle *tri = cur->data; RoamPoint *points[] = {tri->p.l, tri->p.m, tri->p.r, tri->split}; for (int i = 0; i < G_N_ELEMENTS(points); i++) { - if (tile->edge.n >= points[i]->lat && points[i]->lat >= tile->edge.s && - tile->edge.e >= points[i]->lon && points[i]->lon >= tile->edge.w) { + if (bounds->n >= points[i]->lat && points[i]->lat >= bounds->s && + bounds->e >= points[i]->lon && points[i]->lon >= bounds->w) { points[i]->height_func = height_func; points[i]->height_data = user_data; roam_point_update_height(points[i]); @@ -547,6 +618,7 @@ static gpointer gis_opengl_add(GisViewer *_opengl, GisObject *object, { g_assert(GIS_IS_OPENGL(_opengl)); GisOpenGL *opengl = GIS_OPENGL(_opengl); + g_mutex_lock(opengl->objects_lock); _load_object(opengl, object); struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)key); if (!level) { @@ -555,18 +627,22 @@ static gpointer gis_opengl_add(GisViewer *_opengl, GisObject *object, } GList *list = sort ? &level->sorted : &level->unsorted; /* Put the link in the list */ - GList *next = g_new0(GList, 1); - next->data = object; - next->prev = list; - next->next = list->next; - list->next = next; - return next; + GList *link = g_new0(GList, 1); + link->data = object; + link->prev = list; + link->next = list->next; + if (list->next) + list->next->prev = link; + list->next = link; + g_mutex_unlock(opengl->objects_lock); + return link; } static GisObject *gis_opengl_remove(GisViewer *_opengl, gpointer _link) { g_assert(GIS_IS_OPENGL(_opengl)); GisOpenGL *opengl = GIS_OPENGL(_opengl); + g_mutex_lock(opengl->objects_lock); GList *link = _link; GisObject *object = link->data; _unload_object(opengl, object); @@ -576,6 +652,7 @@ static GisObject *gis_opengl_remove(GisViewer *_opengl, gpointer _link) link->next->prev = link->prev; g_free(link); g_object_unref(object); + g_mutex_unlock(opengl->objects_lock); return object; } @@ -614,6 +691,7 @@ static void gis_opengl_init(GisOpenGL *opengl) g_object_unref(glconfig); 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(); @@ -656,6 +734,7 @@ static void gis_opengl_finalize(GObject *_opengl) GisOpenGL *opengl = GIS_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_OBJECT_CLASS(gis_opengl_parent_class)->finalize(_opengl); }