]> git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
Make SetLocal actually work instead of crashing immediately; due to required longevit...
[wxWidgets.git] / 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
6 // Modified by:
7 // Created: 1995, 1999
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // for compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #if defined(__BORLANDC__)
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_GLCANVAS
21
22 #include "wx/glcanvas.h"
23 #include "wx/utils.h"
24 #include "wx/app.h"
25 #include "wx/log.h"
26
27 #ifdef __VMS
28 # pragma message disable nosimpint
29 #endif
30 #include <X11/Xlib.h>
31 #ifdef __VMS
32 # pragma message enable nosimpint
33 #endif
34 #include "wx/x11/private.h"
35
36 // DLL options compatibility check:
37 #include "wx/build.h"
38 WX_CHECK_BUILD_OPTIONS("wxGL")
39
40 static inline WXWindow wxGetClientAreaWindow(wxWindow* win)
41 {
42 #ifdef __WXMOTIF__
43 return win->GetClientXWindow();
44 #else
45 return win->GetClientAreaWindow();
46 #endif
47 }
48
49 #ifdef OLD_MESA
50 // workaround for bug in Mesa's glx.c
51 static int bitcount( unsigned long n )
52 {
53 int bits;
54 for (bits=0; n>0;)
55 {
56 if(n & 1) bits++;
57 n = n >> 1;
58 }
59 return bits;
60 }
61 #endif
62
63 /*
64 * GLContext implementation
65 */
66
67 IMPLEMENT_CLASS(wxGLContext,wxObject)
68
69 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win,
70 const wxPalette& WXUNUSED(palette) )
71 {
72 m_window = win;
73 // m_widget = win->m_wxwindow;
74
75 wxGLCanvas *gc = (wxGLCanvas*) win;
76 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
77
78 wxCHECK_RET( vi, wxT("invalid visual for OpenGL") );
79
80 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
81 None, GL_TRUE);
82
83 wxCHECK_RET( m_glContext, wxT("Couldn't create OpenGL context") );
84 }
85
86 wxGLContext::wxGLContext(
87 bool WXUNUSED(isRGB), wxWindow *win,
88 const wxPalette& WXUNUSED(palette),
89 const wxGLContext *other /* for sharing display lists */
90 )
91 {
92 m_window = win;
93 // m_widget = win->m_wxwindow;
94
95 wxGLCanvas *gc = (wxGLCanvas*) win;
96 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
97
98 wxCHECK_RET( vi, wxT("invalid visual for OpenGL") );
99
100 if( other != 0 )
101 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
102 other->m_glContext, GL_TRUE );
103 else
104 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
105 None, GL_TRUE );
106
107 wxCHECK_RET( m_glContext, wxT("Couldn't create OpenGL context") );
108 }
109
110 wxGLContext::~wxGLContext()
111 {
112 if (!m_glContext) return;
113
114 if (m_glContext == glXGetCurrentContext())
115 {
116 glXMakeCurrent( (Display*) wxGetDisplay(), None, NULL);
117 }
118
119 glXDestroyContext( (Display*) wxGetDisplay(), m_glContext );
120 }
121
122 void wxGLContext::SwapBuffers()
123 {
124 if (m_glContext)
125 {
126 Display* display = (Display*) wxGetDisplay();
127 glXSwapBuffers(display, (Window) wxGetClientAreaWindow(m_window));
128 }
129 }
130
131 void wxGLContext::SetCurrent()
132 {
133 if (m_glContext)
134 {
135 Display* display = (Display*) wxGetDisplay();
136 glXMakeCurrent(display, (Window) wxGetClientAreaWindow(m_window),
137 m_glContext );
138 }
139 }
140
141 void wxGLContext::SetColour(const wxChar *colour)
142 {
143 wxColour the_colour = wxTheColourDatabase->Find(colour);
144 if(the_colour.Ok())
145 {
146 GLboolean b;
147 glGetBooleanv(GL_RGBA_MODE, &b);
148 if(b)
149 {
150 glColor3ub(the_colour.Red(),
151 the_colour.Green(),
152 the_colour.Blue());
153 }
154 else
155 {
156 #ifdef __WXMOTIF__
157 the_colour.AllocColour(m_window->GetXDisplay());
158 #else
159 the_colour.CalcPixel(wxTheApp->GetMainColormap(wxGetDisplay()));
160 #endif
161 GLint pix = (GLint)the_colour.GetPixel();
162 if(pix == -1)
163 {
164 wxLogError(wxT("wxGLCanvas: cannot allocate color\n"));
165 return;
166 }
167 glIndexi(pix);
168 }
169 }
170 }
171
172 void wxGLContext::SetupPixelFormat()
173 {
174 }
175
176 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
177 {
178 }
179
180 wxPalette wxGLContext::CreateDefaultPalette()
181 {
182 return wxNullPalette;
183 }
184
185
186
187
188 /*
189 * GLCanvas implementation
190 */
191
192 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
193
194 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
195 // EVT_SIZE(wxGLCanvas::OnSize)
196 END_EVENT_TABLE()
197
198
199 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
200 const wxPoint& pos, const wxSize& size,
201 long style, const wxString& name,
202 int *attribList,
203 const wxPalette& palette )
204 : wxScrolledWindow(parent, id, pos, size, style, name)
205 {
206 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
207 }
208
209 wxGLCanvas::wxGLCanvas( wxWindow *parent,
210 const wxGLContext *shared,
211 wxWindowID id,
212 const wxPoint& pos, const wxSize& size,
213 long style, const wxString& name,
214 int *attribList,
215 const wxPalette& palette )
216 : wxScrolledWindow(parent, id, pos, size, style, name)
217 {
218 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
219 }
220
221 wxGLCanvas::wxGLCanvas( wxWindow *parent,
222 const wxGLCanvas *shared,
223 wxWindowID id,
224 const wxPoint& pos, const wxSize& size,
225 long style, const wxString& name,
226 int *attribList,
227 const wxPalette& palette )
228 : wxScrolledWindow(parent, id, pos, size, style, name)
229 {
230 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
231 }
232
233
234 /*
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)
241 */
242
243 bool wxGLCanvas::Create( wxWindow *parent,
244 const wxGLContext *shared,
245 const wxGLCanvas *shared_context_of,
246 wxWindowID id,
247 const wxPoint& pos, const wxSize& size,
248 long style, const wxString& name,
249 int *attribList,
250 const wxPalette& palette)
251 {
252 XVisualInfo *vi, vi_templ;
253 XWindowAttributes xwa;
254 int val, n;
255
256 m_sharedContext = (wxGLContext*)shared; // const_cast
257 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
258 m_glContext = (wxGLContext*) NULL;
259
260 Display* display = (Display*) wxGetDisplay();
261
262 // Check for the presence of the GLX extension
263 if(!glXQueryExtension(display, NULL, NULL))
264 {
265 wxLogDebug(wxT("wxGLCanvas: GLX extension is missing\n"));
266 return false;
267 }
268
269 if(attribList) {
270 int data[512], arg=0, p=0;
271
272 while( (attribList[arg]!=0) && (p<512) )
273 {
274 switch( attribList[arg++] )
275 {
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;
279 case WX_GL_LEVEL:
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;
285 case WX_GL_MIN_RED:
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;
289 case WX_GL_MIN_BLUE:
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;
305 default:
306 break;
307 }
308 }
309 data[p] = 0;
310
311 attribList = (int*) data;
312 // Get an appropriate visual
313 vi = glXChooseVisual(display, DefaultScreen(display), attribList);
314 if(!vi) return FALSE;
315
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.
319 } else {
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.
331 #ifdef OLD_MESA
332 // Construct an attribute list matching the visual
333 int a_list[32];
334 n = 0;
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;
345 a_list[n++] = val;
346 } else { // Color index visual
347 glXGetConfig(display, vi, GLX_BUFFER_SIZE, &val);
348 a_list[n++] = GLX_BUFFER_SIZE;
349 a_list[n++] = val;
350 }
351 a_list[n] = None;
352 // XFree(vi);
353 vi = glXChooseVisual(display, DefaultScreen(display), a_list);
354 if(!vi) return FALSE;
355 #endif /* OLD_MESA */
356 }
357
358 m_vi = vi; // safe for later use
359
360 wxCHECK_MSG( m_vi, FALSE, wxT("required visual couldn't be found") );
361
362 // Create the GLX context and make it current
363
364 wxGLContext *share= m_sharedContext;
365 if (share==NULL && m_sharedContextOf)
366 share = m_sharedContextOf->GetContext();
367
368 m_glContext = new wxGLContext( TRUE, this, wxNullPalette, share );
369
370 #ifndef OLD_MESA
371 // XFree(vi);
372 #endif
373 SetCurrent();
374
375 return true;
376 }
377
378 wxGLCanvas::~wxGLCanvas(void)
379 {
380 XVisualInfo *vi = (XVisualInfo *) m_vi;
381
382 if (vi) XFree( vi );
383 if (m_glContext) delete m_glContext;
384
385 // Display* display = (Display*) GetXDisplay();
386 // if(glx_cx) glXDestroyContext(display, glx_cx);
387 }
388
389 void wxGLCanvas::SwapBuffers()
390 {
391 if( m_glContext ) m_glContext->SwapBuffers();
392
393 // Display* display = (Display*) GetXDisplay();
394 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
395 }
396
397 void wxGLCanvas::SetCurrent()
398 {
399 if( m_glContext ) m_glContext->SetCurrent();
400
401 // Display* display = (Display*) GetXDisplay();
402 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
403 }
404
405 void wxGLCanvas::SetColour(const wxChar *col)
406 {
407 if( m_glContext ) m_glContext->SetColour(col);
408 }
409
410 #endif
411 // wxUSE_GLCANVAS