]> Pileus Git - grits/blobdiff - src/objects/gis-object.c
Move OpenGL code from GisOpenGL to objects
[grits] / src / objects / gis-object.c
index 84e5c4722eb3d07af753736cac19f80e2b8921a1..735e62e2cb78ff5d8e0b9c964258d0115ce1f100 100644 (file)
@@ -17,7 +17,7 @@
 
 /**
  * SECTION:gis-object
- * @short_description: Base classes for drawing operations
+ * @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
  */
 
 #include <config.h>
+#include <math.h>
+#include <GL/gl.h>
+
 #include "gis-object.h"
 
-/************
- * GisPoint *
- ************/
+
+/*************
+ * GisObject *
+ *************/
 /**
- * gis_point_new:
+ * gis_object_draw:
+ * @object: the object
+ * @opengl: the viewer the object is being displayed in
  *
- * Create a new #GisPoint
+ * Perform any OpenGL commands necessasairy to draw the object.
  *
- * Returns: the new point
+ * The GL_PROJECTION and GL_MODELVIEW matricies and GL_ALL_ATTRIB_BITS will be
+ * restored to the default state after the call to draw.
  */
-GisPoint *gis_point_new()
+void gis_object_draw(GisObject *object, GisOpenGL *opengl)
 {
-       return g_new0(GisPoint, 1);
-}
+       GisObjectClass *klass = GIS_OBJECT_GET_CLASS(object);
+       if (!klass->draw) {
+               g_warning("GisObject: draw - Unimplemented");
+               return;
+       }
 
-/**
- * gis_point_set_lle:
- * @point: the point to modify
- * @lat:   the new latitude
- * @lon:   the new longitude
- * @elev:  the new elevation
- *
- * Set the latitude, longitude, and elevation for a point.
- */
-void gis_point_set_lle(GisPoint *point, gdouble lat, gdouble lon, gdouble elev)
-{
-       point->lat  = lat;
-       point->lon  = lon;
-       point->elev = elev;
-}
+       /* Skip hidden objects */
+       if (object->hidden)
+               return;
 
-/**
- * gis_point_free:
- * @point: The point to free
- *
- * Free data used by a #GisPoint
- */
-void gis_point_free(GisPoint *point)
-{
-       g_free(point);
-}
+       /* 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);
+}
 
-/*************
- * GisObject *
- *************/
 /* GObject stuff */
 G_DEFINE_ABSTRACT_TYPE(GisObject, gis_object, G_TYPE_OBJECT);
 static void gis_object_init(GisObject *object)