]> Pileus Git - grits/blob - src/grits-opengl.c
Add support for Mac OS
[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         gtk_gl_begin(GTK_WIDGET(opengl));
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)(gintptr)key);
493         if (!level) {
494                 level = g_new0(struct RenderLevel, 1);
495                 g_tree_insert(opengl->objects, (gpointer)(gintptr)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         gintptr a = (gintptr)_a, b = (gintptr)_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         gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
557         g_signal_connect(opengl, "map", G_CALLBACK(on_realize), NULL);
558 }
559 static void grits_opengl_dispose(GObject *_opengl)
560 {
561         g_debug("GritsOpenGL: dispose");
562         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
563         if (opengl->sm_source[0]) {
564                 g_source_remove(opengl->sm_source[0]);
565                 opengl->sm_source[0] = 0;
566         }
567         if (opengl->sm_source[1]) {
568                 g_source_remove(opengl->sm_source[1]);
569                 opengl->sm_source[1] = 0;
570         }
571         if (opengl->ue_source) {
572                 g_source_remove(opengl->ue_source);
573                 opengl->ue_source = 0;
574         }
575         G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
576 }
577 static void grits_opengl_finalize(GObject *_opengl)
578 {
579         g_debug("GritsOpenGL: finalize");
580         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
581         roam_sphere_free(opengl->sphere);
582         g_tree_destroy(opengl->objects);
583         g_mutex_free(opengl->objects_lock);
584         g_mutex_free(opengl->sphere_lock);
585         G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
586 }
587 static void grits_opengl_class_init(GritsOpenGLClass *klass)
588 {
589         g_debug("GritsOpenGL: class_init");
590         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
591         gobject_class->finalize = grits_opengl_finalize;
592         gobject_class->dispose = grits_opengl_dispose;
593
594         GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
595         viewer_class->center_position   = grits_opengl_center_position;
596         viewer_class->project           = grits_opengl_project;
597         viewer_class->clear_height_func = grits_opengl_clear_height_func;
598         viewer_class->set_height_func   = grits_opengl_set_height_func;
599         viewer_class->add               = grits_opengl_add;
600         viewer_class->remove            = grits_opengl_remove;
601 }