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
28 void (*GLWidget_sharedContextCreated)() = 0;
30 void (*GLWidget_sharedContextDestroyed)() = 0;
32 unsigned int g_context_count = 0;
34 ui::GLArea g_shared{ui::null};
36 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 GLWidget_sharedContextCreated();
49 void _glwidget_context_destroyed(ui::GLArea self, void *data)
51 if (--g_context_count == 0) {
52 GlobalOpenGL().contextValid = false;
54 GLWidget_sharedContextDestroyed();
57 g_shared = ui::GLArea(ui::null);
61 void glwidget_destroy_context(ui::GLArea self)
65 void glwidget_create_context(ui::GLArea self)
73 GdkGLContext *glwidget_context_created(ui::GLArea self)
75 _glwidget_context_created(self, nullptr);
76 return gtk_gl_area_get_context(self);
79 ui::GLArea glwidget_new(bool zbuffer)
81 auto self = ui::GLArea(GTK_GL_AREA(gtk_gl_area_new()));
82 gtk_gl_area_set_has_depth_buffer(self, zbuffer);
83 gtk_gl_area_set_auto_render(self, false);
85 self.connect("realize", G_CALLBACK(glwidget_context_created), nullptr);
89 bool glwidget_make_current(ui::GLArea self)
91 // if (!g_context_count) {
92 // glwidget_context_created(self);
94 gtk_gl_area_make_current(self);
95 auto valid = GlobalOpenGL().contextValid;
99 void glwidget_swap_buffers(ui::GLArea self)
101 gtk_gl_area_queue_render(self);
109 #include <gtk/gtkglwidget.h>
117 typedef const config_t *configs_iterator;
119 int config_rgba32[] = {
122 GDK_GL_BUFFER_SIZE, 24,
123 GDK_GL_ATTRIB_LIST_NONE,
126 int config_rgba[] = {
129 GDK_GL_BUFFER_SIZE, 16,
130 GDK_GL_ATTRIB_LIST_NONE,
133 const config_t configs[] = {
135 "colour-buffer = 32bpp, depth-buffer = none",
139 "colour-buffer = 16bpp, depth-buffer = none",
144 GdkGLConfig *glconfig_new()
146 for (configs_iterator i = configs, end = configs + 2; i != end; ++i) {
147 if (auto glconfig = gdk_gl_config_new(i->attribs)) {
148 globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
152 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
153 return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
156 int config_rgba32_depth32[] = {
163 GDK_GL_ATTRIB_LIST_NONE,
166 int config_rgba32_depth24[] = {
169 GDK_GL_BUFFER_SIZE, 24,
170 GDK_GL_DEPTH_SIZE, 24,
171 GDK_GL_ATTRIB_LIST_NONE,
174 int config_rgba32_depth16[] = {
177 GDK_GL_BUFFER_SIZE, 24,
178 GDK_GL_DEPTH_SIZE, 16,
179 GDK_GL_ATTRIB_LIST_NONE,
182 int config_rgba32_depth[] = {
185 GDK_GL_BUFFER_SIZE, 24,
186 GDK_GL_DEPTH_SIZE, 1,
187 GDK_GL_ATTRIB_LIST_NONE,
190 int config_rgba_depth16[] = {
193 GDK_GL_BUFFER_SIZE, 16,
194 GDK_GL_DEPTH_SIZE, 16,
195 GDK_GL_ATTRIB_LIST_NONE,
198 int config_rgba_depth[] = {
201 GDK_GL_BUFFER_SIZE, 16,
202 GDK_GL_DEPTH_SIZE, 1,
203 GDK_GL_ATTRIB_LIST_NONE,
206 const config_t configs_with_depth[] =
209 "colour-buffer = 32bpp, depth-buffer = 32bpp",
210 config_rgba32_depth32,
213 "colour-buffer = 32bpp, depth-buffer = 24bpp",
214 config_rgba32_depth24,
217 "colour-buffer = 32bpp, depth-buffer = 16bpp",
218 config_rgba32_depth16,
221 "colour-buffer = 32bpp, depth-buffer = auto",
225 "colour-buffer = 16bpp, depth-buffer = 16bpp",
229 "colour-buffer = auto, depth-buffer = auto",
234 GdkGLConfig *glconfig_new_with_depth()
236 for (configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i) {
237 if (auto glconfig = gdk_gl_config_new(i->attribs)) {
238 globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
242 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
243 return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
246 int glwidget_context_created(ui::GLArea self, void *data)
248 _glwidget_context_created(self, data);
252 int glwidget_context_destroyed(ui::GLArea self, void *data)
254 _glwidget_context_destroyed(self, data);
258 bool glwidget_enable_gl(ui::GLArea self, ui::Widget root, gpointer data)
260 if (!root && !gtk_widget_is_gl_capable(self)) {
261 const auto zbuffer = g_object_get_data(G_OBJECT(self), "zbuffer");
262 GdkGLConfig *glconfig = zbuffer ? glconfig_new_with_depth() : glconfig_new();
263 ASSERT_MESSAGE(glconfig, "failed to create OpenGL config");
265 const auto share_list = g_shared ? gtk_widget_get_gl_context(g_shared) : nullptr;
266 gtk_widget_set_gl_capability(self, glconfig, share_list, true, GDK_GL_RGBA_TYPE);
268 gtk_widget_realize(self);
277 ui::GLArea glwidget_new(bool zbuffer)
279 auto self = ui::GLArea::from(gtk_drawing_area_new());
281 g_object_set_data(G_OBJECT(self), "zbuffer", gint_to_pointer(zbuffer));
283 self.connect("hierarchy-changed", G_CALLBACK(glwidget_enable_gl), 0);
285 self.connect("realize", G_CALLBACK(glwidget_context_created), 0);
286 self.connect("unrealize", G_CALLBACK(glwidget_context_destroyed), 0);
291 void glwidget_swap_buffers(ui::GLArea self)
293 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
294 gdk_gl_drawable_swap_buffers(gldrawable);
297 bool glwidget_make_current(ui::GLArea self)
299 GdkGLContext *glcontext = gtk_widget_get_gl_context(self);
300 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
301 return gdk_gl_drawable_gl_begin(gldrawable, glcontext);