]>
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"
28 # pragma message disable nosimpint
32 # pragma message enable nosimpint
34 #include "wx/x11/private.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxGL")
40 static inline WXWindow
wxGetClientAreaWindow(wxWindow
* win
)
43 return win
->GetClientXWindow();
45 return win
->GetClientAreaWindow();
50 // workaround for bug in Mesa's glx.c
51 static int bitcount( unsigned long n
)
64 * GLContext implementation
67 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
69 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
70 const wxPalette
& WXUNUSED(palette
) )
73 // m_widget = win->m_wxwindow;
75 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
76 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
78 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
80 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
83 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
86 wxGLContext::wxGLContext(
87 bool WXUNUSED(isRGB
), wxWindow
*win
,
88 const wxPalette
& WXUNUSED(palette
),
89 const wxGLContext
*other
/* for sharing display lists */
93 // m_widget = win->m_wxwindow;
95 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
96 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
98 wxCHECK_RET( vi
, wxT("invalid visual for OpenGL") );
101 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
102 other
->m_glContext
, GL_TRUE
);
104 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
107 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGL context") );
110 wxGLContext::~wxGLContext()
112 if (!m_glContext
) return;
114 if (m_glContext
== glXGetCurrentContext())
116 glXMakeCurrent( (Display
*) wxGetDisplay(), None
, NULL
);
119 glXDestroyContext( (Display
*) wxGetDisplay(), m_glContext
);
122 void wxGLContext::SwapBuffers()
126 Display
* display
= (Display
*) wxGetDisplay();
127 glXSwapBuffers(display
, (Window
) wxGetClientAreaWindow(m_window
));
131 void wxGLContext::SetCurrent()
135 Display
* display
= (Display
*) wxGetDisplay();
136 glXMakeCurrent(display
, (Window
) wxGetClientAreaWindow(m_window
),
141 void wxGLContext::SetColour(const wxChar
*colour
)
143 wxColour the_colour
= wxTheColourDatabase
->Find(colour
);
147 glGetBooleanv(GL_RGBA_MODE
, &b
);
150 glColor3ub(the_colour
.Red(),
157 the_colour
.AllocColour(m_window
->GetXDisplay());
159 the_colour
.CalcPixel(wxTheApp
->GetMainColormap(wxGetDisplay()));
161 GLint pix
= (GLint
)the_colour
.GetPixel();
164 wxLogError(wxT("wxGLCanvas: cannot allocate color\n"));
172 void wxGLContext::SetupPixelFormat()
176 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
180 wxPalette
wxGLContext::CreateDefaultPalette()
182 return wxNullPalette
;
189 * GLCanvas implementation
192 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
194 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
195 // 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
)
204 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
206 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
209 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
210 const wxGLContext
*shared
,
212 const wxPoint
& pos
, const wxSize
& size
,
213 long style
, const wxString
& name
,
215 const wxPalette
& palette
)
216 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
218 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
221 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
222 const wxGLCanvas
*shared
,
224 const wxPoint
& pos
, const wxSize
& size
,
225 long style
, const wxString
& name
,
227 const wxPalette
& palette
)
228 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
230 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
235 bool wxGLCanvas::Create(wxWindow *parent,
236 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
237 wxWindowID id = -1, const wxPoint& pos,
238 const wxSize& size, long style,
239 const wxString& name, int *attribList, const wxPalette& palette):
240 wxScrolledWindow(parent, id, pos, size, style, name)
243 bool wxGLCanvas::Create( wxWindow
*parent
,
244 const wxGLContext
*shared
,
245 const wxGLCanvas
*shared_context_of
,
247 const wxPoint
& pos
, const wxSize
& size
,
248 long style
, const wxString
& name
,
250 const wxPalette
& palette
)
252 XVisualInfo
*vi
, vi_templ
;
253 XWindowAttributes xwa
;
256 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
257 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
258 m_glContext
= (wxGLContext
*) NULL
;
260 Display
* display
= (Display
*) wxGetDisplay();
262 // Check for the presence of the GLX extension
263 if(!glXQueryExtension(display
, NULL
, NULL
))
265 wxLogDebug(wxT("wxGLCanvas: GLX extension is missing\n"));
270 int data
[512], arg
=0, p
=0;
272 while( (attribList
[arg
]!=0) && (p
<512) )
274 switch( attribList
[arg
++] )
276 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
277 case WX_GL_BUFFER_SIZE
:
278 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
280 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
281 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
282 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
283 case WX_GL_AUX_BUFFERS
:
284 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
286 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
287 case WX_GL_MIN_GREEN
:
288 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
290 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
291 case WX_GL_MIN_ALPHA
:
292 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
293 case WX_GL_DEPTH_SIZE
:
294 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
295 case WX_GL_STENCIL_SIZE
:
296 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
297 case WX_GL_MIN_ACCUM_RED
:
298 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
299 case WX_GL_MIN_ACCUM_GREEN
:
300 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
301 case WX_GL_MIN_ACCUM_BLUE
:
302 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
303 case WX_GL_MIN_ACCUM_ALPHA
:
304 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
311 attribList
= (int*) data
;
312 // Get an appropriate visual
313 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
314 if(!vi
) return FALSE
;
316 // Here we should make sure that vi is the same visual as the
317 // one used by the xwindow drawable in wxCanvas. However,
318 // there is currently no mechanism for this in wx_canvs.cc.
320 // By default, we use the visual of xwindow
321 // NI: is this really senseful ? opengl in e.g. color index mode ?
322 XGetWindowAttributes(display
, (Window
)wxGetClientAreaWindow(this), &xwa
);
323 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
324 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
325 if(!vi
) return FALSE
;
326 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
327 if(!val
) return FALSE
;
328 // Basically, this is it. It should be possible to use vi
329 // in glXCreateContext() below. But this fails with Mesa.
330 // I notified the Mesa author about it; there may be a fix.
332 // Construct an attribute list matching the visual
335 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
336 a_list
[n
++] = GLX_RGBA
;
337 a_list
[n
++] = GLX_RED_SIZE
;
338 a_list
[n
++] = bitcount(vi
->red_mask
);
339 a_list
[n
++] = GLX_GREEN_SIZE
;
340 a_list
[n
++] = bitcount(vi
->green_mask
);
341 a_list
[n
++] = GLX_BLUE_SIZE
;
342 a_list
[n
++] = bitcount(vi
->blue_mask
);
343 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
344 a_list
[n
++] = GLX_ALPHA_SIZE
;
346 } else { // Color index visual
347 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
348 a_list
[n
++] = GLX_BUFFER_SIZE
;
353 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
354 if(!vi
) return FALSE
;
355 #endif /* OLD_MESA */
358 m_vi
= vi
; // safe for later use
360 wxCHECK_MSG( m_vi
, FALSE
, wxT("required visual couldn't be found") );
362 // Create the GLX context and make it current
364 wxGLContext
*share
= m_sharedContext
;
365 if (share
==NULL
&& m_sharedContextOf
)
366 share
= m_sharedContextOf
->GetContext();
368 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
378 wxGLCanvas::~wxGLCanvas(void)
380 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
383 if (m_glContext
) delete m_glContext
;
385 // Display* display = (Display*) GetXDisplay();
386 // if(glx_cx) glXDestroyContext(display, glx_cx);
389 void wxGLCanvas::SwapBuffers()
391 if( m_glContext
) m_glContext
->SwapBuffers();
393 // Display* display = (Display*) GetXDisplay();
394 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
397 void wxGLCanvas::SetCurrent()
399 if( m_glContext
) m_glContext
->SetCurrent();
401 // Display* display = (Display*) GetXDisplay();
402 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
405 void wxGLCanvas::SetColour(const wxChar
*col
)
407 if( m_glContext
) m_glContext
->SetColour(col
);