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