1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/glcanvas.mm
3 // Purpose: wxGLCanvas, for using OpenGL with wxWidgets under Macintosh
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: glcanvas.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
22 #if defined(__BORLANDC__)
28 #include "wx/glcanvas.h"
33 #include "wx/settings.h"
36 #include "wx/osx/private.h"
38 WXGLContext WXGLCreateContext( WXGLPixelFormat pixelFormat, WXGLContext shareContext )
40 WXGLContext context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext: shareContext];
42 wxFAIL_MSG("NSOpenGLContext creation failed");
46 void WXGLDestroyContext( WXGLContext context )
54 void WXGLSwapBuffers( WXGLContext context )
56 [context flushBuffer];
59 WXGLContext WXGLGetCurrentContext()
61 return [NSOpenGLContext currentContext];
64 void WXGLDestroyPixelFormat( WXGLPixelFormat pixelFormat )
68 [pixelFormat release];
73 WXGLPixelFormat WXGLChoosePixelFormat(const int *attribList)
75 NSOpenGLPixelFormatAttribute data[512];
76 const NSOpenGLPixelFormatAttribute defaultAttribs[] =
78 NSOpenGLPFADoubleBuffer,
79 NSOpenGLPFAMinimumPolicy,
80 NSOpenGLPFAColorSize,(NSOpenGLPixelFormatAttribute)8,
81 NSOpenGLPFAAlphaSize,(NSOpenGLPixelFormatAttribute)0,
82 NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)8,
83 (NSOpenGLPixelFormatAttribute)nil
86 const NSOpenGLPixelFormatAttribute *attribs;
89 attribs = defaultAttribs;
94 data[p++] = NSOpenGLPFAMinimumPolicy; // make _SIZE tags behave more like GLX
96 for ( unsigned arg = 0; attribList[arg] !=0 && p < WXSIZEOF(data); )
98 switch ( attribList[arg++] )
101 //data[p++] = AGL_RGBA;
104 case WX_GL_BUFFER_SIZE:
105 //data[p++] = AGL_BUFFER_SIZE;
106 //data[p++] = attribList[arg++];
110 //data[p++]=AGL_LEVEL;
111 //data[p++]=attribList[arg++];
114 case WX_GL_DOUBLEBUFFER:
115 data[p++] = NSOpenGLPFADoubleBuffer;
119 data[p++] = NSOpenGLPFAStereo;
122 case WX_GL_AUX_BUFFERS:
123 data[p++] = NSOpenGLPFAAuxBuffers;
124 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
128 data[p++] = NSOpenGLPFAColorSize;
129 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
132 case WX_GL_MIN_GREEN:
133 //data[p++] = AGL_GREEN_SIZE;
134 //data[p++] = attribList[arg++];
138 //data[p++] = AGL_BLUE_SIZE;
139 //data[p++] = attribList[arg++];
142 case WX_GL_MIN_ALPHA:
143 data[p++] = NSOpenGLPFAAlphaSize;
144 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
147 case WX_GL_DEPTH_SIZE:
148 data[p++] = NSOpenGLPFADepthSize;
149 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
152 case WX_GL_STENCIL_SIZE:
153 data[p++] = NSOpenGLPFAStencilSize;
154 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
157 case WX_GL_MIN_ACCUM_RED:
158 data[p++] = NSOpenGLPFAAccumSize;
159 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
162 case WX_GL_MIN_ACCUM_GREEN:
163 //data[p++] = AGL_ACCUM_GREEN_SIZE;
164 //data[p++] = attribList[arg++];
167 case WX_GL_MIN_ACCUM_BLUE:
168 //data[p++] = AGL_ACCUM_BLUE_SIZE;
169 //data[p++] = attribList[arg++];
172 case WX_GL_MIN_ACCUM_ALPHA:
173 //data[p++] = AGL_ACCUM_ALPHA_SIZE;
174 //data[p++] = attribList[arg++];
177 case WX_GL_SAMPLE_BUFFERS:
178 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
180 if ( !attribList[arg++] )
186 data[p++] = NSOpenGLPFASampleBuffers;
187 if ( (data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++]) == true )
189 // don't use software fallback
190 data[p++] = NSOpenGLPFANoRecovery;
195 if ( !wxGLCanvas::IsAGLMultiSampleAvailable() )
197 if ( !attribList[arg++] )
203 data[p++] = NSOpenGLPFASamples;
204 data[p++] = (NSOpenGLPixelFormatAttribute) attribList[arg++];
209 data[p] = (NSOpenGLPixelFormatAttribute)nil;
214 return [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
217 bool wxGLContext::SetCurrent(const wxGLCanvas& win) const
222 [m_glContext setView: win.GetHandle() ];
223 [m_glContext update];
225 [m_glContext makeCurrentContext];
230 @interface wxNSCustomOpenGLView : NSView
232 WXCOCOAIMPL_COMMON_MEMBERS
233 NSOpenGLContext* context;
236 - (id)initWithFrame:(NSRect)frame;
238 WXCOCOAIMPL_COMMON_INTERFACE
242 @implementation wxNSCustomOpenGLView
244 - (id)initWithFrame:(NSRect)frame
246 [super initWithFrame:frame];
251 WXCOCOAIMPL_COMMON_IMPLEMENTATION
260 bool wxGLCanvas::Create(wxWindow *parent,
265 const wxString& name,
266 const int *attribList,
267 const wxPalette& WXUNUSED(palette))
269 m_glFormat = WXGLChoosePixelFormat(attribList);
273 // m_macIsUserPane = false ;
275 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
279 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
280 wxNSCustomOpenGLView* v = [[wxNSCustomOpenGLView alloc] initWithFrame:r];
281 m_peer = new wxWidgetCocoaImpl( this, v );
282 [v setImplementation:m_peer];
284 MacPostControlCreate(pos, size) ;
289 wxGLCanvas::~wxGLCanvas()
292 WXGLDestroyPixelFormat(m_glFormat);
296 #endif // wxUSE_GLCANVAS