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