+
+ roam_sphere_update_view(self->sphere);
+}
+
+
+/********************
+ * Object handleing *
+ ********************/
+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);
+}
+
+static void _draw_marker(GisOpenGL *self, GisMarker *marker)
+{
+ GisPoint *point = gis_object_center(GIS_OBJECT(marker));
+ gdouble px, py, pz;
+ gis_viewer_project(GIS_VIEWER(self),
+ point->lat, point->lon, point->elev,
+ &px, &py, &pz);
+ if (pz > 1)
+ return;
+
+ //g_debug("GisOpenGL: draw_marker - %s pz=%f ", marker->label, pz);
+
+ cairo_surface_t *surface = cairo_get_target(marker->cairo);
+ gdouble width = cairo_image_surface_get_width(surface);
+ gdouble height = cairo_image_surface_get_height(surface);
+
+ glMatrixMode(GL_PROJECTION); glLoadIdentity();
+ glMatrixMode(GL_MODELVIEW); glLoadIdentity();
+ glOrtho(0, GTK_WIDGET(self)->allocation.width,
+ 0, GTK_WIDGET(self)->allocation.height, -1, 1);
+ glTranslated(px - marker->xoff,
+ py - marker->yoff, 0);
+
+ glDisable(GL_LIGHTING);
+ glDisable(GL_COLOR_MATERIAL);
+ glDisable(GL_DEPTH_TEST);
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, marker->tex);
+ glBegin(GL_QUADS);
+ glTexCoord2f(1, 1); glVertex3f(width, 0 , 0);
+ glTexCoord2f(1, 0); glVertex3f(width, height, 0);
+ glTexCoord2f(0, 0); glVertex3f(0 , height, 0);
+ glTexCoord2f(0, 1); glVertex3f(0 , 0 , 0);
+ glEnd();
+}
+
+static void _draw_callback(GisOpenGL *self, GisCallback *callback)
+{
+ callback->callback(callback, callback->user_data);
+}
+
+static void _draw_object(GisOpenGL *self, GisObject *object)
+{
+ //g_debug("GisOpenGL: draw_object");
+ /* Skip out of range objects */
+ if (object->lod > 0) {
+ gdouble eye[3], obj[3];
+ gis_viewer_get_location(GIS_VIEWER(self), &eye[0], &eye[1], &eye[2]);
+ lle2xyz(eye[0], eye[1], eye[2], &eye[0], &eye[1], &eye[2]);
+ lle2xyz(object->center.lat, object->center.lon, object->center.elev,
+ &obj[0], &obj[1], &obj[2]);
+ gdouble dist = distd(obj, eye);
+ if (object->lod < dist)
+ return;
+ }
+
+ /* Draw */
+ glMatrixMode(GL_PROJECTION); glPushMatrix();
+ glMatrixMode(GL_MODELVIEW); glPushMatrix();
+ glPushAttrib(GL_ALL_ATTRIB_BITS);
+ if (GIS_IS_MARKER(object)) {
+ _draw_marker(self, GIS_MARKER(object));
+ } else if (GIS_IS_CALLBACK(object)) {
+ _draw_callback(self, GIS_CALLBACK(object));
+ } else if (GIS_IS_TILE(object)) {
+ _draw_tiles(self, GIS_TILE(object));
+ }
+ glPopAttrib();
+ glMatrixMode(GL_PROJECTION); glPopMatrix();
+ glMatrixMode(GL_MODELVIEW); glPopMatrix();
+}
+
+static void _load_object(GisOpenGL *self, GisObject *object)
+{
+ g_debug("GisOpenGL: load_object");
+ if (GIS_IS_MARKER(object)) {
+ GisMarker *marker = GIS_MARKER(object);
+ cairo_surface_t *surface = cairo_get_target(marker->cairo);
+ gdouble width = cairo_image_surface_get_width(surface);
+ gdouble height = cairo_image_surface_get_height(surface);
+
+ _gis_opengl_begin(self);
+ glEnable(GL_TEXTURE_2D);
+ glGenTextures(1, &marker->tex);
+ glBindTexture(GL_TEXTURE_2D, marker->tex);
+
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glPixelStorei(GL_PACK_ALIGNMENT, 1);
+ glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
+ cairo_image_surface_get_data(surface));
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ g_debug("load_texture: %d", marker->tex);
+ _gis_opengl_end(self);
+ }
+}
+
+static void _unload_object(GisOpenGL *self, GisObject *object)
+{
+ g_debug("GisOpenGL: unload_object");
+ if (GIS_IS_MARKER(object)) {
+ GisMarker *marker = GIS_MARKER(object);
+ g_debug("delete_texture: %d", marker->tex);
+ glDeleteTextures(1, &marker->tex);
+ }