]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "glcanvas.h"
16 #include "wx/wxprec.h"
18 #if defined(__BORLANDC__)
30 #include "wx/settings.h"
33 #include "wx/glcanvas.h"
34 #include "wx/mac/uma.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxGL")
41 * GLContext implementation
44 wxGLContext::wxGLContext(
45 AGLPixelFormat fmt
, wxGLCanvas
*win
,
46 const wxPalette
& palette
,
47 const wxGLContext
*other
/* for sharing display lists */
52 m_drawable
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetTopLevelWindowRef()));
54 m_glContext
= aglCreateContext(fmt
, other
? other
->m_glContext
: NULL
);
55 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
58 b
= aglSetDrawable(m_glContext
, m_drawable
);
59 wxCHECK_RET( b
, wxT("Couldn't bind OpenGl context") );
60 aglEnable(m_glContext
, AGL_BUFFER_RECT
) ;
61 b
= aglSetCurrentContext(m_glContext
);
62 wxCHECK_RET( b
, wxT("Couldn't activate OpenGl context") );
65 wxGLContext::~wxGLContext()
69 aglSetCurrentContext(NULL
);
70 aglDestroyContext(m_glContext
);
74 void wxGLContext::SwapBuffers()
78 aglSwapBuffers(m_glContext
);
82 void wxGLContext::SetCurrent()
86 aglSetCurrentContext(m_glContext
);
90 void wxGLContext::Update()
94 aglUpdateContext(m_glContext
);
98 void wxGLContext::SetColour(const wxChar
*colour
)
100 wxColour col
= wxTheColourDatabase
->Find(colour
);
103 float r
= (float)(col
.Red()/256.0);
104 float g
= (float)(col
.Green()/256.0);
105 float b
= (float)(col
.Blue()/256.0);
112 * wxGLCanvas implementation
115 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
117 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
118 EVT_SIZE(wxGLCanvas::OnSize
)
121 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
122 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
123 int *attribList
, const wxPalette
& palette
)
125 Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
128 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
129 const wxGLContext
*shared
, wxWindowID id
,
130 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
131 int *attribList
, const wxPalette
& palette
)
133 Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
136 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, const wxGLCanvas
*shared
, wxWindowID id
,
137 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
138 int *attribList
, const wxPalette
& palette
)
140 Create(parent
, shared
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
143 wxGLCanvas::~wxGLCanvas()
145 if (m_glContext
!= NULL
) {
151 static AGLPixelFormat
ChoosePixelFormat(const int *attribList
)
154 GLint defaultAttribs
[] = { AGL_RGBA
,
157 AGL_DEPTH_SIZE
, 1, // use largest available depth buffer
166 attribs
= defaultAttribs
;
172 data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX
173 while( (attribList
[arg
]!=0) && (p
<512) )
175 switch( attribList
[arg
++] )
177 case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break;
178 case WX_GL_BUFFER_SIZE
:
179 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
181 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
182 case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break;
183 case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break;
184 case WX_GL_AUX_BUFFERS
:
185 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
187 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
188 case WX_GL_MIN_GREEN
:
189 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
191 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
192 case WX_GL_MIN_ALPHA
:
193 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
194 case WX_GL_DEPTH_SIZE
:
195 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
196 case WX_GL_STENCIL_SIZE
:
197 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
198 case WX_GL_MIN_ACCUM_RED
:
199 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
200 case WX_GL_MIN_ACCUM_GREEN
:
201 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
202 case WX_GL_MIN_ACCUM_BLUE
:
203 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
204 case WX_GL_MIN_ACCUM_ALPHA
:
205 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
215 return aglChoosePixelFormat(NULL
, 0, attribs
);
218 bool wxGLCanvas::Create(wxWindow
*parent
, const wxGLContext
*shared
, wxWindowID id
,
219 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
220 int *attribList
, const wxPalette
& palette
)
222 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
224 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
225 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
227 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
228 m_macCanvasIsShown
= true ;
229 aglDestroyPixelFormat(fmt
);
234 void wxGLCanvas::SwapBuffers()
237 m_glContext
->SwapBuffers();
240 void wxGLCanvas::UpdateContext()
243 m_glContext
->Update();
246 void wxGLCanvas::SetViewport()
248 // viewport is initially set to entire port
249 // adjust glViewport to just this window
253 wxWindow
* iter
= this ;
254 while( iter
->GetParent() )
256 iter
= iter
->GetParent() ;
259 if ( iter
&& iter
->IsTopLevel() )
261 MacClientToRootWindow( &x
, &y
) ;
263 GetClientSize(& width
, & height
);
265 GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds
) ;
268 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
272 if ( !m_macCanvasIsShown
)
274 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
278 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
283 void wxGLCanvas::MacUpdateView()
288 m_glContext
->SetCurrent();
293 void wxGLCanvas::MacSuperChangedPosition()
296 wxWindow::MacSuperChangedPosition() ;
299 void wxGLCanvas::MacTopLevelWindowChangedPosition()
302 wxWindow::MacTopLevelWindowChangedPosition() ;
305 void wxGLCanvas::SetCurrent()
309 m_glContext
->SetCurrent();
313 void wxGLCanvas::SetColour(const wxChar
*colour
)
316 m_glContext
->SetColour(colour
);
319 bool wxGLCanvas::Show(bool show
)
321 if ( !wxWindow::Show( show
) )
326 if ( m_macCanvasIsShown )
328 m_macCanvasIsShown = false ;
334 if ( IsControlVisible( *m_peer ) && !m_macCanvasIsShown )
336 m_macCanvasIsShown = true ;
344 void wxGLCanvas::MacVisibilityChanged()
346 if ( !IsControlVisible( *m_peer
) )
348 if ( m_macCanvasIsShown
)
350 m_macCanvasIsShown
= false ;
356 if ( !m_macCanvasIsShown
)
358 m_macCanvasIsShown
= true ;
362 wxWindowMac::MacVisibilityChanged() ;
365 //---------------------------------------------------------------------------
367 //---------------------------------------------------------------------------
369 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
371 bool wxGLApp::InitGLVisual(int *attribList
)
373 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
375 aglDestroyPixelFormat(fmt
);
381 wxGLApp::~wxGLApp(void)
385 #endif // wxUSE_GLCANVAS