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