]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets
4 // Uses the GLX extension.
5 // Author: Julian Smart and Wolfram Gloger
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/glcanvas.h"
23 # pragma message disable nosimpint
27 # pragma message enable nosimpint
29 #include "wx/x11/private.h"
31 // DLL options compatibility check:
33 WX_CHECK_BUILD_OPTIONS("wxGL")
35 static inline WXWindow
wxGetClientAreaWindow(wxWindow
* win
)
38 return win
->GetClientXWindow();
40 return win
->GetClientAreaWindow();
45 // workaround for bug in Mesa's glx.c
46 static int bitcount( unsigned long n
)
58 * GLContext implementation
61 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
63 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
64 const wxPalette
& WXUNUSED(palette
) )
67 // m_widget = win->m_wxwindow;
69 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
70 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
72 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
74 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
77 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
80 wxGLContext::wxGLContext(
81 bool WXUNUSED(isRGB
), wxWindow
*win
,
82 const wxPalette
& WXUNUSED(palette
),
83 const wxGLContext
*other
/* for sharing display lists */
87 // m_widget = win->m_wxwindow;
89 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
90 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
92 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
95 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
96 other
->m_glContext
, GL_TRUE
);
98 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
101 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
104 wxGLContext::~wxGLContext()
106 if (!m_glContext
) return;
108 if (m_glContext
== glXGetCurrentContext())
110 glXMakeCurrent( (Display
*) wxGetDisplay(), None
, NULL
);
113 glXDestroyContext( (Display
*) wxGetDisplay(), m_glContext
);
116 void wxGLContext::SwapBuffers()
120 Display
* display
= (Display
*) wxGetDisplay();
121 glXSwapBuffers(display
, (Window
) wxGetClientAreaWindow(m_window
));
125 void wxGLContext::SetCurrent()
129 Display
* display
= (Display
*) wxGetDisplay();
130 glXMakeCurrent(display
, (Window
) wxGetClientAreaWindow(m_window
),
135 void wxGLContext::SetColour(const wxChar
*colour
)
137 wxColour the_colour
= wxTheColourDatabase
->Find(colour
);
141 glGetBooleanv(GL_RGBA_MODE
, &b
);
144 glColor3ub(the_colour
.Red(),
151 the_colour
.AllocColour(m_window
->GetXDisplay());
153 the_colour
.CalcPixel(wxTheApp
->GetMainColormap(wxGetDisplay()));
155 GLint pix
= (GLint
)the_colour
.GetPixel();
158 wxLogError(wxT("wxGLCanvas: cannot allocate color\n"));
166 void wxGLContext::SetupPixelFormat()
170 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
174 wxPalette
wxGLContext::CreateDefaultPalette()
176 return wxNullPalette
;
183 * GLCanvas implementation
186 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
188 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
189 // EVT_SIZE(wxGLCanvas::OnSize)
193 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
194 const wxPoint
& pos
, const wxSize
& size
,
195 long style
, const wxString
& name
,
197 const wxPalette
& palette
)
198 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
200 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
203 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
204 const wxGLContext
*shared
,
206 const wxPoint
& pos
, const wxSize
& size
,
207 long style
, const wxString
& name
,
209 const wxPalette
& palette
)
210 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
212 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
215 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
216 const wxGLCanvas
*shared
,
218 const wxPoint
& pos
, const wxSize
& size
,
219 long style
, const wxString
& name
,
221 const wxPalette
& palette
)
222 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
224 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
229 bool wxGLCanvas::Create(wxWindow *parent,
230 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
231 wxWindowID id = -1, const wxPoint& pos,
232 const wxSize& size, long style,
233 const wxString& name, int *attribList, const wxPalette& palette):
234 wxScrolledWindow(parent, id, pos, size, style, name)
237 bool wxGLCanvas::Create( wxWindow
*parent
,
238 const wxGLContext
*shared
,
239 const wxGLCanvas
*shared_context_of
,
241 const wxPoint
& pos
, const wxSize
& size
,
242 long style
, const wxString
& name
,
244 const wxPalette
& palette
)
246 XVisualInfo
*vi
, vi_templ
;
247 XWindowAttributes xwa
;
250 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
251 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
252 m_glContext
= (wxGLContext
*) NULL
;
254 Display
* display
= (Display
*) wxGetDisplay();
256 // Check for the presence of the GLX extension
257 if(!glXQueryExtension(display
, NULL
, NULL
)) {
258 wxLogDebug(wxT("wxGLCanvas: GLX extension is missing\n"));
263 int data
[512], arg
=0, p
=0;
265 while( (attribList
[arg
]!=0) && (p
<512) )
267 switch( attribList
[arg
++] )
269 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
270 case WX_GL_BUFFER_SIZE
:
271 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
273 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
274 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
275 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
276 case WX_GL_AUX_BUFFERS
:
277 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
279 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
280 case WX_GL_MIN_GREEN
:
281 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
283 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
284 case WX_GL_MIN_ALPHA
:
285 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
286 case WX_GL_DEPTH_SIZE
:
287 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
288 case WX_GL_STENCIL_SIZE
:
289 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
290 case WX_GL_MIN_ACCUM_RED
:
291 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
292 case WX_GL_MIN_ACCUM_GREEN
:
293 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
294 case WX_GL_MIN_ACCUM_BLUE
:
295 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
296 case WX_GL_MIN_ACCUM_ALPHA
:
297 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
304 attribList
= (int*) data
;
305 // Get an appropriate visual
306 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
307 if(!vi
) return FALSE
;
309 // Here we should make sure that vi is the same visual as the
310 // one used by the xwindow drawable in wxCanvas. However,
311 // there is currently no mechanism for this in wx_canvs.cc.
313 // By default, we use the visual of xwindow
314 // NI: is this really senseful ? opengl in e.g. color index mode ?
315 XGetWindowAttributes(display
, (Window
)wxGetClientAreaWindow(this), &xwa
);
316 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
317 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
318 if(!vi
) return FALSE
;
319 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
320 if(!val
) return FALSE
;
321 // Basically, this is it. It should be possible to use vi
322 // in glXCreateContext() below. But this fails with Mesa.
323 // I notified the Mesa author about it; there may be a fix.
325 // Construct an attribute list matching the visual
328 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
329 a_list
[n
++] = GLX_RGBA
;
330 a_list
[n
++] = GLX_RED_SIZE
;
331 a_list
[n
++] = bitcount(vi
->red_mask
);
332 a_list
[n
++] = GLX_GREEN_SIZE
;
333 a_list
[n
++] = bitcount(vi
->green_mask
);
334 a_list
[n
++] = GLX_BLUE_SIZE
;
335 a_list
[n
++] = bitcount(vi
->blue_mask
);
336 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
337 a_list
[n
++] = GLX_ALPHA_SIZE
;
339 } else { // Color index visual
340 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
341 a_list
[n
++] = GLX_BUFFER_SIZE
;
346 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
347 if(!vi
) return FALSE
;
348 #endif /* OLD_MESA */
351 m_vi
= vi
; // safe for later use
353 wxCHECK_MSG( m_vi
, FALSE
, wxT("required visual couldn't be found") );
355 // Create the GLX context and make it current
357 wxGLContext
*share
= m_sharedContext
;
358 if (share
==NULL
&& m_sharedContextOf
)
359 share
= m_sharedContextOf
->GetContext();
361 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
371 wxGLCanvas::~wxGLCanvas(void)
373 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
376 if (m_glContext
) delete m_glContext
;
378 // Display* display = (Display*) GetXDisplay();
379 // if(glx_cx) glXDestroyContext(display, glx_cx);
382 void wxGLCanvas::SwapBuffers()
384 if( m_glContext
) m_glContext
->SwapBuffers();
386 // Display* display = (Display*) GetXDisplay();
387 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
390 void wxGLCanvas::SetCurrent()
392 if( m_glContext
) m_glContext
->SetCurrent();
394 // Display* display = (Display*) GetXDisplay();
395 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
398 void wxGLCanvas::SetColour(const wxChar
*col
)
400 if( m_glContext
) m_glContext
->SetColour(col
);