]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/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
->MacGetRootWindow()));
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 wxWindow::Create( parent
, id
, pos
, size
, style
, name
);
219 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
220 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
222 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
223 m_macCanvasIsShown
= true ;
224 aglDestroyPixelFormat(fmt
);
229 void wxGLCanvas::SwapBuffers()
232 m_glContext
->SwapBuffers();
235 void wxGLCanvas::UpdateContext()
238 m_glContext
->Update();
241 void wxGLCanvas::SetViewport()
243 // viewport is initially set to entire port
244 // adjust glViewport to just this window
248 wxWindow
* iter
= this ;
249 while( iter
->GetParent() )
251 iter
= iter
->GetParent() ;
254 if ( iter
&& iter
->IsTopLevel() )
256 MacClientToRootWindow( &x
, &y
) ;
258 GetClientSize(& width
, & height
);
260 GetWindowPortBounds( MAC_WXHWND(MacGetRootWindow()) , &bounds
) ;
263 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
267 if ( !m_macCanvasIsShown
)
269 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
273 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
278 void wxGLCanvas::MacUpdateView()
283 m_glContext
->SetCurrent();
288 void wxGLCanvas::MacSuperChangedPosition()
291 wxWindow::MacSuperChangedPosition() ;
294 void wxGLCanvas::MacTopLevelWindowChangedPosition()
297 wxWindow::MacTopLevelWindowChangedPosition() ;
300 void wxGLCanvas::SetCurrent()
304 m_glContext
->SetCurrent();
308 void wxGLCanvas::SetColour(const wxChar
*colour
)
311 m_glContext
->SetColour(colour
);
314 bool wxGLCanvas::Show(bool show
)
316 if ( !wxWindow::Show( show
) )
321 if ( m_macCanvasIsShown
)
323 m_macCanvasIsShown
= false ;
329 if ( MacIsReallyShown() && !m_macCanvasIsShown
)
331 m_macCanvasIsShown
= true ;
338 void wxGLCanvas::MacSuperShown( bool show
)
342 if ( m_macCanvasIsShown
)
344 m_macCanvasIsShown
= false ;
350 if ( MacIsReallyShown() && !m_macCanvasIsShown
)
352 m_macCanvasIsShown
= true ;
357 wxWindow::MacSuperShown( show
) ;
360 //---------------------------------------------------------------------------
362 //---------------------------------------------------------------------------
364 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
366 bool wxGLApp::InitGLVisual(int *attribList
)
368 AGLPixelFormat fmt
= ChoosePixelFormat(attribList
);
370 aglDestroyPixelFormat(fmt
);
376 wxGLApp::~wxGLApp(void)
380 #endif // wxUSE_GLCANVAS