]>
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 JS |
253 | |
254 | //----------------------------------------------------------------------------- | |
255 | // "draw" of m_wxwindow | |
256 | //----------------------------------------------------------------------------- | |
257 | ||
2b5b9325 | 258 | #ifndef __WXGTK20__ |
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 | ||
266 | win->m_exposed = TRUE; | |
267 | ||
268 | win->GetUpdateRegion().Union( rect->x, rect->y, | |
269 | rect->width, rect->height ); | |
270 | } | |
865bb325 | 271 | } |
2b5b9325 | 272 | #endif |
8b089c5e JS |
273 | |
274 | //----------------------------------------------------------------------------- | |
275 | // "size_allocate" of m_wxwindow | |
276 | //----------------------------------------------------------------------------- | |
277 | ||
865bb325 | 278 | extern "C" { |
2b5f62a0 | 279 | static void |
8b089c5e JS |
280 | gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win ) |
281 | { | |
282 | if (g_isIdle) | |
283 | wxapp_install_idle_handler(); | |
284 | ||
285 | if (!win->m_hasVMT) | |
286 | return; | |
287 | ||
288 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
289 | event.SetEventObject( win ); | |
290 | win->GetEventHandler()->ProcessEvent( event ); | |
291 | } | |
865bb325 | 292 | } |
8b089c5e JS |
293 | |
294 | //--------------------------------------------------------------------------- | |
295 | // wxGlCanvas | |
296 | //--------------------------------------------------------------------------- | |
297 | ||
4660d7e5 | 298 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
8b089c5e | 299 | |
4660d7e5 | 300 | BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow) |
8b089c5e JS |
301 | EVT_SIZE(wxGLCanvas::OnSize) |
302 | END_EVENT_TABLE() | |
303 | ||
304 | wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id, | |
2b5f62a0 VZ |
305 | const wxPoint& pos, const wxSize& size, |
306 | long style, const wxString& name, | |
307 | int *attribList, | |
308 | const wxPalette& palette ) | |
8b089c5e JS |
309 | { |
310 | Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette ); | |
311 | } | |
312 | ||
2b5f62a0 | 313 | wxGLCanvas::wxGLCanvas( wxWindow *parent, |
8b089c5e JS |
314 | const wxGLContext *shared, |
315 | wxWindowID id, | |
2b5f62a0 VZ |
316 | const wxPoint& pos, const wxSize& size, |
317 | long style, const wxString& name, | |
318 | int *attribList, | |
319 | const wxPalette& palette ) | |
320 | { | |
8b089c5e JS |
321 | Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette ); |
322 | } | |
323 | ||
2b5f62a0 | 324 | wxGLCanvas::wxGLCanvas( wxWindow *parent, |
8b089c5e JS |
325 | const wxGLCanvas *shared, |
326 | wxWindowID id, | |
2b5f62a0 VZ |
327 | const wxPoint& pos, const wxSize& size, |
328 | long style, const wxString& name, | |
329 | int *attribList, | |
330 | const wxPalette& palette ) | |
331 | { | |
8b089c5e JS |
332 | Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette ); |
333 | } | |
334 | ||
2b5f62a0 | 335 | bool wxGLCanvas::Create( wxWindow *parent, |
8b089c5e JS |
336 | const wxGLContext *shared, |
337 | const wxGLCanvas *shared_context_of, | |
338 | wxWindowID id, | |
2b5f62a0 VZ |
339 | const wxPoint& pos, const wxSize& size, |
340 | long style, const wxString& name, | |
341 | int *attribList, | |
342 | const wxPalette& palette) | |
8b089c5e JS |
343 | { |
344 | m_sharedContext = (wxGLContext*)shared; // const_cast | |
345 | m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast | |
346 | m_glContext = (wxGLContext*) NULL; | |
2b5f62a0 | 347 | |
8b089c5e JS |
348 | m_exposed = FALSE; |
349 | m_noExpose = TRUE; | |
350 | m_nativeSizeEvent = TRUE; | |
34a34b02 VZ |
351 | m_fbc = NULL; |
352 | m_vi = NULL; | |
353 | ||
354 | // to be sure the glx version is known | |
355 | wxGLCanvas::QueryGLXVersion(); | |
356 | ||
357 | if (wxGLCanvas::GetGLXVersion() >= 13) | |
b4d06fb7 MR |
358 | { |
359 | // GLX >= 1.3 uses a GLXFBConfig | |
360 | GLXFBConfig * fbc = NULL; | |
361 | if (wxTheApp->m_glFBCInfo != NULL) | |
362 | { | |
363 | fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo; | |
364 | m_canFreeFBC = FALSE; // owned by wxTheApp - don't free upon destruction | |
365 | } | |
366 | else | |
367 | { | |
368 | fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList); | |
369 | m_canFreeFBC = TRUE; | |
370 | } | |
371 | m_fbc = fbc; // save for later use | |
372 | wxCHECK_MSG( m_fbc, FALSE, _T("required FBConfig couldn't be found") ); | |
373 | } | |
2b5f62a0 | 374 | |
a6f5aa49 | 375 | XVisualInfo *vi = NULL; |
2b5f62a0 VZ |
376 | if (wxTheApp->m_glVisualInfo != NULL) |
377 | { | |
b4d06fb7 | 378 | vi = (XVisualInfo *)wxTheApp->m_glVisualInfo; |
a6f5aa49 | 379 | m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction |
2b5f62a0 VZ |
380 | } |
381 | else | |
382 | { | |
b4d06fb7 MR |
383 | if (wxGLCanvas::GetGLXVersion() >= 13) |
384 | // GLX >= 1.3 | |
385 | vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]); | |
386 | else | |
387 | // GLX <= 1.2 | |
388 | vi = (XVisualInfo *) ChooseGLVisual(attribList); | |
389 | ||
a6f5aa49 VZ |
390 | m_canFreeVi = TRUE; |
391 | } | |
b4d06fb7 | 392 | |
a6f5aa49 | 393 | m_vi = vi; // save for later use |
2b5f62a0 VZ |
394 | |
395 | wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") ); | |
fee7a683 MR |
396 | GdkVisual *visual; |
397 | GdkColormap *colormap; | |
2b5f62a0 | 398 | |
fee7a683 | 399 | // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME) |
b3f4c570 | 400 | #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0) |
fee7a683 MR |
401 | if (!gtk_check_version(2,2,0)) |
402 | { | |
403 | wxWindow::Create( parent, id, pos, size, style, name ); | |
404 | ||
405 | m_glWidget = m_wxwindow; | |
406 | ||
407 | GdkScreen *screen = gtk_widget_get_screen( m_glWidget ); | |
408 | colormap = gdk_screen_get_default_colormap(screen); | |
409 | visual = gdk_colormap_get_visual(colormap); | |
410 | ||
411 | if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid) | |
412 | { | |
413 | visual = gdk_x11_screen_lookup_visual( screen, vi->visualid ); | |
414 | colormap = gdk_colormap_new(visual, FALSE); | |
415 | } | |
416 | ||
417 | gtk_widget_set_colormap( m_glWidget, colormap ); | |
418 | } | |
419 | else | |
420 | #endif | |
421 | { | |
422 | visual = gdkx_visual_get( vi->visualid ); | |
423 | colormap = gdk_colormap_new( visual, TRUE ); | |
a6f5aa49 | 424 | |
fee7a683 MR |
425 | gtk_widget_push_colormap( colormap ); |
426 | gtk_widget_push_visual( visual ); | |
a6f5aa49 | 427 | |
fee7a683 MR |
428 | wxWindow::Create( parent, id, pos, size, style, name ); |
429 | m_glWidget = m_wxwindow; | |
430 | } | |
2b5f62a0 | 431 | |
2b5b9325 RR |
432 | #ifdef __WXGTK20__ |
433 | gtk_widget_set_double_buffered( m_glWidget, FALSE ); | |
434 | #endif | |
435 | ||
a6f5aa49 | 436 | gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE ); |
2b5f62a0 | 437 | |
a6f5aa49 VZ |
438 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize", |
439 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this ); | |
440 | ||
441 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map", | |
442 | GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this ); | |
443 | ||
444 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event", | |
445 | GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this ); | |
446 | ||
2b5b9325 | 447 | #ifndef __WXGTK20__ |
a6f5aa49 VZ |
448 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw", |
449 | GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this ); | |
2b5b9325 | 450 | #endif |
2b5f62a0 | 451 | |
a6f5aa49 VZ |
452 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
453 | GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this ); | |
454 | ||
b3f4c570 | 455 | #ifdef __WXGTK20__ |
fee7a683 | 456 | if (gtk_check_version(2,2,0) != NULL) |
b3f4c570 | 457 | #endif |
fee7a683 MR |
458 | { |
459 | gtk_widget_pop_visual(); | |
460 | gtk_widget_pop_colormap(); | |
461 | } | |
2b5f62a0 | 462 | |
bc869971 VZ |
463 | // if our parent window is already visible, we had been realized before we |
464 | // connected to the "realize" signal and hence our m_glContext hasn't been | |
465 | // initialized yet and we have to do it now | |
466 | if (GTK_WIDGET_REALIZED(m_wxwindow)) | |
467 | gtk_glwindow_realized_callback( m_wxwindow, this ); | |
468 | ||
469 | if (GTK_WIDGET_MAPPED(m_wxwindow)) | |
470 | gtk_glwindow_map_callback( m_wxwindow, this ); | |
471 | ||
a6f5aa49 VZ |
472 | return TRUE; |
473 | } | |
474 | ||
475 | wxGLCanvas::~wxGLCanvas() | |
476 | { | |
b4d06fb7 MR |
477 | GLXFBConfig * fbc = (GLXFBConfig *) m_fbc; |
478 | if (fbc && m_canFreeFBC) | |
479 | XFree( fbc ); | |
480 | ||
481 | XVisualInfo *vi = (XVisualInfo *) m_vi; | |
482 | if (vi && m_canFreeVi) | |
483 | XFree( vi ); | |
2b5f62a0 | 484 | |
4230303c | 485 | delete m_glContext; |
a6f5aa49 VZ |
486 | } |
487 | ||
488 | void* wxGLCanvas::ChooseGLVisual(int *attribList) | |
489 | { | |
490 | int data[512]; | |
b4d06fb7 MR |
491 | GetGLAttribListFromWX( attribList, data ); |
492 | attribList = (int*) data; | |
34a34b02 | 493 | |
b4d06fb7 MR |
494 | Display *dpy = GDK_DISPLAY(); |
495 | ||
496 | return glXChooseVisual( dpy, DefaultScreen(dpy), attribList ); | |
34a34b02 | 497 | } |
0f8d11dc | 498 | |
34a34b02 VZ |
499 | void* wxGLCanvas::ChooseGLFBC(int *attribList) |
500 | { | |
b4d06fb7 MR |
501 | int data[512]; |
502 | GetGLAttribListFromWX( attribList, data ); | |
503 | attribList = (int*) data; | |
34a34b02 | 504 | |
b4d06fb7 MR |
505 | int returned; |
506 | return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()), | |
507 | attribList, &returned ); | |
34a34b02 VZ |
508 | } |
509 | ||
510 | ||
511 | void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList ) | |
512 | { | |
b4d06fb7 | 513 | if (!wx_attribList) |
34a34b02 | 514 | { |
b4d06fb7 MR |
515 | if (wxGLCanvas::GetGLXVersion() >= 13) |
516 | // leave GLX >= 1.3 choose the default attributes | |
517 | gl_attribList[0] = 0; | |
518 | else | |
519 | { | |
520 | int i = 0; | |
521 | // default settings if attriblist = 0 | |
522 | gl_attribList[i++] = GLX_RGBA; | |
523 | gl_attribList[i++] = GLX_DOUBLEBUFFER; | |
524 | gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1; | |
525 | gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1; | |
526 | gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1; | |
527 | gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1; | |
528 | gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0; | |
529 | gl_attribList[i++] = None; | |
530 | } | |
8b089c5e JS |
531 | } |
532 | else | |
533 | { | |
b4d06fb7 MR |
534 | int arg=0, p=0; |
535 | while( (wx_attribList[arg]!=0) && (p<510) ) | |
8b089c5e | 536 | { |
b4d06fb7 MR |
537 | switch( wx_attribList[arg++] ) |
538 | { | |
539 | case WX_GL_RGBA: | |
540 | if (wxGLCanvas::GetGLXVersion() <= 12) | |
541 | // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm) | |
542 | gl_attribList[p++] = GLX_RGBA; | |
543 | break; | |
544 | case WX_GL_BUFFER_SIZE: | |
545 | gl_attribList[p++] = GLX_BUFFER_SIZE; | |
546 | gl_attribList[p++] = wx_attribList[arg++]; | |
547 | break; | |
548 | case WX_GL_LEVEL: | |
549 | gl_attribList[p++] = GLX_LEVEL; | |
550 | gl_attribList[p++] = wx_attribList[arg++]; | |
551 | break; | |
552 | case WX_GL_DOUBLEBUFFER: | |
553 | if (wxGLCanvas::GetGLXVersion() <= 12) | |
554 | gl_attribList[p++] = GLX_DOUBLEBUFFER; | |
555 | else | |
556 | // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True) | |
557 | // it seems this flag is useless for some hardware opengl implementation. | |
558 | // but for Mesa 6.2.1, this flag is used so don't ignore it. | |
559 | gl_attribList[p++] = GLX_DOUBLEBUFFER; | |
560 | gl_attribList[p++] = 1; | |
561 | break; | |
562 | case WX_GL_STEREO: | |
563 | gl_attribList[p++] = GLX_STEREO; | |
564 | break; | |
565 | case WX_GL_AUX_BUFFERS: | |
566 | gl_attribList[p++] = GLX_AUX_BUFFERS; | |
567 | gl_attribList[p++] = wx_attribList[arg++]; | |
568 | break; | |
569 | case WX_GL_MIN_RED: | |
570 | gl_attribList[p++] = GLX_RED_SIZE; | |
571 | gl_attribList[p++] = wx_attribList[arg++]; | |
572 | break; | |
573 | case WX_GL_MIN_GREEN: | |
574 | gl_attribList[p++] = GLX_GREEN_SIZE; | |
575 | gl_attribList[p++] = wx_attribList[arg++]; | |
576 | break; | |
577 | case WX_GL_MIN_BLUE: | |
578 | gl_attribList[p++] = GLX_BLUE_SIZE; | |
579 | gl_attribList[p++] = wx_attribList[arg++]; | |
580 | break; | |
581 | case WX_GL_MIN_ALPHA: | |
582 | gl_attribList[p++] = GLX_ALPHA_SIZE; | |
583 | gl_attribList[p++] = wx_attribList[arg++]; | |
584 | break; | |
585 | case WX_GL_DEPTH_SIZE: | |
586 | gl_attribList[p++] = GLX_DEPTH_SIZE; | |
587 | gl_attribList[p++] = wx_attribList[arg++]; | |
588 | break; | |
589 | case WX_GL_STENCIL_SIZE: | |
590 | gl_attribList[p++] = GLX_STENCIL_SIZE; | |
591 | gl_attribList[p++] = wx_attribList[arg++]; | |
592 | break; | |
593 | case WX_GL_MIN_ACCUM_RED: | |
594 | gl_attribList[p++] = GLX_ACCUM_RED_SIZE; | |
595 | gl_attribList[p++] = wx_attribList[arg++]; | |
596 | break; | |
597 | case WX_GL_MIN_ACCUM_GREEN: | |
598 | gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE; | |
599 | gl_attribList[p++] = wx_attribList[arg++]; | |
600 | break; | |
601 | case WX_GL_MIN_ACCUM_BLUE: | |
602 | gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE; | |
603 | gl_attribList[p++] = wx_attribList[arg++]; | |
604 | break; | |
605 | case WX_GL_MIN_ACCUM_ALPHA: | |
606 | gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE; | |
607 | gl_attribList[p++] = wx_attribList[arg++]; | |
608 | break; | |
609 | default: | |
610 | break; | |
611 | } | |
8b089c5e | 612 | } |
b4d06fb7 MR |
613 | |
614 | gl_attribList[p] = 0; | |
8b089c5e | 615 | } |
34a34b02 | 616 | } |
2b5f62a0 | 617 | |
34a34b02 VZ |
618 | void wxGLCanvas::QueryGLXVersion() |
619 | { | |
b4d06fb7 | 620 | if (m_glxVersion == 0) |
34a34b02 | 621 | { |
b4d06fb7 MR |
622 | // check the GLX version |
623 | int glxMajorVer, glxMinorVer; | |
624 | bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer); | |
625 | wxASSERT_MSG( ok, _T("GLX version not found") ); | |
626 | if (!ok) | |
627 | m_glxVersion = 10; // 1.0 by default | |
628 | else | |
629 | m_glxVersion = glxMajorVer*10 + glxMinorVer; | |
34a34b02 VZ |
630 | } |
631 | } | |
2b5f62a0 | 632 | |
34a34b02 VZ |
633 | int wxGLCanvas::GetGLXVersion() |
634 | { | |
b4d06fb7 MR |
635 | wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") ); |
636 | return m_glxVersion; | |
8b089c5e JS |
637 | } |
638 | ||
34a34b02 | 639 | |
8b089c5e JS |
640 | void wxGLCanvas::SwapBuffers() |
641 | { | |
2b5f62a0 VZ |
642 | if (m_glContext) |
643 | m_glContext->SwapBuffers(); | |
8b089c5e JS |
644 | } |
645 | ||
646 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
647 | { | |
8b089c5e JS |
648 | } |
649 | ||
650 | void wxGLCanvas::SetCurrent() | |
651 | { | |
2b5f62a0 VZ |
652 | if (m_glContext) |
653 | m_glContext->SetCurrent(); | |
8b089c5e JS |
654 | } |
655 | ||
2b5f62a0 | 656 | void wxGLCanvas::SetColour( const wxChar *colour ) |
8b089c5e | 657 | { |
2b5f62a0 VZ |
658 | if (m_glContext) |
659 | m_glContext->SetColour( colour ); | |
8b089c5e JS |
660 | } |
661 | ||
662 | void wxGLCanvas::OnInternalIdle() | |
663 | { | |
664 | if (m_glContext && m_exposed) | |
665 | { | |
666 | wxPaintEvent event( GetId() ); | |
667 | event.SetEventObject( this ); | |
668 | GetEventHandler()->ProcessEvent( event ); | |
669 | ||
670 | m_exposed = FALSE; | |
671 | GetUpdateRegion().Clear(); | |
672 | } | |
2b5f62a0 | 673 | |
8b089c5e JS |
674 | wxWindow::OnInternalIdle(); |
675 | } | |
676 | ||
a6f5aa49 VZ |
677 | |
678 | ||
679 | //--------------------------------------------------------------------------- | |
680 | // wxGLApp | |
681 | //--------------------------------------------------------------------------- | |
682 | ||
683 | IMPLEMENT_CLASS(wxGLApp, wxApp) | |
2b5f62a0 | 684 | |
a6f5aa49 VZ |
685 | wxGLApp::~wxGLApp() |
686 | { | |
b4d06fb7 MR |
687 | if (m_glFBCInfo) |
688 | XFree(m_glFBCInfo); | |
2b5f62a0 VZ |
689 | if (m_glVisualInfo) |
690 | XFree(m_glVisualInfo); | |
a6f5aa49 VZ |
691 | } |
692 | ||
693 | bool wxGLApp::InitGLVisual(int *attribList) | |
694 | { | |
b4d06fb7 | 695 | wxGLCanvas::QueryGLXVersion(); |
34a34b02 | 696 | |
b4d06fb7 | 697 | if (wxGLCanvas::GetGLXVersion() >= 13) |
34a34b02 | 698 | { |
b4d06fb7 MR |
699 | // GLX >= 1.3 |
700 | if (m_glFBCInfo) | |
701 | XFree(m_glFBCInfo); | |
702 | m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList); | |
703 | ||
704 | if (m_glFBCInfo) | |
705 | { | |
706 | if (m_glVisualInfo) | |
707 | XFree(m_glVisualInfo); | |
708 | m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]); | |
709 | } | |
710 | return (m_glFBCInfo != NULL) && (m_glVisualInfo != NULL); | |
34a34b02 | 711 | } |
b4d06fb7 | 712 | else |
34a34b02 | 713 | { |
b4d06fb7 MR |
714 | // GLX <= 1.2 |
715 | if (m_glVisualInfo) | |
716 | XFree(m_glVisualInfo); | |
717 | m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList); | |
718 | return m_glVisualInfo != NULL; | |
34a34b02 | 719 | } |
a6f5aa49 VZ |
720 | } |
721 | ||
8b089c5e JS |
722 | #endif |
723 | // wxUSE_GLCANVAS |