2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
19 * SECTION:grits-object
20 * @short_description: Base class for drawing operations
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
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.
35 #include "grits-object.h"
43 static guint signals[NUM_SIGNALS];
45 void grits_object_pickdraw(GritsObject *object, GritsOpenGL *opengl, gboolean pick)
47 GritsObjectClass *klass = GRITS_OBJECT_GET_CLASS(object);
49 g_warning("GritsObject: draw - Unimplemented");
53 /* Skip hidden objects */
57 /* Support GritsTester */
58 if (!GRITS_IS_OPENGL(opengl)) {
59 g_debug("GritsObject: draw - drawing raw object");
60 klass->draw(object, opengl);
64 /* Calculae distance for LOD and horizon tests */
65 GritsPoint *center = &object->center;
66 if ((!(object->skip & GRITS_SKIP_LOD) ||
67 !(object->skip & GRITS_SKIP_HORIZON)) &&
68 (center->elev != -EARTH_R)) {
70 gdouble eye[3], obj[3];
71 grits_viewer_get_location(GRITS_VIEWER(opengl),
72 &eye[0], &eye[1], &eye[2]);
73 gdouble elev = eye[2];
74 lle2xyz(eye[0], eye[1], eye[2],
75 &eye[0], &eye[1], &eye[2]);
76 lle2xyz(center->lat, center->lon, center->elev,
77 &obj[0], &obj[1], &obj[2]);
78 gdouble dist = distd(obj, eye);
80 /* Level of detail test */
81 if (!(object->skip & GRITS_SKIP_LOD)
83 if (object->lod < dist)
88 if (!(object->skip & GRITS_SKIP_HORIZON)) {
89 gdouble c = EARTH_R+elev;
91 gdouble horizon = sqrt(c*c - a*a);
97 /* Save state, draw, restore state */
98 g_mutex_lock(opengl->sphere_lock);
99 if (!(object->skip & GRITS_SKIP_STATE)) {
100 glPushAttrib(GL_ALL_ATTRIB_BITS);
101 glMatrixMode(GL_PROJECTION); glPushMatrix();
102 glMatrixMode(GL_MODELVIEW); glPushMatrix();
105 if (!(object->skip & GRITS_SKIP_CENTER))
106 grits_viewer_center_position(GRITS_VIEWER(opengl),
109 object->center.elev);
111 if (pick && klass->pick)
112 klass->pick(object, opengl);
114 klass->draw(object, opengl);
116 if (!(object->skip & GRITS_SKIP_STATE)) {
118 glMatrixMode(GL_PROJECTION); glPopMatrix();
119 glMatrixMode(GL_MODELVIEW); glPopMatrix();
121 g_mutex_unlock(opengl->sphere_lock);
126 * @object: the object
127 * @opengl: the viewer the object is being displayed in
129 * Perform any OpenGL commands necessasairy to draw the object.
131 * The GL_PROJECTION and GL_MODELVIEW matricies and GL_ALL_ATTRIB_BITS will be
132 * restored to the default state after the call to draw.
134 void grits_object_draw(GritsObject *object, GritsOpenGL *opengl)
136 grits_object_pickdraw(object, opengl, FALSE);
139 void grits_object_hide(GritsObject *object, gboolean hidden)
141 GritsObjectClass *klass = GRITS_OBJECT_GET_CLASS(object);
142 object->hidden = hidden;
144 klass->hide(object, hidden);
147 void grits_object_queue_draw(GritsObject *object)
150 gtk_widget_queue_draw(GTK_WIDGET(object->viewer));
154 void grits_object_pick_begin(GritsObject *object, GritsOpenGL *opengl)
156 object->state.picked = FALSE;
158 /* Check for connected signals */
159 for (int i = 0; i < NUM_SIGNALS; i++) {
160 if (g_signal_has_handler_pending(object, signals[i], 0, FALSE)) {
161 /* Someone is watching, render the object _once_ */
162 glPushName((guint)object);
163 grits_object_pickdraw(object, opengl, TRUE);
170 void grits_object_pick_pointer(GritsObject *object, double x, double y)
172 object->state.picked = TRUE;
175 void grits_object_pick_end(GritsObject *object)
177 if (object->state.picked) {
178 if (!object->state.selected)
179 g_signal_emit(object, signals[SIG_ENTER], 0);
180 object->state.selected = TRUE;
182 if (object->state.selected)
183 g_signal_emit(object, signals[SIG_LEAVE], 0);
184 object->state.selected = FALSE;
189 G_DEFINE_ABSTRACT_TYPE(GritsObject, grits_object, G_TYPE_OBJECT);
190 static void grits_object_init(GritsObject *object)
192 object->center.lat = 0;
193 object->center.lon = 0;
194 object->center.elev = -EARTH_R;
197 static void grits_object_class_init(GritsObjectClass *klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
202 * GritsObject::enter:
203 * @object: the object.
205 * The ::enter signal is emitted when the pointer moves over the object
207 signals[SIG_ENTER] = g_signal_new(
209 G_TYPE_FROM_CLASS(gobject_class),
214 g_cclosure_marshal_VOID__VOID,
219 * GritsViewer::leave:
220 * @object: the object.
222 * The ::leave signal is emitted when the pointer moves away from the
225 signals[SIG_LEAVE] = g_signal_new(
227 G_TYPE_FROM_CLASS(gobject_class),
232 g_cclosure_marshal_VOID__VOID,