]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if defined(__BORLANDC__)
24 #include "wx/settings.h"
27 #include "wx/glcanvas.h"
28 #include "wx/mac/uma.h"
30 // DLL options compatibility check:
32 WX_CHECK_BUILD_OPTIONS("wxGL")
35 * GLContext implementation
38 wxGLContext::wxGLContext(
39 AGLPixelFormat fmt
, wxGLCanvas
*win
,
40 const wxPalette
& palette
,
41 const wxGLContext
*other
/* for sharing display lists */
46 m_drawable
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetTopLevelWindowRef()));
48 m_glContext
= aglCreateContext(fmt
, other
? other
->m_glContext
: NULL
);
49 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
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") );
59 wxGLContext::~wxGLContext()
63 aglSetCurrentContext(NULL
);
64 aglDestroyContext(m_glContext
);
68 void wxGLContext::SwapBuffers()
72 aglSwapBuffers(m_glContext
);
76 void wxGLContext::SetCurrent()
80 aglSetCurrentContext(m_glContext
);
84 void wxGLContext::Update()
88 aglUpdateContext(m_glContext
);
92 void wxGLContext::SetColour(const wxChar
*colour
)
94 wxColour col
= wxTheColourDatabase
->Find(colour
);
97 float r
= (float)(col
.Red()/256.0);
98 float g
= (float)(col
.Green()/256.0);
99 float b
= (float)(col
.Blue()/256.0);
106 * wxGLCanvas implementation
109 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
111 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
112 EVT_SIZE(wxGLCanvas::OnSize
)
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
)
119 Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
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
)
127 Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
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
)
134 Create(parent
, shared
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
137 wxGLCanvas::~wxGLCanvas()
139 if (m_glContext
!= NULL
) {
145 static AGLPixelFormat
ChoosePixelFormat(const int *attribList
)
148 GLint defaultAttribs
[] = { AGL_RGBA
,
151 AGL_DEPTH_SIZE
, 1, // use largest available depth buffer
160 attribs
= defaultAttribs
;
166 data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX
167 while( (attribList
[arg
]!=0) && (p
<512) )
169 switch( attribList
[arg
++] )
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;
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;
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;
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;
209 return aglChoosePixelFormat(NULL
, 0, attribs
);
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
)
216 m_macCanvasIsShown
= false ;
218 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
220 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
221 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
223 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
224 m_macCanvasIsShown
= true ;
225 aglDestroyPixelFormat(fmt
);
230 void wxGLCanvas::SwapBuffers()
233 m_glContext
->SwapBuffers();
236 void wxGLCanvas::UpdateContext()
239 m_glContext
->Update();
242 void wxGLCanvas::SetViewport()
244 // viewport is initially set to entire port
245 // adjust glViewport to just this window
249 wxWindow
* iter
= this ;
250 while( iter
->GetParent() )
252 iter
= iter
->GetParent() ;
255 if ( iter
&& iter
->IsTopLevel() )
257 MacClientToRootWindow( &x
, &y
) ;
259 GetClientSize(& width
, & height
);
261 GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds
) ;
264 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
268 if ( !m_macCanvasIsShown
)
270 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
274 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
279 void wxGLCanvas::MacUpdateView()
284 m_glContext
->SetCurrent();
289 void wxGLCanvas::MacSuperChangedPosition()
292 wxWindow::MacSuperChangedPosition() ;
295 void wxGLCanvas::MacTopLevelWindowChangedPosition()
298 wxWindow::MacTopLevelWindowChangedPosition() ;
301 void wxGLCanvas::SetCurrent()
305 m_glContext
->SetCurrent();
309 void wxGLCanvas::SetColour(const wxChar
*colour
)
312 m_glContext
->SetColour(colour
);
315 bool wxGLCanvas::Show(bool show
)
317 if ( !wxWindow::Show( show
) )
322 if ( m_macCanvasIsShown )
324 m_macCanvasIsShown = false ;
330 if ( m_peer->IsVisible()&& !m_macCanvasIsShown )
332 m_macCanvasIsShown = true ;
340 void wxGLCanvas::MacVisibilityChanged()
342 if ( !MacIsReallyShown() )
344 if ( m_macCanvasIsShown
)
346 m_macCanvasIsShown
= false ;
352 if ( !m_macCanvasIsShown
)
354 m_macCanvasIsShown
= true ;
358 wxWindowMac::MacVisibilityChanged() ;
361 //---------------------------------------------------------------------------
363 //---------------------------------------------------------------------------
365 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
367 bool wxGLApp::InitGLVisual(int *attribList
)
369 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
371 aglDestroyPixelFormat(fmt
);
377 wxGLApp::~wxGLApp(void)
381 #endif // wxUSE_GLCANVAS