+ GisOpenGL *self = _self;
+ if (tri->error < 0) return;
+
+ /* Get lat-lon min and maxes for the triangle */
+ gdouble lat[3], lon[3], elev[3];
+ xyz2lle(tri->p.r->x, tri->p.r->y, tri->p.r->z, &lat[0], &lon[0], &elev[0]);
+ xyz2lle(tri->p.m->x, tri->p.m->y, tri->p.m->z, &lat[1], &lon[1], &elev[1]);
+ xyz2lle(tri->p.l->x, tri->p.l->y, tri->p.l->z, &lat[2], &lon[2], &elev[2]);
+ gdouble lat_max = MAX(MAX(lat[0], lat[1]), lat[2]);
+ gdouble lat_min = MIN(MIN(lat[0], lat[1]), lat[2]);
+ gdouble lon_max = MAX(MAX(lon[0], lon[1]), lon[2]);
+ gdouble lon_min = MIN(MIN(lon[0], lon[1]), lon[2]);
+
+ /* Get target resolution */
+ gdouble cam_lle[3], cam_xyz[3];
+ gis_view_get_location(self->view, &cam_lle[0], &cam_lle[1], &cam_lle[2]);
+ lle2xyz(cam_lle[0], cam_lle[1], cam_lle[2], &cam_xyz[0], &cam_xyz[1], &cam_xyz[2]);
+ gdouble distr = distd(cam_xyz, (double*)tri->p.r);
+ gdouble distm = distd(cam_xyz, (double*)tri->p.m);
+ gdouble distl = distd(cam_xyz, (double*)tri->p.l);
+ double res = MPPX(MIN(MIN(distr, distm), distl));
+
+ /* TODO:
+ * - Fetch needed textures, not all corners
+ * - Also fetch center textures that aren't touched by a corner
+ * - Idea: send {lat,lon}{min,max} to fetch_cache and handle it in the recursion */
+ /* Fetch textures */
+ WmsCacheNode *textures[4] = {
+ wms_info_fetch_cache(self->bmng, res, lat_min, lon_min, NULL, roam_queue_draw, self),
+ wms_info_fetch_cache(self->bmng, res, lat_max, lon_min, NULL, roam_queue_draw, self),
+ wms_info_fetch_cache(self->bmng, res, lat_min, lon_max, NULL, roam_queue_draw, self),
+ wms_info_fetch_cache(self->bmng, res, lat_max, lon_max, NULL, roam_queue_draw, self),
+ };
+
+ /* Vertex color for hieght map viewing, 8848m == Everest */
+ gfloat colors[] = {
+ (elev[0]-EARTH_R)/8848,
+ (elev[1]-EARTH_R)/8848,
+ (elev[2]-EARTH_R)/8848,
+ };
+
+ /* Draw each texture */
+ /* TODO: Prevent double exposure when of hi-res textures on top of
+ * low-res textures when some high-res textures are not yet loaded. */
+ glBlendFunc(GL_ONE, GL_ZERO);
+ for (int i = 0; i < 4; i++) {
+ /* Skip missing textures */
+ if (textures[i] == NULL)
+ continue;
+ /* Skip already drawn textures */
+ switch (i) {
+ case 3: if (textures[i] == textures[2]) continue;
+ case 2: if (textures[i] == textures[1]) continue;
+ case 1: if (textures[i] == textures[0]) continue;
+ }
+
+ WmsCacheNode *node = textures[i];
+
+ if (node->latlon[0] == -180) {
+ 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;
+ }
+ } else if (node->latlon[2] == 180.0) {
+ if (lon[0] < -90) lon[0] += 360;
+ if (lon[1] < -90) lon[1] += 360;
+ if (lon[2] < -90) lon[2] += 360;
+ }
+
+ gdouble xmin = node->latlon[0];
+ gdouble ymin = node->latlon[1];
+ gdouble xmax = node->latlon[2];
+ gdouble ymax = node->latlon[3];
+
+ gdouble xdist = xmax - xmin;
+ gdouble ydist = ymax - ymin;
+
+ gdouble xy[][3] = {
+ {(lon[0]-xmin)/xdist, 1-(lat[0]-ymin)/ydist},
+ {(lon[1]-xmin)/xdist, 1-(lat[1]-ymin)/ydist},
+ {(lon[2]-xmin)/xdist, 1-(lat[2]-ymin)/ydist},
+ };
+
+ glBindTexture(GL_TEXTURE_2D, *(guint*)node->data);
+
+ glBegin(GL_TRIANGLES);
+ glColor3fv(colors); glNormal3dv(tri->p.r->norm); glTexCoord2dv(xy[0]); glVertex3dv((double*)tri->p.r);
+ glColor3fv(colors); glNormal3dv(tri->p.m->norm); glTexCoord2dv(xy[1]); glVertex3dv((double*)tri->p.m);
+ glColor3fv(colors); glNormal3dv(tri->p.l->norm); glTexCoord2dv(xy[2]); glVertex3dv((double*)tri->p.l);
+ glEnd();
+ glBlendFunc(GL_ONE, GL_ONE);
+ }