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