]> Pileus Git - grits/blob - src/grits-opengl.c
Move OpenGL includes to a common place
[grits] / src / grits-opengl.c
1 /*
2  * Copyright (C) 2009-2011 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-opengl
20  * @short_description: OpenGL based virtual globe
21  *
22  * #GritsOpenGL is the core rendering engine used by grits. Theoretically other
23  * renderers could be writte, but they have not been. GritsOpenGL uses the ROAM
24  * algorithm for updating surface mesh the planet. The only thing GritsOpenGL
25  * can actually render on it's own is a wireframe of a sphere.
26  *
27  * GritsOpenGL requires (at least) OpenGL 2.0.
28  */
29
30 #include <config.h>
31 #include <math.h>
32 #include <string.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
35
36 #include "grits-opengl.h"
37 #include "grits-util.h"
38 #include "gtkgl.h"
39 #include "roam.h"
40
41 // #define ROAM_DEBUG
42
43 /* Tessellation, "finding intersecting triangles" */
44 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
45 /* http://www.opengl.org/wiki/Alpha_Blending */
46
47 /* The unsorted/sroted GLists are blank head nodes,
48  * This way us we can remove objects from the level just by fixing up links
49  * I.e. we don't need to do a lookup to remove an object if we have its GList */
50 struct RenderLevel {
51         GList unsorted;
52         GList sorted;
53 };
54
55 /***********
56  * Helpers *
57  ***********/
58 static void _set_visuals(GritsOpenGL *opengl)
59 {
60         glMatrixMode(GL_MODELVIEW);
61         glLoadIdentity();
62
63         /* Camera 1 */
64         double lat, lon, elev, rx, ry, rz;
65         grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
66         grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
67         glRotatef(rx, 1, 0, 0);
68         glRotatef(rz, 0, 0, 1);
69
70         /* Lighting */
71 #ifdef ROAM_DEBUG
72         float light_ambient[]  = {0.7f, 0.7f, 0.7f, 1.0f};
73         float light_diffuse[]  = {2.0f, 2.0f, 2.0f, 1.0f};
74 #else
75         float light_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
76         float light_diffuse[]  = {0.8f, 0.8f, 0.8f, 1.0f};
77 #endif
78         float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
79         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
80         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
81         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
82         glEnable(GL_LIGHT0);
83         glEnable(GL_LIGHTING);
84
85         float material_ambient[]  = {1.0, 1.0, 1.0, 1.0};
86         float material_diffuse[]  = {1.0, 1.0, 1.0, 1.0};
87         float material_specular[] = {0.0, 0.0, 0.0, 1.0};
88         float material_emission[] = {0.0, 0.0, 0.0, 1.0};
89         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,  material_ambient);
90         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,  material_diffuse);
91         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
92         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
93         glDisable(GL_TEXTURE_2D);
94         glDisable(GL_COLOR_MATERIAL);
95
96         /* Camera 2 */
97         glTranslatef(0, 0, -elev2rad(elev));
98         glRotatef(lat, 1, 0, 0);
99         glRotatef(-lon, 0, 1, 0);
100
101         glDisable(GL_ALPHA_TEST);
102
103         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
104         glEnable(GL_BLEND);
105
106 #ifndef ROAM_DEBUG
107         glCullFace(GL_BACK);
108         glEnable(GL_CULL_FACE);
109
110         glClearDepth(1.0);
111         glDepthFunc(GL_LEQUAL);
112         glEnable(GL_DEPTH_TEST);
113 #endif
114
115         glEnable(GL_LINE_SMOOTH);
116
117         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
118         //glShadeModel(GL_FLAT);
119
120         g_mutex_lock(opengl->sphere_lock);
121         roam_sphere_update_view(opengl->sphere);
122         g_mutex_unlock(opengl->sphere_lock);
123 }
124
125 static gboolean _foreach_object_cb(gpointer key, gpointer value, gpointer pointers)
126 {
127         struct RenderLevel *level = value;
128         GFunc    user_func = ((gpointer*)pointers)[0];
129         gpointer user_data = ((gpointer*)pointers)[1];
130         for (GList *cur = level->unsorted.next; cur; cur = cur->next)
131                 user_func(cur->data, user_data);
132         for (GList *cur = level->sorted.next;   cur; cur = cur->next)
133                 user_func(cur->data, user_data);
134         return FALSE;
135 }
136
137 static void _foreach_object(GritsOpenGL *opengl, GFunc func, gpointer user_data)
138 {
139         gpointer pointers[2] = {func, user_data};
140         g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
141 }
142
143 static void _add_object(GritsObject *object, GPtrArray *array)
144 {
145         g_ptr_array_add(array, object);
146 }
147
148 static GPtrArray *_objects_to_array(GritsOpenGL *opengl)
149 {
150         GPtrArray *array = g_ptr_array_new();
151         _foreach_object(opengl, (GFunc)_add_object, array);
152         return array;
153 }
154
155 /*************
156  * Callbacks *
157  *************/
158 static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
159 {
160         g_debug("GritsOpenGL: on_configure");
161
162         double width  = GTK_WIDGET(opengl)->allocation.width;
163         double height = GTK_WIDGET(opengl)->allocation.height;
164
165         /* Setup OpenGL Window */
166         glViewport(0, 0, width, height);
167         glMatrixMode(GL_PROJECTION);
168         glLoadIdentity();
169         double ang = atan(height/FOV_DIST);
170         gluPerspective(rad2deg(ang)*2, width/height, 10, 100*EARTH_R);
171
172 #ifndef ROAM_DEBUG
173         g_mutex_lock(opengl->sphere_lock);
174         roam_sphere_update_errors(opengl->sphere);
175         g_mutex_unlock(opengl->sphere_lock);
176 #endif
177
178         return FALSE;
179 }
180
181 static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
182 {
183         g_debug("GritsOpenGL: _draw_level - level=%-4ld", (glong)key);
184         GritsOpenGL *opengl = user_data;
185         struct RenderLevel *level = value;
186         int nsorted = 0, nunsorted = 0;
187         GList *cur = NULL;
188
189         /* Draw opaque objects without sorting */
190         glDepthMask(TRUE);
191         glClear(GL_DEPTH_BUFFER_BIT);
192         for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
193                 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
194
195         /* Freeze depth buffer and draw transparent objects sorted */
196         /* TODO: sorting */
197         //glDepthMask(FALSE);
198         glAlphaFunc(GL_GREATER, 0.1);
199         for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
200                 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
201
202         /* TODO: Prune empty levels */
203
204         g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
205                         nunsorted, nsorted);
206         return FALSE;
207 }
208
209 static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
210 {
211         g_debug("GritsOpenGL: on_expose - begin");
212
213         glClear(GL_COLOR_BUFFER_BIT);
214
215         _set_visuals(opengl);
216 #ifdef ROAM_DEBUG
217         glColor4f(1.0, 1.0, 1.0, 1.0);
218         glLineWidth(2);
219         glDisable(GL_TEXTURE_2D);
220         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
221         roam_sphere_draw(opengl->sphere);
222         (void)_draw_level;
223         //roam_sphere_draw_normals(opengl->sphere);
224 #else
225         g_mutex_lock(opengl->objects_lock);
226         g_tree_foreach(opengl->objects, _draw_level, opengl);
227         if (opengl->wireframe) {
228                 glClear(GL_DEPTH_BUFFER_BIT);
229                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
230                 roam_sphere_draw(opengl->sphere);
231                 g_tree_foreach(opengl->objects, _draw_level, opengl);
232         }
233         g_mutex_unlock(opengl->objects_lock);
234 #endif
235
236         gtk_gl_end(GTK_WIDGET(opengl));
237
238         g_debug("GritsOpenGL: on_expose - end\n");
239         return FALSE;
240 }
241
242 static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
243 {
244         gdouble height = GTK_WIDGET(opengl)->allocation.height;
245         gdouble gl_x   = event->x;
246         gdouble gl_y   = height - event->y;
247
248         /* Configure view */
249         gint viewport[4];
250         gdouble projection[16];
251         glGetIntegerv(GL_VIEWPORT, viewport);
252         glGetDoublev(GL_PROJECTION_MATRIX, projection);
253
254         glMatrixMode(GL_PROJECTION);
255         glPushMatrix();
256         glLoadIdentity();
257         gluPickMatrix(gl_x, gl_y, 2, 2, viewport);
258         glMultMatrixd(projection);
259
260         /* Prepare for picking */
261         guint buffer[100][4] = {};
262         glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
263         glRenderMode(GL_SELECT);
264         glInitNames();
265
266         /* Run picking */
267         g_mutex_lock(opengl->objects_lock);
268         GPtrArray *objects = _objects_to_array(opengl);
269         for (guint i = 0; i < objects->len; i++) {
270                 glPushName(i);
271                 GritsObject *object = objects->pdata[i];
272                 object->state.picked = FALSE;
273                 grits_object_pick(object, opengl);
274                 glPopName();
275         }
276         int hits = glRenderMode(GL_RENDER);
277         g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf",
278                         hits, gl_x, gl_y);
279         for (int i = 0; i < hits; i++) {
280                 //g_debug("\tHit: %d",     i);
281                 //g_debug("\t\tcount: %d", buffer[i][0]);
282                 //g_debug("\t\tz1:    %f", (float)buffer[i][1]/0x7fffffff);
283                 //g_debug("\t\tz2:    %f", (float)buffer[i][2]/0x7fffffff);
284                 //g_debug("\t\tname:  %p", (gpointer)buffer[i][3]);
285                 guint        index  = buffer[i][3];
286                 GritsObject *object = objects->pdata[index];
287                 object->state.picked = TRUE;
288         }
289         for (guint i = 0; i < objects->len; i++) {
290                 GritsObject *object = objects->pdata[i];
291                 grits_object_set_pointer(object, object->state.picked);
292         }
293         g_ptr_array_free(objects, TRUE);
294         g_mutex_unlock(opengl->objects_lock);
295
296         /* Cleanup */
297         glMatrixMode(GL_PROJECTION);
298         glPopMatrix();
299
300         return FALSE;
301 }
302
303 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
304 {
305         g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
306                         event->keyval, event->state, GDK_plus);
307
308         guint kv = event->keyval;
309         /* Testing */
310         if (kv == GDK_w) {
311                 opengl->wireframe = !opengl->wireframe;
312                 gtk_widget_queue_draw(GTK_WIDGET(opengl));
313         }
314 #ifdef ROAM_DEBUG
315         else if (kv == GDK_n) roam_sphere_split_one(opengl->sphere);
316         else if (kv == GDK_p) roam_sphere_merge_one(opengl->sphere);
317         else if (kv == GDK_r) roam_sphere_split_merge(opengl->sphere);
318         else if (kv == GDK_u) roam_sphere_update_errors(opengl->sphere);
319         gtk_widget_queue_draw(GTK_WIDGET(opengl));
320 #endif
321         return FALSE;
322 }
323
324 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
325 {
326         _foreach_object(opengl, (GFunc)grits_object_event, event);
327         return FALSE;
328 }
329
330 static gboolean _update_errors_cb(gpointer _opengl)
331 {
332         GritsOpenGL *opengl = _opengl;
333         g_mutex_lock(opengl->sphere_lock);
334         roam_sphere_update_errors(opengl->sphere);
335         g_mutex_unlock(opengl->sphere_lock);
336         opengl->ue_source = 0;
337         return FALSE;
338 }
339 static void on_view_changed(GritsOpenGL *opengl,
340                 gdouble _1, gdouble _2, gdouble _3)
341 {
342         g_debug("GritsOpenGL: on_view_changed");
343         _set_visuals(opengl);
344 #ifndef ROAM_DEBUG
345         if (!opengl->ue_source)
346                 opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30,
347                                 _update_errors_cb, opengl, NULL);
348         //roam_sphere_update_errors(opengl->sphere);
349 #else
350         (void)_update_errors_cb;
351 #endif
352 }
353
354 static gboolean on_idle(GritsOpenGL *opengl)
355 {
356         //g_debug("GritsOpenGL: on_idle");
357         g_mutex_lock(opengl->sphere_lock);
358         if (roam_sphere_split_merge(opengl->sphere))
359                 gtk_widget_queue_draw(GTK_WIDGET(opengl));
360         g_mutex_unlock(opengl->sphere_lock);
361         return TRUE;
362 }
363
364 static void on_realize(GritsOpenGL *opengl, gpointer _)
365 {
366         g_debug("GritsOpenGL: on_realize");
367         gtk_gl_begin(GTK_WIDGET(opengl));
368
369         /* Connect signals and idle functions now that opengl is fully initialized */
370         gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
371         g_signal_connect(opengl, "configure-event",  G_CALLBACK(on_configure),    NULL);
372         g_signal_connect(opengl, "expose-event",     G_CALLBACK(on_expose),       NULL);
373
374         g_signal_connect(opengl, "key-press-event",  G_CALLBACK(on_key_press),    NULL);
375
376         g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
377         g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
378
379         g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
380         g_signal_connect_after(opengl, "key-press-event",      G_CALLBACK(on_chained_event), NULL);
381         g_signal_connect_after(opengl, "key-release-event",    G_CALLBACK(on_chained_event), NULL);
382         g_signal_connect_after(opengl, "button-press-event",   G_CALLBACK(on_chained_event), NULL);
383         g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
384         g_signal_connect_after(opengl, "motion-notify-event",  G_CALLBACK(on_chained_event), NULL);
385
386 #ifndef ROAM_DEBUG
387         opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33,  (GSourceFunc)on_idle, opengl, NULL);
388         opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
389 #else
390         (void)on_idle;
391         (void)_update_errors_cb;
392 #endif
393
394         /* Re-queue resize incase configure was triggered before realize */
395         gtk_widget_queue_resize(GTK_WIDGET(opengl));
396 }
397
398 /*********************
399  * GritsViewer methods *
400  *********************/
401 /**
402  * grits_opengl_new:
403  * @plugins: the plugins store to use
404  * @prefs:   the preferences object to use
405  *
406  * Create a new OpenGL renderer.
407  *
408  * Returns: the new #GritsOpenGL
409  */
410 GritsViewer *grits_opengl_new(GritsPlugins *plugins, GritsPrefs *prefs)
411 {
412         g_debug("GritsOpenGL: new");
413         GritsViewer *opengl = g_object_new(GRITS_TYPE_OPENGL, NULL);
414         grits_viewer_setup(opengl, plugins, prefs);
415         return opengl;
416 }
417
418 static void grits_opengl_center_position(GritsViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
419 {
420         glRotatef(lon, 0, 1, 0);
421         glRotatef(-lat, 1, 0, 0);
422         glTranslatef(0, 0, elev2rad(elev));
423 }
424
425 static void grits_opengl_project(GritsViewer *_opengl,
426                 gdouble lat, gdouble lon, gdouble elev,
427                 gdouble *px, gdouble *py, gdouble *pz)
428 {
429         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
430         gdouble x, y, z;
431         lle2xyz(lat, lon, elev, &x, &y, &z);
432         gluProject(x, y, z,
433                 opengl->sphere->view->model,
434                 opengl->sphere->view->proj,
435                 opengl->sphere->view->view,
436                 px, py, pz);
437 }
438
439 static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
440                 RoamHeightFunc height_func, gpointer user_data, gboolean update)
441 {
442         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
443         /* TODO: get points? */
444         g_mutex_lock(opengl->sphere_lock);
445         GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
446                         bounds->n, bounds->s, bounds->e, bounds->w);
447         for (GList *cur = triangles; cur; cur = cur->next) {
448                 RoamTriangle *tri = cur->data;
449                 RoamPoint *points[] = {tri->p.l, tri->p.m, tri->p.r, tri->split};
450                 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
451                         if (bounds->n >= points[i]->lat && points[i]->lat >= bounds->s &&
452                             bounds->e >= points[i]->lon && points[i]->lon >= bounds->w) {
453                                 points[i]->height_func = height_func;
454                                 points[i]->height_data = user_data;
455                                 roam_point_update_height(points[i]);
456                         }
457                 }
458         }
459         g_list_free(triangles);
460         g_mutex_unlock(opengl->sphere_lock);
461 }
462
463 static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
464 {
465         if (!root)
466                 return;
467         RoamPoint *points[] = {root->p.l, root->p.m, root->p.r, root->split};
468         for (int i = 0; i < G_N_ELEMENTS(points); i++) {
469                 points[i]->height_func = NULL;
470                 points[i]->height_data = NULL;
471                 roam_point_update_height(points[i]);
472         }
473         _grits_opengl_clear_height_func_rec(root->kids[0]);
474         _grits_opengl_clear_height_func_rec(root->kids[1]);
475 }
476
477 static void grits_opengl_clear_height_func(GritsViewer *_opengl)
478 {
479         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
480         for (int i = 0; i < G_N_ELEMENTS(opengl->sphere->roots); i++)
481                 _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
482 }
483
484 static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
485                 gint key, gboolean sort)
486 {
487         g_assert(GRITS_IS_OPENGL(_opengl));
488         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
489         g_mutex_lock(opengl->objects_lock);
490         struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)(gintptr)key);
491         if (!level) {
492                 level = g_new0(struct RenderLevel, 1);
493                 g_tree_insert(opengl->objects, (gpointer)(gintptr)key, level);
494         }
495         GList *list = sort ? &level->sorted : &level->unsorted;
496         /* Put the link in the list */
497         GList *link = g_new0(GList, 1);
498         link->data = object;
499         link->prev = list;
500         link->next = list->next;
501         if (list->next)
502                 list->next->prev = link;
503         list->next = link;
504         g_mutex_unlock(opengl->objects_lock);
505         return link;
506 }
507
508 static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
509 {
510         g_assert(GRITS_IS_OPENGL(_opengl));
511         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
512         GList *link = object->ref;
513         g_mutex_lock(opengl->objects_lock);
514         /* Just unlink and free it, link->prev is assured */
515         link->prev->next = link->next;
516         if (link->next)
517                 link->next->prev = link->prev;
518         g_mutex_unlock(opengl->objects_lock);
519         object->ref    = NULL;
520         object->viewer = NULL;
521         g_free(link);
522         g_object_unref(object);
523         return object;
524 }
525
526 /****************
527  * GObject code *
528  ****************/
529 static int _objects_cmp(gconstpointer _a, gconstpointer _b, gpointer _)
530 {
531         gintptr a = (gintptr)_a, b = (gintptr)_b;
532         return a < b ? -1 :
533                a > b ?  1 : 0;
534 }
535 static void _objects_free(gpointer value)
536 {
537         struct RenderLevel *level = value;
538         if (level->sorted.next)
539                 g_list_free(level->sorted.next);
540         if (level->unsorted.next)
541                 g_list_free(level->unsorted.next);
542         g_free(level);
543 }
544
545 G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
546 static void grits_opengl_init(GritsOpenGL *opengl)
547 {
548         g_debug("GritsOpenGL: init");
549         opengl->objects      = g_tree_new_full(_objects_cmp, NULL, NULL, _objects_free);
550         opengl->objects_lock = g_mutex_new();
551         opengl->sphere       = roam_sphere_new(opengl);
552         opengl->sphere_lock  = g_mutex_new();
553         gtk_gl_enable(GTK_WIDGET(opengl));
554         g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
555 }
556 static void grits_opengl_dispose(GObject *_opengl)
557 {
558         g_debug("GritsOpenGL: dispose");
559         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
560         if (opengl->sm_source[0]) {
561                 g_source_remove(opengl->sm_source[0]);
562                 opengl->sm_source[0] = 0;
563         }
564         if (opengl->sm_source[1]) {
565                 g_source_remove(opengl->sm_source[1]);
566                 opengl->sm_source[1] = 0;
567         }
568         if (opengl->ue_source) {
569                 g_source_remove(opengl->ue_source);
570                 opengl->ue_source = 0;
571         }
572         G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
573 }
574 static void grits_opengl_finalize(GObject *_opengl)
575 {
576         g_debug("GritsOpenGL: finalize");
577         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
578         roam_sphere_free(opengl->sphere);
579         g_tree_destroy(opengl->objects);
580         g_mutex_free(opengl->objects_lock);
581         g_mutex_free(opengl->sphere_lock);
582         G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
583 }
584 static void grits_opengl_class_init(GritsOpenGLClass *klass)
585 {
586         g_debug("GritsOpenGL: class_init");
587         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
588         gobject_class->finalize = grits_opengl_finalize;
589         gobject_class->dispose = grits_opengl_dispose;
590
591         GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
592         viewer_class->center_position   = grits_opengl_center_position;
593         viewer_class->project           = grits_opengl_project;
594         viewer_class->clear_height_func = grits_opengl_clear_height_func;
595         viewer_class->set_height_func   = grits_opengl_set_height_func;
596         viewer_class->add               = grits_opengl_add;
597         viewer_class->remove            = grits_opengl_remove;
598 }