1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gtk1/win_gtk.h"
33 // DLL options compatibility check:
35 WX_CHECK_BUILD_OPTIONS("wxGL")
38 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 int wxGLCanvas::m_glxVersion
= 0;
43 //---------------------------------------------------------------------------
45 //---------------------------------------------------------------------------
47 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 extern void wxapp_install_idle_handler();
55 //---------------------------------------------------------------------------
57 //---------------------------------------------------------------------------
59 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
61 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
64 m_widget
= win
->m_wxwindow
;
66 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
68 if (wxGLCanvas::GetGLXVersion() >= 13)
71 GLXFBConfig
*fbc
= gc
->m_fbc
;
72 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
73 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
, None
, GL_TRUE
);
78 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
79 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
80 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
, None
, GL_TRUE
);
83 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
86 wxGLContext::wxGLContext(
87 bool WXUNUSED(isRGB
), wxWindow
*win
,
88 const wxPalette
& WXUNUSED(palette
),
89 const wxGLContext
*other
/* for sharing display lists */
93 m_widget
= win
->m_wxwindow
;
95 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
97 if (wxGLCanvas::GetGLXVersion() >= 13)
100 GLXFBConfig
*fbc
= gc
->m_fbc
;
101 wxCHECK_RET( fbc
, _T("invalid GLXFBConfig for OpenGl") );
102 m_glContext
= glXCreateNewContext( GDK_DISPLAY(), fbc
[0], GLX_RGBA_TYPE
,
103 other
? other
->m_glContext
: None
,
109 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
110 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
111 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
,
112 other
? other
->m_glContext
: None
,
118 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
122 wxGLContext::~wxGLContext()
124 if (!m_glContext
) return;
126 if (m_glContext
== glXGetCurrentContext())
128 if (wxGLCanvas::GetGLXVersion() >= 13)
130 glXMakeContextCurrent( GDK_DISPLAY(), None
, None
, NULL
);
133 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
136 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
139 void wxGLContext::SwapBuffers()
143 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
144 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
148 void wxGLContext::SetCurrent()
152 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
154 if (wxGLCanvas::GetGLXVersion() >= 13)
156 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
),
157 GDK_WINDOW_XWINDOW(window
), m_glContext
);
160 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
164 void wxGLContext::SetColour(const wxChar
*colour
)
166 wxColour col
= wxTheColourDatabase
->Find(colour
);
169 float r
= (float)(col
.Red()/256.0);
170 float g
= (float)(col
.Green()/256.0);
171 float b
= (float)(col
.Blue()/256.0);
176 void wxGLContext::SetupPixelFormat()
180 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
184 wxPalette
wxGLContext::CreateDefaultPalette()
186 return wxNullPalette
;
189 //-----------------------------------------------------------------------------
190 // "realize" from m_wxwindow
191 //-----------------------------------------------------------------------------
195 gtk_glwindow_realized_callback( GtkWidget
*WXUNUSED(widget
), wxGLCanvas
*win
)
197 if ( !win
->m_glContext
)
199 wxGLContext
*share
= win
->m_sharedContext
;
200 if ( !share
&& win
->m_sharedContextOf
)
201 share
= win
->m_sharedContextOf
->GetContext();
203 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
210 //-----------------------------------------------------------------------------
211 // "map" from m_wxwindow
212 //-----------------------------------------------------------------------------
216 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
218 if (win
->m_glContext
/* && win->m_exposed*/)
220 wxPaintEvent
event( win
->GetId() );
221 event
.SetEventObject( win
);
222 win
->GetEventHandler()->ProcessEvent( event
);
224 win
->m_exposed
= FALSE
;
225 win
->GetUpdateRegion().Clear();
232 //-----------------------------------------------------------------------------
233 // "expose_event" of m_wxwindow
234 //-----------------------------------------------------------------------------
238 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
241 wxapp_install_idle_handler();
243 win
->m_exposed
= TRUE
;
245 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
247 gdk_event
->area
.width
,
248 gdk_event
->area
.height
);
252 //-----------------------------------------------------------------------------
253 // "draw" of m_wxwindow
254 //-----------------------------------------------------------------------------
258 gtk_glwindow_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxGLCanvas
*win
)
261 wxapp_install_idle_handler();
263 win
->m_exposed
= TRUE
;
265 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
,
266 rect
->width
, rect
->height
);
270 //-----------------------------------------------------------------------------
271 // "size_allocate" of m_wxwindow
272 //-----------------------------------------------------------------------------
276 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
279 wxapp_install_idle_handler();
284 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
285 event
.SetEventObject( win
);
286 win
->GetEventHandler()->ProcessEvent( event
);
290 //---------------------------------------------------------------------------
292 //---------------------------------------------------------------------------
294 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
296 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
297 EVT_SIZE(wxGLCanvas::OnSize
)
300 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
301 const wxPoint
& pos
, const wxSize
& size
,
302 long style
, const wxString
& name
,
304 const wxPalette
& palette
)
306 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
309 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
310 const wxGLContext
*shared
,
312 const wxPoint
& pos
, const wxSize
& size
,
313 long style
, const wxString
& name
,
315 const wxPalette
& palette
)
317 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
320 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
321 const wxGLCanvas
*shared
,
323 const wxPoint
& pos
, const wxSize
& size
,
324 long style
, const wxString
& name
,
326 const wxPalette
& palette
)
328 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
331 bool wxGLCanvas::Create( wxWindow
*parent
,
332 const wxGLContext
*shared
,
333 const wxGLCanvas
*shared_context_of
,
335 const wxPoint
& pos
, const wxSize
& size
,
336 long style
, const wxString
& name
,
338 const wxPalette
& palette
)
340 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
341 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
342 m_glContext
= (wxGLContext
*) NULL
;
346 m_nativeSizeEvent
= TRUE
;
350 // to be sure the glx version is known
351 wxGLCanvas::QueryGLXVersion();
353 if (wxGLCanvas::GetGLXVersion() >= 13)
355 // GLX >= 1.3 uses a GLXFBConfig
356 GLXFBConfig
* fbc
= NULL
;
357 if (wxTheApp
->m_glFBCInfo
!= NULL
)
359 fbc
= (GLXFBConfig
*) wxTheApp
->m_glFBCInfo
;
360 m_canFreeFBC
= FALSE
; // owned by wxTheApp - don't free upon destruction
364 fbc
= (GLXFBConfig
*) wxGLCanvas::ChooseGLFBC(attribList
);
367 m_fbc
= fbc
; // save for later use
368 wxCHECK_MSG( m_fbc
, FALSE
, _T("required FBConfig couldn't be found") );
371 XVisualInfo
*vi
= NULL
;
372 if (wxTheApp
->m_glVisualInfo
!= NULL
)
374 vi
= (XVisualInfo
*)wxTheApp
->m_glVisualInfo
;
375 m_canFreeVi
= FALSE
; // owned by wxTheApp - don't free upon destruction
379 if (wxGLCanvas::GetGLXVersion() >= 13)
381 vi
= glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc
[0]);
384 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
389 m_vi
= vi
; // save for later use
391 wxCHECK_MSG( m_vi
, FALSE
, _T("required visual couldn't be found") );
393 GdkColormap
*colormap
;
395 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
397 if (!gtk_check_version(2,2,0))
399 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
401 m_glWidget
= m_wxwindow
;
403 GdkScreen
*screen
= gtk_widget_get_screen( m_glWidget
);
404 colormap
= gdk_screen_get_default_colormap(screen
);
405 visual
= gdk_colormap_get_visual(colormap
);
407 if (GDK_VISUAL_XVISUAL(visual
)->visualid
!= vi
->visualid
)
409 visual
= gdk_x11_screen_lookup_visual( screen
, vi
->visualid
);
410 colormap
= gdk_colormap_new(visual
, FALSE
);
413 gtk_widget_set_colormap( m_glWidget
, colormap
);
418 visual
= gdkx_visual_get( vi
->visualid
);
419 colormap
= gdk_colormap_new( visual
, TRUE
);
421 gtk_widget_push_colormap( colormap
);
422 gtk_widget_push_visual( visual
);
424 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
425 m_glWidget
= m_wxwindow
;
428 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow
), FALSE
);
430 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "realize",
431 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
433 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "map",
434 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback
), (gpointer
) this );
436 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "expose_event",
437 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback
), (gpointer
)this );
439 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "draw",
440 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback
), (gpointer
)this );
442 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
443 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback
), (gpointer
)this );
445 gtk_widget_pop_visual();
446 gtk_widget_pop_colormap();
448 // if our parent window is already visible, we had been realized before we
449 // connected to the "realize" signal and hence our m_glContext hasn't been
450 // initialized yet and we have to do it now
451 if (GTK_WIDGET_REALIZED(m_wxwindow
))
452 gtk_glwindow_realized_callback( m_wxwindow
, this );
454 if (GTK_WIDGET_MAPPED(m_wxwindow
))
455 gtk_glwindow_map_callback( m_wxwindow
, this );
460 wxGLCanvas::~wxGLCanvas()
462 GLXFBConfig
* fbc
= (GLXFBConfig
*) m_fbc
;
463 if (fbc
&& m_canFreeFBC
)
466 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
467 if (vi
&& m_canFreeVi
)
473 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
476 GetGLAttribListFromWX( attribList
, data
);
477 attribList
= (int*) data
;
479 Display
*dpy
= GDK_DISPLAY();
481 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
484 void* wxGLCanvas::ChooseGLFBC(int *attribList
)
487 GetGLAttribListFromWX( attribList
, data
);
488 attribList
= (int*) data
;
491 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
492 attribList
, &returned
);
496 void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList
, int *gl_attribList
)
500 if (wxGLCanvas::GetGLXVersion() >= 13)
501 // leave GLX >= 1.3 choose the default attributes
502 gl_attribList
[0] = 0;
506 // default settings if attriblist = 0
507 gl_attribList
[i
++] = GLX_RGBA
;
508 gl_attribList
[i
++] = GLX_DOUBLEBUFFER
;
509 gl_attribList
[i
++] = GLX_DEPTH_SIZE
; gl_attribList
[i
++] = 1;
510 gl_attribList
[i
++] = GLX_RED_SIZE
; gl_attribList
[i
++] = 1;
511 gl_attribList
[i
++] = GLX_GREEN_SIZE
; gl_attribList
[i
++] = 1;
512 gl_attribList
[i
++] = GLX_BLUE_SIZE
; gl_attribList
[i
++] = 1;
513 gl_attribList
[i
++] = GLX_ALPHA_SIZE
; gl_attribList
[i
++] = 0;
514 gl_attribList
[i
++] = None
;
520 while( (wx_attribList
[arg
]!=0) && (p
<510) )
522 switch( wx_attribList
[arg
++] )
525 if (wxGLCanvas::GetGLXVersion() <= 12)
526 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
527 gl_attribList
[p
++] = GLX_RGBA
;
529 case WX_GL_BUFFER_SIZE
:
530 gl_attribList
[p
++] = GLX_BUFFER_SIZE
;
531 gl_attribList
[p
++] = wx_attribList
[arg
++];
534 gl_attribList
[p
++] = GLX_LEVEL
;
535 gl_attribList
[p
++] = wx_attribList
[arg
++];
537 case WX_GL_DOUBLEBUFFER
:
538 if (wxGLCanvas::GetGLXVersion() <= 12)
539 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
541 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
542 // it seems this flag is useless for some hardware opengl implementation.
543 // but for Mesa 6.2.1, this flag is used so don't ignore it.
544 gl_attribList
[p
++] = GLX_DOUBLEBUFFER
;
545 gl_attribList
[p
++] = 1;
548 gl_attribList
[p
++] = GLX_STEREO
;
550 case WX_GL_AUX_BUFFERS
:
551 gl_attribList
[p
++] = GLX_AUX_BUFFERS
;
552 gl_attribList
[p
++] = wx_attribList
[arg
++];
555 gl_attribList
[p
++] = GLX_RED_SIZE
;
556 gl_attribList
[p
++] = wx_attribList
[arg
++];
558 case WX_GL_MIN_GREEN
:
559 gl_attribList
[p
++] = GLX_GREEN_SIZE
;
560 gl_attribList
[p
++] = wx_attribList
[arg
++];
563 gl_attribList
[p
++] = GLX_BLUE_SIZE
;
564 gl_attribList
[p
++] = wx_attribList
[arg
++];
566 case WX_GL_MIN_ALPHA
:
567 gl_attribList
[p
++] = GLX_ALPHA_SIZE
;
568 gl_attribList
[p
++] = wx_attribList
[arg
++];
570 case WX_GL_DEPTH_SIZE
:
571 gl_attribList
[p
++] = GLX_DEPTH_SIZE
;
572 gl_attribList
[p
++] = wx_attribList
[arg
++];
574 case WX_GL_STENCIL_SIZE
:
575 gl_attribList
[p
++] = GLX_STENCIL_SIZE
;
576 gl_attribList
[p
++] = wx_attribList
[arg
++];
578 case WX_GL_MIN_ACCUM_RED
:
579 gl_attribList
[p
++] = GLX_ACCUM_RED_SIZE
;
580 gl_attribList
[p
++] = wx_attribList
[arg
++];
582 case WX_GL_MIN_ACCUM_GREEN
:
583 gl_attribList
[p
++] = GLX_ACCUM_GREEN_SIZE
;
584 gl_attribList
[p
++] = wx_attribList
[arg
++];
586 case WX_GL_MIN_ACCUM_BLUE
:
587 gl_attribList
[p
++] = GLX_ACCUM_BLUE_SIZE
;
588 gl_attribList
[p
++] = wx_attribList
[arg
++];
590 case WX_GL_MIN_ACCUM_ALPHA
:
591 gl_attribList
[p
++] = GLX_ACCUM_ALPHA_SIZE
;
592 gl_attribList
[p
++] = wx_attribList
[arg
++];
599 gl_attribList
[p
] = 0;
603 void wxGLCanvas::QueryGLXVersion()
605 if (m_glxVersion
== 0)
607 // check the GLX version
608 int glxMajorVer
, glxMinorVer
;
609 bool ok
= glXQueryVersion(GDK_DISPLAY(), &glxMajorVer
, &glxMinorVer
);
610 wxASSERT_MSG( ok
, _T("GLX version not found") );
612 m_glxVersion
= 10; // 1.0 by default
614 m_glxVersion
= glxMajorVer
*10 + glxMinorVer
;
618 int wxGLCanvas::GetGLXVersion()
620 wxASSERT_MSG( m_glxVersion
>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
625 void wxGLCanvas::SwapBuffers()
628 m_glContext
->SwapBuffers();
631 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
635 void wxGLCanvas::SetCurrent()
638 m_glContext
->SetCurrent();
641 void wxGLCanvas::SetColour( const wxChar
*colour
)
644 m_glContext
->SetColour( colour
);
647 void wxGLCanvas::OnInternalIdle()
649 if (m_glContext
&& m_exposed
)
651 wxPaintEvent
event( GetId() );
652 event
.SetEventObject( this );
653 GetEventHandler()->ProcessEvent( event
);
656 GetUpdateRegion().Clear();
659 wxWindow::OnInternalIdle();
664 //---------------------------------------------------------------------------
666 //---------------------------------------------------------------------------
668 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
675 XFree(m_glVisualInfo
);
678 bool wxGLApp::InitGLVisual(int *attribList
)
680 wxGLCanvas::QueryGLXVersion();
682 if (wxGLCanvas::GetGLXVersion() >= 13)
687 m_glFBCInfo
= wxGLCanvas::ChooseGLFBC(attribList
);
692 XFree(m_glVisualInfo
);
693 m_glVisualInfo
= glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig
*)m_glFBCInfo
)[0]);
695 return (m_glFBCInfo
!= NULL
) && (m_glVisualInfo
!= NULL
);
701 XFree(m_glVisualInfo
);
702 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
703 return m_glVisualInfo
!= NULL
;