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"
20 #include "wx/colour.h"
21 #include "wx/module.h"
31 #include "wx/gtk/win_gtk.h"
32 #include "wx/gtk/private.h"
34 // DLL options compatibility check:
36 WX_CHECK_BUILD_OPTIONS("wxGL")
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
42 int wxGLCanvas::m_glxVersion
= 0;
44 //---------------------------------------------------------------------------
46 //---------------------------------------------------------------------------
48 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
50 //---------------------------------------------------------------------------
52 //---------------------------------------------------------------------------
54 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
56 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
59 m_widget
= win
->m_wxwindow
;
61 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
63 if (wxGLCanvas::GetGLXVersion() >= 13)
66 GLXFBConfig
*fbc
= gc
->m_fbc
;
67 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
68 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
, None
, GL_TRUE
);
73 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
74 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
75 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
, None
, GL_TRUE
);
78 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
81 wxGLContext::wxGLContext(
82 bool WXUNUSED(isRGB
), wxWindow
*win
,
83 const wxPalette
& WXUNUSED(palette
),
84 const wxGLContext
*other
/* for sharing display lists */
88 m_widget
= win
->m_wxwindow
;
90 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
92 if (wxGLCanvas::GetGLXVersion() >= 13)
95 GLXFBConfig
*fbc
= gc
->m_fbc
;
96 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
97 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
,
98 other
? other
->m_glContext
: None
,
104 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
105 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
106 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
,
107 other
? other
->m_glContext
: None
,
113 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
117 wxGLContext::~wxGLContext()
119 if (!m_glContext
) return;
121 if (m_glContext
== glXGetCurrentContext())
123 if (wxGLCanvas::GetGLXVersion() >= 13)
125 glXMakeContextCurrent( GDK_DISPLAY(), None
, None
, NULL
);
128 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
131 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
134 void wxGLContext::SwapBuffers()
138 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
139 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
143 void wxGLContext::SetCurrent()
147 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
149 if (wxGLCanvas::GetGLXVersion() >= 13)
151 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
),
152 GDK_WINDOW_XWINDOW(window
), m_glContext
);
155 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
159 void wxGLContext::SetColour(const wxChar
*colour
)
161 wxColour col
= wxTheColourDatabase
->Find(colour
);
164 float r
= (float)(col
.Red()/256.0);
165 float g
= (float)(col
.Green()/256.0);
166 float b
= (float)(col
.Blue()/256.0);
171 void wxGLContext::SetupPixelFormat()
175 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
179 wxPalette
wxGLContext::CreateDefaultPalette()
181 return wxNullPalette
;
184 //-----------------------------------------------------------------------------
185 // "realize" from m_wxwindow
186 //-----------------------------------------------------------------------------
190 gtk_glwindow_realized_callback( GtkWidget
*WXUNUSED(widget
), wxGLCanvas
*win
)
192 if ( !win
->m_glContext
)
194 wxGLContext
*share
= win
->m_sharedContext
;
195 if ( !share
&& win
->m_sharedContextOf
)
196 share
= win
->m_sharedContextOf
->GetContext();
198 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
205 //-----------------------------------------------------------------------------
206 // "map" from m_wxwindow
207 //-----------------------------------------------------------------------------
211 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
213 if (win
->m_glContext
/* && win->m_exposed*/)
215 wxPaintEvent
event( win
->GetId() );
216 event
.SetEventObject( win
);
217 win
->GetEventHandler()->ProcessEvent( event
);
219 win
->m_exposed
= FALSE
;
220 win
->GetUpdateRegion().Clear();
227 //-----------------------------------------------------------------------------
228 // "expose_event" of m_wxwindow
229 //-----------------------------------------------------------------------------
233 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
236 wxapp_install_idle_handler();
238 win
->m_exposed
= TRUE
;
240 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
242 gdk_event
->area
.width
,
243 gdk_event
->area
.height
);
247 //-----------------------------------------------------------------------------
248 // "size_allocate" of m_wxwindow
249 //-----------------------------------------------------------------------------
253 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
256 wxapp_install_idle_handler();
261 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
262 event
.SetEventObject( win
);
263 win
->GetEventHandler()->ProcessEvent( event
);
267 //---------------------------------------------------------------------------
269 //---------------------------------------------------------------------------
271 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
273 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
274 EVT_SIZE(wxGLCanvas::OnSize
)
277 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
278 const wxPoint
& pos
, const wxSize
& size
,
279 long style
, const wxString
& name
,
281 const wxPalette
& palette
)
283 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
286 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
287 const wxGLContext
*shared
,
289 const wxPoint
& pos
, const wxSize
& size
,
290 long style
, const wxString
& name
,
292 const wxPalette
& palette
)
294 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
297 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
298 const wxGLCanvas
*shared
,
300 const wxPoint
& pos
, const wxSize
& size
,
301 long style
, const wxString
& name
,
303 const wxPalette
& palette
)
305 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
308 bool wxGLCanvas::Create( wxWindow
*parent
,
309 const wxGLContext
*shared
,
310 const wxGLCanvas
*shared_context_of
,
312 const wxPoint
& pos
, const wxSize
& size
,
313 long style
, const wxString
& name
,
315 const wxPalette
& palette
)
317 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
318 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
319 m_glContext
= (wxGLContext
*) NULL
;
323 m_nativeSizeEvent
= TRUE
;
327 // to be sure the glx version is known
328 wxGLCanvas::QueryGLXVersion();
330 if (wxGLCanvas::GetGLXVersion() >= 13)
332 // GLX >= 1.3 uses a GLXFBConfig
333 GLXFBConfig
* fbc
= NULL
;
334 if (wxTheApp
->m_glFBCInfo
!= NULL
)
336 fbc
= (GLXFBConfig
*) wxTheApp
->m_glFBCInfo
;
337 m_canFreeFBC
= FALSE
; // owned by wxTheApp - don't free upon destruction
341 fbc
= (GLXFBConfig
*) wxGLCanvas::ChooseGLFBC(attribList
);
344 m_fbc
= fbc
; // save for later use
345 wxCHECK_MSG( m_fbc
, FALSE
, _T("required FBConfig couldn't be found") );
348 XVisualInfo
*vi
= NULL
;
349 if (wxTheApp
->m_glVisualInfo
!= NULL
)
351 vi
= (XVisualInfo
*)wxTheApp
->m_glVisualInfo
;
352 m_canFreeVi
= FALSE
; // owned by wxTheApp - don't free upon destruction
356 if (wxGLCanvas::GetGLXVersion() >= 13)
358 vi
= glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc
[0]);
361 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
366 m_vi
= vi
; // save for later use
368 wxCHECK_MSG( m_vi
, FALSE
, _T("required visual couldn't be found") );
370 GdkColormap
*colormap
;
372 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
373 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
374 if (!gtk_check_version(2,2,0))
376 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
378 m_glWidget
= m_wxwindow
;
380 GdkScreen
*screen
= gtk_widget_get_screen( m_glWidget
);
381 colormap
= gdk_screen_get_default_colormap(screen
);
382 visual
= gdk_colormap_get_visual(colormap
);
384 if (GDK_VISUAL_XVISUAL(visual
)->visualid
!= vi
->visualid
)
386 visual
= gdk_x11_screen_lookup_visual( screen
, vi
->visualid
);
387 colormap
= gdk_colormap_new(visual
, FALSE
);
390 gtk_widget_set_colormap( m_glWidget
, colormap
);
395 visual
= gdkx_visual_get( vi
->visualid
);
396 colormap
= gdk_colormap_new( visual
, TRUE
);
398 gtk_widget_push_colormap( colormap
);
400 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
401 m_glWidget
= m_wxwindow
;
404 gtk_widget_set_double_buffered( m_glWidget
, FALSE
);
406 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow
), FALSE
);
408 g_signal_connect (m_wxwindow
, "realize",
409 G_CALLBACK (gtk_glwindow_realized_callback
),
411 g_signal_connect (m_wxwindow
, "map",
412 G_CALLBACK (gtk_glwindow_map_callback
),
414 g_signal_connect (m_wxwindow
, "expose_event",
415 G_CALLBACK (gtk_glwindow_expose_callback
),
417 g_signal_connect (m_widget
, "size_allocate",
418 G_CALLBACK (gtk_glcanvas_size_callback
),
421 if (gtk_check_version(2,2,0) != NULL
)
423 gtk_widget_pop_colormap();
426 // if our parent window is already visible, we had been realized before we
427 // connected to the "realize" signal and hence our m_glContext hasn't been
428 // initialized yet and we have to do it now
429 if (GTK_WIDGET_REALIZED(m_wxwindow
))
430 gtk_glwindow_realized_callback( m_wxwindow
, this );
432 if (GTK_WIDGET_MAPPED(m_wxwindow
))
433 gtk_glwindow_map_callback( m_wxwindow
, this );
438 wxGLCanvas::~wxGLCanvas()
440 GLXFBConfig
* fbc
= (GLXFBConfig
*) m_fbc
;
441 if (fbc
&& m_canFreeFBC
)
444 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
445 if (vi
&& m_canFreeVi
)
451 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
454 GetGLAttribListFromWX( attribList
, data
);
455 attribList
= (int*) data
;
457 Display
*dpy
= GDK_DISPLAY();
459 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
462 void* wxGLCanvas::ChooseGLFBC(int *attribList
)
465 GetGLAttribListFromWX( attribList
, data
);
466 attribList
= (int*) data
;
469 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
470 attribList
, &returned
);
474 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList
, int *gl_attribList
)
478 if (wxGLCanvas::GetGLXVersion() >= 13)
479 // leave GLX >= 1.3 choose the default attributes
480 gl_attribList
[0] = 0;
484 // default settings if attriblist = 0
485 gl_attribList
[i
++] = GLX_RGBA
;
486 gl_attribList
[i
++] = GLX_DOUBLEBUFFER
;
487 gl_attribList
[i
++] = GLX_DEPTH_SIZE
; gl_attribList
[i
++] = 1;
488 gl_attribList
[i
++] = GLX_RED_SIZE
; gl_attribList
[i
++] = 1;
489 gl_attribList
[i
++] = GLX_GREEN_SIZE
; gl_attribList
[i
++] = 1;
490 gl_attribList
[i
++] = GLX_BLUE_SIZE
; gl_attribList
[i
++] = 1;
491 gl_attribList
[i
++] = GLX_ALPHA_SIZE
; gl_attribList
[i
++] = 0;
492 gl_attribList
[i
++] = None
;
498 while( (wx_attribList
[arg
]!=0) && (p
<510) )
500 switch( wx_attribList
[arg
++] )
503 if (wxGLCanvas::GetGLXVersion() <= 12)
504 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
505 gl_attribList
[p
++] = GLX_RGBA
;
507 case WX_GL_BUFFER_SIZE
:
508 gl_attribList
[p
++] = GLX_BUFFER_SIZE
;
509 gl_attribList
[p
++] = wx_attribList
[arg
++];
512 gl_attribList
[p
++] = GLX_LEVEL
;
513 gl_attribList
[p
++] = wx_attribList
[arg
++];
515 case WX_GL_DOUBLEBUFFER
:
516 if (wxGLCanvas::GetGLXVersion() <= 12)
517 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
519 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
520 // it seems this flag is useless for some hardware opengl implementation.
521 // but for Mesa 6.2.1, this flag is used so don't ignore it.
522 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
523 gl_attribList
[p
++] = 1;
526 gl_attribList
[p
++] = GLX_STEREO
;
528 case WX_GL_AUX_BUFFERS
:
529 gl_attribList
[p
++] = GLX_AUX_BUFFERS
;
530 gl_attribList
[p
++] = wx_attribList
[arg
++];
533 gl_attribList
[p
++] = GLX_RED_SIZE
;
534 gl_attribList
[p
++] = wx_attribList
[arg
++];
536 case WX_GL_MIN_GREEN
:
537 gl_attribList
[p
++] = GLX_GREEN_SIZE
;
538 gl_attribList
[p
++] = wx_attribList
[arg
++];
541 gl_attribList
[p
++] = GLX_BLUE_SIZE
;
542 gl_attribList
[p
++] = wx_attribList
[arg
++];
544 case WX_GL_MIN_ALPHA
:
545 gl_attribList
[p
++] = GLX_ALPHA_SIZE
;
546 gl_attribList
[p
++] = wx_attribList
[arg
++];
548 case WX_GL_DEPTH_SIZE
:
549 gl_attribList
[p
++] = GLX_DEPTH_SIZE
;
550 gl_attribList
[p
++] = wx_attribList
[arg
++];
552 case WX_GL_STENCIL_SIZE
:
553 gl_attribList
[p
++] = GLX_STENCIL_SIZE
;
554 gl_attribList
[p
++] = wx_attribList
[arg
++];
556 case WX_GL_MIN_ACCUM_RED
:
557 gl_attribList
[p
++] = GLX_ACCUM_RED_SIZE
;
558 gl_attribList
[p
++] = wx_attribList
[arg
++];
560 case WX_GL_MIN_ACCUM_GREEN
:
561 gl_attribList
[p
++] = GLX_ACCUM_GREEN_SIZE
;
562 gl_attribList
[p
++] = wx_attribList
[arg
++];
564 case WX_GL_MIN_ACCUM_BLUE
:
565 gl_attribList
[p
++] = GLX_ACCUM_BLUE_SIZE
;
566 gl_attribList
[p
++] = wx_attribList
[arg
++];
568 case WX_GL_MIN_ACCUM_ALPHA
:
569 gl_attribList
[p
++] = GLX_ACCUM_ALPHA_SIZE
;
570 gl_attribList
[p
++] = wx_attribList
[arg
++];
577 gl_attribList
[p
] = 0;
581 void wxGLCanvas::QueryGLXVersion()
583 if (m_glxVersion
== 0)
585 // check the GLX version
586 int glxMajorVer
, glxMinorVer
;
587 bool ok
= glXQueryVersion(GDK_DISPLAY(), &glxMajorVer
, &glxMinorVer
);
588 wxASSERT_MSG( ok
, _T("GLX version not found") );
590 m_glxVersion
= 10; // 1.0 by default
592 m_glxVersion
= glxMajorVer
*10 + glxMinorVer
;
596 int wxGLCanvas::GetGLXVersion()
598 wxASSERT_MSG( m_glxVersion
>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
603 void wxGLCanvas::SwapBuffers()
606 m_glContext
->SwapBuffers();
609 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
613 void wxGLCanvas::SetCurrent()
616 m_glContext
->SetCurrent();
619 void wxGLCanvas::SetColour( const wxChar
*colour
)
622 m_glContext
->SetColour( colour
);
625 void wxGLCanvas::OnInternalIdle()
627 if (m_glContext
&& m_exposed
)
629 wxPaintEvent
event( GetId() );
630 event
.SetEventObject( this );
631 GetEventHandler()->ProcessEvent( event
);
634 GetUpdateRegion().Clear();
637 wxWindow::OnInternalIdle();
642 //---------------------------------------------------------------------------
644 //---------------------------------------------------------------------------
646 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
653 XFree(m_glVisualInfo
);
656 bool wxGLApp::InitGLVisual(int *attribList
)
658 wxGLCanvas::QueryGLXVersion();
660 if (wxGLCanvas::GetGLXVersion() >= 13)
665 m_glFBCInfo
= wxGLCanvas::ChooseGLFBC(attribList
);
670 XFree(m_glVisualInfo
);
671 m_glVisualInfo
= glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig
*)m_glFBCInfo
)[0]);
673 return (m_glFBCInfo
!= NULL
) && (m_glVisualInfo
!= NULL
);
679 XFree(m_glVisualInfo
);
680 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
681 return m_glVisualInfo
!= NULL
;