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 */
59 static void _set_visuals(GritsOpenGL *opengl)
61 double lat, lon, elev, rx, ry, rz;
62 grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
63 grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
65 /* Set projection and clipping planes */
66 glMatrixMode(GL_PROJECTION);
69 double width = GTK_WIDGET(opengl)->allocation.width;
70 double height = GTK_WIDGET(opengl)->allocation.height;
71 double ang = atan((height/2)/FOV_DIST)*2;
72 double atmos = 100000;
73 double near = MAX(elev*0.75 - atmos, 50); // View 100km of atmosphere
74 double far = elev + 2*EARTH_R + atmos; // on both sides of the earth
76 grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
77 glViewport(0, 0, width, height);
78 gluPerspective(rad2deg(ang), width/height, near, far);
80 /* Setup camera and lighting */
81 glMatrixMode(GL_MODELVIEW);
85 glRotatef(rx, 1, 0, 0);
86 glRotatef(rz, 0, 0, 1);
90 float light_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
91 float light_diffuse[] = {2.0f, 2.0f, 2.0f, 1.0f};
93 float light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
94 float light_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
96 float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
97 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
98 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
99 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
101 glEnable(GL_LIGHTING);
103 float material_ambient[] = {1.0, 1.0, 1.0, 1.0};
104 float material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
105 float material_specular[] = {0.0, 0.0, 0.0, 1.0};
106 float material_emission[] = {0.0, 0.0, 0.0, 1.0};
107 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
108 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
109 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
110 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_emission);
111 glDisable(GL_TEXTURE_2D);
112 glDisable(GL_COLOR_MATERIAL);
115 glTranslatef(0, 0, -elev2rad(elev));
116 glRotatef(lat, 1, 0, 0);
117 glRotatef(-lon, 0, 1, 0);
119 glDisable(GL_ALPHA_TEST);
121 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126 glEnable(GL_CULL_FACE);
129 glDepthFunc(GL_LEQUAL);
130 glEnable(GL_DEPTH_TEST);
133 glEnable(GL_LINE_SMOOTH);
135 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
136 //glShadeModel(GL_FLAT);
138 g_mutex_lock(opengl->sphere_lock);
139 roam_sphere_update_view(opengl->sphere);
140 g_mutex_unlock(opengl->sphere_lock);
143 static GPtrArray *_objects_to_array(GritsOpenGL *opengl, gboolean ortho)
145 GPtrArray *array = g_ptr_array_new();
146 for (GList *i = opengl->objects->head; i; i = i->next) {
147 struct RenderLevel *level = i->data;
148 if ((ortho == TRUE && level->num < GRITS_LEVEL_HUD) ||
149 (ortho == FALSE && level->num >= GRITS_LEVEL_HUD))
151 for (GList *j = level->unsorted.next; j; j = j->next)
152 g_ptr_array_add(array, j->data);
153 for (GList *j = level->sorted.next; j; j = j->next)
154 g_ptr_array_add(array, j->data);
162 static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
164 g_debug("GritsOpenGL: on_configure");
166 _set_visuals(opengl);
168 g_mutex_lock(opengl->sphere_lock);
169 roam_sphere_update_errors(opengl->sphere);
170 g_mutex_unlock(opengl->sphere_lock);
176 static gint run_picking(GritsOpenGL *opengl, GdkEvent *event,
177 GPtrArray *objects, GritsObject **top)
179 /* Setup picking buffers */
180 guint buffer[100][4] = {};
181 glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
182 if (!opengl->pickmode)
183 glRenderMode(GL_SELECT);
186 /* Render/pick objects */
187 for (guint i = 0; i < objects->len; i++) {
189 GritsObject *object = objects->pdata[i];
190 object->state.picked = FALSE;
191 grits_object_pick(object, opengl);
195 int hits = glRenderMode(GL_RENDER);
198 for (int i = 0; i < hits; i++) {
199 //g_debug("\tHit: %d", i);
200 //g_debug("\t\tcount: %d", buffer[i][0]);
201 //g_debug("\t\tz1: %f", (float)buffer[i][1]/0x7fffffff);
202 //g_debug("\t\tz2: %f", (float)buffer[i][2]/0x7fffffff);
203 //g_debug("\t\tname: %p", (gpointer)buffer[i][3]);
204 guint index = buffer[i][3];
205 GritsObject *object = objects->pdata[index];
206 object->state.picked = TRUE;
210 /* Notify objects of pointer movements */
211 for (guint i = 0; i < objects->len; i++) {
212 GritsObject *object = objects->pdata[i];
213 grits_object_set_pointer(object, event, object->state.picked);
219 static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
221 gdouble height = GTK_WIDGET(opengl)->allocation.height;
222 gdouble gl_x = event->x;
223 gdouble gl_y = height - event->y;
224 gdouble delta = opengl->pickmode ? 200 : 2;
226 if (opengl->pickmode) {
227 gtk_gl_begin(GTK_WIDGET(opengl));
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232 gdouble projection[16];
233 gint viewport[4]; // x=0,y=0,w,h
234 glGetDoublev(GL_PROJECTION_MATRIX, projection);
235 glGetIntegerv(GL_VIEWPORT, viewport);
236 glMatrixMode(GL_MODELVIEW); glPushMatrix();
237 glMatrixMode(GL_PROJECTION); glPushMatrix();
239 g_mutex_lock(opengl->objects_lock);
241 GritsObject *top = NULL;
242 GPtrArray *ortho = _objects_to_array(opengl, TRUE);
243 GPtrArray *world = _objects_to_array(opengl, FALSE);
245 /* Run perspective picking */
246 glMatrixMode(GL_PROJECTION); glLoadIdentity();
247 gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
248 glMultMatrixd(projection);
249 gint world_hits = run_picking(opengl, (GdkEvent*)event, world, &top);
251 /* Run ortho picking */
252 glMatrixMode(GL_PROJECTION); glLoadIdentity();
253 gluPickMatrix(gl_x, gl_y, delta, delta, viewport);
254 glMatrixMode(GL_MODELVIEW); glLoadIdentity();
255 glOrtho(0, viewport[2], viewport[3], 0, 1000, -1000);
256 gint ortho_hits = run_picking(opengl, (GdkEvent*)event, ortho, &top);
259 static GdkCursor *cursor = NULL;
260 static GdkWindow *window = NULL;
261 if (!window || !cursor) {
262 cursor = gdk_cursor_new(GDK_FLEUR);
263 window = gtk_widget_get_window(GTK_WIDGET(opengl));
265 GdkCursor *topcursor = top && top->cursor ? top->cursor : cursor;
266 gdk_window_set_cursor(window, topcursor);
268 g_debug("GritsOpenGL: on_motion_notify - hits=%d/%d,%d/%d ev=%.0lf,%.0lf",
269 world_hits, world->len, ortho_hits, ortho->len, gl_x, gl_y);
271 g_ptr_array_free(world, TRUE);
272 g_ptr_array_free(ortho, TRUE);
274 g_mutex_unlock(opengl->objects_lock);
278 //gdouble lat, lon, elev;
279 //grits_viewer_unproject(GRITS_VIEWER(opengl),
280 // gl_x, gl_y, -1, &lat, &lon, &elev);
283 glMatrixMode(GL_PROJECTION); glPopMatrix();
284 glMatrixMode(GL_MODELVIEW); glPopMatrix();
286 if (opengl->pickmode)
287 gtk_gl_end(GTK_WIDGET(opengl));
292 static void _draw_level(gpointer _level, gpointer _opengl)
294 GritsOpenGL *opengl = _opengl;
295 struct RenderLevel *level = _level;
297 g_debug("GritsOpenGL: _draw_level - level=%-4d", level->num);
298 int nsorted = 0, nunsorted = 0;
301 /* Configure individual levels */
302 if (level->num < GRITS_LEVEL_WORLD) {
303 /* Disable depth for background levels */
305 glDisable(GL_ALPHA_TEST);
306 } else if (level->num < GRITS_LEVEL_OVERLAY) {
307 /* Enable depth and alpha for world levels */
308 glEnable(GL_ALPHA_TEST);
309 glAlphaFunc(GL_GREATER, 0.1);
311 /* Disable depth for Overlay/HUD levels */
316 if (level->num >= GRITS_LEVEL_HUD) {
317 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
318 glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
319 gint win_width = GTK_WIDGET(opengl)->allocation.width;
320 gint win_height = GTK_WIDGET(opengl)->allocation.height;
321 glOrtho(0, win_width, win_height, 0, 1000, -1000);
324 /* Draw unsorted objects without depth testing,
325 * these are polygons, etc, rather than physical objects */
326 glDisable(GL_DEPTH_TEST);
327 for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
328 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
330 /* Draw sorted objects using depth testing
331 * These are things that are actually part of the world */
332 glEnable(GL_DEPTH_TEST);
333 for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
334 grits_object_draw(GRITS_OBJECT(cur->data), opengl);
337 if (level->num >= GRITS_LEVEL_HUD) {
338 glMatrixMode(GL_PROJECTION); glPopMatrix();
339 glMatrixMode(GL_MODELVIEW); glPopMatrix();
342 /* Leave depth buffer write enabled */
345 /* TODO: Prune empty levels */
347 g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
351 static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
353 g_debug("GritsOpenGL: on_expose - begin");
355 if (opengl->pickmode)
356 return on_motion_notify(opengl, (GdkEventMotion*)event, NULL);
358 gtk_gl_begin(GTK_WIDGET(opengl));
360 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
362 _set_visuals(opengl);
364 glColor4f(1.0, 1.0, 1.0, 1.0);
366 glDisable(GL_TEXTURE_2D);
367 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
368 roam_sphere_draw(opengl->sphere);
370 //roam_sphere_draw_normals(opengl->sphere);
372 g_mutex_lock(opengl->objects_lock);
373 if (opengl->wireframe)
374 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
375 g_queue_foreach(opengl->objects, _draw_level, opengl);
376 g_mutex_unlock(opengl->objects_lock);
379 gtk_gl_end(GTK_WIDGET(opengl));
381 g_debug("GritsOpenGL: on_expose - end\n");
385 static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
387 g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
388 event->keyval, event->state, GDK_plus);
390 guint kv = event->keyval;
393 opengl->wireframe = !opengl->wireframe;
394 gtk_widget_queue_draw(GTK_WIDGET(opengl));
397 opengl->pickmode = !opengl->pickmode;
398 gtk_widget_queue_draw(GTK_WIDGET(opengl));
401 else if (kv == GDK_n) roam_sphere_split_one(opengl->sphere);
402 else if (kv == GDK_p) roam_sphere_merge_one(opengl->sphere);
403 else if (kv == GDK_r) roam_sphere_split_merge(opengl->sphere);
404 else if (kv == GDK_u) roam_sphere_update_errors(opengl->sphere);
405 gtk_widget_queue_draw(GTK_WIDGET(opengl));
410 static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
412 for (GList *i = opengl->objects->tail; i; i = i->prev) {
413 struct RenderLevel *level = i->data;
414 for (GList *j = level->unsorted.next; j; j = j->next)
415 if (grits_object_event(j->data, event))
417 for (GList *j = level->sorted.next; j; j = j->next)
418 if (grits_object_event(j->data, event))
424 static gboolean _update_errors_cb(gpointer _opengl)
426 GritsOpenGL *opengl = _opengl;
427 g_mutex_lock(opengl->sphere_lock);
428 roam_sphere_update_errors(opengl->sphere);
429 g_mutex_unlock(opengl->sphere_lock);
430 opengl->ue_source = 0;
433 static void on_view_changed(GritsOpenGL *opengl,
434 gdouble _1, gdouble _2, gdouble _3)
436 g_debug("GritsOpenGL: on_view_changed");
437 _set_visuals(opengl);
439 if (!opengl->ue_source)
440 opengl->ue_source = g_idle_add_full(G_PRIORITY_HIGH_IDLE+30,
441 _update_errors_cb, opengl, NULL);
442 //roam_sphere_update_errors(opengl->sphere);
444 (void)_update_errors_cb;
448 static gboolean on_idle(GritsOpenGL *opengl)
450 //g_debug("GritsOpenGL: on_idle");
451 g_mutex_lock(opengl->sphere_lock);
452 if (roam_sphere_split_merge(opengl->sphere))
453 gtk_widget_queue_draw(GTK_WIDGET(opengl));
454 g_mutex_unlock(opengl->sphere_lock);
458 static void on_realize(GritsOpenGL *opengl, gpointer _)
460 g_debug("GritsOpenGL: on_realize");
461 gtk_gl_begin(GTK_WIDGET(opengl));
463 /* Connect signals and idle functions now that opengl is fully initialized */
464 gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
465 g_signal_connect(opengl, "configure-event", G_CALLBACK(on_configure), NULL);
466 g_signal_connect(opengl, "expose-event", G_CALLBACK(on_expose), NULL);
468 g_signal_connect(opengl, "key-press-event", G_CALLBACK(on_key_press), NULL);
470 g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
471 g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
473 g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
474 g_signal_connect_after(opengl, "key-press-event", G_CALLBACK(on_chained_event), NULL);
475 g_signal_connect_after(opengl, "key-release-event", G_CALLBACK(on_chained_event), NULL);
476 g_signal_connect_after(opengl, "button-press-event", G_CALLBACK(on_chained_event), NULL);
477 g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
478 g_signal_connect_after(opengl, "motion-notify-event", G_CALLBACK(on_chained_event), NULL);
481 opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33, (GSourceFunc)on_idle, opengl, NULL);
482 opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
485 (void)_update_errors_cb;
488 /* Re-queue resize incase configure was triggered before realize */
489 gtk_widget_queue_resize(GTK_WIDGET(opengl));
492 /*********************
493 * GritsViewer methods *
494 *********************/
497 * @plugins: the plugins store to use
498 * @prefs: the preferences object to use
500 * Create a new OpenGL renderer.
502 * Returns: the new #GritsOpenGL
504 GritsViewer *grits_opengl_new(GritsPlugins *plugins, GritsPrefs *prefs)
506 g_debug("GritsOpenGL: new");
507 GritsViewer *opengl = g_object_new(GRITS_TYPE_OPENGL, NULL);
508 grits_viewer_setup(opengl, plugins, prefs);
512 static void grits_opengl_center_position(GritsViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
514 glRotatef(lon, 0, 1, 0);
515 glRotatef(-lat, 1, 0, 0);
516 glTranslatef(0, 0, elev2rad(elev));
519 static void grits_opengl_project(GritsViewer *_opengl,
520 gdouble lat, gdouble lon, gdouble elev,
521 gdouble *px, gdouble *py, gdouble *pz)
523 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
525 lle2xyz(lat, lon, elev, &x, &y, &z);
527 opengl->sphere->view->model,
528 opengl->sphere->view->proj,
529 opengl->sphere->view->view,
533 static void grits_opengl_unproject(GritsViewer *_opengl,
534 gdouble px, gdouble py, gdouble pz,
535 gdouble *lat, gdouble *lon, gdouble *elev)
537 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
538 if (!opengl->sphere->view)
543 glReadPixels(px, py, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tmp);
546 gluUnProject(px, py, pz,
547 opengl->sphere->view->model,
548 opengl->sphere->view->proj,
549 opengl->sphere->view->view,
551 xyz2lle(x, y, z, lat, lon, elev);
552 //g_message("GritsOpenGL: unproject - "
553 // "%4.0lf,%4.0lf,(%5.3lf) -> "
554 // "%8.0lf,%8.0lf,%8.0lf -> "
555 // "%6.2lf,%7.2lf,%4.0lf",
556 // px, py, pz, x, y, z, *lat, *lon, *elev);
559 static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
560 RoamHeightFunc height_func, gpointer user_data, gboolean update)
562 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
563 /* TODO: get points? */
564 g_mutex_lock(opengl->sphere_lock);
565 GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
566 bounds->n, bounds->s, bounds->e, bounds->w);
567 for (GList *cur = triangles; cur; cur = cur->next) {
568 RoamTriangle *tri = cur->data;
569 RoamPoint *points[] = {tri->p.l, tri->p.m, tri->p.r, tri->split};
570 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
571 if (bounds->n >= points[i]->lat && points[i]->lat >= bounds->s &&
572 bounds->e >= points[i]->lon && points[i]->lon >= bounds->w) {
573 points[i]->height_func = height_func;
574 points[i]->height_data = user_data;
575 roam_point_update_height(points[i]);
579 g_list_free(triangles);
580 g_mutex_unlock(opengl->sphere_lock);
583 static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
587 RoamPoint *points[] = {root->p.l, root->p.m, root->p.r, root->split};
588 for (int i = 0; i < G_N_ELEMENTS(points); i++) {
589 points[i]->height_func = NULL;
590 points[i]->height_data = NULL;
591 roam_point_update_height(points[i]);
593 _grits_opengl_clear_height_func_rec(root->kids[0]);
594 _grits_opengl_clear_height_func_rec(root->kids[1]);
597 static void grits_opengl_clear_height_func(GritsViewer *_opengl)
599 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
600 for (int i = 0; i < G_N_ELEMENTS(opengl->sphere->roots); i++)
601 _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
604 static gint _objects_find(gconstpointer a, gconstpointer b)
606 const struct RenderLevel *level = a;
608 return level->num == *key ? 0 : 1;
611 static gint _objects_sort(gconstpointer _a, gconstpointer _b, gpointer _)
613 const struct RenderLevel *a = _a;
614 const struct RenderLevel *b = _b;
615 return a->num < b->num ? -1 :
616 a->num > b->num ? 1 : 0;
619 static void _objects_free(gpointer value, gpointer _)
621 struct RenderLevel *level = value;
622 if (level->sorted.next)
623 g_list_free(level->sorted.next);
624 if (level->unsorted.next)
625 g_list_free(level->unsorted.next);
629 static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
630 gint num, gboolean sort)
632 g_assert(GRITS_IS_OPENGL(_opengl));
633 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
634 g_mutex_lock(opengl->objects_lock);
635 struct RenderLevel *level = NULL;
636 GList *tmp = g_queue_find_custom(opengl->objects, &num, _objects_find);
640 level = g_new0(struct RenderLevel, 1);
642 g_queue_insert_sorted(opengl->objects, level, _objects_sort, NULL);
644 GList *list = sort ? &level->sorted : &level->unsorted;
645 /* Put the link in the list */
646 GList *link = g_new0(GList, 1);
649 link->next = list->next;
651 list->next->prev = link;
653 g_mutex_unlock(opengl->objects_lock);
657 static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
659 g_assert(GRITS_IS_OPENGL(_opengl));
660 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
661 GList *link = object->ref;
662 g_mutex_lock(opengl->objects_lock);
663 /* Just unlink and free it, link->prev is assured */
664 link->prev->next = link->next;
666 link->next->prev = link->prev;
667 g_mutex_unlock(opengl->objects_lock);
669 object->viewer = NULL;
671 g_object_unref(object);
678 G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
679 static void grits_opengl_init(GritsOpenGL *opengl)
681 g_debug("GritsOpenGL: init");
682 opengl->objects = g_queue_new();
683 opengl->objects_lock = g_mutex_new();
684 opengl->sphere = roam_sphere_new(opengl);
685 opengl->sphere_lock = g_mutex_new();
686 gtk_gl_enable(GTK_WIDGET(opengl));
687 gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
688 g_signal_connect(opengl, "map", G_CALLBACK(on_realize), NULL);
690 static void grits_opengl_dispose(GObject *_opengl)
692 g_debug("GritsOpenGL: dispose");
693 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
694 if (opengl->sm_source[0]) {
695 g_source_remove(opengl->sm_source[0]);
696 opengl->sm_source[0] = 0;
698 if (opengl->sm_source[1]) {
699 g_source_remove(opengl->sm_source[1]);
700 opengl->sm_source[1] = 0;
702 if (opengl->ue_source) {
703 g_source_remove(opengl->ue_source);
704 opengl->ue_source = 0;
706 G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
708 static void grits_opengl_finalize(GObject *_opengl)
710 g_debug("GritsOpenGL: finalize");
711 GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
712 roam_sphere_free(opengl->sphere);
713 g_queue_foreach(opengl->objects, _objects_free, NULL);
714 g_queue_free(opengl->objects);
715 g_mutex_free(opengl->objects_lock);
716 g_mutex_free(opengl->sphere_lock);
717 G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
719 static void grits_opengl_class_init(GritsOpenGLClass *klass)
721 g_debug("GritsOpenGL: class_init");
722 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
723 gobject_class->finalize = grits_opengl_finalize;
724 gobject_class->dispose = grits_opengl_dispose;
726 GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
727 viewer_class->center_position = grits_opengl_center_position;
728 viewer_class->project = grits_opengl_project;
729 viewer_class->unproject = grits_opengl_unproject;
730 viewer_class->clear_height_func = grits_opengl_clear_height_func;
731 viewer_class->set_height_func = grits_opengl_set_height_func;
732 viewer_class->add = grits_opengl_add;
733 viewer_class->remove = grits_opengl_remove;