+static void _draw_tile(GisOpenGL *self, GisTile *tile)
+{
+ if (!tile || !tile->data)
+ return;
+ GList *triangles = roam_sphere_get_intersect(self->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);
+ for (GList *cur = triangles; cur; cur = cur->next) {
+ RoamTriangle *tri = cur->data;
+
+ gdouble lat[3] = {tri->p.r->lat, tri->p.m->lat, tri->p.l->lat};
+ gdouble lon[3] = {tri->p.r->lon, tri->p.m->lon, tri->p.l->lon};
+
+ if (lon[0] < -90 || lon[1] < -90 || lon[2] < -90) {
+ if (lon[0] > 90) lon[0] -= 360;
+ if (lon[1] > 90) lon[1] -= 360;
+ 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},
+ {(lon[2]-w)/londist, 1-(lat[2]-s)/latdist},
+ };
+
+ //if ((lat[0] == 90 && (xy[0][0] < 0 || xy[0][0] > 1)) ||
+ // (lat[1] == 90 && (xy[1][0] < 0 || xy[1][0] > 1)) ||
+ // (lat[2] == 90 && (xy[2][0] < 0 || xy[2][0] > 1)))
+ // g_message("w,e=%4.f,%4.f "
+ // "lat,lon,x,y="
+ // "%4.1f,%4.0f,%4.2f,%4.2f "
+ // "%4.1f,%4.0f,%4.2f,%4.2f "
+ // "%4.1f,%4.0f,%4.2f,%4.2f ",
+ // w,e,
+ // lat[0], lon[0], xy[0][0], xy[0][1],
+ // lat[1], lon[1], xy[1][0], xy[1][1],
+ // lat[2], lon[2], xy[2][0], xy[2][1]);
+
+ /* Fix poles */
+ if (lat[0] == 90 || lat[0] == -90) xy[0][0] = 0.5;
+ if (lat[1] == 90 || lat[1] == -90) xy[1][0] = 0.5;
+ if (lat[2] == 90 || lat[2] == -90) xy[2][0] = 0.5;
+
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, *(guint*)tile->data);
+ 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 *self, GisTile *tile)
+{
+ /* Only draw children if possible */
+ gboolean has_children = TRUE;
+ 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(self, child);
+ else
+ /* No children, draw this tile */
+ _draw_tile(self, tile);
+}
+