X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fobjects%2Fgrits-tile.c;h=e42a1606338d916f0704ff1959e4aece9db7edbe;hp=68e71ccb4513aa2bdb8a14d3abc84c1e851cf342;hb=c07abad157dd10d3f8cc229b2f616c3a33f6c900;hpb=04171adb63df1084005faadd94419a64db072596 diff --git a/src/objects/grits-tile.c b/src/objects/grits-tile.c index 68e71cc..e42a160 100644 --- a/src/objects/grits-tile.c +++ b/src/objects/grits-tile.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 Andy Spencer + * Copyright (C) 2009-2010, 2012 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,11 +32,15 @@ * representing the OpenGL texture to use when drawing the tile. */ +#define GL_GLEXT_PROTOTYPES #include #include +#include #include "gtkgl.h" #include "grits-tile.h" +static guint grits_tile_mask = 0; + gchar *grits_tile_path_table[2][2] = { {"00.", "01."}, {"10.", "11."}, @@ -63,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; } @@ -120,17 +124,18 @@ static gboolean _grits_tile_precise(GritsPoint *eye, GritsBounds *bounds, gdouble lon_dist = bounds->e - bounds->w; gdouble tile_res = ll2m(lon_dist, lat_point)/width; - /* This isn't really right, but it helps with memory since we don't (yet?) test if the tile - * would be drawn */ + /* This isn't really right, but it helps with memory since we don't + * (yet?) test if the tile would be drawn */ gdouble scale = eye->elev / min_dist; view_res /= scale; + view_res *= 1.8; //view_res /= 1.4; /* make it a little nicer, not sure why this is needed */ //g_message("tile=(%7.2f %7.2f %7.2f %7.2f) " // "eye=(%9.1f %9.1f %9.1f) " // "elev=%9.1f / dist=%9.1f = %f", - // tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w, - // lat, lon, elev, - // elev, min_dist, scale); + // bounds->n, bounds->s, bounds->e, bounds->w, + // eye->lat, eye->lon, eye->elev, + // eye->elev, min_dist, scale); return tile_res < max_res || tile_res < view_res; @@ -138,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; @@ -146,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) @@ -193,33 +205,133 @@ 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; - GRITS_OBJECT(tile)->hidden = TRUE; - if (_grits_tile_precise(eye, &tile->edge, res, width, height)) + //g_debug("GritsTile: update - %p->atime = %u", + // tile, (guint)tile->atime); + + /* 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; - tile->atime = time(NULL); - GRITS_OBJECT(tile)->hidden = FALSE; + } - if (!tile->data) + /* Load the tile */ + if (!tile->load && !tile->data && !tile->tex && !tile->pixels && !tile->pixbuf) load_func(tile, user_data); + tile->atime = time(NULL); + tile->load = TRUE; + GRITS_OBJECT(tile)->hidden = FALSE; - if (!tile->children[0][0]) { - switch (tile->proj) { - case GRITS_PROJ_LATLON: _grits_tile_split_latlon(tile); break; - case GRITS_PROJ_MERCATOR: _grits_tile_split_mercator(tile); break; + /* Split tile if needed */ + grits_tile_foreach(tile, child) { + if (child == NULL) { + switch (tile->proj) { + case GRITS_PROJ_LATLON: _grits_tile_split_latlon(tile); break; + case GRITS_PROJ_MERCATOR: _grits_tile_split_mercator(tile); break; + } } } - GritsTile *child; + /* Update recursively */ grits_tile_foreach(tile, child) grits_tile_update(child, eye, res, width, height, load_func, user_data); +} + +/** + * grits_tile_load_pixels: + * @tile: the tile to load data into + * @pixels: buffered pixel data + * @width: width of the pixel buffer (in pixels) + * @height: height of the pixel buffer (in pixels) + * @alpha: TRUE if the pixel data contains an alpha channel + * + * Load tile data from an in memory pixel buffer. + * + * This function is thread safe and my be called from outside the main thread. + * + * Ownership of the pixel buffer is passed to the tile, it should not be freed + * or modified after calling this function. + * + * Returns: TRUE if the image was loaded successfully + */ +gboolean grits_tile_load_pixels(GritsTile *tile, guchar *pixels, + gint width, gint height, gint alpha) +{ + g_debug("GritsTile: load_pixels - %p -> %p (%dx%d:%d)", + tile, pixels, width, height, alpha); + + /* Copy pixbuf data for callback */ + tile->width = width; + tile->height = height; + tile->alpha = alpha; + tile->pixels = pixels; + + /* Queue OpenGL texture load/draw */ + grits_object_queue_draw(GRITS_OBJECT(tile)); + return TRUE; +} + +/** + * grits_tile_load_file: + * @tile: the tile to load data into + * @file: path to an image file to load + * + * Load tile data from a GdkPixbuf + * This function is thread safe and my be called from outside the main thread. + * + * Returns: TRUE if the image was loaded successfully + */ +gboolean grits_tile_load_pixbuf(GritsTile *tile, GdkPixbuf *pixbuf) +{ + g_debug("GritsTile: load_pixbuf %p -> %p", tile, pixbuf); + + /* Copy pixbuf data for callback */ + tile->pixbuf = g_object_ref(pixbuf); + tile->width = gdk_pixbuf_get_width(pixbuf); + tile->height = gdk_pixbuf_get_height(pixbuf); + tile->alpha = gdk_pixbuf_get_has_alpha(pixbuf); + + /* Queue OpenGL texture load/draw */ + grits_object_queue_draw(GRITS_OBJECT(tile)); + + return TRUE; +} + +/** + * grits_tile_load_file: + * @tile: the tile to load data into + * @file: path to an image file to load + * + * Load tile data from an image file + * This function is thread safe and my be called from outside the main thread. + * + * Returns: TRUE if the image was loaded successfully + */ +gboolean grits_tile_load_file(GritsTile *tile, const gchar *file) +{ + g_debug("GritsTile: load_file %p -> %s", tile, file); + /* Copy pixbuf data for callback */ + tile->pixbuf = gdk_pixbuf_new_from_file(file, NULL); + if (!tile->pixbuf) + return FALSE; + tile->width = gdk_pixbuf_get_width(tile->pixbuf); + tile->height = gdk_pixbuf_get_height(tile->pixbuf); + tile->alpha = gdk_pixbuf_get_has_alpha(tile->pixbuf); + + /* Queue OpenGL texture load/draw */ + grits_object_queue_draw(GRITS_OBJECT(tile)); + + return TRUE; } /** @@ -230,7 +342,7 @@ void grits_tile_update(GritsTile *tile, GritsPoint *eye, * * Locate the subtile with the highest resolution which contains the given * lat/lon point. - * + * * Returns: the child tile */ GritsTile *grits_tile_find(GritsTile *root, gdouble lat, gdouble lon) @@ -288,10 +400,23 @@ 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); + int thread_safe = !root->load || root->data || root->tex || root->pixels || root->pixbuf; + if (root->parent && !has_children && root->atime < atime && thread_safe) { + //g_debug("GritsTile: gc/free - %p", root); + if (root->pixbuf) + g_object_unref(root->pixbuf); + if (root->pixels) + g_free(root->pixels); + if (root->tex) + glDeleteTextures(1, &root->tex); + if (root->data) { + if (free_func) + free_func(root, user_data); + else + g_free(root->data); + } g_object_unref(root); return NULL; } @@ -319,14 +444,88 @@ void grits_tile_free(GritsTile *root, GritsTileFreeFunc free_func, gpointer user g_object_unref(root); } +/* 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; + 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, 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; +} + +/* Load the texture from saved pixel data */ +static gboolean _grits_tile_load_tex(GritsTile *tile) +{ + /* Abort for null tiles */ + if (!tile) + return FALSE; + + /* Defer loading of hidden tiles */ + if (GRITS_OBJECT(tile)->hidden) + return FALSE; + + /* If we're already done loading the text stop */ + if (tile->tex) + return TRUE; + + /* Check if the tile has data yet */ + if (!tile->pixels && !tile->pixbuf) + return FALSE; + + /* Get correct pixel buffer */ + guchar *pixels = tile->pixels ?: + gdk_pixbuf_get_pixels(tile->pixbuf); + + /* Create texture */ + g_debug("GritsTile: load_tex"); + glGenTextures(1, &tile->tex); + glBindTexture(GL_TEXTURE_2D, tile->tex); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, 4, tile->width, tile->height, 0, + (tile->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + /* Free data */ + if (tile->pixbuf) { + g_object_unref(tile->pixbuf); + tile->pixbuf = NULL; + } + if (tile->pixels) { + g_free(tile->pixels); + tile->pixels = NULL; + } + + return TRUE; + +} + /* Draw a single tile */ static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *triangles) { - if (!tile || !tile->data) + if (!tile || !tile->tex) return; 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); + //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); tile->atime = time(NULL); @@ -342,6 +541,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; @@ -384,66 +585,81 @@ static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *tri 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); + /* Draw triangle */ + glBindTexture(GL_TEXTURE_2D, tile->tex); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 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); + 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(); } } /* Draw the tile */ -static void grits_tile_draw_rec(GritsTile *tile, GritsOpenGL *opengl) +static gboolean grits_tile_draw_rec(GritsTile *tile, GritsOpenGL *opengl) { - /* Only draw children if possible */ - gboolean has_children = FALSE; - GritsTile *child; + //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 (!_grits_tile_load_tex(tile)) + return FALSE; + + GritsTile *child = NULL; + + /* Draw child tiles */ + gboolean draw_parent = FALSE; grits_tile_foreach(tile, child) - if (child && child->data) - has_children = TRUE; + if (!grits_tile_draw_rec(child, opengl)) + draw_parent = TRUE; - GList *triangles = NULL; - if (has_children && !GRITS_OBJECT(tile)->hidden) { - /* 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; - grits_tile_foreach_index(tile, row, col) { - GritsTile *child = tile->children[row][col]; - if (child && child->data) { - grits_tile_draw_rec(child, opengl); - } 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, + /* 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); - } - if (triangles) grits_tile_draw_one(tile, opengl, triangles); - g_list_free(triangles); + g_list_free(triangles); + } + + return TRUE; } static void grits_tile_draw(GritsObject *tile, GritsOpenGL *opengl) { glEnable(GL_DEPTH_TEST); 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); }