]> Pileus Git - grits/blob - src/grits-opengl.c
794d776f660a6471f08c273c7fb926d7ce5ba9fd
[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 relies on #GtkGlExt and 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 <gtk/gtkgl.h>
36 #include <GL/gl.h>
37 #include <GL/glu.h>
38
39 #include "grits-opengl.h"
40 #include "grits-util.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 /*************
146  * Callbacks *
147  *************/
148 static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
149 {
150         g_debug("GritsOpenGL: on_configure");
151
152         double width  = GTK_WIDGET(opengl)->allocation.width;
153         double height = GTK_WIDGET(opengl)->allocation.height;
154
155         /* Setup OpenGL Window */
156         glViewport(0, 0, width, height);
157         glMatrixMode(GL_PROJECTION);
158         glLoadIdentity();
159         double ang = atan(height/FOV_DIST);
160         gluPerspective(rad2deg(ang)*2, width/height, 10, 100*EARTH_R);
161
162 #ifndef ROAM_DEBUG
163         g_mutex_lock(opengl->sphere_lock);
164         roam_sphere_update_errors(opengl->sphere);
165         g_mutex_unlock(opengl->sphere_lock);
166 #endif
167
168         return FALSE;
169 }
170
171 static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
172 {
173         g_debug("GritsOpenGL: _draw_level - level=%-4d", (int)key);
174         GritsOpenGL *opengl = user_data;
175         struct RenderLevel *level = value;
176         int nsorted = 0, nunsorted = 0;
177         GList *cur = NULL;
178
179         /* Draw opaque objects without sorting */
180         glDepthMask(TRUE);
181         glClear(GL_DEPTH_BUFFER_BIT);
182         for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
183                 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
184
185         /* Freeze depth buffer and draw transparent objects sorted */
186         /* TODO: sorting */
187         //glDepthMask(FALSE);
188         glAlphaFunc(GL_GREATER, 0.1);
189         for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
190                 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
191
192         /* TODO: Prune empty levels */
193
194         g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
195                         nunsorted, nsorted);
196         return FALSE;
197 }
198
199 static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
200 {
201         g_debug("GritsOpenGL: on_expose - begin");
202
203         glClear(GL_COLOR_BUFFER_BIT);
204
205         _set_visuals(opengl);
206 #ifdef ROAM_DEBUG
207         glColor4f(1.0, 1.0, 1.0, 1.0);
208         glLineWidth(2);
209         glDisable(GL_TEXTURE_2D);
210         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
211         roam_sphere_draw(opengl->sphere);
212         (void)_draw_level;
213         //roam_sphere_draw_normals(opengl->sphere);
214 #else
215         g_mutex_lock(opengl->objects_lock);
216         g_tree_foreach(opengl->objects, _draw_level, opengl);
217         if (opengl->wireframe) {
218                 glClear(GL_DEPTH_BUFFER_BIT);
219                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
220                 roam_sphere_draw(opengl->sphere);
221                 g_tree_foreach(opengl->objects, _draw_level, opengl);
222         }
223         g_mutex_unlock(opengl->objects_lock);
224 #endif
225
226         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
227         gdk_gl_drawable_swap_buffers(gldrawable);
228
229         g_debug("GritsOpenGL: on_expose - end\n");
230         return FALSE;
231 }
232
233 static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
234 {
235         gdouble height = GTK_WIDGET(opengl)->allocation.height;
236         gdouble gl_x   = event->x;
237         gdouble gl_y   = height - event->y;
238
239         /* Configure view */
240         gint viewport[4];
241         gdouble projection[16];
242         glGetIntegerv(GL_VIEWPORT, viewport);
243         glGetDoublev(GL_PROJECTION_MATRIX, projection);
244
245         glMatrixMode(GL_PROJECTION);
246         glPushMatrix();
247         glLoadIdentity();
248         gluPickMatrix(gl_x, gl_y, 2, 2, viewport);
249         glMultMatrixd(projection);
250
251         /* Prepare for picking */
252         guint buffer[100][4] = {};
253         glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
254         glRenderMode(GL_SELECT);
255         glInitNames();
256
257         /* Run picking */
258         g_mutex_lock(opengl->objects_lock);
259         _foreach_object(opengl, (GFunc)grits_object_pick_begin, opengl);
260         int hits = glRenderMode(GL_RENDER);
261         g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf",
262                         hits, gl_x, gl_y);
263         for (int i = 0; i < hits; i++) {
264                 //g_debug("\tHit: %d",     i);
265                 //g_debug("\t\tcount: %d", buffer[i][0]);
266                 //g_debug("\t\tz1:    %f", (float)buffer[i][1]/0x7fffffff);
267                 //g_debug("\t\tz2:    %f", (float)buffer[i][2]/0x7fffffff);
268                 //g_debug("\t\tname:  %p", (gpointer)buffer[i][3]);
269                 GritsObject *object = GRITS_OBJECT(buffer[i][3]);
270                 grits_object_pick_pointer(object, gl_x, gl_y);
271         }
272         _foreach_object(opengl, (GFunc)grits_object_pick_end, NULL);
273         g_mutex_unlock(opengl->objects_lock);
274
275         /* Cleanup */
276         glMatrixMode(GL_PROJECTION);
277         glPopMatrix();
278
279         return FALSE;
280 }
281
282 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
283 {
284         g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
285                         event->keyval, event->state, GDK_plus);
286
287         guint kv = event->keyval;
288         /* Testing */
289         if (kv == GDK_w) {
290                 opengl->wireframe = !opengl->wireframe;
291                 gtk_widget_queue_draw(GTK_WIDGET(opengl));
292         }
293 #ifdef ROAM_DEBUG
294         else if (kv == GDK_n) roam_sphere_split_one(opengl->sphere);
295         else if (kv == GDK_p) roam_sphere_merge_one(opengl->sphere);
296         else if (kv == GDK_r) roam_sphere_split_merge(opengl->sphere);
297         else if (kv == GDK_u) roam_sphere_update_errors(opengl->sphere);
298         gtk_widget_queue_draw(GTK_WIDGET(opengl));
299 #endif
300         return FALSE;
301 }
302
303 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
304 {
305         _foreach_object(opengl, (GFunc)grits_object_event, event);
306         return FALSE;
307 }
308
309 static gboolean _update_errors_cb(gpointer _opengl)
310 {
311         GritsOpenGL *opengl = _opengl;
312         g_mutex_lock(opengl->sphere_lock);
313         roam_sphere_update_errors(opengl->sphere);
314         g_mutex_unlock(opengl->sphere_lock);
315         opengl->ue_source = 0;
316         return FALSE;
317 }
318 static void on_view_changed(GritsOpenGL *opengl,
319                 gdouble _1, gdouble _2, gdouble _3)
320 {
321         g_debug("GritsOpenGL: on_view_changed");
322         _set_visuals(opengl);
323 #ifndef ROAM_DEBUG
324         if (!opengl->ue_source)
325                 opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30,
326                                 _update_errors_cb, opengl, NULL);
327         //roam_sphere_update_errors(opengl->sphere);
328 #else
329         (void)_update_errors_cb;
330 #endif
331 }
332
333 static gboolean on_idle(GritsOpenGL *opengl)
334 {
335         //g_debug("GritsOpenGL: on_idle");
336         g_mutex_lock(opengl->sphere_lock);
337         if (roam_sphere_split_merge(opengl->sphere))
338                 gtk_widget_queue_draw(GTK_WIDGET(opengl));
339         g_mutex_unlock(opengl->sphere_lock);
340         return TRUE;
341 }
342
343 static void on_realize(GritsOpenGL *opengl, gpointer _)
344 {
345         g_debug("GritsOpenGL: on_realize");
346
347         /* Start OpenGL */
348         GdkGLContext   *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(opengl));
349         GdkGLDrawable  *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
350         if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
351                 g_assert_not_reached();
352
353         /* Connect signals and idle functions now that opengl is fully initialized */
354         gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
355         g_signal_connect(opengl, "configure-event",  G_CALLBACK(on_configure),    NULL);
356         g_signal_connect(opengl, "expose-event",     G_CALLBACK(on_expose),       NULL);
357
358         g_signal_connect(opengl, "key-press-event",  G_CALLBACK(on_key_press),    NULL);
359
360         g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
361         g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
362
363         g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
364         g_signal_connect_after(opengl, "key-press-event",      G_CALLBACK(on_chained_event), NULL);
365         g_signal_connect_after(opengl, "key-release-event",    G_CALLBACK(on_chained_event), NULL);
366         g_signal_connect_after(opengl, "button-press-event",   G_CALLBACK(on_chained_event), NULL);
367         g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
368         g_signal_connect_after(opengl, "motion-notify-event",  G_CALLBACK(on_chained_event), NULL);
369
370 #ifndef ROAM_DEBUG
371         opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33,  (GSourceFunc)on_idle, opengl, NULL);
372         opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
373 #else
374         (void)on_idle;
375         (void)_update_errors_cb;
376 #endif
377
378         /* Re-queue resize incase configure was triggered before realize */
379         gtk_widget_queue_resize(GTK_WIDGET(opengl));
380 }
381
382 /*********************
383  * GritsViewer methods *
384  *********************/
385 /**
386  * grits_opengl_new:
387  * @plugins: the plugins store to use
388  * @prefs:   the preferences object to use
389  *
390  * Create a new OpenGL renderer.
391  *
392  * Returns: the new #GritsOpenGL
393  */
394 GritsViewer *grits_opengl_new(GritsPlugins *plugins, GritsPrefs *prefs)
395 {
396         g_debug("GritsOpenGL: new");
397         GritsViewer *opengl = g_object_new(GRITS_TYPE_OPENGL, NULL);
398         grits_viewer_setup(opengl, plugins, prefs);
399         return opengl;
400 }
401
402 static void grits_opengl_center_position(GritsViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
403 {
404         glRotatef(lon, 0, 1, 0);
405         glRotatef(-lat, 1, 0, 0);
406         glTranslatef(0, 0, elev2rad(elev));
407 }
408
409 static void grits_opengl_project(GritsViewer *_opengl,
410                 gdouble lat, gdouble lon, gdouble elev,
411                 gdouble *px, gdouble *py, gdouble *pz)
412 {
413         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
414         gdouble x, y, z;
415         lle2xyz(lat, lon, elev, &x, &y, &z);
416         gluProject(x, y, z,
417                 opengl->sphere->view->model,
418                 opengl->sphere->view->proj,
419                 opengl->sphere->view->view,
420                 px, py, pz);
421 }
422
423 static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
424                 RoamHeightFunc height_func, gpointer user_data, gboolean update)
425 {
426         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
427         /* TODO: get points? */
428         g_mutex_lock(opengl->sphere_lock);
429         GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
430                         bounds->n, bounds->s, bounds->e, bounds->w);
431         for (GList *cur = triangles; cur; cur = cur->next) {
432                 RoamTriangle *tri = cur->data;
433                 RoamPoint *points[] = {tri->p.l, tri->p.m, tri->p.r, tri->split};
434                 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
435                         if (bounds->n >= points[i]->lat && points[i]->lat >= bounds->s &&
436                             bounds->e >= points[i]->lon && points[i]->lon >= bounds->w) {
437                                 points[i]->height_func = height_func;
438                                 points[i]->height_data = user_data;
439                                 roam_point_update_height(points[i]);
440                         }
441                 }
442         }
443         g_list_free(triangles);
444         g_mutex_unlock(opengl->sphere_lock);
445 }
446
447 static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
448 {
449         if (!root)
450                 return;
451         RoamPoint *points[] = {root->p.l, root->p.m, root->p.r, root->split};
452         for (int i = 0; i < G_N_ELEMENTS(points); i++) {
453                 points[i]->height_func = NULL;
454                 points[i]->height_data = NULL;
455                 roam_point_update_height(points[i]);
456         }
457         _grits_opengl_clear_height_func_rec(root->kids[0]);
458         _grits_opengl_clear_height_func_rec(root->kids[1]);
459 }
460
461 static void grits_opengl_clear_height_func(GritsViewer *_opengl)
462 {
463         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
464         for (int i = 0; i < G_N_ELEMENTS(opengl->sphere->roots); i++)
465                 _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
466 }
467
468 static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
469                 gint key, gboolean sort)
470 {
471         g_assert(GRITS_IS_OPENGL(_opengl));
472         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
473         g_mutex_lock(opengl->objects_lock);
474         struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)key);
475         if (!level) {
476                 level = g_new0(struct RenderLevel, 1);
477                 g_tree_insert(opengl->objects, (gpointer)key, level);
478         }
479         GList *list = sort ? &level->sorted : &level->unsorted;
480         /* Put the link in the list */
481         GList *link = g_new0(GList, 1);
482         link->data = object;
483         link->prev = list;
484         link->next = list->next;
485         if (list->next)
486                 list->next->prev = link;
487         list->next = link;
488         g_mutex_unlock(opengl->objects_lock);
489         return link;
490 }
491
492 static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
493 {
494         g_assert(GRITS_IS_OPENGL(_opengl));
495         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
496         GList *link = object->ref;
497         g_mutex_lock(opengl->objects_lock);
498         /* Just unlink and free it, link->prev is assured */
499         link->prev->next = link->next;
500         if (link->next)
501                 link->next->prev = link->prev;
502         g_mutex_unlock(opengl->objects_lock);
503         object->ref    = NULL;
504         object->viewer = NULL;
505         g_free(link);
506         g_object_unref(object);
507         return object;
508 }
509
510 /****************
511  * GObject code *
512  ****************/
513 static int _objects_cmp(gconstpointer _a, gconstpointer _b, gpointer _)
514 {
515         gint a = (int)_a, b = (int)_b;
516         return a < b ? -1 :
517                a > b ?  1 : 0;
518 }
519 static void _objects_free(gpointer value)
520 {
521         struct RenderLevel *level = value;
522         if (level->sorted.next)
523                 g_list_free(level->sorted.next);
524         if (level->unsorted.next)
525                 g_list_free(level->unsorted.next);
526         g_free(level);
527 }
528
529 G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
530 static void grits_opengl_init(GritsOpenGL *opengl)
531 {
532         g_debug("GritsOpenGL: init");
533         opengl->objects      = g_tree_new_full(_objects_cmp, NULL, NULL, _objects_free);
534         opengl->objects_lock = g_mutex_new();
535         opengl->sphere       = roam_sphere_new(opengl);
536         opengl->sphere_lock  = g_mutex_new();
537
538         /* Set OpenGL before "realize" */
539         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
540                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
541                         GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
542         if (!glconfig)
543                 g_error("Failed to create glconfig");
544         if (!gtk_widget_set_gl_capability(GTK_WIDGET(opengl),
545                                 glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
546                 g_error("GL lacks required capabilities");
547         g_object_unref(glconfig);
548
549         /* Finish OpenGL init after it's realized */
550         g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
551 }
552 static void grits_opengl_dispose(GObject *_opengl)
553 {
554         g_debug("GritsOpenGL: dispose");
555         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
556         if (opengl->sm_source[0]) {
557                 g_source_remove(opengl->sm_source[0]);
558                 opengl->sm_source[0] = 0;
559         }
560         if (opengl->sm_source[1]) {
561                 g_source_remove(opengl->sm_source[1]);
562                 opengl->sm_source[1] = 0;
563         }
564         if (opengl->ue_source) {
565                 g_source_remove(opengl->ue_source);
566                 opengl->ue_source = 0;
567         }
568         G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
569 }
570 static void grits_opengl_finalize(GObject *_opengl)
571 {
572         g_debug("GritsOpenGL: finalize");
573         GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
574         roam_sphere_free(opengl->sphere);
575         g_tree_destroy(opengl->objects);
576         g_mutex_free(opengl->objects_lock);
577         g_mutex_free(opengl->sphere_lock);
578         G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
579 }
580 static void grits_opengl_class_init(GritsOpenGLClass *klass)
581 {
582         g_debug("GritsOpenGL: class_init");
583         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
584         gobject_class->finalize = grits_opengl_finalize;
585         gobject_class->dispose = grits_opengl_dispose;
586
587         GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
588         viewer_class->center_position   = grits_opengl_center_position;
589         viewer_class->project           = grits_opengl_project;
590         viewer_class->clear_height_func = grits_opengl_clear_height_func;
591         viewer_class->set_height_func   = grits_opengl_set_height_func;
592         viewer_class->add               = grits_opengl_add;
593         viewer_class->remove            = grits_opengl_remove;
594 }