1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "glcanvas.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/glcanvas.h"
26 #include "wx/colour.h"
27 #include "wx/module.h"
37 #include "wx/gtk/win_gtk.h"
39 // DLL options compatibility check:
41 WX_CHECK_BUILD_OPTIONS("wxGL")
44 //---------------------------------------------------------------------------
46 //---------------------------------------------------------------------------
47 int wxGLCanvas::m_glxVersion
= 0;
49 //---------------------------------------------------------------------------
51 //---------------------------------------------------------------------------
53 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 extern void wxapp_install_idle_handler();
61 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
65 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
67 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
70 m_widget
= win
->m_wxwindow
;
72 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
74 if (wxGLCanvas::GetGLXVersion() >= 13)
77 GLXFBConfig
*fbc
= gc
->m_fbc
;
78 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
79 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
, None
, GL_TRUE
);
84 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
85 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
86 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
, None
, GL_TRUE
);
89 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
92 wxGLContext::wxGLContext(
93 bool WXUNUSED(isRGB
), wxWindow
*win
,
94 const wxPalette
& WXUNUSED(palette
),
95 const wxGLContext
*other
/* for sharing display lists */
99 m_widget
= win
->m_wxwindow
;
101 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
103 if (wxGLCanvas::GetGLXVersion() >= 13)
106 GLXFBConfig
*fbc
= gc
->m_fbc
;
107 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
108 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
,
109 other
? other
->m_glContext
: None
,
115 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
116 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
117 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
,
118 other
? other
->m_glContext
: None
,
124 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
128 wxGLContext::~wxGLContext()
130 if (!m_glContext
) return;
132 if (m_glContext
== glXGetCurrentContext())
134 if (wxGLCanvas::GetGLXVersion() >= 13)
136 glXMakeContextCurrent( GDK_DISPLAY(), None
, None
, NULL
);
139 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
142 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
145 void wxGLContext::SwapBuffers()
149 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
150 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
154 void wxGLContext::SetCurrent()
158 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
160 if (wxGLCanvas::GetGLXVersion() >= 13)
162 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
),
163 GDK_WINDOW_XWINDOW(window
), m_glContext
);
166 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
170 void wxGLContext::SetColour(const wxChar
*colour
)
172 wxColour col
= wxTheColourDatabase
->Find(colour
);
175 float r
= (float)(col
.Red()/256.0);
176 float g
= (float)(col
.Green()/256.0);
177 float b
= (float)(col
.Blue()/256.0);
182 void wxGLContext::SetupPixelFormat()
186 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
190 wxPalette
wxGLContext::CreateDefaultPalette()
192 return wxNullPalette
;
195 //-----------------------------------------------------------------------------
196 // "realize" from m_wxwindow
197 //-----------------------------------------------------------------------------
201 gtk_glwindow_realized_callback( GtkWidget
*WXUNUSED(widget
), wxGLCanvas
*win
)
203 if ( !win
->m_glContext
)
205 wxGLContext
*share
= win
->m_sharedContext
;
206 if ( !share
&& win
->m_sharedContextOf
)
207 share
= win
->m_sharedContextOf
->GetContext();
209 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
216 //-----------------------------------------------------------------------------
217 // "map" from m_wxwindow
218 //-----------------------------------------------------------------------------
222 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
224 if (win
->m_glContext
/* && win->m_exposed*/)
226 wxPaintEvent
event( win
->GetId() );
227 event
.SetEventObject( win
);
228 win
->GetEventHandler()->ProcessEvent( event
);
230 win
->m_exposed
= FALSE
;
231 win
->GetUpdateRegion().Clear();
238 //-----------------------------------------------------------------------------
239 // "expose_event" of m_wxwindow
240 //-----------------------------------------------------------------------------
244 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
247 wxapp_install_idle_handler();
249 win
->m_exposed
= TRUE
;
251 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
253 gdk_event
->area
.width
,
254 gdk_event
->area
.height
);
258 //-----------------------------------------------------------------------------
259 // "draw" of m_wxwindow
260 //-----------------------------------------------------------------------------
265 gtk_glwindow_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxGLCanvas
*win
)
268 wxapp_install_idle_handler();
270 win
->m_exposed
= TRUE
;
272 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
,
273 rect
->width
, rect
->height
);
278 //-----------------------------------------------------------------------------
279 // "size_allocate" of m_wxwindow
280 //-----------------------------------------------------------------------------
284 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
287 wxapp_install_idle_handler();
292 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
293 event
.SetEventObject( win
);
294 win
->GetEventHandler()->ProcessEvent( event
);
298 //---------------------------------------------------------------------------
300 //---------------------------------------------------------------------------
302 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
304 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
305 EVT_SIZE(wxGLCanvas::OnSize
)
308 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
309 const wxPoint
& pos
, const wxSize
& size
,
310 long style
, const wxString
& name
,
312 const wxPalette
& palette
)
314 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
317 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
318 const wxGLContext
*shared
,
320 const wxPoint
& pos
, const wxSize
& size
,
321 long style
, const wxString
& name
,
323 const wxPalette
& palette
)
325 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
328 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
329 const wxGLCanvas
*shared
,
331 const wxPoint
& pos
, const wxSize
& size
,
332 long style
, const wxString
& name
,
334 const wxPalette
& palette
)
336 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
339 bool wxGLCanvas::Create( wxWindow
*parent
,
340 const wxGLContext
*shared
,
341 const wxGLCanvas
*shared_context_of
,
343 const wxPoint
& pos
, const wxSize
& size
,
344 long style
, const wxString
& name
,
346 const wxPalette
& palette
)
348 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
349 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
350 m_glContext
= (wxGLContext
*) NULL
;
354 m_nativeSizeEvent
= TRUE
;
358 // to be sure the glx version is known
359 wxGLCanvas::QueryGLXVersion();
361 if (wxGLCanvas::GetGLXVersion() >= 13)
363 // GLX >= 1.3 uses a GLXFBConfig
364 GLXFBConfig
* fbc
= NULL
;
365 if (wxTheApp
->m_glFBCInfo
!= NULL
)
367 fbc
= (GLXFBConfig
*) wxTheApp
->m_glFBCInfo
;
368 m_canFreeFBC
= FALSE
; // owned by wxTheApp - don't free upon destruction
372 fbc
= (GLXFBConfig
*) wxGLCanvas::ChooseGLFBC(attribList
);
375 m_fbc
= fbc
; // save for later use
376 wxCHECK_MSG( m_fbc
, FALSE
, _T("required FBConfig couldn't be found") );
379 XVisualInfo
*vi
= NULL
;
380 if (wxTheApp
->m_glVisualInfo
!= NULL
)
382 vi
= (XVisualInfo
*)wxTheApp
->m_glVisualInfo
;
383 m_canFreeVi
= FALSE
; // owned by wxTheApp - don't free upon destruction
387 if (wxGLCanvas::GetGLXVersion() >= 13)
389 vi
= glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc
[0]);
392 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
397 m_vi
= vi
; // save for later use
399 wxCHECK_MSG( m_vi
, FALSE
, _T("required visual couldn't be found") );
401 GdkColormap
*colormap
;
403 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
404 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
405 if (!gtk_check_version(2,2,0))
407 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
409 m_glWidget
= m_wxwindow
;
411 GdkScreen
*screen
= gtk_widget_get_screen( m_glWidget
);
412 colormap
= gdk_screen_get_default_colormap(screen
);
413 visual
= gdk_colormap_get_visual(colormap
);
415 if (GDK_VISUAL_XVISUAL(visual
)->visualid
!= vi
->visualid
)
417 visual
= gdk_x11_screen_lookup_visual( screen
, vi
->visualid
);
418 colormap
= gdk_colormap_new(visual
, FALSE
);
421 gtk_widget_set_colormap( m_glWidget
, colormap
);
426 visual
= gdkx_visual_get( vi
->visualid
);
427 colormap
= gdk_colormap_new( visual
, TRUE
);
429 gtk_widget_push_colormap( colormap
);
430 gtk_widget_push_visual( visual
);
432 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
433 m_glWidget
= m_wxwindow
;
437 gtk_widget_set_double_buffered( m_glWidget
, FALSE
);
440 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow
), FALSE
);
442 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "realize",
443 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
445 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "map",
446 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback
), (gpointer
) this );
448 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "expose_event",
449 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback
), (gpointer
)this );
452 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "draw",
453 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback
), (gpointer
)this );
456 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
457 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback
), (gpointer
)this );
460 if (gtk_check_version(2,2,0) != NULL
)
463 gtk_widget_pop_visual();
464 gtk_widget_pop_colormap();
467 // if our parent window is already visible, we had been realized before we
468 // connected to the "realize" signal and hence our m_glContext hasn't been
469 // initialized yet and we have to do it now
470 if (GTK_WIDGET_REALIZED(m_wxwindow
))
471 gtk_glwindow_realized_callback( m_wxwindow
, this );
473 if (GTK_WIDGET_MAPPED(m_wxwindow
))
474 gtk_glwindow_map_callback( m_wxwindow
, this );
479 wxGLCanvas::~wxGLCanvas()
481 GLXFBConfig
* fbc
= (GLXFBConfig
*) m_fbc
;
482 if (fbc
&& m_canFreeFBC
)
485 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
486 if (vi
&& m_canFreeVi
)
492 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
495 GetGLAttribListFromWX( attribList
, data
);
496 attribList
= (int*) data
;
498 Display
*dpy
= GDK_DISPLAY();
500 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
503 void* wxGLCanvas::ChooseGLFBC(int *attribList
)
506 GetGLAttribListFromWX( attribList
, data
);
507 attribList
= (int*) data
;
510 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
511 attribList
, &returned
);
515 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList
, int *gl_attribList
)
519 if (wxGLCanvas::GetGLXVersion() >= 13)
520 // leave GLX >= 1.3 choose the default attributes
521 gl_attribList
[0] = 0;
525 // default settings if attriblist = 0
526 gl_attribList
[i
++] = GLX_RGBA
;
527 gl_attribList
[i
++] = GLX_DOUBLEBUFFER
;
528 gl_attribList
[i
++] = GLX_DEPTH_SIZE
; gl_attribList
[i
++] = 1;
529 gl_attribList
[i
++] = GLX_RED_SIZE
; gl_attribList
[i
++] = 1;
530 gl_attribList
[i
++] = GLX_GREEN_SIZE
; gl_attribList
[i
++] = 1;
531 gl_attribList
[i
++] = GLX_BLUE_SIZE
; gl_attribList
[i
++] = 1;
532 gl_attribList
[i
++] = GLX_ALPHA_SIZE
; gl_attribList
[i
++] = 0;
533 gl_attribList
[i
++] = None
;
539 while( (wx_attribList
[arg
]!=0) && (p
<510) )
541 switch( wx_attribList
[arg
++] )
544 if (wxGLCanvas::GetGLXVersion() <= 12)
545 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
546 gl_attribList
[p
++] = GLX_RGBA
;
548 case WX_GL_BUFFER_SIZE
:
549 gl_attribList
[p
++] = GLX_BUFFER_SIZE
;
550 gl_attribList
[p
++] = wx_attribList
[arg
++];
553 gl_attribList
[p
++] = GLX_LEVEL
;
554 gl_attribList
[p
++] = wx_attribList
[arg
++];
556 case WX_GL_DOUBLEBUFFER
:
557 if (wxGLCanvas::GetGLXVersion() <= 12)
558 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
560 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
561 // it seems this flag is useless for some hardware opengl implementation.
562 // but for Mesa 6.2.1, this flag is used so don't ignore it.
563 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
564 gl_attribList
[p
++] = 1;
567 gl_attribList
[p
++] = GLX_STEREO
;
569 case WX_GL_AUX_BUFFERS
:
570 gl_attribList
[p
++] = GLX_AUX_BUFFERS
;
571 gl_attribList
[p
++] = wx_attribList
[arg
++];
574 gl_attribList
[p
++] = GLX_RED_SIZE
;
575 gl_attribList
[p
++] = wx_attribList
[arg
++];
577 case WX_GL_MIN_GREEN
:
578 gl_attribList
[p
++] = GLX_GREEN_SIZE
;
579 gl_attribList
[p
++] = wx_attribList
[arg
++];
582 gl_attribList
[p
++] = GLX_BLUE_SIZE
;
583 gl_attribList
[p
++] = wx_attribList
[arg
++];
585 case WX_GL_MIN_ALPHA
:
586 gl_attribList
[p
++] = GLX_ALPHA_SIZE
;
587 gl_attribList
[p
++] = wx_attribList
[arg
++];
589 case WX_GL_DEPTH_SIZE
:
590 gl_attribList
[p
++] = GLX_DEPTH_SIZE
;
591 gl_attribList
[p
++] = wx_attribList
[arg
++];
593 case WX_GL_STENCIL_SIZE
:
594 gl_attribList
[p
++] = GLX_STENCIL_SIZE
;
595 gl_attribList
[p
++] = wx_attribList
[arg
++];
597 case WX_GL_MIN_ACCUM_RED
:
598 gl_attribList
[p
++] = GLX_ACCUM_RED_SIZE
;
599 gl_attribList
[p
++] = wx_attribList
[arg
++];
601 case WX_GL_MIN_ACCUM_GREEN
:
602 gl_attribList
[p
++] = GLX_ACCUM_GREEN_SIZE
;
603 gl_attribList
[p
++] = wx_attribList
[arg
++];
605 case WX_GL_MIN_ACCUM_BLUE
:
606 gl_attribList
[p
++] = GLX_ACCUM_BLUE_SIZE
;
607 gl_attribList
[p
++] = wx_attribList
[arg
++];
609 case WX_GL_MIN_ACCUM_ALPHA
:
610 gl_attribList
[p
++] = GLX_ACCUM_ALPHA_SIZE
;
611 gl_attribList
[p
++] = wx_attribList
[arg
++];
618 gl_attribList
[p
] = 0;
622 void wxGLCanvas::QueryGLXVersion()
624 if (m_glxVersion
== 0)
626 // check the GLX version
627 int glxMajorVer
, glxMinorVer
;
628 bool ok
= glXQueryVersion(GDK_DISPLAY(), &glxMajorVer
, &glxMinorVer
);
629 wxASSERT_MSG( ok
, _T("GLX version not found") );
631 m_glxVersion
= 10; // 1.0 by default
633 m_glxVersion
= glxMajorVer
*10 + glxMinorVer
;
637 int wxGLCanvas::GetGLXVersion()
639 wxASSERT_MSG( m_glxVersion
>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
644 void wxGLCanvas::SwapBuffers()
647 m_glContext
->SwapBuffers();
650 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
654 void wxGLCanvas::SetCurrent()
657 m_glContext
->SetCurrent();
660 void wxGLCanvas::SetColour( const wxChar
*colour
)
663 m_glContext
->SetColour( colour
);
666 void wxGLCanvas::OnInternalIdle()
668 if (m_glContext
&& m_exposed
)
670 wxPaintEvent
event( GetId() );
671 event
.SetEventObject( this );
672 GetEventHandler()->ProcessEvent( event
);
675 GetUpdateRegion().Clear();
678 wxWindow::OnInternalIdle();
683 //---------------------------------------------------------------------------
685 //---------------------------------------------------------------------------
687 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
694 XFree(m_glVisualInfo
);
697 bool wxGLApp::InitGLVisual(int *attribList
)
699 wxGLCanvas::QueryGLXVersion();
701 if (wxGLCanvas::GetGLXVersion() >= 13)
706 m_glFBCInfo
= wxGLCanvas::ChooseGLFBC(attribList
);
711 XFree(m_glVisualInfo
);
712 m_glVisualInfo
= glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig
*)m_glFBCInfo
)[0]);
714 return (m_glFBCInfo
!= NULL
) && (m_glVisualInfo
!= NULL
);
720 XFree(m_glVisualInfo
);
721 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
722 return m_glVisualInfo
!= NULL
;