1 /////////////////////////////////////////////////////////////////////////////
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"
19 #include "wx/colour.h"
20 #include "wx/module.h"
29 #include "wx/gtk/win_gtk.h"
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
35 XVisualInfo
*g_vi
= (XVisualInfo
*) NULL
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
44 //---------------------------------------------------------------------------
46 //---------------------------------------------------------------------------
48 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
50 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
53 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
55 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
57 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
59 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
62 wxGLContext::wxGLContext(
63 bool WXUNUSED(isRGB
), wxWindow
*win
,
64 const wxPalette
& WXUNUSED(palette
),
65 const wxGLContext
*other
/* for sharing display lists */
69 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
71 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
74 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, other
->m_glContext
,
77 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
79 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
82 wxGLContext::~wxGLContext()
84 if (!m_glContext
) return;
86 if (m_glContext
== glXGetCurrentContext())
88 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
91 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
94 void wxGLContext::SwapBuffers()
98 GdkWindow
*window
= GTK_MYFIXED(m_widget
)->bin_window
;
99 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window
) );
103 void wxGLContext::SetCurrent()
107 GdkWindow
*window
= GTK_MYFIXED(m_widget
)->bin_window
;
108 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window
), m_glContext
);
112 void wxGLContext::SetColour(const char *colour
)
117 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
120 r
= (float)(col
->Red()/256.0);
121 g
= (float)(col
->Green()/256.0);
122 b
= (float)(col
->Blue()/256.0);
127 void wxGLContext::SetupPixelFormat()
131 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
135 wxPalette
wxGLContext::CreateDefaultPalette()
137 return wxNullPalette
;
140 //-----------------------------------------------------------------------------
141 // "realize" from m_wxwindow
142 //-----------------------------------------------------------------------------
145 gtk_glwindow_realized_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
147 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, win
->m_sharedContext
);
150 g_vi
= (XVisualInfo
*) NULL
;
155 //-----------------------------------------------------------------------------
156 // "expose_event" of m_wxwindow
157 //-----------------------------------------------------------------------------
160 gtk_glwindow_expose_callback( GtkWidget
*WXUNUSED(widget
), GdkEventExpose
*gdk_event
, wxGLCanvas
*win
)
163 wxapp_install_idle_handler();
165 win
->m_exposed
= TRUE
;
167 win
->GetUpdateRegion().Union( gdk_event
->area
.x
,
169 gdk_event
->area
.width
,
170 gdk_event
->area
.height
);
173 //-----------------------------------------------------------------------------
174 // "draw" of m_wxwindow
175 //-----------------------------------------------------------------------------
178 gtk_glwindow_draw_callback( GtkWidget
*WXUNUSED(widget
), GdkRectangle
*rect
, wxGLCanvas
*win
)
181 wxapp_install_idle_handler();
183 win
->m_exposed
= TRUE
;
185 win
->GetUpdateRegion().Union( rect
->x
, rect
->y
,
186 rect
->width
, rect
->height
);
189 //---------------------------------------------------------------------------
191 //---------------------------------------------------------------------------
193 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
195 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
196 EVT_SIZE(wxGLCanvas::OnSize
)
199 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
200 const wxPoint
& pos
, const wxSize
& size
,
201 long style
, const wxString
& name
,
203 const wxPalette
& palette
)
205 Create( parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
208 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
209 const wxGLContext
*shared
,
211 const wxPoint
& pos
, const wxSize
& size
,
212 long style
, const wxString
& name
,
214 const wxPalette
& palette
)
216 Create( parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
219 bool wxGLCanvas::Create( wxWindow
*parent
,
220 const wxGLContext
*shared
,
222 const wxPoint
& pos
, const wxSize
& size
,
223 long style
, const wxString
& name
,
225 const wxPalette
& palette
)
227 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
234 int data
[] = { GLX_RGBA
,
236 GLX_DEPTH_SIZE
, 1, // use largest available depth buffer
242 attribList
= (int*) data
;
246 int data
[512], arg
=0, p
=0;
248 while( (attribList
[arg
]!=0) && (p
<512) )
250 switch( attribList
[arg
++] )
252 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
253 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
254 case WX_GL_DEPTH_SIZE
:
255 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
257 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
258 case WX_GL_MIN_GREEN
:
259 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
261 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
268 attribList
= (int*) data
;
272 Display
*dpy
= GDK_DISPLAY();
274 g_vi
= glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
276 wxCHECK_MSG( g_vi
, FALSE
, "required visual couldn't be found" );
278 GdkVisual
*visual
= gdkx_visual_get( g_vi
->visualid
);
279 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(g_vi
->visualid
), TRUE
);
281 gtk_widget_push_colormap( colormap
);
282 gtk_widget_push_visual( visual
);
284 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
286 m_glWidget
= m_wxwindow
;
288 gtk_my_fixed_set_clear( GTK_MYFIXED(m_wxwindow
), FALSE
);
290 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "realize",
291 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
293 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "expose_event",
294 GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback
), (gpointer
)this );
296 gtk_signal_connect( GTK_OBJECT(m_wxwindow
), "draw",
297 GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback
), (gpointer
)this );
299 gtk_widget_pop_visual();
300 gtk_widget_pop_colormap();
305 wxGLCanvas::~wxGLCanvas()
307 if (m_glContext
) delete m_glContext
;
310 void wxGLCanvas::SwapBuffers()
312 if (m_glContext
) m_glContext
->SwapBuffers();
315 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
318 GetClientSize( &width
, &height
);
320 if (m_glContext
&& GTK_WIDGET_REALIZED(m_glWidget
) )
324 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
325 glMatrixMode(GL_PROJECTION
);
327 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
328 glMatrixMode(GL_MODELVIEW
);
332 void wxGLCanvas::SetCurrent()
334 if (m_glContext
) m_glContext
->SetCurrent();
337 void wxGLCanvas::SetColour( const char *colour
)
339 if (m_glContext
) m_glContext
->SetColour( colour
);
342 void wxGLCanvas::OnInternalIdle()
344 if (m_glContext
&& m_exposed
)
346 wxPaintEvent
event( GetId() );
347 event
.SetEventObject( this );
348 GetEventHandler()->ProcessEvent( event
);
351 GetUpdateRegion().Clear();
354 wxWindow::OnInternalIdle();