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