]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows 2.0 for Motif.
4 // Uses the GLX extension.
5 // Author: Julian Smart and Wolfram Gloger
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "glcanvas.h"
21 // #error Sorry, wxGLCanvas does not work yet with wxX11
23 #include "wx/glcanvas.h"
29 # pragma message disable nosimpint
33 # pragma message enable nosimpint
35 #include "wx/motif/private.h"
38 // workaround for bug in Mesa's glx.c
39 static int bitcount( unsigned long n
)
51 * GLContext implementation
54 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
56 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
57 const wxPalette
& WXUNUSED(palette
) )
60 // m_widget = win->m_wxwindow;
62 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
63 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
65 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
67 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
70 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
73 wxGLContext::wxGLContext(
74 bool WXUNUSED(isRGB
), wxWindow
*win
,
75 const wxPalette
& WXUNUSED(palette
),
76 const wxGLContext
*other
/* for sharing display lists */
80 // m_widget = win->m_wxwindow;
82 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
83 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
85 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
88 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
89 other
->m_glContext
, GL_TRUE
);
91 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(), vi
,
94 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
97 wxGLContext::~wxGLContext()
99 if (!m_glContext
) return;
101 if (m_glContext
== glXGetCurrentContext())
103 glXMakeCurrent( (Display
*) wxGetDisplay(), None
, NULL
);
106 glXDestroyContext( (Display
*) wxGetDisplay(), m_glContext
);
109 void wxGLContext::SwapBuffers()
113 Display
* display
= (Display
*) wxGetDisplay();
114 glXSwapBuffers(display
, (Window
) m_window
->GetClientAreaWindow());
118 void wxGLContext::SetCurrent()
122 Display
* display
= (Display
*) wxGetDisplay();
123 glXMakeCurrent(display
, (Window
) m_window
->GetClientAreaWindow(),
128 void wxGLContext::SetColour(const char *colour
)
130 wxColour
*the_colour
= wxTheColourDatabase
->FindColour(colour
);
133 glGetBooleanv(GL_RGBA_MODE
, &b
);
135 glColor3ub(the_colour
->Red(),
139 the_colour
->CalcPixel(wxTheApp
->GetMainColormap(wxGetDisplay()));
140 GLint pix
= (GLint
)the_colour
->GetPixel();
143 wxLogError("wxGLCanvas: cannot allocate color\n");
151 void wxGLContext::SetupPixelFormat()
155 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
159 wxPalette
wxGLContext::CreateDefaultPalette()
161 return wxNullPalette
;
168 * GLCanvas implementation
171 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
173 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
174 // EVT_SIZE(wxGLCanvas::OnSize)
178 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
179 const wxPoint
& pos
, const wxSize
& size
,
180 long style
, const wxString
& name
,
182 const wxPalette
& palette
)
183 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
185 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
188 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
189 const wxGLContext
*shared
,
191 const wxPoint
& pos
, const wxSize
& size
,
192 long style
, const wxString
& name
,
194 const wxPalette
& palette
)
195 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
197 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
200 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
201 const wxGLCanvas
*shared
,
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
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
214 bool wxGLCanvas::Create(wxWindow *parent,
215 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
216 wxWindowID id = -1, const wxPoint& pos,
217 const wxSize& size, long style,
218 const wxString& name, int *attribList, const wxPalette& palette):
219 wxScrolledWindow(parent, id, pos, size, style, name)
222 bool wxGLCanvas::Create( wxWindow
*parent
,
223 const wxGLContext
*shared
,
224 const wxGLCanvas
*shared_context_of
,
226 const wxPoint
& pos
, const wxSize
& size
,
227 long style
, const wxString
& name
,
229 const wxPalette
& palette
)
231 XVisualInfo
*vi
, vi_templ
;
232 XWindowAttributes xwa
;
235 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
236 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
237 m_glContext
= (wxGLContext
*) NULL
;
239 Display
* display
= (Display
*) wxGetDisplay();
241 // Check for the presence of the GLX extension
242 if(!glXQueryExtension(display
, NULL
, NULL
)) {
243 wxLogDebug("wxGLCanvas: GLX extension is missing\n");
248 int data
[512], arg
=0, p
=0;
250 while( (attribList
[arg
]!=0) && (p
<512) )
252 switch( attribList
[arg
++] )
254 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
255 case WX_GL_BUFFER_SIZE
:
256 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
258 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
259 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
260 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
261 case WX_GL_AUX_BUFFERS
:
262 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
264 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
265 case WX_GL_MIN_GREEN
:
266 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
268 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
269 case WX_GL_MIN_ALPHA
:
270 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
271 case WX_GL_DEPTH_SIZE
:
272 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
273 case WX_GL_STENCIL_SIZE
:
274 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
275 case WX_GL_MIN_ACCUM_RED
:
276 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
277 case WX_GL_MIN_ACCUM_GREEN
:
278 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
279 case WX_GL_MIN_ACCUM_BLUE
:
280 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
281 case WX_GL_MIN_ACCUM_ALPHA
:
282 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
289 attribList
= (int*) data
;
290 // Get an appropriate visual
291 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
292 if(!vi
) return FALSE
;
294 // Here we should make sure that vi is the same visual as the
295 // one used by the xwindow drawable in wxCanvas. However,
296 // there is currently no mechanism for this in wx_canvs.cc.
298 // By default, we use the visual of xwindow
299 // NI: is this really senseful ? opengl in e.g. color index mode ?
300 XGetWindowAttributes(display
, (Window
) GetClientAreaWindow(), &xwa
);
301 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
302 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
303 if(!vi
) return FALSE
;
304 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
305 if(!val
) return FALSE
;
306 // Basically, this is it. It should be possible to use vi
307 // in glXCreateContext() below. But this fails with Mesa.
308 // I notified the Mesa author about it; there may be a fix.
310 // Construct an attribute list matching the visual
313 if(vi
->c_class
==TrueColor
|| vi
->c_class
==DirectColor
) { // RGBA visual
314 a_list
[n
++] = GLX_RGBA
;
315 a_list
[n
++] = GLX_RED_SIZE
;
316 a_list
[n
++] = bitcount(vi
->red_mask
);
317 a_list
[n
++] = GLX_GREEN_SIZE
;
318 a_list
[n
++] = bitcount(vi
->green_mask
);
319 a_list
[n
++] = GLX_BLUE_SIZE
;
320 a_list
[n
++] = bitcount(vi
->blue_mask
);
321 glXGetConfig(display
, vi
, GLX_ALPHA_SIZE
, &val
);
322 a_list
[n
++] = GLX_ALPHA_SIZE
;
324 } else { // Color index visual
325 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
326 a_list
[n
++] = GLX_BUFFER_SIZE
;
331 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
332 if(!vi
) return FALSE
;
333 #endif /* OLD_MESA */
336 m_vi
= vi
; // safe for later use
338 wxCHECK_MSG( m_vi
, FALSE
, "required visual couldn't be found" );
340 // Create the GLX context and make it current
342 wxGLContext
*share
= m_sharedContext
;
343 if (share
==NULL
&& m_sharedContextOf
)
344 share
= m_sharedContextOf
->GetContext();
346 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
356 wxGLCanvas::~wxGLCanvas(void)
358 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
361 if (m_glContext
) delete m_glContext
;
363 // Display* display = (Display*) GetXDisplay();
364 // if(glx_cx) glXDestroyContext(display, glx_cx);
367 void wxGLCanvas::SwapBuffers()
369 if( m_glContext
) m_glContext
->SwapBuffers();
371 // Display* display = (Display*) GetXDisplay();
372 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
375 void wxGLCanvas::SetCurrent()
377 if( m_glContext
) m_glContext
->SetCurrent();
379 // Display* display = (Display*) GetXDisplay();
380 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
383 void wxGLCanvas::SetColour(const char *col
)
385 if( m_glContext
) m_glContext
->SetColour(col
);