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 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
43 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
, const wxPalette
& WXUNUSED(palette
) )
46 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
48 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
50 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
52 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
55 wxGLContext::wxGLContext(
56 bool WXUNUSED(isRGB
), wxWindow
*win
,
57 const wxPalette
& WXUNUSED(palette
),
58 const wxGLContext
*other
/* for sharing display lists */
62 m_widget
= ((wxGLCanvas
*)win
)->m_glWidget
;
64 wxCHECK_RET( g_vi
, "invalid visual for OpenGl" );
67 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, other
->m_glContext
,
70 m_glContext
= glXCreateContext( GDK_DISPLAY(), g_vi
, None
, GL_TRUE
);
72 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
75 wxGLContext::~wxGLContext()
77 if (!m_glContext
) return;
79 if (m_glContext
== glXGetCurrentContext())
81 glXMakeCurrent( GDK_DISPLAY(), None
, NULL
);
84 glXDestroyContext( GDK_DISPLAY(), m_glContext
);
87 void wxGLContext::SwapBuffers()
91 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget
->window
) );
95 void wxGLContext::SetCurrent()
99 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget
->window
), m_glContext
);
103 void wxGLContext::SetColour(const char *colour
)
108 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
111 r
= (float)(col
->Red()/256.0);
112 g
= (float)(col
->Green()/256.0);
113 b
= (float)(col
->Blue()/256.0);
118 void wxGLContext::SetupPixelFormat()
122 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
126 wxPalette
wxGLContext::CreateDefaultPalette()
128 return wxNullPalette
;
131 //-----------------------------------------------------------------------------
132 // "realize" from m_wxwindow
133 //-----------------------------------------------------------------------------
136 gtk_glwindow_realized_callback( GtkWidget
* WXUNUSED(widget
), wxGLCanvas
*win
)
138 win
->m_glContext
= new wxGLContext( TRUE
, win
, wxNullPalette
, win
->m_glContext
);
143 //---------------------------------------------------------------------------
145 //---------------------------------------------------------------------------
147 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
149 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
150 EVT_SIZE(wxGLCanvas::OnSize
)
153 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
154 const wxPoint
& pos
, const wxSize
& size
,
155 long style
, const wxString
& name
,
157 const wxPalette
& palette
)
159 Create( parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
162 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
163 const wxGLContext
*shared
,
165 const wxPoint
& pos
, const wxSize
& size
,
166 long style
, const wxString
& name
,
168 const wxPalette
& palette
)
170 Create( parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
173 bool wxGLCanvas::Create( wxWindow
*parent
,
174 const wxGLContext
*shared
,
176 const wxPoint
& pos
, const wxSize
& size
,
177 long style
, const wxString
& name
,
179 const wxPalette
& palette
)
181 m_glContext
= (wxGLContext
*)shared
; // const_cast
185 int data
[] = { GLX_RGBA
,
187 GLX_DEPTH_SIZE
, 1, // use largest available depth buffer
193 attribList
= (int*) data
;
197 int data
[512], arg
=0, p
=0;
199 while( (attribList
[arg
]!=0) && (p
<512) )
201 switch( attribList
[arg
++] )
203 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
204 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
205 case WX_GL_DEPTH_SIZE
:
206 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
208 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
209 case WX_GL_MIN_GREEN
:
210 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
212 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
219 attribList
= (int*) data
;
223 Display
*dpy
= GDK_DISPLAY();
225 g_vi
= glXChooseVisual( dpy
, DefaultScreen(dpy
), attribList
);
227 wxCHECK_MSG( g_vi
, FALSE
, "required visual couldn't be found" );
229 GdkVisual
*visual
= gdkx_visual_get( g_vi
->visualid
);
230 GdkColormap
*colormap
= gdk_colormap_new( gdkx_visual_get(g_vi
->visualid
), TRUE
);
232 gtk_widget_push_colormap( colormap
);
233 gtk_widget_push_visual( visual
);
235 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
237 m_glWidget
= m_wxwindow
;
239 gtk_signal_connect( GTK_OBJECT(m_glWidget
), "realize",
240 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback
), (gpointer
) this );
241 gtk_widget_pop_visual();
242 gtk_widget_pop_colormap();
244 /* must be realized for OpenGl output */
245 gtk_widget_realize( m_glWidget
);
248 g_vi
= (XVisualInfo
*) NULL
;
253 wxGLCanvas::~wxGLCanvas()
255 if (m_glContext
) delete m_glContext
;
258 void wxGLCanvas::SwapBuffers()
260 if (m_glContext
) m_glContext
->SwapBuffers();
263 void wxGLCanvas::OnSize(wxSizeEvent
& WXUNUSED(event
))
266 GetClientSize( &width
, &height
);
268 if (m_glContext
&& GTK_WIDGET_REALIZED(m_glWidget
) )
272 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
273 glMatrixMode(GL_PROJECTION
);
275 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
276 glMatrixMode(GL_MODELVIEW
);
280 void wxGLCanvas::SetCurrent()
282 if (m_glContext
) m_glContext
->SetCurrent();
285 void wxGLCanvas::SetColour( const char *colour
)
287 if (m_glContext
) m_glContext
->SetColour( colour
);
290 GtkWidget
*wxGLCanvas::GetConnectWidget()
295 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow
*window
)
297 return (window
== m_wxwindow
->window
);