]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows 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
->MacGetRootWindow()));
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
)
103 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
106 r
= (float)(col
->Red()/256.0);
107 g
= (float)(col
->Green()/256.0);
108 b
= (float)(col
->Blue()/256.0);
115 * wxGLCanvas implementation
118 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
120 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
121 EVT_SIZE(wxGLCanvas::OnSize
)
124 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
125 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
126 int *attribList
, const wxPalette
& palette
)
128 Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
131 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
132 const wxGLContext
*shared
, wxWindowID id
,
133 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
134 int *attribList
, const wxPalette
& palette
)
136 Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
);
139 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, const wxGLCanvas
*shared
, wxWindowID id
,
140 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
141 int *attribList
, const wxPalette
& palette
)
143 Create(parent
, shared
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
146 wxGLCanvas::~wxGLCanvas()
148 if (m_glContext
!= NULL
) {
154 static AGLPixelFormat
ChoosePixelFormat(const int *attribList
)
157 GLint defaultAttribs
[] = { AGL_RGBA
,
160 AGL_DEPTH_SIZE
, 1, // use largest available depth buffer
169 attribs
= defaultAttribs
;
175 data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX
176 while( (attribList
[arg
]!=0) && (p
<512) )
178 switch( attribList
[arg
++] )
180 case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break;
181 case WX_GL_BUFFER_SIZE
:
182 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
184 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
185 case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break;
186 case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break;
187 case WX_GL_AUX_BUFFERS
:
188 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
190 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
191 case WX_GL_MIN_GREEN
:
192 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
194 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
195 case WX_GL_MIN_ALPHA
:
196 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
197 case WX_GL_DEPTH_SIZE
:
198 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
199 case WX_GL_STENCIL_SIZE
:
200 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
201 case WX_GL_MIN_ACCUM_RED
:
202 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
203 case WX_GL_MIN_ACCUM_GREEN
:
204 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
205 case WX_GL_MIN_ACCUM_BLUE
:
206 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
207 case WX_GL_MIN_ACCUM_ALPHA
:
208 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
218 return aglChoosePixelFormat(NULL
, 0, attribs
);
221 bool wxGLCanvas::Create(wxWindow
*parent
, const wxGLContext
*shared
, wxWindowID id
,
222 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
223 int *attribList
, const wxPalette
& palette
)
225 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
227 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
228 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
230 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
231 m_macCanvasIsShown
= true ;
232 aglDestroyPixelFormat(fmt
);
237 void wxGLCanvas::SwapBuffers()
240 m_glContext
->SwapBuffers();
243 void wxGLCanvas::UpdateContext()
246 m_glContext
->Update();
249 void wxGLCanvas::SetViewport()
251 // viewport is initially set to entire port
252 // adjust glViewport to just this window
256 wxWindow
* iter
= this ;
257 while( iter
->GetParent() )
259 iter
= iter
->GetParent() ;
262 if ( iter
&& iter
->IsTopLevel() )
264 MacClientToRootWindow( &x
, &y
) ;
266 GetClientSize(& width
, & height
);
268 GetWindowPortBounds( MAC_WXHWND(MacGetRootWindow()) , &bounds
) ;
271 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
275 if ( !m_macCanvasIsShown
)
277 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
281 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
286 void wxGLCanvas::MacUpdateView()
291 m_glContext
->SetCurrent();
296 void wxGLCanvas::MacSuperChangedPosition()
299 wxWindow::MacSuperChangedPosition() ;
302 void wxGLCanvas::MacTopLevelWindowChangedPosition()
305 wxWindow::MacTopLevelWindowChangedPosition() ;
308 void wxGLCanvas::SetCurrent()
312 m_glContext
->SetCurrent();
316 void wxGLCanvas::SetColour(const wxChar
*colour
)
319 m_glContext
->SetColour(colour
);
322 bool wxGLCanvas::Show(bool show
)
324 if ( !wxWindow::Show( show
) )
329 if ( m_macCanvasIsShown
)
331 m_macCanvasIsShown
= false ;
337 if ( MacIsReallyShown() && !m_macCanvasIsShown
)
339 m_macCanvasIsShown
= true ;
346 void wxGLCanvas::MacSuperShown( bool show
)
350 if ( m_macCanvasIsShown
)
352 m_macCanvasIsShown
= false ;
358 if ( MacIsReallyShown() && !m_macCanvasIsShown
)
360 m_macCanvasIsShown
= true ;
365 wxWindow::MacSuperShown( show
) ;
368 //---------------------------------------------------------------------------
370 //---------------------------------------------------------------------------
372 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
374 bool wxGLApp::InitGLVisual(int *attribList
)
376 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
378 aglDestroyPixelFormat(fmt
);
384 wxGLApp::~wxGLApp(void)
388 #endif // wxUSE_GLCANVAS