]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/glcanvas.cpp
Include wx/bitmap.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if defined(__BORLANDC__)
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_GLCANVAS
19
20 #include "wx/glcanvas.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/frame.h"
24 #include "wx/log.h"
25 #include "wx/settings.h"
26 #endif
27
28 #include "wx/mac/uma.h"
29
30 // DLL options compatibility check:
31 #include "wx/build.h"
32 WX_CHECK_BUILD_OPTIONS("wxGL")
33
34 /*
35 * GLContext implementation
36 */
37
38 wxGLContext::wxGLContext(
39 AGLPixelFormat fmt, wxGLCanvas *win,
40 const wxPalette& palette,
41 const wxGLContext *other /* for sharing display lists */
42 )
43 {
44 m_window = win;
45
46 m_drawable = (AGLDrawable) UMAGetWindowPort(MAC_WXHWND(win->MacGetTopLevelWindowRef()));
47
48 m_glContext = aglCreateContext(fmt, other ? other->m_glContext : NULL);
49 wxCHECK_RET( m_glContext, wxT("Couldn't create OpenGl context") );
50
51 GLboolean b;
52 b = aglSetDrawable(m_glContext, m_drawable);
53 wxCHECK_RET( b, wxT("Couldn't bind OpenGl context") );
54 aglEnable(m_glContext , AGL_BUFFER_RECT ) ;
55 b = aglSetCurrentContext(m_glContext);
56 wxCHECK_RET( b, wxT("Couldn't activate OpenGl context") );
57 }
58
59 wxGLContext::~wxGLContext()
60 {
61 if (m_glContext)
62 {
63 aglSetCurrentContext(NULL);
64 aglDestroyContext(m_glContext);
65 }
66 }
67
68 void wxGLContext::SwapBuffers()
69 {
70 if (m_glContext)
71 {
72 aglSwapBuffers(m_glContext);
73 }
74 }
75
76 void wxGLContext::SetCurrent()
77 {
78 if (m_glContext)
79 {
80 aglSetCurrentContext(m_glContext);
81 }
82 }
83
84 void wxGLContext::Update()
85 {
86 if (m_glContext)
87 {
88 aglUpdateContext(m_glContext);
89 }
90 }
91
92 void wxGLContext::SetColour(const wxChar *colour)
93 {
94 wxColour col = wxTheColourDatabase->Find(colour);
95 if (col.Ok())
96 {
97 float r = (float)(col.Red()/256.0);
98 float g = (float)(col.Green()/256.0);
99 float b = (float)(col.Blue()/256.0);
100 glColor3f( r, g, b);
101 }
102 }
103
104
105 /*
106 * wxGLCanvas implementation
107 */
108
109 IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
110
111 BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
112 EVT_SIZE(wxGLCanvas::OnSize)
113 END_EVENT_TABLE()
114
115 wxGLCanvas::wxGLCanvas(wxWindow *parent, wxWindowID id,
116 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
117 int *attribList, const wxPalette& palette)
118 {
119 Create(parent, NULL, id, pos, size, style, name, attribList, palette);
120 }
121
122 wxGLCanvas::wxGLCanvas( wxWindow *parent,
123 const wxGLContext *shared, wxWindowID id,
124 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
125 int *attribList, const wxPalette& palette )
126 {
127 Create(parent, shared, id, pos, size, style, name, attribList, palette);
128 }
129
130 wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID id,
131 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
132 int *attribList, const wxPalette& palette )
133 {
134 Create(parent, shared ? shared->GetContext() : NULL, id, pos, size, style, name, attribList, palette);
135 }
136
137 wxGLCanvas::~wxGLCanvas()
138 {
139 if (m_glContext != NULL) {
140 delete m_glContext;
141 m_glContext = NULL;
142 }
143 }
144
145 static AGLPixelFormat ChoosePixelFormat(const int *attribList)
146 {
147 GLint data[512];
148 GLint defaultAttribs[] = { AGL_RGBA,
149 AGL_DOUBLEBUFFER,
150 AGL_MINIMUM_POLICY,
151 AGL_DEPTH_SIZE, 1, // use largest available depth buffer
152 AGL_RED_SIZE, 1,
153 AGL_GREEN_SIZE, 1,
154 AGL_BLUE_SIZE, 1,
155 AGL_ALPHA_SIZE, 0,
156 AGL_NONE };
157 GLint *attribs;
158 if (!attribList)
159 {
160 attribs = defaultAttribs;
161 }
162 else
163 {
164 int arg=0, p=0;
165
166 data[p++] = AGL_MINIMUM_POLICY; // make _SIZE tags behave more like GLX
167 while( (attribList[arg]!=0) && (p<512) )
168 {
169 switch( attribList[arg++] )
170 {
171 case WX_GL_RGBA: data[p++] = AGL_RGBA; break;
172 case WX_GL_BUFFER_SIZE:
173 data[p++]=AGL_BUFFER_SIZE; data[p++]=attribList[arg++]; break;
174 case WX_GL_LEVEL:
175 data[p++]=AGL_LEVEL; data[p++]=attribList[arg++]; break;
176 case WX_GL_DOUBLEBUFFER: data[p++] = AGL_DOUBLEBUFFER; break;
177 case WX_GL_STEREO: data[p++] = AGL_STEREO; break;
178 case WX_GL_AUX_BUFFERS:
179 data[p++]=AGL_AUX_BUFFERS; data[p++]=attribList[arg++]; break;
180 case WX_GL_MIN_RED:
181 data[p++]=AGL_RED_SIZE; data[p++]=attribList[arg++]; break;
182 case WX_GL_MIN_GREEN:
183 data[p++]=AGL_GREEN_SIZE; data[p++]=attribList[arg++]; break;
184 case WX_GL_MIN_BLUE:
185 data[p++]=AGL_BLUE_SIZE; data[p++]=attribList[arg++]; break;
186 case WX_GL_MIN_ALPHA:
187 data[p++]=AGL_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
188 case WX_GL_DEPTH_SIZE:
189 data[p++]=AGL_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
190 case WX_GL_STENCIL_SIZE:
191 data[p++]=AGL_STENCIL_SIZE; data[p++]=attribList[arg++]; break;
192 case WX_GL_MIN_ACCUM_RED:
193 data[p++]=AGL_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break;
194 case WX_GL_MIN_ACCUM_GREEN:
195 data[p++]=AGL_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break;
196 case WX_GL_MIN_ACCUM_BLUE:
197 data[p++]=AGL_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break;
198 case WX_GL_MIN_ACCUM_ALPHA:
199 data[p++]=AGL_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break;
200 default:
201 break;
202 }
203 }
204 data[p] = 0;
205
206 attribs = data;
207 }
208
209 return aglChoosePixelFormat(NULL, 0, attribs);
210 }
211
212 bool wxGLCanvas::Create(wxWindow *parent, const wxGLContext *shared, wxWindowID id,
213 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
214 int *attribList, const wxPalette& palette)
215 {
216 m_macCanvasIsShown = false ;
217 m_glContext = 0 ;
218 wxWindow::Create( parent, id, pos, size, style, name );
219
220 AGLPixelFormat fmt = ChoosePixelFormat(attribList);
221 wxCHECK_MSG( fmt, false, wxT("Couldn't create OpenGl pixel format") );
222
223 m_glContext = new wxGLContext(fmt, this, palette, shared);
224 m_macCanvasIsShown = true ;
225 aglDestroyPixelFormat(fmt);
226
227 return true;
228 }
229
230 void wxGLCanvas::SwapBuffers()
231 {
232 if (m_glContext)
233 m_glContext->SwapBuffers();
234 }
235
236 void wxGLCanvas::UpdateContext()
237 {
238 if (m_glContext)
239 m_glContext->Update();
240 }
241
242 void wxGLCanvas::SetViewport()
243 {
244 // viewport is initially set to entire port
245 // adjust glViewport to just this window
246 int x = 0 ;
247 int y = 0 ;
248
249 wxWindow* iter = this ;
250 while( iter->GetParent() )
251 {
252 iter = iter->GetParent() ;
253 }
254
255 if ( iter && iter->IsTopLevel() )
256 {
257 MacClientToRootWindow( &x , &y ) ;
258 int width, height;
259 GetClientSize(& width, & height);
260 Rect bounds ;
261 GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds ) ;
262 GLint parms[4] ;
263 parms[0] = x ;
264 parms[1] = bounds.bottom - bounds.top - ( y + height ) ;
265 parms[2] = width ;
266 parms[3] = height ;
267
268 if ( !m_macCanvasIsShown )
269 parms[0] += 20000 ;
270 aglSetInteger( m_glContext->m_glContext , AGL_BUFFER_RECT , parms ) ;
271 }
272 }
273
274 void wxGLCanvas::OnSize(wxSizeEvent& event)
275 {
276 MacUpdateView() ;
277 }
278
279 void wxGLCanvas::MacUpdateView()
280 {
281 if (m_glContext)
282 {
283 UpdateContext();
284 m_glContext->SetCurrent();
285 SetViewport();
286 }
287 }
288
289 void wxGLCanvas::MacSuperChangedPosition()
290 {
291 MacUpdateView() ;
292 wxWindow::MacSuperChangedPosition() ;
293 }
294
295 void wxGLCanvas::MacTopLevelWindowChangedPosition()
296 {
297 MacUpdateView() ;
298 wxWindow::MacTopLevelWindowChangedPosition() ;
299 }
300
301 void wxGLCanvas::SetCurrent()
302 {
303 if (m_glContext)
304 {
305 m_glContext->SetCurrent();
306 }
307 }
308
309 void wxGLCanvas::SetColour(const wxChar *colour)
310 {
311 if (m_glContext)
312 m_glContext->SetColour(colour);
313 }
314
315 bool wxGLCanvas::Show(bool show)
316 {
317 if ( !wxWindow::Show( show ) )
318 return false ;
319 /*
320 if ( !show )
321 {
322 if ( m_macCanvasIsShown )
323 {
324 m_macCanvasIsShown = false ;
325 SetViewport() ;
326 }
327 }
328 else
329 {
330 if ( m_peer->IsVisible()&& !m_macCanvasIsShown )
331 {
332 m_macCanvasIsShown = true ;
333 SetViewport() ;
334 }
335 }
336 */
337 return true ;
338 }
339
340 void wxGLCanvas::MacVisibilityChanged()
341 {
342 if ( !MacIsReallyShown() )
343 {
344 if ( m_macCanvasIsShown )
345 {
346 m_macCanvasIsShown = false ;
347 SetViewport() ;
348 }
349 }
350 else
351 {
352 if ( !m_macCanvasIsShown )
353 {
354 m_macCanvasIsShown = true ;
355 SetViewport() ;
356 }
357 }
358 wxWindowMac::MacVisibilityChanged() ;
359 }
360
361 //---------------------------------------------------------------------------
362 // wxGLApp
363 //---------------------------------------------------------------------------
364
365 IMPLEMENT_CLASS(wxGLApp, wxApp)
366
367 bool wxGLApp::InitGLVisual(int *attribList)
368 {
369 AGLPixelFormat fmt = ChoosePixelFormat(attribList);
370 if (fmt != NULL) {
371 aglDestroyPixelFormat(fmt);
372 return true;
373 } else
374 return false;
375 }
376
377 wxGLApp::~wxGLApp(void)
378 {
379 }
380
381 #endif // wxUSE_GLCANVAS