]> git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
Applied patch [ 774886 ] wxnotebook bug
[wxWidgets.git] / src / x11 / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows
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 #ifdef __GNUG__
14 #pragma implementation "glcanvas.h"
15 #endif
16
17 #include "wx/setup.h"
18
19 #if wxUSE_GLCANVAS
20
21 #include "wx/glcanvas.h"
22 #include "wx/utils.h"
23 #include "wx/app.h"
24 #include "wx/log.h"
25
26 #ifdef __VMS
27 # pragma message disable nosimpint
28 #endif
29 #include <X11/Xlib.h>
30 #ifdef __VMS
31 # pragma message enable nosimpint
32 #endif
33 #include "wx/x11/private.h"
34
35 static inline WXWindow wxGetClientAreaWindow(wxWindow* win)
36 {
37 #ifdef __WXMOTIF__
38 return win->GetClientXWindow();
39 #else
40 return win->GetClientAreaWindow();
41 #endif
42 }
43
44 #ifdef OLD_MESA
45 // workaround for bug in Mesa's glx.c
46 static int bitcount( unsigned long n )
47 {
48 int bits;
49 for (bits=0; n>0;) {
50 if(n & 1) bits++;
51 n = n >> 1;
52 }
53 return bits;
54 }
55 #endif
56
57 /*
58 * GLContext implementation
59 */
60
61 IMPLEMENT_CLASS(wxGLContext,wxObject)
62
63 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win,
64 const wxPalette& WXUNUSED(palette) )
65 {
66 m_window = win;
67 // m_widget = win->m_wxwindow;
68
69 wxGLCanvas *gc = (wxGLCanvas*) win;
70 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
71
72 wxCHECK_RET( vi, "invalid visual for OpenGl" );
73
74 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
75 None, GL_TRUE);
76
77 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
78 }
79
80 wxGLContext::wxGLContext(
81 bool WXUNUSED(isRGB), wxWindow *win,
82 const wxPalette& WXUNUSED(palette),
83 const wxGLContext *other /* for sharing display lists */
84 )
85 {
86 m_window = win;
87 // m_widget = win->m_wxwindow;
88
89 wxGLCanvas *gc = (wxGLCanvas*) win;
90 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
91
92 wxCHECK_RET( vi, "invalid visual for OpenGl" );
93
94 if( other != 0 )
95 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
96 other->m_glContext, GL_TRUE );
97 else
98 m_glContext = glXCreateContext( (Display *)wxGetDisplay(), vi,
99 None, GL_TRUE );
100
101 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
102 }
103
104 wxGLContext::~wxGLContext()
105 {
106 if (!m_glContext) return;
107
108 if (m_glContext == glXGetCurrentContext())
109 {
110 glXMakeCurrent( (Display*) wxGetDisplay(), None, NULL);
111 }
112
113 glXDestroyContext( (Display*) wxGetDisplay(), m_glContext );
114 }
115
116 void wxGLContext::SwapBuffers()
117 {
118 if (m_glContext)
119 {
120 Display* display = (Display*) wxGetDisplay();
121 glXSwapBuffers(display, (Window) wxGetClientAreaWindow(m_window));
122 }
123 }
124
125 void wxGLContext::SetCurrent()
126 {
127 if (m_glContext)
128 {
129 Display* display = (Display*) wxGetDisplay();
130 glXMakeCurrent(display, (Window) wxGetClientAreaWindow(m_window),
131 m_glContext );;
132 }
133 }
134
135 void wxGLContext::SetColour(const char *colour)
136 {
137 wxColour *the_colour = wxTheColourDatabase->FindColour(colour);
138 if(the_colour) {
139 GLboolean b;
140 glGetBooleanv(GL_RGBA_MODE, &b);
141 if(b) {
142 glColor3ub(the_colour->Red(),
143 the_colour->Green(),
144 the_colour->Blue());
145 } else {
146 #ifdef __WXMOTIF__
147 the_colour->AllocColour(m_window->GetXDisplay());
148 #else
149 the_colour->CalcPixel(wxTheApp->GetMainColormap(wxGetDisplay()));
150 #endif
151 GLint pix = (GLint)the_colour->GetPixel();
152 if(pix == -1)
153 {
154 wxLogError("wxGLCanvas: cannot allocate color\n");
155 return;
156 }
157 glIndexi(pix);
158 }
159 }
160 }
161
162 void wxGLContext::SetupPixelFormat()
163 {
164 }
165
166 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
167 {
168 }
169
170 wxPalette wxGLContext::CreateDefaultPalette()
171 {
172 return wxNullPalette;
173 }
174
175
176
177
178 /*
179 * GLCanvas implementation
180 */
181
182 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
183
184 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
185 // EVT_SIZE(wxGLCanvas::OnSize)
186 END_EVENT_TABLE()
187
188
189 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
190 const wxPoint& pos, const wxSize& size,
191 long style, const wxString& name,
192 int *attribList,
193 const wxPalette& palette )
194 : wxScrolledWindow(parent, id, pos, size, style, name)
195 {
196 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
197 }
198
199 wxGLCanvas::wxGLCanvas( wxWindow *parent,
200 const wxGLContext *shared,
201 wxWindowID id,
202 const wxPoint& pos, const wxSize& size,
203 long style, const wxString& name,
204 int *attribList,
205 const wxPalette& palette )
206 : wxScrolledWindow(parent, id, pos, size, style, name)
207 {
208 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
209 }
210
211 wxGLCanvas::wxGLCanvas( wxWindow *parent,
212 const wxGLCanvas *shared,
213 wxWindowID id,
214 const wxPoint& pos, const wxSize& size,
215 long style, const wxString& name,
216 int *attribList,
217 const wxPalette& palette )
218 : wxScrolledWindow(parent, id, pos, size, style, name)
219 {
220 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
221 }
222
223
224 /*
225 bool wxGLCanvas::Create(wxWindow *parent,
226 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
227 wxWindowID id = -1, const wxPoint& pos,
228 const wxSize& size, long style,
229 const wxString& name, int *attribList, const wxPalette& palette):
230 wxScrolledWindow(parent, id, pos, size, style, name)
231 */
232
233 bool wxGLCanvas::Create( wxWindow *parent,
234 const wxGLContext *shared,
235 const wxGLCanvas *shared_context_of,
236 wxWindowID id,
237 const wxPoint& pos, const wxSize& size,
238 long style, const wxString& name,
239 int *attribList,
240 const wxPalette& palette)
241 {
242 XVisualInfo *vi, vi_templ;
243 XWindowAttributes xwa;
244 int val, n;
245
246 m_sharedContext = (wxGLContext*)shared; // const_cast
247 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
248 m_glContext = (wxGLContext*) NULL;
249
250 Display* display = (Display*) wxGetDisplay();
251
252 // Check for the presence of the GLX extension
253 if(!glXQueryExtension(display, NULL, NULL)) {
254 wxLogDebug("wxGLCanvas: GLX extension is missing\n");
255 return FALSE;
256 }
257
258 if(attribList) {
259 int data[512], arg=0, p=0;
260
261 while( (attribList[arg]!=0) && (p<512) )
262 {
263 switch( attribList[arg++] )
264 {
265 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
266 case WX_GL_BUFFER_SIZE:
267 data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
268 case WX_GL_LEVEL:
269 data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break;
270 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
271 case WX_GL_STEREO: data[p++] = GLX_STEREO; break;
272 case WX_GL_AUX_BUFFERS:
273 data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
274 case WX_GL_MIN_RED:
275 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
276 case WX_GL_MIN_GREEN:
277 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
278 case WX_GL_MIN_BLUE:
279 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
280 case WX_GL_MIN_ALPHA:
281 data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
282 case WX_GL_DEPTH_SIZE:
283 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
284 case WX_GL_STENCIL_SIZE:
285 data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
286 case WX_GL_MIN_ACCUM_RED:
287 data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
288 case WX_GL_MIN_ACCUM_GREEN:
289 data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
290 case WX_GL_MIN_ACCUM_BLUE:
291 data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
292 case WX_GL_MIN_ACCUM_ALPHA:
293 data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
294 default:
295 break;
296 }
297 }
298 data[p] = 0;
299
300 attribList = (int*) data;
301 // Get an appropriate visual
302 vi = glXChooseVisual(display, DefaultScreen(display), attribList);
303 if(!vi) return FALSE;
304
305 // Here we should make sure that vi is the same visual as the
306 // one used by the xwindow drawable in wxCanvas. However,
307 // there is currently no mechanism for this in wx_canvs.cc.
308 } else {
309 // By default, we use the visual of xwindow
310 // NI: is this really senseful ? opengl in e.g. color index mode ?
311 XGetWindowAttributes(display, (Window)wxGetClientAreaWindow(this), &xwa);
312 vi_templ.visualid = XVisualIDFromVisual(xwa.visual);
313 vi = XGetVisualInfo(display, VisualIDMask, &vi_templ, &n);
314 if(!vi) return FALSE;
315 glXGetConfig(display, vi, GLX_USE_GL, &val);
316 if(!val) return FALSE;
317 // Basically, this is it. It should be possible to use vi
318 // in glXCreateContext() below. But this fails with Mesa.
319 // I notified the Mesa author about it; there may be a fix.
320 #ifdef OLD_MESA
321 // Construct an attribute list matching the visual
322 int a_list[32];
323 n = 0;
324 if(vi->c_class==TrueColor || vi->c_class==DirectColor) { // RGBA visual
325 a_list[n++] = GLX_RGBA;
326 a_list[n++] = GLX_RED_SIZE;
327 a_list[n++] = bitcount(vi->red_mask);
328 a_list[n++] = GLX_GREEN_SIZE;
329 a_list[n++] = bitcount(vi->green_mask);
330 a_list[n++] = GLX_BLUE_SIZE;
331 a_list[n++] = bitcount(vi->blue_mask);
332 glXGetConfig(display, vi, GLX_ALPHA_SIZE, &val);
333 a_list[n++] = GLX_ALPHA_SIZE;
334 a_list[n++] = val;
335 } else { // Color index visual
336 glXGetConfig(display, vi, GLX_BUFFER_SIZE, &val);
337 a_list[n++] = GLX_BUFFER_SIZE;
338 a_list[n++] = val;
339 }
340 a_list[n] = None;
341 // XFree(vi);
342 vi = glXChooseVisual(display, DefaultScreen(display), a_list);
343 if(!vi) return FALSE;
344 #endif /* OLD_MESA */
345 }
346
347 m_vi = vi; // safe for later use
348
349 wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" );
350
351 // Create the GLX context and make it current
352
353 wxGLContext *share= m_sharedContext;
354 if (share==NULL && m_sharedContextOf)
355 share = m_sharedContextOf->GetContext();
356
357 m_glContext = new wxGLContext( TRUE, this, wxNullPalette, share );
358
359 #ifndef OLD_MESA
360 // XFree(vi);
361 #endif
362 SetCurrent();
363
364 return TRUE;
365 }
366
367 wxGLCanvas::~wxGLCanvas(void)
368 {
369 XVisualInfo *vi = (XVisualInfo *) m_vi;
370
371 if (vi) XFree( vi );
372 if (m_glContext) delete m_glContext;
373
374 // Display* display = (Display*) GetXDisplay();
375 // if(glx_cx) glXDestroyContext(display, glx_cx);
376 }
377
378 void wxGLCanvas::SwapBuffers()
379 {
380 if( m_glContext ) m_glContext->SwapBuffers();
381
382 // Display* display = (Display*) GetXDisplay();
383 // if(glx_cx) glXSwapBuffers(display, (Window) GetClientAreaWindow());
384 }
385
386 void wxGLCanvas::SetCurrent()
387 {
388 if( m_glContext ) m_glContext->SetCurrent();
389
390 // Display* display = (Display*) GetXDisplay();
391 // if(glx_cx) glXMakeCurrent(display, (Window) GetClientAreaWindow(), glx_cx);
392 }
393
394 void wxGLCanvas::SetColour(const char *col)
395 {
396 if( m_glContext ) m_glContext->SetColour(col);
397 }
398
399 #endif
400 // wxUSE_GLCANVAS
401