2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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/>.
20 #include <gdk/gdkkeysyms.h>
22 #include <gtk/gtkgl.h>
26 #include "gis-opengl.h"
31 G_DEFINE_TYPE(GisOpenGL, gis_opengl, G_TYPE_OBJECT);
32 static void gis_opengl_init(GisOpenGL *self)
34 g_debug("GisOpenGL: init");
36 static GObject *gis_opengl_constructor(GType gtype, guint n_properties,
37 GObjectConstructParam *properties)
39 g_debug("GisOpengl: constructor");
40 GObjectClass *parent_class = G_OBJECT_CLASS(gis_opengl_parent_class);
41 return parent_class->constructor(gtype, n_properties, properties);
43 static void gis_opengl_dispose(GObject *gobject)
45 g_debug("GisOpenGL: dispose");
46 GisOpenGL *self = GIS_OPENGL(gobject);
48 g_object_unref(self->world);
52 g_object_unref(self->view);
56 g_object_unref(self->drawing);
59 G_OBJECT_CLASS(gis_opengl_parent_class)->dispose(gobject);
61 static void gis_opengl_finalize(GObject *gobject)
63 g_debug("GisOpenGL: finalize");
64 G_OBJECT_CLASS(gis_opengl_parent_class)->finalize(gobject);
68 static void gis_opengl_class_init(GisOpenGLClass *klass)
70 g_debug("GisOpenGL: class_init");
71 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
72 gobject_class->constructor = gis_opengl_constructor;
73 gobject_class->dispose = gis_opengl_dispose;
74 gobject_class->finalize = gis_opengl_finalize;
80 gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, GisOpenGL *self)
82 g_debug("GisOpenGL: on_drawing_button_press - Grabbing focus");
83 gtk_widget_grab_focus(GTK_WIDGET(self->drawing));
86 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, GisOpenGL *self)
88 g_debug("GisOpenGL: on_drawing_key_press - key=%x, state=%x, plus=%x",
89 event->keyval, event->state, GDK_plus);
91 gis_view_get_location(self->view, &x, &y, &z);
92 guint kv = event->keyval;
93 if (kv == GDK_Left || kv == GDK_h) gis_view_pan(self->view, -z/10, 0, 0);
94 else if (kv == GDK_Down || kv == GDK_j) gis_view_pan(self->view, 0, -z/10, 0);
95 else if (kv == GDK_Up || kv == GDK_k) gis_view_pan(self->view, 0, z/10, 0);
96 else if (kv == GDK_Right || kv == GDK_l) gis_view_pan(self->view, z/10, 0, 0);
97 else if (kv == GDK_minus || kv == GDK_o) gis_view_zoom(self->view, 10./9);
98 else if (kv == GDK_plus || kv == GDK_i) gis_view_zoom(self->view, 9./10);
99 else if (kv == GDK_H ) gis_view_rotate(self->view, 0, -10, 0);
100 else if (kv == GDK_J ) gis_view_rotate(self->view, 10, 0, 0);
101 else if (kv == GDK_K ) gis_view_rotate(self->view, -10, 0, 0);
102 else if (kv == GDK_L ) gis_view_rotate(self->view, 0, 10, 0);
107 gboolean on_map(GtkWidget *drawing, GdkEventConfigure *event, GisOpenGL *self)
109 g_debug("GisOpenGL: on_map");
113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
114 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
116 /* Tessellation, "finding intersecting triangles" */
117 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
118 /* http://www.opengl.org/wiki/Alpha_Blending */
119 glAlphaFunc(GL_GREATER,0.1);
120 glEnable(GL_ALPHA_TEST);
124 glDepthFunc(GL_LEQUAL);
125 glEnable(GL_DEPTH_TEST);
127 gis_opengl_end(self);
131 gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, GisOpenGL *self)
133 g_debug("GisOpenGL: on_confiure");
134 gis_opengl_begin(self);
137 gis_view_get_location(self->view, &x, &y, &z);
139 /* Window is at 500 m from camera */
140 double width = GTK_WIDGET(self->drawing)->allocation.width;
141 double height = GTK_WIDGET(self->drawing)->allocation.height;
143 glViewport(0, 0, width, height);
146 glMatrixMode(GL_PROJECTION);
148 double ang = atan((height/2)/500);
150 //gluPerspective(r2d(ang)*2, width/height, -z-20, -z+20);
151 gluPerspective(r2d(ang)*2, width/height, 1, 500*1000);
153 gis_opengl_end(self);
157 gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, GisOpenGL *self)
159 g_debug("GisOpenGL: on_expose - begin");
160 gis_opengl_begin(self);
164 gis_view_get_location(self->view, &lx, &ly, &lz);
165 gis_view_get_rotation(self->view, &rx, &ry, &rz);
167 glMatrixMode(GL_MODELVIEW);
169 glTranslatef(lx, ly, lz);
170 glRotatef(rx, 1, 0, 0);
171 glRotatef(ry, 0, 1, 0);
172 glRotatef(rz, 0, 0, 1);
175 gis_plugins_foreach(self->plugins, G_CALLBACK(gis_plugin_expose), NULL);
177 gis_opengl_end(self);
178 gis_opengl_flush(self);
179 g_debug("GisOpenGL: on_expose - end\n");
183 void on_state_changed(GisView *view,
184 gdouble x, gdouble y, gdouble z, GisOpenGL *self)
186 /* Reset clipping area and redraw */
187 on_configure(NULL, NULL, self);
188 gis_opengl_redraw(self);
194 GisOpenGL *gis_opengl_new(GisWorld *world, GisView *view, GtkDrawingArea *drawing)
196 g_debug("GisOpenGL: new");
197 GisOpenGL *self = g_object_new(GIS_TYPE_OPENGL, NULL);
200 self->drawing = drawing;
203 g_object_ref(drawing);
206 GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
207 GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
208 GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
210 g_error("Failed to create glconfig");
211 if (!gtk_widget_set_gl_capability(GTK_WIDGET(self->drawing),
212 glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
213 g_error("GL lacks required capabilities");
215 g_signal_connect(self->view, "location-changed", G_CALLBACK(on_state_changed), self);
216 g_signal_connect(self->view, "rotation-changed", G_CALLBACK(on_state_changed), self);
218 g_signal_connect(self->drawing, "map-event", G_CALLBACK(on_map), self);
219 g_signal_connect(self->drawing, "configure-event", G_CALLBACK(on_configure), self);
220 g_signal_connect(self->drawing, "expose-event", G_CALLBACK(on_expose), self);
222 g_signal_connect(self->drawing, "button-press-event", G_CALLBACK(on_button_press), self);
223 g_signal_connect(self->drawing, "enter-notify-event", G_CALLBACK(on_button_press), self);
224 g_signal_connect(self->drawing, "key-press-event", G_CALLBACK(on_key_press), self);
228 void gis_opengl_redraw(GisOpenGL *self)
230 g_debug("GisOpenGL: gl_redraw");
231 gtk_widget_queue_draw(GTK_WIDGET(self->drawing));
233 void gis_opengl_begin(GisOpenGL *self)
235 g_assert(GIS_IS_OPENGL(self));
237 GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(self->drawing));
238 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(self->drawing));
240 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
241 g_assert_not_reached();
243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245 void gis_opengl_end(GisOpenGL *self)
247 g_assert(GIS_IS_OPENGL(self));
248 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
249 gdk_gl_drawable_gl_end(gldrawable);
251 void gis_opengl_flush(GisOpenGL *self)
253 g_assert(GIS_IS_OPENGL(self));
254 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
255 if (gdk_gl_drawable_is_double_buffered(gldrawable))
256 gdk_gl_drawable_swap_buffers(gldrawable);
259 gdk_gl_drawable_gl_end(gldrawable);