]> Pileus Git - grits/blob - src/gis/gis-opengl.c
Splitting GIS into a shared library, and a lot more
[grits] / src / gis / gis-opengl.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 #include <config.h>
19 #include <math.h>
20 #include <gdk/gdkkeysyms.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtkgl.h>
23 #include <GL/gl.h>
24 #include <GL/glu.h>
25
26 #include "gis-opengl.h"
27
28 /****************
29  * GObject code *
30  ****************/
31 G_DEFINE_TYPE(GisOpenGL, gis_opengl, G_TYPE_OBJECT);
32 static void gis_opengl_init(GisOpenGL *self)
33 {
34         g_debug("GisOpenGL: init");
35 }
36 static GObject *gis_opengl_constructor(GType gtype, guint n_properties,
37                 GObjectConstructParam *properties)
38 {
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);
42 }
43 static void gis_opengl_dispose(GObject *gobject)
44 {
45         g_debug("GisOpenGL: dispose");
46         GisOpenGL *self = GIS_OPENGL(gobject);
47         if (self->world) {
48                 g_object_unref(self->world);
49                 self->world = NULL;
50         }
51         if (self->view) {
52                 g_object_unref(self->view);
53                 self->view = NULL;
54         }
55         if (self->drawing) {
56                 g_object_unref(self->drawing);
57                 self->drawing = NULL;
58         }
59         G_OBJECT_CLASS(gis_opengl_parent_class)->dispose(gobject);
60 }
61 static void gis_opengl_finalize(GObject *gobject)
62 {
63         g_debug("GisOpenGL: finalize");
64         G_OBJECT_CLASS(gis_opengl_parent_class)->finalize(gobject);
65         gtk_main_quit();
66
67 }
68 static void gis_opengl_class_init(GisOpenGLClass *klass)
69 {
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;
75 }
76
77 /*************
78  * Callbacks *
79  *************/
80 gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, GisOpenGL *self)
81 {
82         g_debug("GisOpenGL: on_drawing_button_press - Grabbing focus");
83         gtk_widget_grab_focus(GTK_WIDGET(self->drawing));
84         return TRUE;
85 }
86 gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, GisOpenGL *self)
87 {
88         g_debug("GisOpenGL: on_drawing_key_press - key=%x, state=%x, plus=%x",
89                         event->keyval, event->state, GDK_plus);
90         double x,y,z;
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);
103         return TRUE;
104 }
105
106
107 gboolean on_map(GtkWidget *drawing, GdkEventConfigure *event, GisOpenGL *self)
108 {
109         g_debug("GisOpenGL: on_map");
110
111         /* Misc */
112         glEnable(GL_BLEND);
113         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
114         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
115
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);
121
122         /* Depth test */
123         glClearDepth(1.0);
124         glDepthFunc(GL_LEQUAL);
125         glEnable(GL_DEPTH_TEST);
126
127         gis_opengl_end(self);
128         return FALSE;
129 }
130
131 gboolean on_configure(GtkWidget *drawing, GdkEventConfigure *event, GisOpenGL *self)
132 {
133         g_debug("GisOpenGL: on_confiure");
134         gis_opengl_begin(self);
135
136         double x, y, z;
137         gis_view_get_location(self->view, &x, &y, &z);
138
139         /* Window is at 500 m from camera */
140         double width  = drawing->allocation.width;
141         double height = drawing->allocation.height;
142
143         glViewport(0, 0, width, height);
144
145         /* Perspective */
146         glMatrixMode(GL_PROJECTION);
147         glLoadIdentity();
148         double ang = atan((height/2)/500);
149
150         //gluPerspective(r2d(ang)*2, width/height, -z-20, -z+20);
151         gluPerspective(r2d(ang)*2, width/height, 1, 500*1000);
152
153         gis_opengl_end(self);
154         return FALSE;
155 }
156
157 gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, GisOpenGL *self)
158 {
159         g_debug("GisOpenGL: on_expose - begin");
160         gis_opengl_begin(self);
161
162         double lx, ly, lz;
163         double rx, ry, rz;
164         gis_view_get_location(self->view, &lx, &ly, &lz);
165         gis_view_get_rotation(self->view, &rx, &ry, &rz);
166
167         glMatrixMode(GL_MODELVIEW);
168         glLoadIdentity();
169         glTranslatef(lx, ly, lz);
170         glRotatef(rx, 1, 0, 0);
171         glRotatef(ry, 0, 1, 0);
172         glRotatef(rz, 0, 0, 1);
173
174         /* Expose plugins */
175         gis_plugins_foreach(self->plugins, G_CALLBACK(gis_plugin_expose), NULL);
176
177         gis_opengl_end(self);
178         gis_opengl_flush(self);
179         g_debug("GisOpenGL: on_expose - end\n");
180         return FALSE;
181 }
182
183 void on_state_changed(GisView *view,
184                 gdouble x, gdouble y, gdouble z, GisOpenGL *self)
185 {
186         /* Reset clipping area and redraw */
187         on_configure(NULL, NULL, self);
188         gis_opengl_redraw(self);
189 }
190
191 /***********
192  * Methods *
193  ***********/
194 GisOpenGL *gis_opengl_new(GisWorld *world, GisView *view, GtkDrawingArea *drawing)
195 {
196         g_debug("GisOpenGL: new");
197         GisOpenGL *self = g_object_new(GIS_TYPE_OPENGL, NULL);
198         self->world   = world;
199         self->view    = view;
200         self->drawing = drawing;
201         g_message("drawing = %p", drawing);
202         g_object_ref(world);
203         g_object_ref(view);
204         g_object_ref(drawing);
205
206         /* OpenGL setup */
207         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
208                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
209                         GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
210         if (!glconfig)
211                 g_error("Failed to create glconfig");
212         if (!gtk_widget_set_gl_capability(GTK_WIDGET(self->drawing),
213                                 glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
214                 g_error("GL lacks required capabilities");
215
216         g_signal_connect(self->view, "location-changed", G_CALLBACK(on_state_changed), self);
217         g_signal_connect(self->view, "rotation-changed", G_CALLBACK(on_state_changed), self);
218
219         g_signal_connect(self->drawing, "map-event",       G_CALLBACK(on_map),       self);
220         g_signal_connect(self->drawing, "configure-event", G_CALLBACK(on_configure), self);
221         g_signal_connect(self->drawing, "expose-event",    G_CALLBACK(on_expose),    self);
222
223         g_signal_connect(self->drawing, "button-press-event", G_CALLBACK(on_button_press), self);
224         g_signal_connect(self->drawing, "enter-notify-event", G_CALLBACK(on_button_press), self);
225         g_signal_connect(self->drawing, "key-press-event",    G_CALLBACK(on_key_press),    self);
226         return self;
227 }
228
229 void gis_opengl_redraw(GisOpenGL *self)
230 {
231         g_debug("GisOpenGL: gl_redraw");
232         gtk_widget_queue_draw(GTK_WIDGET(self->drawing));
233 }
234 void gis_opengl_begin(GisOpenGL *self)
235 {
236         g_assert(GIS_IS_OPENGL(self));
237
238         g_message("drawing = %p", self->drawing);
239         GdkGLContext   *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(self->drawing));
240         GdkGLDrawable  *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(self->drawing));
241
242         if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
243                 g_assert_not_reached();
244
245         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
246 }
247 void gis_opengl_end(GisOpenGL *self)
248 {
249         g_assert(GIS_IS_OPENGL(self));
250         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
251         gdk_gl_drawable_gl_end(gldrawable);
252 }
253 void gis_opengl_flush(GisOpenGL *self)
254 {
255         g_assert(GIS_IS_OPENGL(self));
256         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
257         if (gdk_gl_drawable_is_double_buffered(gldrawable))
258                 gdk_gl_drawable_swap_buffers(gldrawable);
259         else
260                 glFlush();
261         gdk_gl_drawable_gl_end(gldrawable);
262 }