X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fobjects%2Fgrits-tile.c;h=6ea70f88cbf052e38073b8087e3ea300c7f19c79;hp=f536c96d496fd99d4f04cfbafdaf668f8745db59;hb=8594927c0cd07daea95e8b99dc36ffef4f12da8a;hpb=e3c32e852b06ab6e1cea08f4860f3c469bea9cde diff --git a/src/objects/grits-tile.c b/src/objects/grits-tile.c index f536c96..6ea70f8 100644 --- a/src/objects/grits-tile.c +++ b/src/objects/grits-tile.c @@ -32,12 +32,14 @@ * representing the OpenGL texture to use when drawing the tile. */ +#define GL_GLEXT_PROTOTYPES #include #include +#include #include "gtkgl.h" #include "grits-tile.h" -guint grits_tile_mask = 0; +static guint grits_tile_mask = 0; gchar *grits_tile_path_table[2][2] = { {"00.", "01."}, @@ -65,7 +67,7 @@ GritsTile *grits_tile_new(GritsTile *parent, grits_bounds_set_bounds(&tile->coords, 0, 1, 1, 0); grits_bounds_set_bounds(&tile->edge, n, s, e, w); if (parent) - tile->proj = parent->proj; + tile->proj = parent->proj; return tile; } @@ -141,6 +143,7 @@ static gboolean _grits_tile_precise(GritsPoint *eye, GritsBounds *bounds, static void _grits_tile_split_latlon(GritsTile *tile) { + //g_debug("GritsTile: split - %p", tile); 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; @@ -149,12 +152,18 @@ static void _grits_tile_split_latlon(GritsTile *tile) const gdouble lon_step = lon_dist / cols; int row, col; - grits_tile_foreach_index(tile, row, col) - tile->children[row][col] = grits_tile_new(tile, - tile->edge.n - lat_step*(row+0), // north - tile->edge.n - lat_step*(row+1), // south - tile->edge.w + lon_step*(col+1), // east - tile->edge.w + lon_step*(col+0)); // west + grits_tile_foreach_index(tile, row, col) { + if (!tile->children[row][col]) + tile->children[row][col] = + grits_tile_new(tile, 0, 0, 0, 0); + /* Set edges aferwards so that north and south + * get reset for mercator projections */ + GritsTile *child = tile->children[row][col]; + child->edge.n = tile->edge.n - lat_step*(row+0); + child->edge.s = tile->edge.n - lat_step*(row+1); + child->edge.e = tile->edge.w + lon_step*(col+1); + child->edge.w = tile->edge.w + lon_step*(col+0); + } } static void _grits_tile_split_mercator(GritsTile *tile) @@ -196,26 +205,31 @@ void grits_tile_update(GritsTile *tile, GritsPoint *eye, gdouble res, gint width, gint height, GritsTileLoadFunc load_func, gpointer user_data) { - //g_debug("GritsTile: update - %p->atime = %u", - // tile, (guint)tile->atime); GritsTile *child; if (tile == NULL) return; - if (!tile->data) - load_func(tile, user_data); - - tile->atime = time(NULL); + //g_debug("GritsTile: update - %p->atime = %u", + // tile, (guint)tile->atime); - /* Is this tile high enough resolution? */ - if (_grits_tile_precise(eye, &tile->edge, res, width, height)) { - grits_tile_foreach(tile, child) - if (child) - GRITS_OBJECT(child)->hidden = TRUE; + /* Is the parent tile's texture high enough + * resolution for this part? */ + gint xs = G_N_ELEMENTS(tile->children); + gint ys = G_N_ELEMENTS(tile->children[0]); + if (tile->parent && _grits_tile_precise(eye, &tile->edge, + res, width/xs, height/ys)) { + GRITS_OBJECT(tile)->hidden = TRUE; return; } + /* Load the tile */ + if (!tile->load && !tile->data) + load_func(tile, user_data); + tile->atime = time(NULL); + tile->load = TRUE; + GRITS_OBJECT(tile)->hidden = FALSE; + /* Split tile if needed */ grits_tile_foreach(tile, child) { if (child == NULL) { @@ -227,12 +241,9 @@ void grits_tile_update(GritsTile *tile, GritsPoint *eye, } /* Update recursively */ - grits_tile_foreach(tile, child) { - GRITS_OBJECT(child)->hidden = FALSE; + grits_tile_foreach(tile, child) grits_tile_update(child, eye, res, width, height, load_func, user_data); - } - } /** @@ -301,10 +312,13 @@ GritsTile *grits_tile_gc(GritsTile *root, time_t atime, if (root->children[x][y]) has_children = TRUE; } - //g_debug("GritsTile: gc - %p->atime=%u < atime=%u", - // root, (guint)root->atime, (guint)atime); - if (!has_children && root->atime < atime && root->data) { - free_func(root, user_data); + //g_debug("GritsTile: gc - %p kids=%d time=%d data=%d load=%d", + // root, !!has_children, root->atime < atime, !!root->data, !!root->load); + if (root->parent && !has_children && root->atime < atime && + (root->data || !root->load)) { + //g_debug("GritsTile: gc/free - %p", root); + if (root->data) + free_func(root, user_data); g_object_unref(root); return NULL; } @@ -335,19 +349,22 @@ void grits_tile_free(GritsTile *root, GritsTileFreeFunc free_func, gpointer user /* Load texture mask so we can draw a texture to just a part of a triangle */ static guint _grits_tile_load_mask(void) { - guint tex; - guint8 byte = 0xff; + guint tex; + const int width = 256, height = 256; + guint8 *bytes = g_malloc(width*height); + memset(bytes, 0xff, width*height); glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1, 1, 0, - GL_ALPHA, GL_UNSIGNED_BYTE, &byte); + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, + GL_ALPHA, GL_UNSIGNED_BYTE, bytes); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + g_free(bytes); return tex; } @@ -359,8 +376,6 @@ static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *tri if (!triangles) g_warning("GritsOpenGL: _draw_tiles - No triangles to draw: edges=%f,%f,%f,%f", tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); - if (!grits_tile_mask) - grits_tile_mask = _grits_tile_load_mask(); //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); @@ -377,6 +392,8 @@ static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *tri gdouble xscale = tile->coords.e - tile->coords.w; gdouble yscale = tile->coords.s - tile->coords.n; + glPolygonOffset(0, -tile->zindex); + for (GList *cur = triangles; cur; cur = cur->next) { RoamTriangle *tri = cur->data; @@ -419,75 +436,44 @@ static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *tri xy[i][1] = tile->coords.n + xy[i][1]*yscale; } - /* Polygon offset */ - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(0, -tile->zindex); - - /* Setup texture */ - glActiveTexture(GL_TEXTURE0); - glEnable(GL_TEXTURE_2D); + /* Draw triangle */ glBindTexture(GL_TEXTURE_2D, *(guint*)tile->data); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - /* Enable texture mask */ - if (tile->proj == GRITS_PROJ_MERCATOR) { - glActiveTexture(GL_TEXTURE1); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, grits_tile_mask); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - /* Hack to show maps tiles with better color */ - float material_emission[] = {0.5, 0.5, 0.5, 1.0}; - glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission); - - glEnable(GL_BLEND); - } - - /* Draw triangle */ glBegin(GL_TRIANGLES); glNormal3dv(tri->p.r->norm); glMultiTexCoord2dv(GL_TEXTURE0, xy[0]); glMultiTexCoord2dv(GL_TEXTURE1, xy[0]); glVertex3dv((double*)tri->p.r); glNormal3dv(tri->p.m->norm); glMultiTexCoord2dv(GL_TEXTURE0, xy[1]); glMultiTexCoord2dv(GL_TEXTURE1, xy[1]); glVertex3dv((double*)tri->p.m); glNormal3dv(tri->p.l->norm); glMultiTexCoord2dv(GL_TEXTURE0, xy[2]); glMultiTexCoord2dv(GL_TEXTURE1, xy[2]); glVertex3dv((double*)tri->p.l); glEnd(); - - /* Disable texture mask */ - glDisable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); } } /* Draw the tile */ static gboolean grits_tile_draw_rec(GritsTile *tile, GritsOpenGL *opengl) { + //g_debug("GritsTile: draw_rec - tile=%p, data=%d, load=%d, hide=%d", tile, + // tile ? !!tile->data : 0, + // tile ? !!tile->load : 0, + // tile ? !!GRITS_OBJECT(tile)->hidden : 0); + if (!tile || !tile->data || GRITS_OBJECT(tile)->hidden) return FALSE; GritsTile *child = NULL; - gboolean done = FALSE; - while (!done) { - /* Only draw children if possible */ - gboolean draw_parent = FALSE; - grits_tile_foreach(tile, child) - if (!child || !child->data || GRITS_OBJECT(child)->hidden) - draw_parent = TRUE; - - /* Draw parent tile underneath */ - if (draw_parent) { - GList *triangles = roam_sphere_get_intersect(opengl->sphere, FALSE, - tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); - grits_tile_draw_one(tile, opengl, triangles); - g_list_free(triangles); - } - - /* Draw child tiles */ - gboolean drew_all_children = TRUE; - grits_tile_foreach(tile, child) - if (!grits_tile_draw_rec(child, opengl)) - drew_all_children = FALSE; - /* Check if tiles were hidden by a thread while drawing */ - done = draw_parent || drew_all_children; + /* Draw child tiles */ + gboolean draw_parent = FALSE; + grits_tile_foreach(tile, child) + if (!grits_tile_draw_rec(child, opengl)) + draw_parent = TRUE; + + /* Draw parent tile underneath using depth test */ + if (draw_parent) { + GList *triangles = roam_sphere_get_intersect(opengl->sphere, FALSE, + tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w); + grits_tile_draw_one(tile, opengl, triangles); + g_list_free(triangles); } + return TRUE; } @@ -497,7 +483,34 @@ static void grits_tile_draw(GritsObject *tile, GritsOpenGL *opengl) glDepthFunc(GL_LESS); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.1); + glEnable(GL_POLYGON_OFFSET_FILL); + glEnable(GL_BLEND); + + /* Setup texture mask */ + if (!grits_tile_mask) + grits_tile_mask = _grits_tile_load_mask(); + glActiveTexture(GL_TEXTURE1); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, grits_tile_mask); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + /* Setup texture */ + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + + /* Hack to show maps tiles with better color */ + if (GRITS_TILE(tile)->proj == GRITS_PROJ_MERCATOR) { + float material_emission[] = {0.5, 0.5, 0.5, 1.0}; + glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission); + } + + /* Draw all tiles */ grits_tile_draw_rec(GRITS_TILE(tile), opengl); + + /* Disable texture mask */ + glActiveTexture(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); }