]> Pileus Git - grits/blobdiff - src/objects/gis-object.c
Move OpenGL code from GisOpenGL to objects
[grits] / src / objects / gis-object.c
index fe51eb13b0e91c2c39afa4d7bb0a71767d93d09a..735e62e2cb78ff5d8e0b9c964258d0115ce1f100 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <glib.h>
-#include <GL/glu.h>
-
-#include "gis-object.h"
-
-/* GisPoint */
-GisPoint *gis_point_new()
-{
-       return g_new0(GisPoint, 1);
-}
-
-void gis_point_set_lle(GisPoint *self, gdouble lat, gdouble lon, gdouble elev)
-{
-       self->lat  = lat;
-       self->lon  = lon;
-       self->elev = elev;
-}
-
-void gis_point_free(GisPoint *self)
-{
-       g_free(self);
-}
-
-
-/* GisObject */
-G_DEFINE_TYPE(GisObject, gis_object, G_TYPE_OBJECT);
-static void gis_object_init(GisObject *self) { }
-static void gis_object_class_init(GisObjectClass *klass) { }
+/**
+ * SECTION:gis-object
+ * @short_description: Base class for drawing operations
+ *
+ * Objects in libgis are things which can be added to the viewer and will be
+ * displayed to the user. Each object has information such as it's location and
+ * level of detail which are used by the viewer to determine which objects
+ * should be drawn.
+ *
+ * Each #GisObject is also a #GObject, but not every GObject in libgis is a
+ * GisObject. The "Object" part of the name is just coincidence.
+ */
 
+#include <config.h>
+#include <math.h>
+#include <GL/gl.h>
 
-/* GisMarker */
-G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
-static void gis_marker_init(GisMarker *self) { }
+#include "gis-object.h"
 
-static void gis_marker_finalize(GObject *_self);
-static void gis_marker_class_init(GisMarkerClass *klass)
-{
-       G_OBJECT_CLASS(klass)->finalize = gis_marker_finalize;
-}
 
-GisMarker *gis_marker_new(const gchar *label)
+/*************
+ * GisObject *
+ *************/
+/**
+ * gis_object_draw:
+ * @object: the object
+ * @opengl: the viewer the object is being displayed in
+ *
+ * Perform any OpenGL commands necessasairy to draw the object.
+ *
+ * The GL_PROJECTION and GL_MODELVIEW matricies and GL_ALL_ATTRIB_BITS will be
+ * restored to the default state after the call to draw.
+ */
+void gis_object_draw(GisObject *object, GisOpenGL *opengl)
 {
-       static const int RADIUS =   4;
-       static const int WIDTH  = 100;
-       static const int HEIGHT =  20;
-
-       GisMarker *self = g_object_new(GIS_TYPE_MARKER, NULL);
-       self->xoff  = RADIUS;
-       self->yoff  = HEIGHT-RADIUS;
-       self->label = g_strdup(label);
-       self->cairo = cairo_create(cairo_image_surface_create(
-                               CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT));
-       cairo_set_source_rgba(self->cairo, 1, 1, 1, 1);
-       cairo_arc(self->cairo, self->xoff, self->yoff, RADIUS, 0, 2*G_PI);
-       cairo_fill(self->cairo);
-       cairo_move_to(self->cairo, self->xoff+4, self->yoff-8);
-       cairo_set_font_size(self->cairo, 10);
-       cairo_show_text(self->cairo, self->label);
-       return self;
+       GisObjectClass *klass = GIS_OBJECT_GET_CLASS(object);
+       if (!klass->draw) {
+               g_warning("GisObject: draw - Unimplemented");
+               return;
+       }
+
+       /* Skip hidden objects */
+       if (object->hidden)
+               return;
+
+       /* Skip out of range objects */
+       if (object->lod > 0) {
+               /* LOD test */
+               gdouble eye[3], obj[3];
+               gis_viewer_get_location(GIS_VIEWER(opengl), &eye[0], &eye[1], &eye[2]);
+               gdouble elev = 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;
+
+               /* Horizon testing */
+               gdouble c = EARTH_R+elev;
+               gdouble a = EARTH_R;
+               gdouble horizon = sqrt(c*c - a*a);
+               if (dist > horizon)
+                       return;
+       }
+
+       /* Save state, draw, restore state */
+       g_mutex_lock(opengl->sphere_lock);
+       glPushAttrib(GL_ALL_ATTRIB_BITS);
+       glMatrixMode(GL_PROJECTION); glPushMatrix();
+       glMatrixMode(GL_MODELVIEW);  glPushMatrix();
+
+       klass->draw(object, opengl);
+
+       glPopAttrib();
+       glMatrixMode(GL_PROJECTION); glPopMatrix();
+       glMatrixMode(GL_MODELVIEW);  glPopMatrix();
+       g_mutex_unlock(opengl->sphere_lock);
 }
 
-static void gis_marker_finalize(GObject *_self)
+/* GObject stuff */
+G_DEFINE_ABSTRACT_TYPE(GisObject, gis_object, G_TYPE_OBJECT);
+static void gis_object_init(GisObject *object)
 {
-       GisMarker *self = GIS_MARKER(_self);
-       cairo_surface_destroy(cairo_get_target(self->cairo));
-       cairo_destroy(self->cairo);
-       g_free(self->label);
-       g_free(self);
 }
 
-
-/* GisCallback */
-G_DEFINE_TYPE(GisCallback, gis_callback, GIS_TYPE_OBJECT);
-static void gis_callback_init(GisCallback *self) { }
-static void gis_callback_class_init(GisCallbackClass *klass) { }
-
-GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data)
+static void gis_object_class_init(GisObjectClass *klass)
 {
-       GisCallback *self = g_object_new(GIS_TYPE_CALLBACK, NULL);
-       self->callback  = callback;
-       self->user_data = user_data;
-       return self;
 }