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