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