]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/glcanvas.cpp
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 /////////////////////////////////////////////////////////////////////////////
13 // for compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
16 #if defined(__BORLANDC__)
22 #include "wx/glcanvas.h"
31 # pragma message disable nosimpint
35 # pragma message enable nosimpint
37 #include "wx/x11/private.h"
39 // DLL options compatibility check:
41 WX_CHECK_BUILD_OPTIONS("wxGL")
43 static inline WXWindow
wxGetClientAreaWindow(wxWindow
* win
)
46 return win
->GetClientXWindow();
48 return win
->GetClientAreaWindow();
53 // workaround for bug in Mesa's glx.c
54 static int bitcount( unsigned long n
)
67 * GLContext implementation
70 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
72 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
73 const wxPalette
& WXUNUSED(palette
) )
76 // m_widget = win->m_wxwindow;
78 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
79 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
81 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
83 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
86 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
89 wxGLContext::wxGLContext(
90 bool WXUNUSED(isRGB
), wxWindow
*win
,
91 const wxPalette
& WXUNUSED(palette
),
92 const wxGLContext
*other
/* for sharing display lists */
96 // m_widget = win->m_wxwindow;
98 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
99 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
101 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
104 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
105 other
->m_glContext
, GL_TRUE
);
107 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
110 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
113 wxGLContext::~wxGLContext()
115 if (!m_glContext
) return;
117 if (m_glContext
== glXGetCurrentContext())
119 glXMakeCurrent( (Display
*) wxGetDisplay(), None
, NULL
);
122 glXDestroyContext( (Display
*) wxGetDisplay(), m_glContext
);
125 void wxGLContext::SwapBuffers()
129 Display
* display
= (Display
*) wxGetDisplay();
130 glXSwapBuffers(display
, (Window
) wxGetClientAreaWindow(m_window
));
134 void wxGLContext::SetCurrent()
138 Display
* display
= (Display
*) wxGetDisplay();
139 glXMakeCurrent(display
, (Window
) wxGetClientAreaWindow(m_window
),
144 void wxGLContext::SetColour(const wxChar
*colour
)
146 wxColour the_colour
= wxTheColourDatabase
->Find(colour
);
150 glGetBooleanv(GL_RGBA_MODE
, &b
);
153 glColor3ub(the_colour
.Red(),
160 the_colour
.AllocColour(m_window
->GetXDisplay());
162 the_colour
.CalcPixel(wxTheApp
->GetMainColormap(wxGetDisplay()));
164 GLint pix
= (GLint
)the_colour
.GetPixel();
167 wxLogError(wxT("wxGLCanvas: cannot allocate color\n"));
175 void wxGLContext::SetupPixelFormat()
179 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
183 wxPalette
wxGLContext::CreateDefaultPalette()
185 return wxNullPalette
;
192 * GLCanvas implementation
195 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
197 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
198 // EVT_SIZE(wxGLCanvas::OnSize)
202 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
203 const wxPoint
& pos
, const wxSize
& size
,
204 long style
, const wxString
& name
,
206 const wxPalette
& palette
)
207 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
209 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
212 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
213 const wxGLContext
*shared
,
215 const wxPoint
& pos
, const wxSize
& size
,
216 long style
, const wxString
& name
,
218 const wxPalette
& palette
)
219 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
221 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
224 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
225 const wxGLCanvas
*shared
,
227 const wxPoint
& pos
, const wxSize
& size
,
228 long style
, const wxString
& name
,
230 const wxPalette
& palette
)
231 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
233 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
238 bool wxGLCanvas::Create(wxWindow *parent,
239 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
240 wxWindowID id = -1, const wxPoint& pos,
241 const wxSize& size, long style,
242 const wxString& name, int *attribList, const wxPalette& palette):
243 wxScrolledWindow(parent, id, pos, size, style, name)
246 bool wxGLCanvas::Create( wxWindow
*parent
,
247 const wxGLContext
*shared
,
248 const wxGLCanvas
*shared_context_of
,
250 const wxPoint
& pos
, const wxSize
& size
,
251 long style
, const wxString
& name
,
253 const wxPalette
& palette
)
255 XVisualInfo
*vi
, vi_templ
;
256 XWindowAttributes xwa
;
259 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
260 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
261 m_glContext
= (wxGLContext
*) NULL
;
263 Display
* display
= (Display
*) wxGetDisplay();
265 // Check for the presence of the GLX extension
266 if(!glXQueryExtension(display
, NULL
, NULL
))
268 wxLogDebug(wxT("wxGLCanvas: GLX extension is missing\n"));
273 int data
[512], arg
=0, p
=0;
275 while( (attribList
[arg
]!=0) && (p
<512) )
277 switch( attribList
[arg
++] )
279 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
280 case WX_GL_BUFFER_SIZE
:
281 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
283 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
284 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
285 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
286 case WX_GL_AUX_BUFFERS
:
287 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
289 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
290 case WX_GL_MIN_GREEN
:
291 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
293 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
294 case WX_GL_MIN_ALPHA
:
295 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
296 case WX_GL_DEPTH_SIZE
:
297 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
298 case WX_GL_STENCIL_SIZE
:
299 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
300 case WX_GL_MIN_ACCUM_RED
:
301 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
302 case WX_GL_MIN_ACCUM_GREEN
:
303 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
304 case WX_GL_MIN_ACCUM_BLUE
:
305 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
306 case WX_GL_MIN_ACCUM_ALPHA
:
307 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
314 attribList
= (int*) data
;
315 // Get an appropriate visual
316 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
317 if(!vi
) return false;
319 // Here we should make sure that vi is the same visual as the
320 // one used by the xwindow drawable in wxCanvas. However,
321 // there is currently no mechanism for this in wx_canvs.cc.
323 // By default, we use the visual of xwindow
324 // NI: is this really senseful ? opengl in e.g. color index mode ?
325 XGetWindowAttributes(display
, (Window
)wxGetClientAreaWindow(this), &xwa
);
326 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
327 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
328 if(!vi
) return false;
329 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
330 if(!val
) return false;
331 // Basically, this is it. It should be possible to use vi
332 // in glXCreateContext() below. But this fails with Mesa.
333 // I notified the Mesa author about it; there may be a fix.
335 // Construct an attribute list matching the visual
338 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
339 a_list
[n
++] = GLX_RGBA
;
340 a_list
[n
++] = GLX_RED_SIZE
;
341 a_list
[n
++] = bitcount(vi
->red_mask
);
342 a_list
[n
++] = GLX_GREEN_SIZE
;
343 a_list
[n
++] = bitcount(vi
->green_mask
);
344 a_list
[n
++] = GLX_BLUE_SIZE
;
345 a_list
[n
++] = bitcount(vi
->blue_mask
);
346 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
347 a_list
[n
++] = GLX_ALPHA_SIZE
;
349 } else { // Color index visual
350 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
351 a_list
[n
++] = GLX_BUFFER_SIZE
;
356 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
357 if(!vi
) return false;
358 #endif /* OLD_MESA */
361 m_vi
= vi
; // safe for later use
363 wxCHECK_MSG( m_vi
, false, wxT("required visual couldn't be found") );
365 // Create the GLX context and make it current
367 wxGLContext
*share
= m_sharedContext
;
368 if (share
==NULL
&& m_sharedContextOf
)
369 share
= m_sharedContextOf
->GetContext();
371 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
381 wxGLCanvas::~wxGLCanvas(void)
383 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
386 if (m_glContext
) delete m_glContext
;
388 // Display* display = (Display*) GetXDisplay();
389 // if(glx_cx) glXDestroyContext(display, glx_cx);
392 void wxGLCanvas::SwapBuffers()
394 if( m_glContext
) m_glContext
->SwapBuffers();
396 // Display* display = (Display*) GetXDisplay();
397 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
400 void wxGLCanvas::SetCurrent()
402 if( m_glContext
) m_glContext
->SetCurrent();
404 // Display* display = (Display*) GetXDisplay();
405 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
408 void wxGLCanvas::SetColour(const wxChar
*col
)
410 if( m_glContext
) m_glContext
->SetColour(col
);