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