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