]> Pileus Git - grits/blob - src/objects/grits-object.c
Allow disabling operations in grits_object_draw
[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:grits-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 #GritsObject is also a #GObject, but not every GObject in grits is a
28  * GritsObject. 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  * grits_object_draw:
40  * @object: the object
41  * @opengl: the viewer the object is being displayed in
42  *
43  * Perform any OpenGL commands necessasairy to draw the object.
44  *
45  * The GL_PROJECTION and GL_MODELVIEW matricies and GL_ALL_ATTRIB_BITS will be
46  * restored to the default state after the call to draw.
47  */
48 void grits_object_draw(GritsObject *object, GritsOpenGL *opengl)
49 {
50         GritsObjectClass *klass = GRITS_OBJECT_GET_CLASS(object);
51         if (!klass->draw) {
52                 g_warning("GritsObject: draw - Unimplemented");
53                 return;
54         }
55
56         /* Skip hidden objects */
57         if (object->hidden)
58                 return;
59
60         /* Support GritsTester */
61         if (!GRITS_IS_OPENGL(opengl)) {
62                 g_debug("GritsObject: draw - drawing raw object");
63                 klass->draw(object, opengl);
64                 return;
65         }
66
67         /* Calculae distance for LOD and horizon tests */
68         GritsPoint *center = &object->center;
69         if ((!(object->skip & GRITS_SKIP_LOD) ||
70              !(object->skip & GRITS_SKIP_HORIZON)) &&
71             (center->elev != -EARTH_R)) {
72                 /* LOD test */
73                 gdouble eye[3], obj[3];
74                 grits_viewer_get_location(GRITS_VIEWER(opengl),
75                                 &eye[0], &eye[1], &eye[2]);
76                 gdouble elev = eye[2];
77                 lle2xyz(eye[0], eye[1], eye[2],
78                                 &eye[0], &eye[1], &eye[2]);
79                 lle2xyz(center->lat, center->lon, center->elev,
80                                 &obj[0], &obj[1], &obj[2]);
81                 gdouble dist = distd(obj, eye);
82
83                 /* Level of detail test */
84                 if (!(object->skip & GRITS_SKIP_LOD)
85                                 && object->lod > 0) {
86                         if (object->lod < dist)
87                                 return;
88                 }
89
90                 /* Horizon test */
91                 if (!(object->skip & GRITS_SKIP_HORIZON)) {
92                         gdouble c = EARTH_R+elev;
93                         gdouble a = EARTH_R;
94                         gdouble horizon = sqrt(c*c - a*a);
95                         if (dist > horizon)
96                                 return;
97                 }
98         }
99
100         /* Save state, draw, restore state */
101         g_mutex_lock(opengl->sphere_lock);
102         if (!(object->skip & GRITS_SKIP_STATE)) {
103                 glPushAttrib(GL_ALL_ATTRIB_BITS);
104                 glMatrixMode(GL_PROJECTION); glPushMatrix();
105                 glMatrixMode(GL_MODELVIEW);  glPushMatrix();
106         }
107
108         if (!(object->skip & GRITS_SKIP_CENTER))
109                 grits_viewer_center_position(GRITS_VIEWER(opengl),
110                                 object->center.lat,
111                                 object->center.lon,
112                                 object->center.elev);
113
114         klass->draw(object, opengl);
115
116         if (!(object->skip & GRITS_SKIP_STATE)) {
117                 glPopAttrib();
118                 glMatrixMode(GL_PROJECTION); glPopMatrix();
119                 glMatrixMode(GL_MODELVIEW);  glPopMatrix();
120         }
121         g_mutex_unlock(opengl->sphere_lock);
122 }
123
124 void grits_object_queue_draw(GritsObject *object)
125 {
126         if (object->viewer)
127                 gtk_widget_queue_draw(GTK_WIDGET(object->viewer));
128 }
129
130 /* GObject stuff */
131 G_DEFINE_ABSTRACT_TYPE(GritsObject, grits_object, G_TYPE_OBJECT);
132 static void grits_object_init(GritsObject *object)
133 {
134         object->center.lat  =  0;
135         object->center.lon  =  0;
136         object->center.elev = -EARTH_R;
137 }
138
139 static void grits_object_class_init(GritsObjectClass *klass)
140 {
141 }