]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/glcanvas.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets
4 // Uses the GLX extension.
5 // Author: Julian Smart and Wolfram Gloger
6 // Modified by: Vadim Zeitlin to update to new API
9 // Copyright: (c) Julian Smart, Wolfram Gloger
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // TODO: merge this with wxGTK version in some src/unix/glcanvasx11.cpp
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // for compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
26 #if defined(__BORLANDC__)
32 #include "wx/glcanvas.h"
41 # pragma message disable nosimpint
45 # pragma message enable nosimpint
47 #include "wx/x11/private.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static inline Window
wxGetClientAreaWindow(const wxWindow
* win
)
59 GetClientAreaWindow();
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 IMPLEMENT_CLASS(wxGLContext
, wxObject
)
73 wxGLContext::wxGLContext(wxGLCanvas
*win
, const wxGLContext
*other
)
75 XVisualInfo
*vi
= (XVisualInfo
*) win
->m_vi
;
76 wxASSERT_MSG( vi
, wxT("invalid visual in wxGLCanvas") );
78 m_glContext
= glXCreateContext( (Display
*)wxGetDisplay(),
80 other
? other
->m_glContext
: None
,
83 wxASSERT_MSG( m_glContext
, wxT("Couldn't create OpenGL context") );
86 wxGLContext::~wxGLContext()
91 if (m_glContext
== glXGetCurrentContext())
93 glXMakeCurrent( (Display
*) wxGetDisplay(), None
, NULL
);
96 glXDestroyContext( (Display
*) wxGetDisplay(), m_glContext
);
99 void wxGLContext::SetCurrent(const wxGLCanvas
& win
) const
103 Display
* display
= (Display
*) wxGetDisplay();
104 glXMakeCurrent(display
, wxGetClientAreaWindow(&win
), m_glContext
);
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
114 wxGLCanvas::wxGLCanvas(wxWindow
*parent
,
116 const int *attribList
,
120 const wxString
& name
,
121 const wxPalette
& palette
)
123 Create(parent
, id
, pos
, size
, style
, name
, attribList
, palette
);
126 bool wxGLCanvas::Create(wxWindow
*parent
,
131 const wxString
& name
,
132 const int *attribList
,
133 const wxPalette
& palette
)
137 if ( !wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
140 XVisualInfo
*vi
, vi_templ
;
141 XWindowAttributes xwa
;
144 Display
* display
= (Display
*) wxGetDisplay();
146 // Check for the presence of the GLX extension
147 if(!glXQueryExtension(display
, NULL
, NULL
))
149 wxLogDebug(wxT("wxGLCanvas: GLX extension is missing"));
154 int data
[512], arg
=0, p
=0;
156 while( (attribList
[arg
]!=0) && (p
<512) )
158 switch( attribList
[arg
++] )
160 case WX_GL_RGBA
: data
[p
++] = GLX_RGBA
; break;
161 case WX_GL_BUFFER_SIZE
:
162 data
[p
++]=GLX_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
164 data
[p
++]=GLX_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
165 case WX_GL_DOUBLEBUFFER
: data
[p
++] = GLX_DOUBLEBUFFER
; break;
166 case WX_GL_STEREO
: data
[p
++] = GLX_STEREO
; break;
167 case WX_GL_AUX_BUFFERS
:
168 data
[p
++]=GLX_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
170 data
[p
++]=GLX_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
171 case WX_GL_MIN_GREEN
:
172 data
[p
++]=GLX_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
174 data
[p
++]=GLX_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
175 case WX_GL_MIN_ALPHA
:
176 data
[p
++]=GLX_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
177 case WX_GL_DEPTH_SIZE
:
178 data
[p
++]=GLX_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
179 case WX_GL_STENCIL_SIZE
:
180 data
[p
++]=GLX_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
181 case WX_GL_MIN_ACCUM_RED
:
182 data
[p
++]=GLX_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
183 case WX_GL_MIN_ACCUM_GREEN
:
184 data
[p
++]=GLX_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
185 case WX_GL_MIN_ACCUM_BLUE
:
186 data
[p
++]=GLX_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
187 case WX_GL_MIN_ACCUM_ALPHA
:
188 data
[p
++]=GLX_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
195 // Get an appropriate visual
196 vi
= glXChooseVisual(display
, DefaultScreen(display
), data
);
197 if(!vi
) return false;
199 // Here we should make sure that vi is the same visual as the
200 // one used by the xwindow drawable in wxCanvas. However,
201 // there is currently no mechanism for this in wx_canvs.cc.
203 else // default attributes
205 // By default, we use the visual of xwindow
206 XGetWindowAttributes(display
, wxGetClientAreaWindow(this), &xwa
);
207 vi_templ
.visualid
= XVisualIDFromVisual(xwa
.visual
);
208 vi
= XGetVisualInfo(display
, VisualIDMask
, &vi_templ
, &n
);
209 if(!vi
) return false;
210 glXGetConfig(display
, vi
, GLX_USE_GL
, &val
);
211 if(!val
) return false;
214 m_vi
= vi
; // safe for later use
216 wxCHECK_MSG( m_vi
, false, wxT("required visual couldn't be found") );
221 wxGLCanvas::~wxGLCanvas()
223 XVisualInfo
*vi
= (XVisualInfo
*) m_vi
;
229 void wxGLCanvas::SwapBuffers()
231 glXSwapBuffers((Display
*)wxGetDisplay(), wxGetClientAreaWindow(this));
234 int wxGLCanvas::GetColourIndex(const wxColour
& col_
)
236 wxColour
& col
= wx_const_cast(wxColour
&, col_
);
239 col
.AllocColour(GetXDisplay());
241 col
.CalcPixel(wxTheApp
->GetMainColormap(wxGetDisplay()));
244 return col
.GetPixel();
247 #endif // wxUSE_GLCANVAS