1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/glcanvas.h"
24 #include "wx/colour.h"
25 #include "wx/module.h"
34 #include "wx/gtk/win_gtk.h"
35 #include "wx/gtk/private.h"
37 // DLL options compatibility check:
39 WX_CHECK_BUILD_OPTIONS("wxGL")
42 //---------------------------------------------------------------------------
44 //---------------------------------------------------------------------------
45 int wxGLCanvas::m_glxVersion
= 0;
47 //---------------------------------------------------------------------------
49 //---------------------------------------------------------------------------
51 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
53 //---------------------------------------------------------------------------
55 //---------------------------------------------------------------------------
57 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
59 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
62 m_widget
= win
->m_wxwindow
;
64 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
66 if (wxGLCanvas::GetGLXVersion() >= 13)
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
);
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
);
81 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
84 wxGLContext::wxGLContext(
85 bool WXUNUSED(isRGB
), wxWindow
*win
,
86 const wxPalette
& WXUNUSED(palette
),
87 const wxGLContext
*other
/* for sharing display lists */
91 m_widget
= win
->m_wxwindow
;
93 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
95 if (wxGLCanvas::GetGLXVersion() >= 13)
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
,
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
,
116 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
120 wxGLContext::~wxGLContext()
122 if (!m_glContext
) return;
124 if (m_glContext
== glXGetCurrentContext())
126 if (wxGLCanvas::GetGLXVersion() >= 13)
128 glXMakeContextCurrent( GDK_DISPLAY(), None
, None
, NULL
);
131 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
134 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
137 void wxGLContext::SwapBuffers()
141 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
142 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
146 void wxGLContext::SetCurrent()
150 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
152 if (wxGLCanvas::GetGLXVersion() >= 13)
154 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
),
155 GDK_WINDOW_XWINDOW(window
), m_glContext
);
158 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
162 void wxGLContext::SetColour(const wxChar
*colour
)
164 wxColour col
= wxTheColourDatabase
->Find(colour
);
167 float r
= (float)(col
.Red()/256.0);
168 float g
= (float)(col
.Green()/256.0);
169 float b
= (float)(col
.Blue()/256.0);
174 void wxGLContext::SetupPixelFormat()
178 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
182 wxPalette
wxGLContext::CreateDefaultPalette()
184 return wxNullPalette
;
187 //-----------------------------------------------------------------------------
188 // "realize" from m_wxwindow
189 //-----------------------------------------------------------------------------
193 gtk_glwindow_realized_callback( GtkWidget
*WXUNUSED(widget
), wxGLCanvas
*win
)
195 if ( !win
->m_glContext
)
197 wxGLContext
*share
= win
->m_sharedContext
;
198 if ( !share
&& win
->m_sharedContextOf
)
199 share
= win
->m_sharedContextOf
->GetContext();
201 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
208 //-----------------------------------------------------------------------------
209 // "map" from m_wxwindow
210 //-----------------------------------------------------------------------------
214 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
216 if (win
->m_glContext
/* && win->m_exposed*/)
218 wxPaintEvent
event( win
->GetId() );
219 event
.SetEventObject( win
);
220 win
->GetEventHandler()->ProcessEvent( event
);
222 win
->m_exposed
= false;
223 win
->GetUpdateRegion().Clear();
230 //-----------------------------------------------------------------------------
231 // "expose_event" of m_wxwindow
232 //-----------------------------------------------------------------------------
236 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
239 wxapp_install_idle_handler();
241 win
->m_exposed
= true;
243 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
245 gdk_event
->area
.width
,
246 gdk_event
->area
.height
);
250 //-----------------------------------------------------------------------------
251 // "size_allocate" of m_wxwindow
252 //-----------------------------------------------------------------------------
256 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
259 wxapp_install_idle_handler();
264 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
265 event
.SetEventObject( win
);
266 win
->GetEventHandler()->ProcessEvent( event
);
270 //---------------------------------------------------------------------------
272 //---------------------------------------------------------------------------
274 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
276 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
277 EVT_SIZE(wxGLCanvas::OnSize
)
280 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
281 const wxPoint
& pos
, const wxSize
& size
,
282 long style
, const wxString
& name
,
284 const wxPalette
& palette
)
286 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
289 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
290 const wxGLContext
*shared
,
292 const wxPoint
& pos
, const wxSize
& size
,
293 long style
, const wxString
& name
,
295 const wxPalette
& palette
)
297 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
300 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
301 const wxGLCanvas
*shared
,
303 const wxPoint
& pos
, const wxSize
& size
,
304 long style
, const wxString
& name
,
306 const wxPalette
& palette
)
308 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
311 bool wxGLCanvas::Create( wxWindow
*parent
,
312 const wxGLContext
*shared
,
313 const wxGLCanvas
*shared_context_of
,
315 const wxPoint
& pos
, const wxSize
& size
,
316 long style
, const wxString
& name
,
318 const wxPalette
& palette
)
320 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
321 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
322 m_glContext
= (wxGLContext
*) NULL
;
326 m_nativeSizeEvent
= true;
330 // to be sure the glx version is known
331 wxGLCanvas::QueryGLXVersion();
333 if (wxGLCanvas::GetGLXVersion() >= 13)
335 // GLX >= 1.3 uses a GLXFBConfig
336 GLXFBConfig
* fbc
= NULL
;
337 if (wxTheApp
->m_glFBCInfo
!= NULL
)
339 fbc
= (GLXFBConfig
*) wxTheApp
->m_glFBCInfo
;
340 m_canFreeFBC
= false; // owned by wxTheApp - don't free upon destruction
344 fbc
= (GLXFBConfig
*) wxGLCanvas::ChooseGLFBC(attribList
);
347 m_fbc
= fbc
; // save for later use
348 wxCHECK_MSG( m_fbc
, false, _T("required FBConfig couldn't be found") );
351 XVisualInfo
*vi
= NULL
;
352 if (wxTheApp
->m_glVisualInfo
!= NULL
)
354 vi
= (XVisualInfo
*)wxTheApp
->m_glVisualInfo
;
355 m_canFreeVi
= false; // owned by wxTheApp - don't free upon destruction
359 if (wxGLCanvas::GetGLXVersion() >= 13)
361 vi
= glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc
[0]);
364 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
369 m_vi
= vi
; // save for later use
371 wxCHECK_MSG( m_vi
, false, _T("required visual couldn't be found") );
373 GdkColormap
*colormap
;
375 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
376 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
377 if (!gtk_check_version(2,2,0))
379 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
381 m_glWidget
= m_wxwindow
;
383 GdkScreen
*screen
= gtk_widget_get_screen( m_glWidget
);
384 colormap
= gdk_screen_get_default_colormap(screen
);
385 visual
= gdk_colormap_get_visual(colormap
);
387 if (GDK_VISUAL_XVISUAL(visual
)->visualid
!= vi
->visualid
)
389 visual
= gdk_x11_screen_lookup_visual( screen
, vi
->visualid
);
390 colormap
= gdk_colormap_new(visual
, FALSE
);
393 gtk_widget_set_colormap( m_glWidget
, colormap
);
398 visual
= gdkx_visual_get( vi
->visualid
);
399 colormap
= gdk_colormap_new( visual
, TRUE
);
401 gtk_widget_push_colormap( colormap
);
403 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
404 m_glWidget
= m_wxwindow
;
407 gtk_widget_set_double_buffered( m_glWidget
, FALSE
);
409 g_signal_connect (m_wxwindow
, "realize",
410 G_CALLBACK (gtk_glwindow_realized_callback
),
412 g_signal_connect (m_wxwindow
, "map",
413 G_CALLBACK (gtk_glwindow_map_callback
),
415 g_signal_connect (m_wxwindow
, "expose_event",
416 G_CALLBACK (gtk_glwindow_expose_callback
),
418 g_signal_connect (m_widget
, "size_allocate",
419 G_CALLBACK (gtk_glcanvas_size_callback
),
422 if (gtk_check_version(2,2,0) != NULL
)
424 gtk_widget_pop_colormap();
427 // if our parent window is already visible, we had been realized before we
428 // connected to the "realize" signal and hence our m_glContext hasn't been
429 // initialized yet and we have to do it now
430 if (GTK_WIDGET_REALIZED(m_wxwindow
))
431 gtk_glwindow_realized_callback( m_wxwindow
, this );
433 if (GTK_WIDGET_MAPPED(m_wxwindow
))
434 gtk_glwindow_map_callback( m_wxwindow
, this );
439 wxGLCanvas::~wxGLCanvas()
441 GLXFBConfig
* fbc
= (GLXFBConfig
*) m_fbc
;
442 if (fbc
&& m_canFreeFBC
)
445 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
446 if (vi
&& m_canFreeVi
)
452 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
455 GetGLAttribListFromWX( attribList
, data
);
456 attribList
= (int*) data
;
458 Display
*dpy
= GDK_DISPLAY();
460 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
463 void* wxGLCanvas::ChooseGLFBC(int *attribList
)
466 GetGLAttribListFromWX( attribList
, data
);
467 attribList
= (int*) data
;
470 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
471 attribList
, &returned
);
475 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList
, int *gl_attribList
)
479 if (wxGLCanvas::GetGLXVersion() >= 13)
480 // leave GLX >= 1.3 choose the default attributes
481 gl_attribList
[0] = 0;
485 // default settings if attriblist = 0
486 gl_attribList
[i
++] = GLX_RGBA
;
487 gl_attribList
[i
++] = GLX_DOUBLEBUFFER
;
488 gl_attribList
[i
++] = GLX_DEPTH_SIZE
; gl_attribList
[i
++] = 1;
489 gl_attribList
[i
++] = GLX_RED_SIZE
; gl_attribList
[i
++] = 1;
490 gl_attribList
[i
++] = GLX_GREEN_SIZE
; gl_attribList
[i
++] = 1;
491 gl_attribList
[i
++] = GLX_BLUE_SIZE
; gl_attribList
[i
++] = 1;
492 gl_attribList
[i
++] = GLX_ALPHA_SIZE
; gl_attribList
[i
++] = 0;
493 gl_attribList
[i
++] = None
;
499 while( (wx_attribList
[arg
]!=0) && (p
<510) )
501 switch( wx_attribList
[arg
++] )
504 if (wxGLCanvas::GetGLXVersion() <= 12)
505 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
506 gl_attribList
[p
++] = GLX_RGBA
;
508 case WX_GL_BUFFER_SIZE
:
509 gl_attribList
[p
++] = GLX_BUFFER_SIZE
;
510 gl_attribList
[p
++] = wx_attribList
[arg
++];
513 gl_attribList
[p
++] = GLX_LEVEL
;
514 gl_attribList
[p
++] = wx_attribList
[arg
++];
516 case WX_GL_DOUBLEBUFFER
:
517 if (wxGLCanvas::GetGLXVersion() <= 12)
518 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
520 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
521 // it seems this flag is useless for some hardware opengl implementation.
522 // but for Mesa 6.2.1, this flag is used so don't ignore it.
523 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
524 gl_attribList
[p
++] = 1;
527 gl_attribList
[p
++] = GLX_STEREO
;
529 case WX_GL_AUX_BUFFERS
:
530 gl_attribList
[p
++] = GLX_AUX_BUFFERS
;
531 gl_attribList
[p
++] = wx_attribList
[arg
++];
534 gl_attribList
[p
++] = GLX_RED_SIZE
;
535 gl_attribList
[p
++] = wx_attribList
[arg
++];
537 case WX_GL_MIN_GREEN
:
538 gl_attribList
[p
++] = GLX_GREEN_SIZE
;
539 gl_attribList
[p
++] = wx_attribList
[arg
++];
542 gl_attribList
[p
++] = GLX_BLUE_SIZE
;
543 gl_attribList
[p
++] = wx_attribList
[arg
++];
545 case WX_GL_MIN_ALPHA
:
546 gl_attribList
[p
++] = GLX_ALPHA_SIZE
;
547 gl_attribList
[p
++] = wx_attribList
[arg
++];
549 case WX_GL_DEPTH_SIZE
:
550 gl_attribList
[p
++] = GLX_DEPTH_SIZE
;
551 gl_attribList
[p
++] = wx_attribList
[arg
++];
553 case WX_GL_STENCIL_SIZE
:
554 gl_attribList
[p
++] = GLX_STENCIL_SIZE
;
555 gl_attribList
[p
++] = wx_attribList
[arg
++];
557 case WX_GL_MIN_ACCUM_RED
:
558 gl_attribList
[p
++] = GLX_ACCUM_RED_SIZE
;
559 gl_attribList
[p
++] = wx_attribList
[arg
++];
561 case WX_GL_MIN_ACCUM_GREEN
:
562 gl_attribList
[p
++] = GLX_ACCUM_GREEN_SIZE
;
563 gl_attribList
[p
++] = wx_attribList
[arg
++];
565 case WX_GL_MIN_ACCUM_BLUE
:
566 gl_attribList
[p
++] = GLX_ACCUM_BLUE_SIZE
;
567 gl_attribList
[p
++] = wx_attribList
[arg
++];
569 case WX_GL_MIN_ACCUM_ALPHA
:
570 gl_attribList
[p
++] = GLX_ACCUM_ALPHA_SIZE
;
571 gl_attribList
[p
++] = wx_attribList
[arg
++];
578 gl_attribList
[p
] = 0;
582 void wxGLCanvas::QueryGLXVersion()
584 if (m_glxVersion
== 0)
586 // check the GLX version
587 int glxMajorVer
, glxMinorVer
;
588 bool ok
= glXQueryVersion(GDK_DISPLAY(), &glxMajorVer
, &glxMinorVer
);
589 wxASSERT_MSG( ok
, _T("GLX version not found") );
591 m_glxVersion
= 10; // 1.0 by default
593 m_glxVersion
= glxMajorVer
*10 + glxMinorVer
;
597 int wxGLCanvas::GetGLXVersion()
599 wxASSERT_MSG( m_glxVersion
>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
604 void wxGLCanvas::SwapBuffers()
607 m_glContext
->SwapBuffers();
610 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
614 void wxGLCanvas::SetCurrent()
617 m_glContext
->SetCurrent();
620 void wxGLCanvas::SetColour( const wxChar
*colour
)
623 m_glContext
->SetColour( colour
);
626 void wxGLCanvas::OnInternalIdle()
628 if (m_glContext
&& m_exposed
)
630 wxPaintEvent
event( GetId() );
631 event
.SetEventObject( this );
632 GetEventHandler()->ProcessEvent( event
);
635 GetUpdateRegion().Clear();
638 wxWindow::OnInternalIdle();
643 //---------------------------------------------------------------------------
645 //---------------------------------------------------------------------------
647 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
654 XFree(m_glVisualInfo
);
657 bool wxGLApp::InitGLVisual(int *attribList
)
659 wxGLCanvas::QueryGLXVersion();
661 if (wxGLCanvas::GetGLXVersion() >= 13)
666 m_glFBCInfo
= wxGLCanvas::ChooseGLFBC(attribList
);
671 XFree(m_glVisualInfo
);
672 m_glVisualInfo
= glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig
*)m_glFBCInfo
)[0]);
674 return (m_glFBCInfo
!= NULL
) && (m_glVisualInfo
!= NULL
);
680 XFree(m_glVisualInfo
);
681 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
682 return m_glVisualInfo
!= NULL
;