]> Pileus Git - grits/blob - src/objects/grits-object.c
libgis -> grits: Update non-technical references
[grits] / src / objects / grits-object.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gis-object
20  * @short_description: Base class for drawing operations
21  *
22  * Objects in grits are things which can be added to the viewer and will be
23  * displayed to the user. Each object has information such as it's location and
24  * level of detail which are used by the viewer to determine which objects
25  * should be drawn.
26  *
27  * Each #GisObject is also a #GObject, but not every GObject in grits is a
28  * GisObject. The "Object" part of the name is just coincidence.
29  */
30
31 #include <config.h>
32 #include <math.h>
33 #include <GL/gl.h>
34
35 #include "grits-object.h"
36
37
38 /*************
39  * GisObject *
40  *************/
41 /**
42  * gis_object_draw:
43  * @object: the object
44  * @opengl: the viewer the object is being displayed in
45  *
46  * Perform any OpenGL commands necessasairy to draw the object.
47  *
48  * The GL_PROJECTION and GL_MODELVIEW matricies and GL_ALL_ATTRIB_BITS will be
49  * restored to the default state after the call to draw.
50  */
51 void gis_object_draw(GisObject *object, GisOpenGL *opengl)
52 {
53         GisObjectClass *klass = GIS_OBJECT_GET_CLASS(object);
54         if (!klass->draw) {
55                 g_warning("GisObject: draw - Unimplemented");
56                 return;
57         }
58
59         /* Skip hidden objects */
60         if (object->hidden)
61                 return;
62
63         /* Skip out of range objects */
64         if (object->lod > 0) {
65                 /* LOD test */
66                 gdouble eye[3], obj[3];
67                 gis_viewer_get_location(GIS_VIEWER(opengl), &eye[0], &eye[1], &eye[2]);
68                 gdouble elev = eye[2];
69                 lle2xyz(eye[0], eye[1], eye[2], &eye[0], &eye[1], &eye[2]);
70                 lle2xyz(object->center.lat, object->center.lon, object->center.elev,
71                         &obj[0], &obj[1], &obj[2]);
72                 gdouble dist = distd(obj, eye);
73                 if (object->lod < dist)
74                         return;
75
76                 /* Horizon testing */
77                 gdouble c = EARTH_R+elev;
78                 gdouble a = EARTH_R;
79                 gdouble horizon = sqrt(c*c - a*a);
80                 if (dist > horizon)
81                         return;
82         }
83
84         /* Save state, draw, restore state */
85         g_mutex_lock(opengl->sphere_lock);
86         glPushAttrib(GL_ALL_ATTRIB_BITS);
87         glMatrixMode(GL_PROJECTION); glPushMatrix();
88         glMatrixMode(GL_MODELVIEW);  glPushMatrix();
89
90         klass->draw(object, opengl);
91
92         glPopAttrib();
93         glMatrixMode(GL_PROJECTION); glPopMatrix();
94         glMatrixMode(GL_MODELVIEW);  glPopMatrix();
95         g_mutex_unlock(opengl->sphere_lock);
96 }
97
98 /* GObject stuff */
99 G_DEFINE_ABSTRACT_TYPE(GisObject, gis_object, G_TYPE_OBJECT);
100 static void gis_object_init(GisObject *object)
101 {
102 }
103
104 static void gis_object_class_init(GisObjectClass *klass)
105 {
106 }