+#ifdef ROAM_DEBUG
+ glBegin(GL_TRIANGLES);
+ glNormal3dv(tri->p.r->norm); glVertex3dv((double*)tri->p.r);
+ glNormal3dv(tri->p.m->norm); glVertex3dv((double*)tri->p.m);
+ glNormal3dv(tri->p.l->norm); glVertex3dv((double*)tri->p.l);
+ glEnd();
+ return;
+#endif
+
+ 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 lat_avg = (lat_min+lat_max)/2;
+ gdouble lon_max = MAX(MAX(lon[0], lon[1]), lon[2]);
+ gdouble lon_min = MIN(MIN(lon[0], lon[1]), lon[2]);
+ gdouble lon_avg = (lon_min+lon_max)/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 */
+ tri->nodes[0] = wms_info_fetch_cache(self->bmng, tri->nodes[0], res, lat_min, lon_min, NULL, roam_queue_draw, self);
+ tri->nodes[1] = wms_info_fetch_cache(self->bmng, tri->nodes[1], res, lat_max, lon_min, NULL, roam_queue_draw, self);
+ tri->nodes[2] = wms_info_fetch_cache(self->bmng, tri->nodes[2], res, lat_min, lon_max, NULL, roam_queue_draw, self);
+ tri->nodes[3] = wms_info_fetch_cache(self->bmng, tri->nodes[3], res, lat_max, lon_max, NULL, roam_queue_draw, self);
+ tri->nodes[4] = wms_info_fetch_cache(self->bmng, tri->nodes[4], res, lat_avg, lon_avg, NULL, roam_queue_draw, self);
+ /* Hopefully get all textures at the same resolution to prevent overlaps */
+ //gdouble maxres = 0;
+ //for (int i = 0; i < 5; i++)
+ // if (tri->nodes[i] && tri->nodes[i]->res > maxres)
+ // maxres = tri->nodes[i]->res;
+ //if (maxres != 0) {
+ // tri->nodes[0] = wms_info_fetch_cache(self->bmng, tri->nodes[0], maxres, lat_min, lon_min, NULL, roam_queue_draw, self);
+ // tri->nodes[1] = wms_info_fetch_cache(self->bmng, tri->nodes[1], maxres, lat_max, lon_min, NULL, roam_queue_draw, self);
+ // tri->nodes[2] = wms_info_fetch_cache(self->bmng, tri->nodes[2], maxres, lat_min, lon_max, NULL, roam_queue_draw, self);
+ // tri->nodes[3] = wms_info_fetch_cache(self->bmng, tri->nodes[3], maxres, lat_max, lon_max, NULL, roam_queue_draw, self);
+ // tri->nodes[4] = wms_info_fetch_cache(self->bmng, tri->nodes[4], maxres, lat_avg, lon_avg, 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 < 5; i++) {
+ /* Skip missing textures */
+ if (tri->nodes[i] == NULL)
+ continue;
+ /* Skip already drawn textures */
+ switch (i) {
+ case 4: if (tri->nodes[i] == tri->nodes[3]) continue;
+ case 3: if (tri->nodes[i] == tri->nodes[2]) continue;
+ case 2: if (tri->nodes[i] == tri->nodes[1]) continue;
+ case 1: if (tri->nodes[i] == tri->nodes[0]) continue;
+ }
+
+ WmsCacheNode *node = tri->nodes[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);
+ }