]>
git.saurik.com Git - wxWidgets.git/blob - 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
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if defined(__BORLANDC__)
20 #include "wx/glcanvas.h"
27 #include "wx/settings.h"
29 #include "wx/mac/uma.h"
31 // DLL options compatibility check:
33 WX_CHECK_BUILD_OPTIONS("wxGL")
36 * GLContext implementation
39 wxGLContext::wxGLContext(
40 AGLPixelFormat fmt
, wxGLCanvas
*win
,
41 const wxPalette
& palette
,
42 const wxGLContext
*other
/* for sharing display lists */
47 m_drawable
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetTopLevelWindowRef()));
49 m_glContext
= aglCreateContext(fmt
, other
? other
->m_glContext
: NULL
);
50 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
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") );
60 wxGLContext::~wxGLContext()
64 aglSetCurrentContext(NULL
);
65 aglDestroyContext(m_glContext
);
69 void wxGLContext::SwapBuffers()
73 aglSwapBuffers(m_glContext
);
77 void wxGLContext::SetCurrent()
81 aglSetCurrentContext(m_glContext
);
85 void wxGLContext::Update()
89 aglUpdateContext(m_glContext
);
93 void wxGLContext::SetColour(const wxChar
*colour
)
95 wxColour col
= wxTheColourDatabase
->Find(colour
);
98 float r
= (float)(col
.Red()/256.0);
99 float g
= (float)(col
.Green()/256.0);
100 float b
= (float)(col
.Blue()/256.0);
107 * wxGLCanvas implementation
110 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
112 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
113 EVT_SIZE(wxGLCanvas::OnSize
)
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
)
120 Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
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
)
128 Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
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
)
135 Create(parent
, shared
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
138 wxGLCanvas::~wxGLCanvas()
140 if (m_glContext
!= NULL
) {
146 static AGLPixelFormat
ChoosePixelFormat(const int *attribList
)
149 GLint defaultAttribs
[] = { AGL_RGBA
,
152 AGL_DEPTH_SIZE
, 1, // use largest available depth buffer
161 attribs
= defaultAttribs
;
167 data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX
168 while( (attribList
[arg
]!=0) && (p
<512) )
170 switch( attribList
[arg
++] )
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;
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;
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;
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;
210 return aglChoosePixelFormat(NULL
, 0, attribs
);
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
)
217 m_macCanvasIsShown
= false ;
219 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
221 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
222 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
224 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
225 m_macCanvasIsShown
= true ;
226 aglDestroyPixelFormat(fmt
);
231 void wxGLCanvas::SwapBuffers()
234 m_glContext
->SwapBuffers();
237 void wxGLCanvas::UpdateContext()
240 m_glContext
->Update();
243 void wxGLCanvas::SetViewport()
245 // viewport is initially set to entire port
246 // adjust glViewport to just this window
250 wxWindow
* iter
= this ;
251 while( iter
->GetParent() )
253 iter
= iter
->GetParent() ;
256 if ( iter
&& iter
->IsTopLevel() )
258 MacClientToRootWindow( &x
, &y
) ;
260 GetClientSize(& width
, & height
);
262 GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds
) ;
265 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
269 if ( !m_macCanvasIsShown
)
271 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
275 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
280 void wxGLCanvas::MacUpdateView()
285 m_glContext
->SetCurrent();
290 void wxGLCanvas::MacSuperChangedPosition()
293 wxWindow::MacSuperChangedPosition() ;
296 void wxGLCanvas::MacTopLevelWindowChangedPosition()
299 wxWindow::MacTopLevelWindowChangedPosition() ;
302 void wxGLCanvas::SetCurrent()
306 m_glContext
->SetCurrent();
310 void wxGLCanvas::SetColour(const wxChar
*colour
)
313 m_glContext
->SetColour(colour
);
316 bool wxGLCanvas::Show(bool show
)
318 if ( !wxWindow::Show( show
) )
323 if ( m_macCanvasIsShown )
325 m_macCanvasIsShown = false ;
331 if ( m_peer->IsVisible()&& !m_macCanvasIsShown )
333 m_macCanvasIsShown = true ;
341 void wxGLCanvas::MacVisibilityChanged()
343 if ( !MacIsReallyShown() )
345 if ( m_macCanvasIsShown
)
347 m_macCanvasIsShown
= false ;
353 if ( !m_macCanvasIsShown
)
355 m_macCanvasIsShown
= true ;
359 wxWindowMac::MacVisibilityChanged() ;
362 //---------------------------------------------------------------------------
364 //---------------------------------------------------------------------------
366 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
368 bool wxGLApp::InitGLVisual(int *attribList
)
370 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
372 aglDestroyPixelFormat(fmt
);
378 wxGLApp::~wxGLApp(void)
382 #endif // wxUSE_GLCANVAS