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