2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // OpenGL widget based on GtkGLExt / GtkGLArea
29 static void (*sharedContextCreated)() = nullptr;
30 static void (*sharedContextDestroyed)() = nullptr;
32 unsigned int g_context_count = 0;
34 ui::GLArea g_shared{ui::null};
36 static void _glwidget_context_created(ui::GLArea self, void *data)
38 if (++g_context_count == 1) {
40 g_object_ref(g_shared._handle);
42 glwidget_make_current(g_shared);
43 GlobalOpenGL().contextValid = true;
45 sharedContextCreated();
49 static void _glwidget_context_destroyed(ui::GLArea self, void *data)
51 if (--g_context_count == 0) {
52 GlobalOpenGL().contextValid = false;
54 sharedContextDestroyed();
57 g_shared = ui::GLArea(ui::null);
61 void glwidget_set_shared_context_constructors(
62 void created(), void destroyed() )
64 sharedContextCreated = created;
65 sharedContextDestroyed = destroyed;
72 static GdkGLContext *glwidget_context_created(ui::GLArea self)
74 _glwidget_context_created(self, nullptr);
75 return gtk_gl_area_get_context(self);
78 ui::GLArea glwidget_new(bool zbuffer)
80 auto self = ui::GLArea::from(GTK_GL_AREA(gtk_gl_area_new()));
81 gtk_gl_area_set_has_depth_buffer(self, zbuffer);
82 gtk_gl_area_set_auto_render(self, true); // FIXME
84 self.connect("realize", G_CALLBACK(glwidget_context_created), nullptr);
88 bool glwidget_make_current(ui::GLArea self)
90 // if (!g_context_count) {
91 // glwidget_context_created(self);
93 gtk_gl_area_make_current(self);
97 void glwidget_swap_buffers(ui::GLArea self)
99 g_assert(GTK_IS_GL_AREA(self));
100 gtk_gl_area_queue_render(self);
108 #include <gtk/gtkglwidget.h>
116 typedef const config_t *configs_iterator;
118 static int config_rgba32[] = {
121 GDK_GL_BUFFER_SIZE, 24,
122 GDK_GL_ATTRIB_LIST_NONE,
125 static int config_rgba[] = {
128 GDK_GL_BUFFER_SIZE, 16,
129 GDK_GL_ATTRIB_LIST_NONE,
132 static const config_t configs[] = {
134 "colour-buffer = 32bpp, depth-buffer = none",
138 "colour-buffer = 16bpp, depth-buffer = none",
143 static GdkGLConfig *glconfig_new()
145 for (configs_iterator i = configs, end = configs + 2; i != end; ++i) {
146 if (auto glconfig = gdk_gl_config_new(i->attribs)) {
147 globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
151 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
152 return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
155 static int config_rgba32_depth32[] = {
162 GDK_GL_ATTRIB_LIST_NONE,
165 static int config_rgba32_depth24[] = {
168 GDK_GL_BUFFER_SIZE, 24,
169 GDK_GL_DEPTH_SIZE, 24,
170 GDK_GL_ATTRIB_LIST_NONE,
173 static int config_rgba32_depth16[] = {
176 GDK_GL_BUFFER_SIZE, 24,
177 GDK_GL_DEPTH_SIZE, 16,
178 GDK_GL_ATTRIB_LIST_NONE,
181 static int config_rgba32_depth[] = {
184 GDK_GL_BUFFER_SIZE, 24,
185 GDK_GL_DEPTH_SIZE, 1,
186 GDK_GL_ATTRIB_LIST_NONE,
189 static int config_rgba_depth16[] = {
192 GDK_GL_BUFFER_SIZE, 16,
193 GDK_GL_DEPTH_SIZE, 16,
194 GDK_GL_ATTRIB_LIST_NONE,
197 static int config_rgba_depth[] = {
200 GDK_GL_BUFFER_SIZE, 16,
201 GDK_GL_DEPTH_SIZE, 1,
202 GDK_GL_ATTRIB_LIST_NONE,
205 static const config_t configs_with_depth[] =
208 "colour-buffer = 32bpp, depth-buffer = 32bpp",
209 config_rgba32_depth32,
212 "colour-buffer = 32bpp, depth-buffer = 24bpp",
213 config_rgba32_depth24,
216 "colour-buffer = 32bpp, depth-buffer = 16bpp",
217 config_rgba32_depth16,
220 "colour-buffer = 32bpp, depth-buffer = auto",
224 "colour-buffer = 16bpp, depth-buffer = 16bpp",
228 "colour-buffer = auto, depth-buffer = auto",
233 static GdkGLConfig *glconfig_new_with_depth()
235 for (configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i) {
236 if (auto glconfig = gdk_gl_config_new(i->attribs)) {
237 globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
241 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
242 return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
245 static int glwidget_context_created(ui::GLArea self, void *data)
247 _glwidget_context_created(self, data);
251 int glwidget_context_destroyed(ui::GLArea self, void *data)
253 _glwidget_context_destroyed(self, data);
257 static bool glwidget_enable_gl(ui::GLArea self, ui::Widget root, gpointer data)
259 if (!root && !gtk_widget_is_gl_capable(self)) {
260 const auto zbuffer = g_object_get_data(G_OBJECT(self), "zbuffer");
261 GdkGLConfig *glconfig = zbuffer ? glconfig_new_with_depth() : glconfig_new();
262 ASSERT_MESSAGE(glconfig, "failed to create OpenGL config");
264 const auto share_list = g_shared ? gtk_widget_get_gl_context(g_shared) : nullptr;
265 gtk_widget_set_gl_capability(self, glconfig, share_list, true, GDK_GL_RGBA_TYPE);
267 gtk_widget_realize(self);
276 ui::GLArea glwidget_new(bool zbuffer)
278 auto self = ui::GLArea::from(gtk_drawing_area_new());
280 g_object_set_data(G_OBJECT(self), "zbuffer", gint_to_pointer(zbuffer));
282 self.connect("hierarchy-changed", G_CALLBACK(glwidget_enable_gl), 0);
284 self.connect("realize", G_CALLBACK(glwidget_context_created), 0);
285 self.connect("unrealize", G_CALLBACK(glwidget_context_destroyed), 0);
290 void glwidget_swap_buffers(ui::GLArea self)
292 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
293 gdk_gl_drawable_swap_buffers(gldrawable);
296 bool glwidget_make_current(ui::GLArea self)
298 GdkGLContext *glcontext = gtk_widget_get_gl_context(self);
299 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
300 return gdk_gl_drawable_gl_begin(gldrawable, glcontext);