]>
Commit | Line | Data |
---|---|---|
8b089c5e | 1 | ///////////////////////////////////////////////////////////////////////////// |
9b5f1895 | 2 | // Name: src/gtk1/glcanvas.cpp |
77ffb593 | 3 | // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK |
8b089c5e JS |
4 | // Author: Robert Roebling |
5 | // Modified by: | |
6 | // Created: 17/08/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
65571936 | 9 | // Licence: wxWindows licence |
8b089c5e JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 VS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
8b089c5e JS |
15 | #if wxUSE_GLCANVAS |
16 | ||
17 | #include "wx/glcanvas.h" | |
18 | ||
670f9935 WS |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/app.h" | |
21 | #endif // WX_PRECOMP | |
22 | ||
8b089c5e JS |
23 | #include "wx/frame.h" |
24 | #include "wx/colour.h" | |
25 | #include "wx/module.h" | |
8b089c5e | 26 | |
2b5f62a0 VZ |
27 | extern "C" |
28 | { | |
8b089c5e JS |
29 | #include "gtk/gtk.h" |
30 | #include "gdk/gdk.h" | |
31 | #include "gdk/gdkx.h" | |
32 | } | |
33 | ||
3cbab641 | 34 | #include "wx/gtk1/win_gtk.h" |
8b089c5e | 35 | |
34fdf762 VS |
36 | // DLL options compatibility check: |
37 | #include "wx/build.h" | |
38 | WX_CHECK_BUILD_OPTIONS("wxGL") | |
39 | ||
34a34b02 VZ |
40 | |
41 | //--------------------------------------------------------------------------- | |
42 | // static variables | |
43 | //--------------------------------------------------------------------------- | |
44 | int wxGLCanvas::m_glxVersion = 0; | |
45 | ||
8b089c5e JS |
46 | //--------------------------------------------------------------------------- |
47 | // global data | |
48 | //--------------------------------------------------------------------------- | |
49 | ||
50 | XVisualInfo *g_vi = (XVisualInfo*) NULL; | |
8b089c5e JS |
51 | //----------------------------------------------------------------------------- |
52 | // idle system | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | extern void wxapp_install_idle_handler(); | |
56 | extern bool g_isIdle; | |
57 | ||
58 | //--------------------------------------------------------------------------- | |
59 | // wxGLContext | |
60 | //--------------------------------------------------------------------------- | |
61 | ||
62 | IMPLEMENT_CLASS(wxGLContext,wxObject) | |
63 | ||
64 | wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) ) | |
65 | { | |
66 | m_window = win; | |
67 | m_widget = win->m_wxwindow; | |
68 | ||
69 | wxGLCanvas *gc = (wxGLCanvas*) win; | |
2b5f62a0 | 70 | |
34a34b02 | 71 | if (wxGLCanvas::GetGLXVersion() >= 13) |
b4d06fb7 MR |
72 | { |
73 | // GLX >= 1.3 | |
74 | GLXFBConfig *fbc = gc->m_fbc; | |
75 | wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") ); | |
76 | m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE, None, GL_TRUE ); | |
77 | } | |
34a34b02 | 78 | else |
b4d06fb7 MR |
79 | { |
80 | // GLX <= 1.2 | |
81 | XVisualInfo *vi = (XVisualInfo *) gc->m_vi; | |
82 | wxCHECK_RET( vi, _T("invalid visual for OpenGl") ); | |
83 | m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE ); | |
84 | } | |
2b5f62a0 VZ |
85 | |
86 | wxCHECK_RET( m_glContext, _T("Couldn't create OpenGl context") ); | |
8b089c5e JS |
87 | } |
88 | ||
2b5f62a0 VZ |
89 | wxGLContext::wxGLContext( |
90 | bool WXUNUSED(isRGB), wxWindow *win, | |
8b089c5e JS |
91 | const wxPalette& WXUNUSED(palette), |
92 | const wxGLContext *other /* for sharing display lists */ | |
93 | ) | |
94 | { | |
95 | m_window = win; | |
96 | m_widget = win->m_wxwindow; | |
97 | ||
98 | wxGLCanvas *gc = (wxGLCanvas*) win; | |
2b5f62a0 | 99 | |
34a34b02 | 100 | if (wxGLCanvas::GetGLXVersion() >= 13) |
b4d06fb7 MR |
101 | { |
102 | // GLX >= 1.3 | |
103 | GLXFBConfig *fbc = gc->m_fbc; | |
104 | wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") ); | |
105 | m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE, | |
106 | other ? other->m_glContext : None, | |
107 | GL_TRUE ); | |
108 | } | |
34a34b02 | 109 | else |
b4d06fb7 MR |
110 | { |
111 | // GLX <= 1.2 | |
112 | XVisualInfo *vi = (XVisualInfo *) gc->m_vi; | |
113 | wxCHECK_RET( vi, _T("invalid visual for OpenGl") ); | |
114 | m_glContext = glXCreateContext( GDK_DISPLAY(), vi, | |
115 | other ? other->m_glContext : None, | |
116 | GL_TRUE ); | |
117 | } | |
2b5f62a0 VZ |
118 | |
119 | if ( !m_glContext ) | |
120 | { | |
121 | wxFAIL_MSG( _T("Couldn't create OpenGl context") ); | |
122 | } | |
8b089c5e JS |
123 | } |
124 | ||
125 | wxGLContext::~wxGLContext() | |
126 | { | |
127 | if (!m_glContext) return; | |
2b5f62a0 | 128 | |
8b089c5e JS |
129 | if (m_glContext == glXGetCurrentContext()) |
130 | { | |
b4d06fb7 MR |
131 | if (wxGLCanvas::GetGLXVersion() >= 13) |
132 | // GLX >= 1.3 | |
133 | glXMakeContextCurrent( GDK_DISPLAY(), None, None, NULL); | |
134 | else | |
135 | // GLX <= 1.2 | |
136 | glXMakeCurrent( GDK_DISPLAY(), None, NULL); | |
8b089c5e | 137 | } |
2b5f62a0 | 138 | |
8b089c5e JS |
139 | glXDestroyContext( GDK_DISPLAY(), m_glContext ); |
140 | } | |
141 | ||
142 | void wxGLContext::SwapBuffers() | |
143 | { | |
144 | if (m_glContext) | |
145 | { | |
146 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; | |
147 | glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) ); | |
148 | } | |
149 | } | |
150 | ||
151 | void wxGLContext::SetCurrent() | |
152 | { | |
2b5f62a0 VZ |
153 | if (m_glContext) |
154 | { | |
8b089c5e | 155 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; |
b4d06fb7 MR |
156 | |
157 | if (wxGLCanvas::GetGLXVersion() >= 13) | |
158 | // GLX >= 1.3 | |
159 | glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), | |
160 | GDK_WINDOW_XWINDOW(window), m_glContext ); | |
161 | else | |
162 | // GLX <= 1.2 | |
163 | glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext ); | |
8b089c5e JS |
164 | } |
165 | } | |
166 | ||
2b5f62a0 | 167 | void wxGLContext::SetColour(const wxChar *colour) |
8b089c5e | 168 | { |
564a150b VZ |
169 | wxColour col = wxTheColourDatabase->Find(colour); |
170 | if (col.Ok()) | |
8b089c5e | 171 | { |
564a150b VZ |
172 | float r = (float)(col.Red()/256.0); |
173 | float g = (float)(col.Green()/256.0); | |
174 | float b = (float)(col.Blue()/256.0); | |
8b089c5e JS |
175 | glColor3f( r, g, b); |
176 | } | |
177 | } | |
178 | ||
179 | void wxGLContext::SetupPixelFormat() | |
180 | { | |
181 | } | |
182 | ||
183 | void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) ) | |
184 | { | |
185 | } | |
186 | ||
187 | wxPalette wxGLContext::CreateDefaultPalette() | |
188 | { | |
189 | return wxNullPalette; | |
190 | } | |
191 | ||
192 | //----------------------------------------------------------------------------- | |
193 | // "realize" from m_wxwindow | |
194 | //----------------------------------------------------------------------------- | |
195 | ||
865bb325 | 196 | extern "C" { |
8b089c5e | 197 | static gint |
34a34b02 | 198 | gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win ) |
8b089c5e | 199 | { |
4230303c VZ |
200 | if ( !win->m_glContext ) |
201 | { | |
202 | wxGLContext *share = win->m_sharedContext; | |
203 | if ( !share && win->m_sharedContextOf ) | |
204 | share = win->m_sharedContextOf->GetContext(); | |
8b089c5e | 205 | |
4230303c VZ |
206 | win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share ); |
207 | } | |
8b089c5e JS |
208 | |
209 | return FALSE; | |
210 | } | |
865bb325 | 211 | } |
8b089c5e JS |
212 | |
213 | //----------------------------------------------------------------------------- | |
214 | // "map" from m_wxwindow | |
215 | //----------------------------------------------------------------------------- | |
216 | ||
865bb325 | 217 | extern "C" { |
8b089c5e JS |
218 | static gint |
219 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
220 | { | |
221 | if (win->m_glContext/* && win->m_exposed*/) | |
222 | { | |
223 | wxPaintEvent event( win->GetId() ); | |
224 | event.SetEventObject( win ); | |
225 | win->GetEventHandler()->ProcessEvent( event ); | |
226 | ||
670f9935 | 227 | win->m_exposed = false; |
8b089c5e JS |
228 | win->GetUpdateRegion().Clear(); |
229 | } | |
230 | ||
231 | return FALSE; | |
232 | } | |
865bb325 | 233 | } |
8b089c5e JS |
234 | |
235 | //----------------------------------------------------------------------------- | |
236 | // "expose_event" of m_wxwindow | |
237 | //----------------------------------------------------------------------------- | |
238 | ||
865bb325 | 239 | extern "C" { |
2b5f62a0 | 240 | static void |
8b089c5e JS |
241 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) |
242 | { | |
2b5f62a0 | 243 | if (g_isIdle) |
8b089c5e JS |
244 | wxapp_install_idle_handler(); |
245 | ||
670f9935 | 246 | win->m_exposed = true; |
8b089c5e JS |
247 | |
248 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
249 | gdk_event->area.y, | |
250 | gdk_event->area.width, | |
251 | gdk_event->area.height ); | |
252 | } | |
865bb325 | 253 | } |
8b089c5e JS |
254 | |
255 | //----------------------------------------------------------------------------- | |
256 | // "draw" of m_wxwindow | |
257 | //----------------------------------------------------------------------------- | |
258 | ||
865bb325 | 259 | extern "C" { |
2b5f62a0 | 260 | static void |
8b089c5e JS |
261 | gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win ) |
262 | { | |
2b5f62a0 | 263 | if (g_isIdle) |
8b089c5e JS |
264 | wxapp_install_idle_handler(); |
265 | ||
670f9935 | 266 | win->m_exposed = true; |
8b089c5e JS |
267 | |
268 | win->GetUpdateRegion().Union( rect->x, rect->y, | |
269 | rect->width, rect->height ); | |
270 | } | |
865bb325 | 271 | } |
8b089c5e JS |
272 | |
273 | //----------------------------------------------------------------------------- | |
274 | // "size_allocate" of m_wxwindow | |
275 | //----------------------------------------------------------------------------- | |
276 | ||
865bb325 | 277 | extern "C" { |
2b5f62a0 | 278 | static void |
8b089c5e JS |
279 | gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win ) |
280 | { | |
281 | if (g_isIdle) | |
282 | wxapp_install_idle_handler(); | |
283 | ||
284 | if (!win->m_hasVMT) | |
285 | return; | |
286 | ||
287 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
288 | event.SetEventObject( win ); | |
289 | win->GetEventHandler()->ProcessEvent( event ); | |
290 | } | |
865bb325 | 291 | } |
8b089c5e JS |
292 | |
293 | //--------------------------------------------------------------------------- | |
294 | // wxGlCanvas | |
295 | //--------------------------------------------------------------------------- | |
296 | ||
4660d7e5 | 297 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 298 | |
4660d7e5 | 299 | BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow) |
8b089c5e JS |
300 | EVT_SIZE(wxGLCanvas::OnSize) |
301 | END_EVENT_TABLE() | |
302 | ||
303 | wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id, | |
2b5f62a0 VZ |
304 | const wxPoint& pos, const wxSize& size, |
305 | long style, const wxString& name, | |
306 | int *attribList, | |
307 | const wxPalette& palette ) | |
8b089c5e JS |
308 | { |
309 | Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette ); | |
310 | } | |
311 | ||
2b5f62a0 | 312 | wxGLCanvas::wxGLCanvas( wxWindow *parent, |
8b089c5e JS |
313 | const wxGLContext *shared, |
314 | wxWindowID id, | |
2b5f62a0 VZ |
315 | const wxPoint& pos, const wxSize& size, |
316 | long style, const wxString& name, | |
317 | int *attribList, | |
318 | const wxPalette& palette ) | |
319 | { | |
8b089c5e JS |
320 | Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette ); |
321 | } | |
322 | ||
2b5f62a0 | 323 | wxGLCanvas::wxGLCanvas( wxWindow *parent, |
8b089c5e JS |
324 | const wxGLCanvas *shared, |
325 | wxWindowID id, | |
2b5f62a0 VZ |
326 | const wxPoint& pos, const wxSize& size, |
327 | long style, const wxString& name, | |
328 | int *attribList, | |
329 | const wxPalette& palette ) | |
330 | { | |
8b089c5e JS |
331 | Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette ); |
332 | } | |
333 | ||
2b5f62a0 | 334 | bool wxGLCanvas::Create( wxWindow *parent, |
8b089c5e JS |
335 | const wxGLContext *shared, |
336 | const wxGLCanvas *shared_context_of, | |
337 | wxWindowID id, | |
2b5f62a0 VZ |
338 | const wxPoint& pos, const wxSize& size, |
339 | long style, const wxString& name, | |
340 | int *attribList, | |
341 | const wxPalette& palette) | |
8b089c5e JS |
342 | { |
343 | m_sharedContext = (wxGLContext*)shared; // const_cast | |
344 | m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast | |
345 | m_glContext = (wxGLContext*) NULL; | |
2b5f62a0 | 346 | |
670f9935 WS |
347 | m_exposed = false; |
348 | m_noExpose = true; | |
349 | m_nativeSizeEvent = true; | |
34a34b02 VZ |
350 | m_fbc = NULL; |
351 | m_vi = NULL; | |
352 | ||
353 | // to be sure the glx version is known | |
354 | wxGLCanvas::QueryGLXVersion(); | |
355 | ||
356 | if (wxGLCanvas::GetGLXVersion() >= 13) | |
b4d06fb7 MR |
357 | { |
358 | // GLX >= 1.3 uses a GLXFBConfig | |
359 | GLXFBConfig * fbc = NULL; | |
360 | if (wxTheApp->m_glFBCInfo != NULL) | |
361 | { | |
362 | fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo; | |
670f9935 | 363 | m_canFreeFBC = false; // owned by wxTheApp - don't free upon destruction |
b4d06fb7 MR |
364 | } |
365 | else | |
366 | { | |
367 | fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList); | |
670f9935 | 368 | m_canFreeFBC = true; |
b4d06fb7 MR |
369 | } |
370 | m_fbc = fbc; // save for later use | |
670f9935 | 371 | wxCHECK_MSG( m_fbc, false, _T("required FBConfig couldn't be found") ); |
b4d06fb7 | 372 | } |
2b5f62a0 | 373 | |
a6f5aa49 | 374 | XVisualInfo *vi = NULL; |
2b5f62a0 VZ |
375 | if (wxTheApp->m_glVisualInfo != NULL) |
376 | { | |
b4d06fb7 | 377 | vi = (XVisualInfo *)wxTheApp->m_glVisualInfo; |
670f9935 | 378 | m_canFreeVi = false; // owned by wxTheApp - don't free upon destruction |
2b5f62a0 VZ |
379 | } |
380 | else | |
381 | { | |
b4d06fb7 MR |
382 | if (wxGLCanvas::GetGLXVersion() >= 13) |
383 | // GLX >= 1.3 | |
384 | vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]); | |
385 | else | |
386 | // GLX <= 1.2 | |
387 | vi = (XVisualInfo *) ChooseGLVisual(attribList); | |
388 | ||
670f9935 | 389 | m_canFreeVi = true; |
a6f5aa49 | 390 | } |
b4d06fb7 | 391 | |
a6f5aa49 | 392 | m_vi = vi; // save for later use |
2b5f62a0 | 393 | |
670f9935 | 394 | wxCHECK_MSG( m_vi, false, _T("required visual couldn't be found") ); |
fee7a683 MR |
395 | GdkVisual *visual; |
396 | GdkColormap *colormap; | |
2b5f62a0 | 397 | |
fee7a683 | 398 | // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME) |
3cbab641 | 399 | #if 0 |
fee7a683 MR |
400 | if (!gtk_check_version(2,2,0)) |
401 | { | |
402 | wxWindow::Create( parent, id, pos, size, style, name ); | |
403 | ||
404 | m_glWidget = m_wxwindow; | |
405 | ||
406 | GdkScreen *screen = gtk_widget_get_screen( m_glWidget ); | |
407 | colormap = gdk_screen_get_default_colormap(screen); | |
408 | visual = gdk_colormap_get_visual(colormap); | |
409 | ||
410 | if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid) | |
411 | { | |
412 | visual = gdk_x11_screen_lookup_visual( screen, vi->visualid ); | |
413 | colormap = gdk_colormap_new(visual, FALSE); | |
414 | } | |
415 | ||
416 | gtk_widget_set_colormap( m_glWidget, colormap ); | |
417 | } | |
418 | else | |
419 | #endif | |
420 | { | |
421 | visual = gdkx_visual_get( vi->visualid ); | |
422 | colormap = gdk_colormap_new( visual, TRUE ); | |
a6f5aa49 | 423 | |
fee7a683 MR |
424 | gtk_widget_push_colormap( colormap ); |
425 | gtk_widget_push_visual( visual ); | |
a6f5aa49 | 426 | |
fee7a683 MR |
427 | wxWindow::Create( parent, id, pos, size, style, name ); |
428 | m_glWidget = m_wxwindow; | |
429 | } | |
2b5f62a0 | 430 | |
a6f5aa49 | 431 | gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE ); |
2b5f62a0 | 432 | |
a6f5aa49 VZ |
433 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize", |
434 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this ); | |
435 | ||
436 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map", | |
437 | GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this ); | |
438 | ||
439 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event", | |
440 | GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this ); | |
441 | ||
442 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw", | |
443 | GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this ); | |
2b5f62a0 | 444 | |
a6f5aa49 VZ |
445 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
446 | GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this ); | |
447 | ||
3cbab641 MR |
448 | gtk_widget_pop_visual(); |
449 | gtk_widget_pop_colormap(); | |
2b5f62a0 | 450 | |
bc869971 VZ |
451 | // if our parent window is already visible, we had been realized before we |
452 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
453 | // initialized yet and we have to do it now | |
454 | if (GTK_WIDGET_REALIZED(m_wxwindow)) | |
455 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
456 | ||
457 | if (GTK_WIDGET_MAPPED(m_wxwindow)) | |
458 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
459 | ||
670f9935 | 460 | return true; |
a6f5aa49 VZ |
461 | } |
462 | ||
463 | wxGLCanvas::~wxGLCanvas() | |
464 | { | |
b4d06fb7 MR |
465 | GLXFBConfig * fbc = (GLXFBConfig *) m_fbc; |
466 | if (fbc && m_canFreeFBC) | |
467 | XFree( fbc ); | |
468 | ||
469 | XVisualInfo *vi = (XVisualInfo *) m_vi; | |
470 | if (vi && m_canFreeVi) | |
471 | XFree( vi ); | |
2b5f62a0 | 472 | |
4230303c | 473 | delete m_glContext; |
a6f5aa49 VZ |
474 | } |
475 | ||
476 | void* wxGLCanvas::ChooseGLVisual(int *attribList) | |
477 | { | |
478 | int data[512]; | |
b4d06fb7 MR |
479 | GetGLAttribListFromWX( attribList, data ); |
480 | attribList = (int*) data; | |
34a34b02 | 481 | |
b4d06fb7 MR |
482 | Display *dpy = GDK_DISPLAY(); |
483 | ||
484 | return glXChooseVisual( dpy, DefaultScreen(dpy), attribList ); | |
34a34b02 | 485 | } |
0f8d11dc | 486 | |
34a34b02 VZ |
487 | void* wxGLCanvas::ChooseGLFBC(int *attribList) |
488 | { | |
b4d06fb7 MR |
489 | int data[512]; |
490 | GetGLAttribListFromWX( attribList, data ); | |
491 | attribList = (int*) data; | |
34a34b02 | 492 | |
b4d06fb7 MR |
493 | int returned; |
494 | return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()), | |
495 | attribList, &returned ); | |
34a34b02 VZ |
496 | } |
497 | ||
498 | ||
499 | void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList ) | |
500 | { | |
b4d06fb7 | 501 | if (!wx_attribList) |
34a34b02 | 502 | { |
b4d06fb7 MR |
503 | if (wxGLCanvas::GetGLXVersion() >= 13) |
504 | // leave GLX >= 1.3 choose the default attributes | |
505 | gl_attribList[0] = 0; | |
506 | else | |
507 | { | |
508 | int i = 0; | |
509 | // default settings if attriblist = 0 | |
510 | gl_attribList[i++] = GLX_RGBA; | |
511 | gl_attribList[i++] = GLX_DOUBLEBUFFER; | |
512 | gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1; | |
513 | gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1; | |
514 | gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1; | |
515 | gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1; | |
516 | gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0; | |
517 | gl_attribList[i++] = None; | |
518 | } | |
8b089c5e JS |
519 | } |
520 | else | |
521 | { | |
b4d06fb7 MR |
522 | int arg=0, p=0; |
523 | while( (wx_attribList[arg]!=0) && (p<510) ) | |
8b089c5e | 524 | { |
b4d06fb7 MR |
525 | switch( wx_attribList[arg++] ) |
526 | { | |
527 | case WX_GL_RGBA: | |
528 | if (wxGLCanvas::GetGLXVersion() <= 12) | |
529 | // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm) | |
530 | gl_attribList[p++] = GLX_RGBA; | |
531 | break; | |
532 | case WX_GL_BUFFER_SIZE: | |
533 | gl_attribList[p++] = GLX_BUFFER_SIZE; | |
534 | gl_attribList[p++] = wx_attribList[arg++]; | |
535 | break; | |
536 | case WX_GL_LEVEL: | |
537 | gl_attribList[p++] = GLX_LEVEL; | |
538 | gl_attribList[p++] = wx_attribList[arg++]; | |
539 | break; | |
540 | case WX_GL_DOUBLEBUFFER: | |
541 | if (wxGLCanvas::GetGLXVersion() <= 12) | |
542 | gl_attribList[p++] = GLX_DOUBLEBUFFER; | |
543 | else | |
544 | // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True) | |
545 | // it seems this flag is useless for some hardware opengl implementation. | |
546 | // but for Mesa 6.2.1, this flag is used so don't ignore it. | |
547 | gl_attribList[p++] = GLX_DOUBLEBUFFER; | |
548 | gl_attribList[p++] = 1; | |
549 | break; | |
550 | case WX_GL_STEREO: | |
551 | gl_attribList[p++] = GLX_STEREO; | |
552 | break; | |
553 | case WX_GL_AUX_BUFFERS: | |
554 | gl_attribList[p++] = GLX_AUX_BUFFERS; | |
555 | gl_attribList[p++] = wx_attribList[arg++]; | |
556 | break; | |
557 | case WX_GL_MIN_RED: | |
558 | gl_attribList[p++] = GLX_RED_SIZE; | |
559 | gl_attribList[p++] = wx_attribList[arg++]; | |
560 | break; | |
561 | case WX_GL_MIN_GREEN: | |
562 | gl_attribList[p++] = GLX_GREEN_SIZE; | |
563 | gl_attribList[p++] = wx_attribList[arg++]; | |
564 | break; | |
565 | case WX_GL_MIN_BLUE: | |
566 | gl_attribList[p++] = GLX_BLUE_SIZE; | |
567 | gl_attribList[p++] = wx_attribList[arg++]; | |
568 | break; | |
569 | case WX_GL_MIN_ALPHA: | |
570 | gl_attribList[p++] = GLX_ALPHA_SIZE; | |
571 | gl_attribList[p++] = wx_attribList[arg++]; | |
572 | break; | |
573 | case WX_GL_DEPTH_SIZE: | |
574 | gl_attribList[p++] = GLX_DEPTH_SIZE; | |
575 | gl_attribList[p++] = wx_attribList[arg++]; | |
576 | break; | |
577 | case WX_GL_STENCIL_SIZE: | |
578 | gl_attribList[p++] = GLX_STENCIL_SIZE; | |
579 | gl_attribList[p++] = wx_attribList[arg++]; | |
580 | break; | |
581 | case WX_GL_MIN_ACCUM_RED: | |
582 | gl_attribList[p++] = GLX_ACCUM_RED_SIZE; | |
583 | gl_attribList[p++] = wx_attribList[arg++]; | |
584 | break; | |
585 | case WX_GL_MIN_ACCUM_GREEN: | |
586 | gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE; | |
587 | gl_attribList[p++] = wx_attribList[arg++]; | |
588 | break; | |
589 | case WX_GL_MIN_ACCUM_BLUE: | |
590 | gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE; | |
591 | gl_attribList[p++] = wx_attribList[arg++]; | |
592 | break; | |
593 | case WX_GL_MIN_ACCUM_ALPHA: | |
594 | gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE; | |
595 | gl_attribList[p++] = wx_attribList[arg++]; | |
596 | break; | |
597 | default: | |
598 | break; | |
599 | } | |
8b089c5e | 600 | } |
b4d06fb7 MR |
601 | |
602 | gl_attribList[p] = 0; | |
8b089c5e | 603 | } |
34a34b02 | 604 | } |
2b5f62a0 | 605 | |
34a34b02 VZ |
606 | void wxGLCanvas::QueryGLXVersion() |
607 | { | |
b4d06fb7 | 608 | if (m_glxVersion == 0) |
34a34b02 | 609 | { |
b4d06fb7 MR |
610 | // check the GLX version |
611 | int glxMajorVer, glxMinorVer; | |
612 | bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer); | |
613 | wxASSERT_MSG( ok, _T("GLX version not found") ); | |
614 | if (!ok) | |
615 | m_glxVersion = 10; // 1.0 by default | |
616 | else | |
617 | m_glxVersion = glxMajorVer*10 + glxMinorVer; | |
34a34b02 VZ |
618 | } |
619 | } | |
2b5f62a0 | 620 | |
34a34b02 VZ |
621 | int wxGLCanvas::GetGLXVersion() |
622 | { | |
b4d06fb7 MR |
623 | wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") ); |
624 | return m_glxVersion; | |
8b089c5e JS |
625 | } |
626 | ||
34a34b02 | 627 | |
8b089c5e JS |
628 | void wxGLCanvas::SwapBuffers() |
629 | { | |
2b5f62a0 VZ |
630 | if (m_glContext) |
631 | m_glContext->SwapBuffers(); | |
8b089c5e JS |
632 | } |
633 | ||
634 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
635 | { | |
8b089c5e JS |
636 | } |
637 | ||
638 | void wxGLCanvas::SetCurrent() | |
639 | { | |
2b5f62a0 VZ |
640 | if (m_glContext) |
641 | m_glContext->SetCurrent(); | |
8b089c5e JS |
642 | } |
643 | ||
2b5f62a0 | 644 | void wxGLCanvas::SetColour( const wxChar *colour ) |
8b089c5e | 645 | { |
2b5f62a0 VZ |
646 | if (m_glContext) |
647 | m_glContext->SetColour( colour ); | |
8b089c5e JS |
648 | } |
649 | ||
650 | void wxGLCanvas::OnInternalIdle() | |
651 | { | |
652 | if (m_glContext && m_exposed) | |
653 | { | |
654 | wxPaintEvent event( GetId() ); | |
655 | event.SetEventObject( this ); | |
656 | GetEventHandler()->ProcessEvent( event ); | |
657 | ||
670f9935 | 658 | m_exposed = false; |
8b089c5e JS |
659 | GetUpdateRegion().Clear(); |
660 | } | |
2b5f62a0 | 661 | |
8b089c5e JS |
662 | wxWindow::OnInternalIdle(); |
663 | } | |
664 | ||
a6f5aa49 VZ |
665 | |
666 | ||
667 | //--------------------------------------------------------------------------- | |
668 | // wxGLApp | |
669 | //--------------------------------------------------------------------------- | |
670 | ||
671 | IMPLEMENT_CLASS(wxGLApp, wxApp) | |
2b5f62a0 | 672 | |
a6f5aa49 VZ |
673 | wxGLApp::~wxGLApp() |
674 | { | |
b4d06fb7 MR |
675 | if (m_glFBCInfo) |
676 | XFree(m_glFBCInfo); | |
2b5f62a0 VZ |
677 | if (m_glVisualInfo) |
678 | XFree(m_glVisualInfo); | |
a6f5aa49 VZ |
679 | } |
680 | ||
681 | bool wxGLApp::InitGLVisual(int *attribList) | |
682 | { | |
b4d06fb7 | 683 | wxGLCanvas::QueryGLXVersion(); |
34a34b02 | 684 | |
b4d06fb7 | 685 | if (wxGLCanvas::GetGLXVersion() >= 13) |
34a34b02 | 686 | { |
b4d06fb7 MR |
687 | // GLX >= 1.3 |
688 | if (m_glFBCInfo) | |
689 | XFree(m_glFBCInfo); | |
690 | m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList); | |
691 | ||
692 | if (m_glFBCInfo) | |
693 | { | |
694 | if (m_glVisualInfo) | |
695 | XFree(m_glVisualInfo); | |
696 | m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]); | |
697 | } | |
698 | return (m_glFBCInfo != NULL) && (m_glVisualInfo != NULL); | |
34a34b02 | 699 | } |
b4d06fb7 | 700 | else |
34a34b02 | 701 | { |
b4d06fb7 MR |
702 | // GLX <= 1.2 |
703 | if (m_glVisualInfo) | |
704 | XFree(m_glVisualInfo); | |
705 | m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList); | |
706 | return m_glVisualInfo != NULL; | |
34a34b02 | 707 | } |
a6f5aa49 VZ |
708 | } |
709 | ||
8b089c5e JS |
710 | #endif |
711 | // wxUSE_GLCANVAS |