1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "glcanvas.h"
20 #include "wx/glcanvas.h"
23 #include "wx/colour.h"
24 #include "wx/module.h"
34 #include "wx/gtk/win_gtk.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxGL")
40 //---------------------------------------------------------------------------
42 //---------------------------------------------------------------------------
44 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern void wxapp_install_idle_handler();
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
;
65 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
67 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
69 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
, None
, GL_TRUE
);
71 wxCHECK_RET( m_glContext
, _T("Couldn't create OpenGl context") );
74 wxGLContext::wxGLContext(
75 bool WXUNUSED(isRGB
), wxWindow
*win
,
76 const wxPalette
& WXUNUSED(palette
),
77 const wxGLContext
*other
/* for sharing display lists */
81 m_widget
= win
->m_wxwindow
;
83 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
84 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
86 wxCHECK_RET( vi
, _T("invalid visual for OpenGl") );
88 m_glContext
= glXCreateContext( GDK_DISPLAY(), vi
,
89 other
? other
->m_glContext
: None
,
94 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
98 wxGLContext::~wxGLContext()
100 if (!m_glContext
) return;
102 if (m_glContext
== glXGetCurrentContext())
104 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
107 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
110 void wxGLContext::SwapBuffers()
114 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
115 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
119 void wxGLContext::SetCurrent()
123 GdkWindow
*window
= GTK_PIZZA(m_widget
)->bin_window
;
124 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
128 void wxGLContext::SetColour(const wxChar
*colour
)
133 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
136 r
= (float)(col
->Red()/256.0);
137 g
= (float)(col
->Green()/256.0);
138 b
= (float)(col
->Blue()/256.0);
143 void wxGLContext::SetupPixelFormat()
147 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
151 wxPalette
wxGLContext::CreateDefaultPalette()
153 return wxNullPalette
;
156 //-----------------------------------------------------------------------------
157 // "realize" from m_wxwindow
158 //-----------------------------------------------------------------------------
161 gtk_glwindow_realized_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
163 wxGLContext
*share
= win
->m_sharedContext
;
164 if (share
==NULL
&& win
->m_sharedContextOf
) share
=win
->m_sharedContextOf
->GetContext();
166 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, share
);
171 //-----------------------------------------------------------------------------
172 // "map" from m_wxwindow
173 //-----------------------------------------------------------------------------
176 gtk_glwindow_map_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
178 if (win
->m_glContext
/* && win->m_exposed*/)
180 wxPaintEvent
event( win
->GetId() );
181 event
.SetEventObject( win
);
182 win
->GetEventHandler()->ProcessEvent( event
);
184 win
->m_exposed
= FALSE
;
185 win
->GetUpdateRegion().Clear();
191 //-----------------------------------------------------------------------------
192 // "expose_event" of m_wxwindow
193 //-----------------------------------------------------------------------------
196 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
199 wxapp_install_idle_handler();
201 win
->m_exposed
= TRUE
;
203 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
205 gdk_event
->area
.width
,
206 gdk_event
->area
.height
);
209 //-----------------------------------------------------------------------------
210 // "draw" of m_wxwindow
211 //-----------------------------------------------------------------------------
215 gtk_glwindow_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxGLCanvas
*win
)
218 wxapp_install_idle_handler();
220 win
->m_exposed
= TRUE
;
222 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
,
223 rect
->width
, rect
->height
);
227 //-----------------------------------------------------------------------------
228 // "size_allocate" of m_wxwindow
229 //-----------------------------------------------------------------------------
232 gtk_glcanvas_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxGLCanvas
*win
)
235 wxapp_install_idle_handler();
240 wxSizeEvent
event( wxSize(win
->m_width
,win
->m_height
), win
->GetId() );
241 event
.SetEventObject( win
);
242 win
->GetEventHandler()->ProcessEvent( event
);
245 //---------------------------------------------------------------------------
247 //---------------------------------------------------------------------------
249 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
251 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
252 EVT_SIZE(wxGLCanvas::OnSize
)
255 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
256 const wxPoint
& pos
, const wxSize
& size
,
257 long style
, const wxString
& name
,
259 const wxPalette
& palette
)
261 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
264 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
265 const wxGLContext
*shared
,
267 const wxPoint
& pos
, const wxSize
& size
,
268 long style
, const wxString
& name
,
270 const wxPalette
& palette
)
272 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
275 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
276 const wxGLCanvas
*shared
,
278 const wxPoint
& pos
, const wxSize
& size
,
279 long style
, const wxString
& name
,
281 const wxPalette
& palette
)
283 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
286 bool wxGLCanvas::Create( wxWindow
*parent
,
287 const wxGLContext
*shared
,
288 const wxGLCanvas
*shared_context_of
,
290 const wxPoint
& pos
, const wxSize
& size
,
291 long style
, const wxString
& name
,
293 const wxPalette
& palette
)
295 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
296 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
297 m_glContext
= (wxGLContext
*) NULL
;
301 m_nativeSizeEvent
= TRUE
;
303 XVisualInfo
*vi
= NULL
;
304 if (wxTheApp
->m_glVisualInfo
!= NULL
)
306 vi
= (XVisualInfo
*) wxTheApp
->m_glVisualInfo
;
307 m_canFreeVi
= FALSE
; // owned by wxTheApp - don't free upon destruction
311 vi
= (XVisualInfo
*) ChooseGLVisual(attribList
);
314 m_vi
= vi
; // save for later use
316 wxCHECK_MSG( m_vi
, FALSE
, _T("required visual couldn't be found") );
318 GdkVisual
*visual
= gdkx_visual_get( vi
->visualid
);
319 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(vi
->visualid
), TRUE
);
321 gtk_widget_push_colormap( colormap
);
322 gtk_widget_push_visual( visual
);
324 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
326 m_glWidget
= m_wxwindow
;
329 gtk_widget_set_double_buffered( m_glWidget
, FALSE
);
332 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow
), FALSE
);
334 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "realize",
335 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
337 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "map",
338 GTK_SIGNAL_FUNC(gtk_glwindow_map_callback
), (gpointer
) this );
340 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "expose_event",
341 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback
), (gpointer
)this );
344 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "draw",
345 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback
), (gpointer
)this );
348 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
349 GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback
), (gpointer
)this );
351 gtk_widget_pop_visual();
352 gtk_widget_pop_colormap();
354 if (GTK_WIDGET_REALIZED(m_wxwindow
))
355 gtk_glwindow_realized_callback( m_wxwindow
, this );
357 if (GTK_WIDGET_MAPPED(m_wxwindow
))
358 gtk_glwindow_map_callback( m_wxwindow
, this );
363 wxGLCanvas::~wxGLCanvas()
365 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
367 if (vi
&& m_canFreeVi
) XFree( vi
);
368 if (m_glContext
) delete m_glContext
;
371 void* wxGLCanvas::ChooseGLVisual(int *attribList
)
376 // default settings if attriblist = 0
378 data
[1] = GLX_DOUBLEBUFFER
;
379 data
[2] = GLX_DEPTH_SIZE
; data
[3] = 1;
380 data
[4] = GLX_RED_SIZE
; data
[5] = 1;
381 data
[6] = GLX_GREEN_SIZE
; data
[7] = 1;
382 data
[8] = GLX_BLUE_SIZE
; data
[9] = 1;
383 data
[10] = GLX_ALPHA_SIZE
; data
[11] = 0;
386 attribList
= (int*) data
;
392 while( (attribList
[arg
]!=0) && (p
<510) )
394 switch( attribList
[arg
++] )
396 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
397 case WX_GL_BUFFER_SIZE
:
398 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
400 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
401 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
402 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
403 case WX_GL_AUX_BUFFERS
:
404 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
406 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
407 case WX_GL_MIN_GREEN
:
408 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
410 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
411 case WX_GL_MIN_ALPHA
:
412 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
413 case WX_GL_DEPTH_SIZE
:
414 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
415 case WX_GL_STENCIL_SIZE
:
416 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
417 case WX_GL_MIN_ACCUM_RED
:
418 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
419 case WX_GL_MIN_ACCUM_GREEN
:
420 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
421 case WX_GL_MIN_ACCUM_BLUE
:
422 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
423 case WX_GL_MIN_ACCUM_ALPHA
:
424 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
431 attribList
= (int*) data
;
435 Display
*dpy
= GDK_DISPLAY();
437 return glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
440 void wxGLCanvas::SwapBuffers()
443 m_glContext
->SwapBuffers();
446 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
450 void wxGLCanvas::SetCurrent()
453 m_glContext
->SetCurrent();
456 void wxGLCanvas::SetColour( const wxChar
*colour
)
459 m_glContext
->SetColour( colour
);
462 void wxGLCanvas::OnInternalIdle()
464 if (m_glContext
&& m_exposed
)
466 wxPaintEvent
event( GetId() );
467 event
.SetEventObject( this );
468 GetEventHandler()->ProcessEvent( event
);
471 GetUpdateRegion().Clear();
474 wxWindow::OnInternalIdle();
479 //---------------------------------------------------------------------------
481 //---------------------------------------------------------------------------
483 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
488 XFree(m_glVisualInfo
);
491 bool wxGLApp::InitGLVisual(int *attribList
)
494 XFree(m_glVisualInfo
);
496 m_glVisualInfo
= wxGLCanvas::ChooseGLVisual(attribList
);
498 return m_glVisualInfo
!= NULL
;