2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * SECTION:grits-opengl
20 * @short_description: OpenGL based virtual globe
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.
27 * GritsOpenGL requires (at least) OpenGL 2.0.
33 #include <gdk/gdkkeysyms.h>
36 #include "grits-opengl.h"
37 #include "grits-util.h"
43 /* Tessellation, "finding intersecting triangles" */
44 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
45 /* http://www.opengl.org/wiki/Alpha_Blending */
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 */
58 static void _set_visuals(GritsOpenGL *opengl)
60 double lat, lon, elev, rx, ry, rz;
61 grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
62 grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
64 /* Set projection and clipping planes */
65 glMatrixMode(GL_PROJECTION);
68 double width = GTK_WIDGET(opengl)->allocation.width;
69 double height = GTK_WIDGET(opengl)->allocation.height;
70 double ang = atan((height/2)/FOV_DIST)*2;
71 double atmos = 100000;
72 double near = MAX(elev*0.75 - atmos, 50); // View 100km of atmosphere
73 double far = elev + 2*EARTH_R + atmos; // on both sides of the earth
75 grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
76 glViewport(0, 0, width, height);
77 gluPerspective(rad2deg(ang), width/height, near, far);
79 /* Setup camera and lighting */
80 glMatrixMode(GL_MODELVIEW);
84 glRotatef(rx, 1, 0, 0);
85 glRotatef(rz, 0, 0, 1);
89 float light_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
90 float light_diffuse[] = {2.0f, 2.0f, 2.0f, 1.0f};
92 float light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
93 float light_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
95 float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
96 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
97 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
98 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
100 glEnable(GL_LIGHTING);
102 float material_ambient[] = {1.0, 1.0, 1.0, 1.0};
103 float material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
104 float material_specular[] = {0.0, 0.0, 0.0, 1.0};
105 float material_emission[] = {0.0, 0.0, 0.0, 1.0};
106 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
107 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
108 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
109 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
110 glDisable(GL_TEXTURE_2D);
111 glDisable(GL_COLOR_MATERIAL);
114 glTranslatef(0, 0, -elev2rad(elev));
115 glRotatef(lat, 1, 0, 0);
116 glRotatef(-lon, 0, 1, 0);
118 glDisable(GL_ALPHA_TEST);
120 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
125 glEnable(GL_CULL_FACE);
128 glDepthFunc(GL_LEQUAL);
129 glEnable(GL_DEPTH_TEST);
132 glEnable(GL_LINE_SMOOTH);
134 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
135 //glShadeModel(GL_FLAT);
137 g_mutex_lock(opengl->sphere_lock);
138 roam_sphere_update_view(opengl->sphere);
139 g_mutex_unlock(opengl->sphere_lock);
142 typedef gboolean (*GritsLevelFunc)(GritsObject *obj, gpointer user_data, gint level, gboolean sorted);
144 static gboolean _foreach_object_cb(gpointer key, gpointer value, gpointer pointers)
146 struct RenderLevel *level = value;
147 GritsLevelFunc user_func = ((gpointer*)pointers)[0];
148 gpointer user_data = ((gpointer*)pointers)[1];
149 for (GList *cur = level->unsorted.next; cur; cur = cur->next)
150 if (user_func(cur->data, user_data, (gint)key, FALSE))
152 for (GList *cur = level->sorted.next; cur; cur = cur->next)
153 if (user_func(cur->data, user_data, (gint)key, TRUE))
158 static void _foreach_object(GritsOpenGL *opengl, GritsLevelFunc func, gpointer user_data)
160 gpointer pointers[2] = {func, user_data};
161 g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
164 static gboolean _add_object_world(GritsObject *object, GPtrArray *array, gint level, gboolean sorted)
166 if (level < GRITS_LEVEL_HUD)
167 g_ptr_array_add(array, object);
171 static gboolean _add_object_ortho(GritsObject *object, GPtrArray *array, gint level, gboolean sorted)
173 if (level >= GRITS_LEVEL_HUD)
174 g_ptr_array_add(array, object);
178 static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho)
180 GPtrArray *array = g_ptr_array_new();
181 GritsLevelFunc func = (GritsLevelFunc)(ortho ? _add_object_ortho : _add_object_world);
182 _foreach_object(opengl, func, array);
189 static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
191 g_debug("GritsOpenGL: on_configure");
193 _set_visuals(opengl);
195 g_mutex_lock(opengl->sphere_lock);
196 roam_sphere_update_errors(opengl->sphere);
197 g_mutex_unlock(opengl->sphere_lock);
203 static gint run_picking(GritsOpenGL *opengl, GdkEvent *event, GPtrArray *objects)
205 /* Setup picking buffers */
206 guint buffer[100][4] = {};
207 glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
208 if (!opengl->pickmode)
209 glRenderMode(GL_SELECT);
212 /* Render/pick objects */
213 for (guint i = 0; i < objects->len; i++) {
215 GritsObject *object = objects->pdata[i];
216 object->state.picked = FALSE;
217 grits_object_pick(object, opengl);
221 int hits = glRenderMode(GL_RENDER);
224 for (int i = 0; i < hits; i++) {
225 //g_debug("\tHit: %d", i);
226 //g_debug("\t\tcount: %d", buffer[i][0]);
227 //g_debug("\t\tz1: %f", (float)buffer[i][1]/0x7fffffff);
228 //g_debug("\t\tz2: %f", (float)buffer[i][2]/0x7fffffff);
229 //g_debug("\t\tname: %p", (gpointer)buffer[i][3]);
230 guint index = buffer[i][3];
231 GritsObject *object = objects->pdata[index];
232 object->state.picked = TRUE;
235 /* Notify objects of pointer movements */
236 for (guint i = 0; i < objects->len; i++) {
237 GritsObject *object = objects->pdata[i];
238 grits_object_set_pointer(object, event, object->state.picked);
244 static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
246 gdouble height = GTK_WIDGET(opengl)->allocation.height;
247 gdouble gl_x = event->x;
248 gdouble gl_y = height - event->y;
249 gdouble delta = opengl->pickmode ? 200 : 2;
251 if (opengl->pickmode) {
252 gtk_gl_begin(GTK_WIDGET(opengl));
253 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
257 gdouble projection[16];
258 gint viewport[4]; // x=0,y=0,w,h
259 glGetDoublev(GL_PROJECTION_MATRIX, projection);
260 glGetIntegerv(GL_VIEWPORT, viewport);
261 glMatrixMode(GL_MODELVIEW); glPushMatrix();
262 glMatrixMode(GL_PROJECTION); glPushMatrix();
264 g_mutex_lock(opengl->objects_lock);
266 GPtrArray *ortho = _objects_to_array(opengl, TRUE);
267 GPtrArray *world = _objects_to_array(opengl, FALSE);
269 /* Run perspective picking */
270 glMatrixMode(GL_PROJECTION); glLoadIdentity();
271 gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
272 glMultMatrixd(projection);
273 gint world_hits = run_picking(opengl, (GdkEvent*)event, world);
275 /* Run ortho picking */
276 glMatrixMode(GL_PROJECTION); glLoadIdentity();
277 gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
278 glMatrixMode(GL_MODELVIEW); glLoadIdentity();
279 glOrtho(0, viewport[2], viewport[3], 0, 1000, -1000);
280 gint ortho_hits = run_picking(opengl, (GdkEvent*)event, ortho);
282 g_debug("GritsOpenGL: on_motion_notify - hits=%d/%d,%d/%d ev=%.0lf,%.0lf",
283 world_hits, world->len, ortho_hits, ortho->len, gl_x, gl_y);
285 g_ptr_array_free(world, TRUE);
286 g_ptr_array_free(ortho, TRUE);
288 g_mutex_unlock(opengl->objects_lock);
292 //gdouble lat, lon, elev;
293 //grits_viewer_unproject(GRITS_VIEWER(opengl),
294 // gl_x, gl_y, -1, &lat, &lon, &elev);
297 glMatrixMode(GL_PROJECTION); glPopMatrix();
298 glMatrixMode(GL_MODELVIEW); glPopMatrix();
300 if (opengl->pickmode)
301 gtk_gl_end(GTK_WIDGET(opengl));
306 static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
308 GritsOpenGL *opengl = user_data;
309 glong lnum = (glong)key;
310 struct RenderLevel *level = value;
312 g_debug("GritsOpenGL: _draw_level - level=%-4ld", lnum);
313 int nsorted = 0, nunsorted = 0;
316 /* Configure individual levels */
317 if (lnum < GRITS_LEVEL_WORLD) {
318 /* Disable depth for background levels */
320 glDisable(GL_ALPHA_TEST);
321 } else if (lnum < GRITS_LEVEL_OVERLAY) {
322 /* Enable depth and alpha for world levels */
323 glEnable(GL_ALPHA_TEST);
324 glAlphaFunc(GL_GREATER, 0.1);
327 /* Disable depth for Overlay/HUD levels */
328 // This causes rendering glitches not sure why..
329 //glDepthMask(FALSE);
333 if (lnum >= GRITS_LEVEL_HUD) {
334 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
335 glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
336 gint win_width = GTK_WIDGET(opengl)->allocation.width;
337 gint win_height = GTK_WIDGET(opengl)->allocation.height;
338 glOrtho(0, win_width, win_height, 0, 1000, -1000);
341 /* Draw unsorted objects without depth testing,
342 * these are polygons, etc, rather than physical objects */
343 glDisable(GL_DEPTH_TEST);
344 for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
345 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
347 /* Draw sorted objects using depth testing
348 * These are things that are actually part of the world */
349 glEnable(GL_DEPTH_TEST);
350 for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
351 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
354 if (lnum >= GRITS_LEVEL_HUD) {
355 glMatrixMode(GL_PROJECTION); glPopMatrix();
356 glMatrixMode(GL_MODELVIEW); glPopMatrix();
359 /* TODO: Prune empty levels */
361 g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
366 static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
368 g_debug("GritsOpenGL: on_expose - begin");
370 if (opengl->pickmode)
371 return on_motion_notify(opengl, (GdkEventMotion*)event, NULL);
373 gtk_gl_begin(GTK_WIDGET(opengl));
375 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
377 _set_visuals(opengl);
379 glColor4f(1.0, 1.0, 1.0, 1.0);
381 glDisable(GL_TEXTURE_2D);
382 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
383 roam_sphere_draw(opengl->sphere);
385 //roam_sphere_draw_normals(opengl->sphere);
387 g_mutex_lock(opengl->objects_lock);
388 if (opengl->wireframe)
389 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
390 g_tree_foreach(opengl->objects, _draw_level, opengl);
391 g_mutex_unlock(opengl->objects_lock);
394 gtk_gl_end(GTK_WIDGET(opengl));
396 g_debug("GritsOpenGL: on_expose - end\n");
400 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
402 g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
403 event->keyval, event->state, GDK_plus);
405 guint kv = event->keyval;
408 opengl->wireframe = !opengl->wireframe;
409 gtk_widget_queue_draw(GTK_WIDGET(opengl));
412 opengl->pickmode = !opengl->pickmode;
413 gtk_widget_queue_draw(GTK_WIDGET(opengl));
416 else if (kv == GDK_n) roam_sphere_split_one(opengl->sphere);
417 else if (kv == GDK_p) roam_sphere_merge_one(opengl->sphere);
418 else if (kv == GDK_r) roam_sphere_split_merge(opengl->sphere);
419 else if (kv == GDK_u) roam_sphere_update_errors(opengl->sphere);
420 gtk_widget_queue_draw(GTK_WIDGET(opengl));
425 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
427 _foreach_object(opengl, (GritsLevelFunc)grits_object_event, event);
431 static gboolean _update_errors_cb(gpointer _opengl)
433 GritsOpenGL *opengl = _opengl;
434 g_mutex_lock(opengl->sphere_lock);
435 roam_sphere_update_errors(opengl->sphere);
436 g_mutex_unlock(opengl->sphere_lock);
437 opengl->ue_source = 0;
440 static void on_view_changed(GritsOpenGL *opengl,
441 gdouble _1, gdouble _2, gdouble _3)
443 g_debug("GritsOpenGL: on_view_changed");
444 _set_visuals(opengl);
446 if (!opengl->ue_source)
447 opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30,
448 _update_errors_cb, opengl, NULL);
449 //roam_sphere_update_errors(opengl->sphere);
451 (void)_update_errors_cb;
455 static gboolean on_idle(GritsOpenGL *opengl)
457 //g_debug("GritsOpenGL: on_idle");
458 g_mutex_lock(opengl->sphere_lock);
459 if (roam_sphere_split_merge(opengl->sphere))
460 gtk_widget_queue_draw(GTK_WIDGET(opengl));
461 g_mutex_unlock(opengl->sphere_lock);
465 static void on_realize(GritsOpenGL *opengl, gpointer _)
467 g_debug("GritsOpenGL: on_realize");
468 gtk_gl_begin(GTK_WIDGET(opengl));
470 /* Connect signals and idle functions now that opengl is fully initialized */
471 gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
472 g_signal_connect(opengl, "configure-event", G_CALLBACK(on_configure), NULL);
473 g_signal_connect(opengl, "expose-event", G_CALLBACK(on_expose), NULL);
475 g_signal_connect(opengl, "key-press-event", G_CALLBACK(on_key_press), NULL);
477 g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
478 g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
480 g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
481 g_signal_connect_after(opengl, "key-press-event", G_CALLBACK(on_chained_event), NULL);
482 g_signal_connect_after(opengl, "key-release-event", G_CALLBACK(on_chained_event), NULL);
483 g_signal_connect_after(opengl, "button-press-event", G_CALLBACK(on_chained_event), NULL);
484 g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
485 g_signal_connect_after(opengl, "motion-notify-event", G_CALLBACK(on_chained_event), NULL);
488 opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33, (GSourceFunc)on_idle, opengl, NULL);
489 opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
492 (void)_update_errors_cb;
495 /* Re-queue resize incase configure was triggered before realize */
496 gtk_widget_queue_resize(GTK_WIDGET(opengl));
499 /*********************
500 * GritsViewer methods *
501 *********************/
504 * @plugins: the plugins store to use
505 * @prefs: the preferences object to use
507 * Create a new OpenGL renderer.
509 * Returns: the new #GritsOpenGL
511 GritsViewer *grits_opengl_new(GritsPlugins *plugins, GritsPrefs *prefs)
513 g_debug("GritsOpenGL: new");
514 GritsViewer *opengl = g_object_new(GRITS_TYPE_OPENGL, NULL);
515 grits_viewer_setup(opengl, plugins, prefs);
519 static void grits_opengl_center_position(GritsViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
521 glRotatef(lon, 0, 1, 0);
522 glRotatef(-lat, 1, 0, 0);
523 glTranslatef(0, 0, elev2rad(elev));
526 static void grits_opengl_project(GritsViewer *_opengl,
527 gdouble lat, gdouble lon, gdouble elev,
528 gdouble *px, gdouble *py, gdouble *pz)
530 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
532 lle2xyz(lat, lon, elev, &x, &y, &z);
534 opengl->sphere->view->model,
535 opengl->sphere->view->proj,
536 opengl->sphere->view->view,
540 static void grits_opengl_unproject(GritsViewer *_opengl,
541 gdouble px, gdouble py, gdouble pz,
542 gdouble *lat, gdouble *lon, gdouble *elev)
544 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
545 if (!opengl->sphere->view)
550 glReadPixels(px, py, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tmp);
553 gluUnProject(px, py, pz,
554 opengl->sphere->view->model,
555 opengl->sphere->view->proj,
556 opengl->sphere->view->view,
558 xyz2lle(x, y, z, lat, lon, elev);
559 //g_message("GritsOpenGL: unproject - "
560 // "%4.0lf,%4.0lf,(%5.3lf) -> "
561 // "%8.0lf,%8.0lf,%8.0lf -> "
562 // "%6.2lf,%7.2lf,%4.0lf",
563 // px, py, pz, x, y, z, *lat, *lon, *elev);
566 static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
567 RoamHeightFunc height_func, gpointer user_data, gboolean update)
569 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
570 /* TODO: get points? */
571 g_mutex_lock(opengl->sphere_lock);
572 GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
573 bounds->n, bounds->s, bounds->e, bounds->w);
574 for (GList *cur = triangles; cur; cur = cur->next) {
575 RoamTriangle *tri = cur->data;
576 RoamPoint *points[] = {tri->p.l, tri->p.m, tri->p.r, tri->split};
577 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
578 if (bounds->n >= points[i]->lat && points[i]->lat >= bounds->s &&
579 bounds->e >= points[i]->lon && points[i]->lon >= bounds->w) {
580 points[i]->height_func = height_func;
581 points[i]->height_data = user_data;
582 roam_point_update_height(points[i]);
586 g_list_free(triangles);
587 g_mutex_unlock(opengl->sphere_lock);
590 static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
594 RoamPoint *points[] = {root->p.l, root->p.m, root->p.r, root->split};
595 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
596 points[i]->height_func = NULL;
597 points[i]->height_data = NULL;
598 roam_point_update_height(points[i]);
600 _grits_opengl_clear_height_func_rec(root->kids[0]);
601 _grits_opengl_clear_height_func_rec(root->kids[1]);
604 static void grits_opengl_clear_height_func(GritsViewer *_opengl)
606 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
607 for (int i = 0; i < G_N_ELEMENTS(opengl->sphere->roots); i++)
608 _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
611 static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
612 gint key, gboolean sort)
614 g_assert(GRITS_IS_OPENGL(_opengl));
615 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
616 g_mutex_lock(opengl->objects_lock);
617 struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)(gintptr)key);
619 level = g_new0(struct RenderLevel, 1);
620 g_tree_insert(opengl->objects, (gpointer)(gintptr)key, level);
622 GList *list = sort ? &level->sorted : &level->unsorted;
623 /* Put the link in the list */
624 GList *link = g_new0(GList, 1);
627 link->next = list->next;
629 list->next->prev = link;
631 g_mutex_unlock(opengl->objects_lock);
635 static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
637 g_assert(GRITS_IS_OPENGL(_opengl));
638 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
639 GList *link = object->ref;
640 g_mutex_lock(opengl->objects_lock);
641 /* Just unlink and free it, link->prev is assured */
642 link->prev->next = link->next;
644 link->next->prev = link->prev;
645 g_mutex_unlock(opengl->objects_lock);
647 object->viewer = NULL;
649 g_object_unref(object);
656 static int _objects_cmp(gconstpointer _a, gconstpointer _b, gpointer _)
658 gintptr a = (gintptr)_a, b = (gintptr)_b;
662 static void _objects_free(gpointer value)
664 struct RenderLevel *level = value;
665 if (level->sorted.next)
666 g_list_free(level->sorted.next);
667 if (level->unsorted.next)
668 g_list_free(level->unsorted.next);
672 G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
673 static void grits_opengl_init(GritsOpenGL *opengl)
675 g_debug("GritsOpenGL: init");
676 opengl->objects = g_tree_new_full(_objects_cmp, NULL, NULL, _objects_free);
677 opengl->objects_lock = g_mutex_new();
678 opengl->sphere = roam_sphere_new(opengl);
679 opengl->sphere_lock = g_mutex_new();
680 gtk_gl_enable(GTK_WIDGET(opengl));
681 gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
682 g_signal_connect(opengl, "map", G_CALLBACK(on_realize), NULL);
684 static void grits_opengl_dispose(GObject *_opengl)
686 g_debug("GritsOpenGL: dispose");
687 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
688 if (opengl->sm_source[0]) {
689 g_source_remove(opengl->sm_source[0]);
690 opengl->sm_source[0] = 0;
692 if (opengl->sm_source[1]) {
693 g_source_remove(opengl->sm_source[1]);
694 opengl->sm_source[1] = 0;
696 if (opengl->ue_source) {
697 g_source_remove(opengl->ue_source);
698 opengl->ue_source = 0;
700 G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
702 static void grits_opengl_finalize(GObject *_opengl)
704 g_debug("GritsOpenGL: finalize");
705 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
706 roam_sphere_free(opengl->sphere);
707 g_tree_destroy(opengl->objects);
708 g_mutex_free(opengl->objects_lock);
709 g_mutex_free(opengl->sphere_lock);
710 G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
712 static void grits_opengl_class_init(GritsOpenGLClass *klass)
714 g_debug("GritsOpenGL: class_init");
715 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
716 gobject_class->finalize = grits_opengl_finalize;
717 gobject_class->dispose = grits_opengl_dispose;
719 GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
720 viewer_class->center_position = grits_opengl_center_position;
721 viewer_class->project = grits_opengl_project;
722 viewer_class->unproject = grits_opengl_unproject;
723 viewer_class->clear_height_func = grits_opengl_clear_height_func;
724 viewer_class->set_height_func = grits_opengl_set_height_func;
725 viewer_class->add = grits_opengl_add;
726 viewer_class->remove = grits_opengl_remove;