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