]> Pileus Git - grits/blobdiff - src/objects/grits-tile.c
Fix bugs in GritsTile
[grits] / src / objects / grits-tile.c
index c8326daeb5ec998d17694c96e288d115e7abce7e..736e374df12812826bb053e30357994c72fe1267 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2010, 2012 Andy Spencer <andy753421@gmail.com>
  *
  * 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
  */
 
 #include <config.h>
+#include <math.h>
 #include "gtkgl.h"
 #include "grits-tile.h"
 
+guint  grits_tile_mask = 0;
+
 gchar *grits_tile_path_table[2][2] = {
        {"00.", "01."},
        {"10.", "11."},
@@ -61,6 +64,8 @@ GritsTile *grits_tile_new(GritsTile *parent,
        tile->atime  = time(NULL);
        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;
        return tile;
 }
 
@@ -117,22 +122,59 @@ 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.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;
 }
 
+static void _grits_tile_split_latlon(GritsTile *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;
+       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)
+               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
+}
+
+static void _grits_tile_split_mercator(GritsTile *tile)
+{
+       GritsTile *child = NULL;
+       GritsBounds tmp = tile->edge;
+
+       /* Project */
+       tile->edge.n = asinh(tan(deg2rad(tile->edge.n)));
+       tile->edge.s = asinh(tan(deg2rad(tile->edge.s)));
+
+       _grits_tile_split_latlon(tile);
+
+       /* Convert back to lat-lon */
+       tile->edge = tmp;
+       grits_tile_foreach(tile, child) {
+               child->edge.n = rad2deg(atan(sinh(child->edge.n)));
+               child->edge.s = rad2deg(atan(sinh(child->edge.s)));
+       }
+}
+
 /**
  * grits_tile_update:
  * @root:      the root tile to split
@@ -149,43 +191,44 @@ static gboolean _grits_tile_precise(GritsPoint *eye, GritsBounds *bounds,
  * the tile is recursively subdivided until a sufficient resolution is
  * achieved.
  */
-void grits_tile_update(GritsTile *root, GritsPoint *eye,
+void grits_tile_update(GritsTile *tile, GritsPoint *eye,
                gdouble res, gint width, gint height,
                GritsTileLoadFunc load_func, gpointer user_data)
 {
-       root->atime = time(NULL);
        //g_debug("GritsTile: update - %p->atime = %u",
-       //              root, (guint)root->atime);
-       const gdouble rows = G_N_ELEMENTS(root->children);
-       const gdouble cols = G_N_ELEMENTS(root->children[0]);
-       const gdouble lat_dist = root->edge.n - root->edge.s;
-       const gdouble lon_dist = root->edge.e - root->edge.w;
-       const gdouble lat_step = lat_dist / rows;
-       const gdouble lon_step = lon_dist / cols;
-       int row, col;
-       grits_tile_foreach_index(root, row, col) {
-               GritsBounds edge;
-               edge.n = root->edge.n-(lat_step*(row+0));
-               edge.s = root->edge.n-(lat_step*(row+1));
-               edge.e = root->edge.w+(lon_step*(col+1));
-               edge.w = root->edge.w+(lon_step*(col+0));
-
-               GritsTile **child = &root->children[row][col];
-               if (!_grits_tile_precise(eye, &edge, res,
-                               width/cols, height/rows)) {
-                       if (!*child) {
-                               *child = grits_tile_new(root, edge.n, edge.s,
-                                               edge.e, edge.w);
-                               load_func(*child, user_data);
-                       }
-                       grits_tile_update(*child, eye,
-                                       res, width, height,
-                                       load_func, user_data);
-                       GRITS_OBJECT(*child)->hidden = FALSE;
-               } else if (*child) {
-                       GRITS_OBJECT(*child)->hidden = TRUE;
+       //              tile, (guint)tile->atime);
+       GritsTile *child;
+
+       if (tile == NULL)
+               return;
+
+       if (!tile->data)
+               load_func(tile, user_data);
+
+       tile->atime = time(NULL);
+
+       /* 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;
+               return;
+       }
+
+       /* Need more resolution, split tile and update recursively */
+       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;
                }
        }
+
+       grits_tile_foreach(tile, child) {
+               GRITS_OBJECT(child)->hidden = FALSE;
+               grits_tile_update(child, eye, res, width, height,
+                               load_func, user_data);
+       }
+
 }
 
 /**
@@ -285,6 +328,25 @@ 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;
+       guint8 byte = 0xff;
+       glGenTextures(1, &tex);
+       glBindTexture(GL_TEXTURE_2D, tex);
+
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1, 1, 0,
+                       GL_ALPHA, GL_UNSIGNED_BYTE, &byte);
+
+       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);
+       return tex;
+}
+
 /* Draw a single tile */
 static void grits_tile_draw_one(GritsTile *tile, GritsOpenGL *opengl, GList *triangles)
 {
@@ -293,6 +355,9 @@ 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);
        tile->atime = time(NULL);
@@ -350,65 +415,84 @@ 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);
+               /* Polygon offset */
                glEnable(GL_POLYGON_OFFSET_FILL);
-               glBindTexture(GL_TEXTURE_2D, *(guint*)tile->data);
                glPolygonOffset(0, -tile->zindex);
+
+               /* Setup texture */
+               glActiveTexture(GL_TEXTURE0);
+               glEnable(GL_TEXTURE_2D);
+               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); 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();
+
+               /* Disable texture mask */
+               glDisable(GL_TEXTURE_2D);
+               glActiveTexture(GL_TEXTURE0);
        }
 }
 
 /* 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;
-       grits_tile_foreach(tile, child)
-               if (child && child->data)
-                       has_children = 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);
-                       }
+       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);
                }
-       } else {
-               triangles = roam_sphere_get_intersect(opengl->sphere, FALSE,
-                               tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w);
+
+               /* 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;
        }
-       if (triangles)
-               grits_tile_draw_one(tile, opengl, 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);
        grits_tile_draw_rec(GRITS_TILE(tile), opengl);
 }