]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows 2.0 for Motif.
4 // Uses the GLX extension.
5 // Author: Julian Smart and Wolfram Gloger
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "glcanvas.h"
21 #include "wx/glcanvas.h"
26 # pragma message disable nosimpint
30 # pragma message enable nosimpint
32 #include "wx/motif/private.h"
35 // workaround for bug in Mesa's glx.c
36 static int bitcount( unsigned long n
)
48 * GLContext implementation
51 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
53 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
54 const wxPalette
& WXUNUSED(palette
) )
57 // m_widget = win->m_wxwindow;
59 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
60 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
62 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
64 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
67 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
70 wxGLContext::wxGLContext(
71 bool WXUNUSED(isRGB
), wxWindow
*win
,
72 const wxPalette
& WXUNUSED(palette
),
73 const wxGLContext
*other
/* for sharing display lists */
77 // m_widget = win->m_wxwindow;
79 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
80 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
82 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
85 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
86 other
->m_glContext
, GL_TRUE
);
88 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
91 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
94 wxGLContext::~wxGLContext()
96 if (!m_glContext
) return;
98 if (m_glContext
== glXGetCurrentContext())
100 glXMakeCurrent( (Display
*) m_window
->GetXDisplay(), None
, NULL
);
103 glXDestroyContext( (Display
*) m_window
->GetXDisplay(), m_glContext
);
106 void wxGLContext::SwapBuffers()
110 Display
* display
= (Display
*) m_window
->GetXDisplay();
111 glXSwapBuffers(display
, (Window
) m_window
->GetXWindow());
115 void wxGLContext::SetCurrent()
119 Display
* display
= (Display
*) m_window
->GetXDisplay();
120 glXMakeCurrent(display
, (Window
) m_window
->GetXWindow(),
125 void wxGLContext::SetColour(const char *colour
)
127 wxColour
*the_colour
= wxTheColourDatabase
->FindColour(colour
);
130 glGetBooleanv(GL_RGBA_MODE
, &b
);
132 glColor3ub(the_colour
->Red(),
136 GLint pix
= (GLint
)the_colour
->m_pixel
;
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()),
146 wxDebugMsg("wxGLCanvas: cannot allocate color\n");
149 pix
= the_colour
->m_pixel
= exact_def
.pixel
;
156 void wxGLContext::SetupPixelFormat()
160 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
164 wxPalette
wxGLContext::CreateDefaultPalette()
166 return wxNullPalette
;
173 * GLCanvas implementation
176 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
178 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
179 // EVT_SIZE(wxGLCanvas::OnSize)
183 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
184 const wxPoint
& pos
, const wxSize
& size
,
185 long style
, const wxString
& name
,
187 const wxPalette
& palette
)
188 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
190 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
193 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
194 const wxGLContext
*shared
,
196 const wxPoint
& pos
, const wxSize
& size
,
197 long style
, const wxString
& name
,
199 const wxPalette
& palette
)
200 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
202 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
205 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
206 const wxGLCanvas
*shared
,
208 const wxPoint
& pos
, const wxSize
& size
,
209 long style
, const wxString
& name
,
211 const wxPalette
& palette
)
212 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
214 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
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)
227 bool wxGLCanvas::Create( wxWindow
*parent
,
228 const wxGLContext
*shared
,
229 const wxGLCanvas
*shared_context_of
,
231 const wxPoint
& pos
, const wxSize
& size
,
232 long style
, const wxString
& name
,
234 const wxPalette
& palette
)
236 XVisualInfo
*vi
, vi_templ
;
237 XWindowAttributes xwa
;
240 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
241 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
242 m_glContext
= (wxGLContext
*) NULL
;
244 Display
* display
= (Display
*) GetXDisplay();
246 // Check for the presence of the GLX extension
247 if(!glXQueryExtension(display
, NULL
, NULL
)) {
248 wxDebugMsg("wxGLCanvas: GLX extension is missing\n");
253 int data
[512], arg
=0, p
=0;
255 while( (attribList
[arg
]!=0) && (p
<512) )
257 switch( attribList
[arg
++] )
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;
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;
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;
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;
294 attribList
= (int*) data
;
295 // Get an appropriate visual
296 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
297 if(!vi
) return false;
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.
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.
315 // Construct an attribute list matching the visual
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
;
329 } else { // Color index visual
330 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
331 a_list
[n
++] = GLX_BUFFER_SIZE
;
336 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
337 if(!vi
) return false;
338 #endif /* OLD_MESA */
341 m_vi
= vi
; // safe for later use
343 wxCHECK_MSG( m_vi
, FALSE
, "required visual couldn't be found" );
345 // Create the GLX context and make it current
347 wxGLContext
*share
= m_sharedContext
;
348 if (share
==NULL
&& m_sharedContextOf
)
349 share
= m_sharedContextOf
->GetContext();
351 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
361 wxGLCanvas::~wxGLCanvas(void)
363 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
366 if (m_glContext
) delete m_glContext
;
368 // Display* display = (Display*) GetXDisplay();
369 // if(glx_cx) glXDestroyContext(display, glx_cx);
372 void wxGLCanvas::SwapBuffers()
374 if( m_glContext
) m_glContext
->SwapBuffers();
376 // Display* display = (Display*) GetXDisplay();
377 // if(glx_cx) glXSwapBuffers(display, (Window) GetXWindow());
380 void wxGLCanvas::SetCurrent()
382 if( m_glContext
) m_glContext
->SetCurrent();
384 // Display* display = (Display*) GetXDisplay();
385 // if(glx_cx) glXMakeCurrent(display, (Window) GetXWindow(), glx_cx);
388 void wxGLCanvas::SetColour(const char *col
)
390 if( m_glContext
) m_glContext
->SetColour(col
);