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