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
26 #include "debugging/debugging.h"
30 #include <gtk/gtkdrawingarea.h>
31 #include <gtk/gtkglwidget.h>
35 void (*GLWidget_sharedContextCreated)() = 0;
36 void (*GLWidget_sharedContextDestroyed)() = 0;
39 typedef int* attribs_t;
45 typedef const config_t* configs_iterator;
47 int config_rgba32[] = {
50 GDK_GL_BUFFER_SIZE, 24,
51 GDK_GL_ATTRIB_LIST_NONE,
57 GDK_GL_BUFFER_SIZE, 16,
58 GDK_GL_ATTRIB_LIST_NONE,
61 const config_t configs[] = {
63 "colour-buffer = 32bpp, depth-buffer = none",
67 "colour-buffer = 16bpp, depth-buffer = none",
72 GdkGLConfig* glconfig_new()
74 GdkGLConfig* glconfig = 0;
76 for(configs_iterator i = configs, end = configs + 2; i != end; ++i)
78 glconfig = gdk_gl_config_new((*i).attribs);
81 globalOutputStream() << "OpenGL window configuration: " << (*i).name << "\n";
86 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
87 return gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
90 int config_rgba32_depth32[] = {
93 GDK_GL_BUFFER_SIZE, 24,
94 GDK_GL_DEPTH_SIZE, 32,
95 GDK_GL_ATTRIB_LIST_NONE,
98 int config_rgba32_depth24[] = {
101 GDK_GL_BUFFER_SIZE, 24,
102 GDK_GL_DEPTH_SIZE, 24,
103 GDK_GL_ATTRIB_LIST_NONE,
106 int config_rgba32_depth16[] = {
109 GDK_GL_BUFFER_SIZE, 24,
110 GDK_GL_DEPTH_SIZE, 16,
111 GDK_GL_ATTRIB_LIST_NONE,
114 int config_rgba32_depth[] = {
117 GDK_GL_BUFFER_SIZE, 24,
118 GDK_GL_DEPTH_SIZE, 1,
119 GDK_GL_ATTRIB_LIST_NONE,
122 int config_rgba_depth16[] = {
125 GDK_GL_BUFFER_SIZE, 16,
126 GDK_GL_DEPTH_SIZE, 16,
127 GDK_GL_ATTRIB_LIST_NONE,
130 int config_rgba_depth[] = {
133 GDK_GL_BUFFER_SIZE, 16,
134 GDK_GL_DEPTH_SIZE, 1,
135 GDK_GL_ATTRIB_LIST_NONE,
138 const config_t configs_with_depth[] =
141 "colour-buffer = 32bpp, depth-buffer = 32bpp",
142 config_rgba32_depth32,
145 "colour-buffer = 32bpp, depth-buffer = 24bpp",
146 config_rgba32_depth24,
149 "colour-buffer = 32bpp, depth-buffer = 16bpp",
150 config_rgba32_depth16,
153 "colour-buffer = 32bpp, depth-buffer = auto",
157 "colour-buffer = 16bpp, depth-buffer = 16bpp",
161 "colour-buffer = auto, depth-buffer = auto",
166 GdkGLConfig* glconfig_new_with_depth()
168 GdkGLConfig* glconfig = 0;
170 for(configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i)
172 glconfig = gdk_gl_config_new((*i).attribs);
175 globalOutputStream() << "OpenGL window configuration: " << (*i).name << "\n";
180 globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
181 return gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
184 unsigned int g_context_count = 0;
188 GtkWidget* g_shared = 0;
191 gint glwidget_context_created(GtkWidget* widget, gpointer data)
193 if(++g_context_count == 1)
196 gtk_widget_ref(g_shared);
198 glwidget_make_current(g_shared);
199 GlobalOpenGL().contextValid = true;
201 GLWidget_sharedContextCreated();
206 gint glwidget_context_destroyed(GtkWidget* widget, gpointer data)
208 if(--g_context_count == 0)
210 GlobalOpenGL().contextValid = false;
212 GLWidget_sharedContextDestroyed();
214 gtk_widget_unref(g_shared);
220 gboolean glwidget_enable_gl(GtkWidget* widget, GtkWidget* widget2, gpointer data)
222 if(widget2 == 0 && !gtk_widget_is_gl_capable(widget))
224 GdkGLConfig* glconfig = (g_object_get_data(G_OBJECT(widget), "zbuffer")) ? glconfig_new_with_depth() : glconfig_new();
225 ASSERT_MESSAGE(glconfig != 0, "failed to create OpenGL config");
227 gtk_widget_set_gl_capability(widget, glconfig, g_shared != 0 ? gtk_widget_get_gl_context(g_shared) : 0, TRUE, GDK_GL_RGBA_TYPE);
229 gtk_widget_realize(widget);
240 GtkWidget* glwidget_new(gboolean zbuffer)
242 GtkWidget* widget = gtk_drawing_area_new();
244 g_object_set_data(G_OBJECT(widget), "zbuffer", gint_to_pointer(zbuffer));
246 g_signal_connect(G_OBJECT(widget), "hierarchy-changed", G_CALLBACK(glwidget_enable_gl), 0);
248 g_signal_connect(G_OBJECT(widget), "realize", G_CALLBACK(glwidget_context_created), 0);
249 g_signal_connect(G_OBJECT(widget), "unrealize", G_CALLBACK(glwidget_context_destroyed), 0);
254 void glwidget_destroy_context (GtkWidget *widget)
258 void glwidget_create_context (GtkWidget *widget)
262 void glwidget_swap_buffers (GtkWidget *widget)
264 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
265 gdk_gl_drawable_swap_buffers (gldrawable);
268 gboolean glwidget_make_current (GtkWidget *widget)
270 GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
271 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
272 return gdk_gl_drawable_gl_begin (gldrawable, glcontext);