]>
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"
27 # pragma message disable nosimpint
31 # pragma message enable nosimpint
33 #include "wx/motif/private.h"
36 // workaround for bug in Mesa's glx.c
37 static int bitcount( unsigned long n
)
49 * GLContext implementation
52 IMPLEMENT_CLASS(wxGLContext
,wxObject
)
54 wxGLContext::wxGLContext( bool WXUNUSED(isRGB
), wxWindow
*win
,
55 const wxPalette
& WXUNUSED(palette
) )
58 // m_widget = win->m_wxwindow;
60 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
61 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
63 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
65 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
68 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
71 wxGLContext::wxGLContext(
72 bool WXUNUSED(isRGB
), wxWindow
*win
,
73 const wxPalette
& WXUNUSED(palette
),
74 const wxGLContext
*other
/* for sharing display lists */
78 // m_widget = win->m_wxwindow;
80 wxGLCanvas
*gc
= (wxGLCanvas
*) win
;
81 XVisualInfo
*vi
= (XVisualInfo
*) gc
->m_vi
;
83 wxCHECK_RET( vi
, "invalid visual for OpenGl" );
86 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
87 other
->m_glContext
, GL_TRUE
);
89 m_glContext
= glXCreateContext( (Display
*)m_window
->GetXDisplay(), vi
,
92 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
95 wxGLContext::~wxGLContext()
97 if (!m_glContext
) return;
99 if (m_glContext
== glXGetCurrentContext())
101 glXMakeCurrent( (Display
*) m_window
->GetXDisplay(), None
, NULL
);
104 glXDestroyContext( (Display
*) m_window
->GetXDisplay(), m_glContext
);
107 void wxGLContext::SwapBuffers()
111 Display
* display
= (Display
*) m_window
->GetXDisplay();
112 glXSwapBuffers(display
, (Window
) m_window
->GetXWindow());
116 void wxGLContext::SetCurrent()
120 Display
* display
= (Display
*) m_window
->GetXDisplay();
121 glXMakeCurrent(display
, (Window
) m_window
->GetXWindow(),
126 void wxGLContext::SetColour(const char *colour
)
128 wxColour
*the_colour
= wxTheColourDatabase
->FindColour(colour
);
131 glGetBooleanv(GL_RGBA_MODE
, &b
);
133 glColor3ub(the_colour
->Red(),
137 GLint pix
= (GLint
)the_colour
->m_pixel
;
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()),
147 wxLogError("wxGLCanvas: cannot allocate color\n");
150 pix
= the_colour
->m_pixel
= exact_def
.pixel
;
157 void wxGLContext::SetupPixelFormat()
161 void wxGLContext::SetupPalette( const wxPalette
& WXUNUSED(palette
) )
165 wxPalette
wxGLContext::CreateDefaultPalette()
167 return wxNullPalette
;
174 * GLCanvas implementation
177 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
179 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
180 // EVT_SIZE(wxGLCanvas::OnSize)
184 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, wxWindowID id
,
185 const wxPoint
& pos
, const wxSize
& size
,
186 long style
, const wxString
& name
,
188 const wxPalette
& palette
)
189 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
191 Create( parent
, NULL
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
194 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
195 const wxGLContext
*shared
,
197 const wxPoint
& pos
, const wxSize
& size
,
198 long style
, const wxString
& name
,
200 const wxPalette
& palette
)
201 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
203 Create( parent
, shared
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
206 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
207 const wxGLCanvas
*shared
,
209 const wxPoint
& pos
, const wxSize
& size
,
210 long style
, const wxString
& name
,
212 const wxPalette
& palette
)
213 : wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
215 Create( parent
, NULL
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
220 bool wxGLCanvas::Create(wxWindow *parent,
221 const wxGLContext *shared, const wxGLCanvas *shared_context_of,
222 wxWindowID id = -1, const wxPoint& pos,
223 const wxSize& size, long style,
224 const wxString& name, int *attribList, const wxPalette& palette):
225 wxScrolledWindow(parent, id, pos, size, style, name)
228 bool wxGLCanvas::Create( wxWindow
*parent
,
229 const wxGLContext
*shared
,
230 const wxGLCanvas
*shared_context_of
,
232 const wxPoint
& pos
, const wxSize
& size
,
233 long style
, const wxString
& name
,
235 const wxPalette
& palette
)
237 XVisualInfo
*vi
, vi_templ
;
238 XWindowAttributes xwa
;
241 m_sharedContext
= (wxGLContext
*)shared
; // const_cast
242 m_sharedContextOf
= (wxGLCanvas
*)shared_context_of
; // const_cast
243 m_glContext
= (wxGLContext
*) NULL
;
245 Display
* display
= (Display
*) GetXDisplay();
247 // Check for the presence of the GLX extension
248 if(!glXQueryExtension(display
, NULL
, NULL
)) {
249 wxLogError("wxGLCanvas: GLX extension is missing\n");
254 int data
[512], arg
=0, p
=0;
256 while( (attribList
[arg
]!=0) && (p
<512) )
258 switch( attribList
[arg
++] )
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;
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;
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;
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;
295 attribList
= (int*) data
;
296 // Get an appropriate visual
297 vi
= glXChooseVisual(display
, DefaultScreen(display
), attribList
);
298 if(!vi
) return false;
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.
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
);
309 if(!vi
) return false;
310 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
311 if(!val
) return false;
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.
316 // Construct an attribute list matching the visual
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
;
330 } else { // Color index visual
331 glXGetConfig(display
, vi
, GLX_BUFFER_SIZE
, &val
);
332 a_list
[n
++] = GLX_BUFFER_SIZE
;
337 vi
= glXChooseVisual(display
, DefaultScreen(display
), a_list
);
338 if(!vi
) return false;
339 #endif /* OLD_MESA */
342 m_vi
= vi
; // safe for later use
344 wxCHECK_MSG( m_vi
, FALSE
, "required visual couldn't be found" );
346 // Create the GLX context and make it current
348 wxGLContext
*share
= m_sharedContext
;
349 if (share
==NULL
&& m_sharedContextOf
)
350 share
= m_sharedContextOf
->GetContext();
352 m_glContext
= new wxGLContext( TRUE
, this, wxNullPalette
, share
);
362 wxGLCanvas::~wxGLCanvas(void)
364 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
367 if (m_glContext
) delete m_glContext
;
369 // Display* display = (Display*) GetXDisplay();
370 // if(glx_cx) glXDestroyContext(display, glx_cx);
373 void wxGLCanvas::SwapBuffers()
375 if( m_glContext
) m_glContext
->SwapBuffers();
377 // Display* display = (Display*) GetXDisplay();
378 // if(glx_cx) glXSwapBuffers(display, (Window) GetXWindow());
381 void wxGLCanvas::SetCurrent()
383 if( m_glContext
) m_glContext
->SetCurrent();
385 // Display* display = (Display*) GetXDisplay();
386 // if(glx_cx) glXMakeCurrent(display, (Window) GetXWindow(), glx_cx);
389 void wxGLCanvas::SetColour(const char *col
)
391 if( m_glContext
) m_glContext
->SetColour(col
);