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