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