]>
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" 
  25     #include "wx/settings.h" 
  28 #include "wx/mac/uma.h" 
  30 // DLL options compatibility check: 
  32 WX_CHECK_BUILD_OPTIONS("wxGL") 
  34 #include "wx/mac/private.h" 
  37 * GLContext implementation 
  40 wxGLContext::wxGLContext( 
  41                          AGLPixelFormat fmt
, wxGLCanvas 
*win
, 
  42                          const wxPalette
& palette
, 
  43                          const wxGLContext 
*other        
/* for sharing display lists */ 
  48     m_drawable 
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetTopLevelWindowRef())); 
  50     m_glContext 
= aglCreateContext(fmt
, other 
? other
->m_glContext 
: NULL
); 
  51     wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") ); 
  54     b 
= aglSetDrawable(m_glContext
, m_drawable
); 
  55     wxCHECK_RET( b
, wxT("Couldn't bind OpenGl context") ); 
  56     aglEnable(m_glContext 
, AGL_BUFFER_RECT 
) ; 
  57     b 
= aglSetCurrentContext(m_glContext
); 
  58     wxCHECK_RET( b
, wxT("Couldn't activate OpenGl context") ); 
  61 wxGLContext::~wxGLContext() 
  65         aglSetCurrentContext(NULL
); 
  66         aglDestroyContext(m_glContext
); 
  70 void wxGLContext::SwapBuffers() 
  74         aglSwapBuffers(m_glContext
); 
  78 void wxGLContext::SetCurrent() 
  82         aglSetCurrentContext(m_glContext
); 
  86 void wxGLContext::Update() 
  90         aglUpdateContext(m_glContext
); 
  94 void wxGLContext::SetColour(const wxChar 
*colour
) 
  96     wxColour col 
= wxTheColourDatabase
->Find(colour
); 
  99         float r 
= (float)(col
.Red()/256.0); 
 100         float g 
= (float)(col
.Green()/256.0); 
 101         float b 
= (float)(col
.Blue()/256.0); 
 108 * wxGLCanvas implementation 
 111 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
) 
 113 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
) 
 114     EVT_SIZE(wxGLCanvas::OnSize
) 
 117 wxGLCanvas::wxGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 118                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 119                        int *attribList
, const wxPalette
& palette
) 
 121     Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 124 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, 
 125                        const wxGLContext 
*shared
, wxWindowID id
, 
 126                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 127                        int *attribList
, const wxPalette
& palette 
) 
 129     Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 132 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, const wxGLCanvas 
*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 
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 139 wxGLCanvas::~wxGLCanvas() 
 141     if (m_glContext 
!= NULL
) { 
 147 static AGLPixelFormat 
ChoosePixelFormat(const int *attribList
) 
 150     GLint defaultAttribs
[] = { AGL_RGBA
, 
 153         AGL_DEPTH_SIZE
, 1,  // use largest available depth buffer 
 162         attribs 
= defaultAttribs
; 
 168         data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX 
 169         while( (attribList
[arg
]!=0) && (p
<512) ) 
 171             switch( attribList
[arg
++] ) 
 173             case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break; 
 174             case WX_GL_BUFFER_SIZE
: 
 175                 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 177                 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break; 
 178             case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break; 
 179             case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break; 
 180             case WX_GL_AUX_BUFFERS
: 
 181                 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break; 
 183                 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 184             case WX_GL_MIN_GREEN
: 
 185                 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 187                 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 188             case WX_GL_MIN_ALPHA
: 
 189                 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 190             case WX_GL_DEPTH_SIZE
: 
 191                 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 192             case WX_GL_STENCIL_SIZE
: 
 193                 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 194             case WX_GL_MIN_ACCUM_RED
: 
 195                 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 196             case WX_GL_MIN_ACCUM_GREEN
: 
 197                 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 198             case WX_GL_MIN_ACCUM_BLUE
: 
 199                 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 200             case WX_GL_MIN_ACCUM_ALPHA
: 
 201                 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 211     return aglChoosePixelFormat(NULL
, 0, attribs
); 
 214 bool wxGLCanvas::Create(wxWindow 
*parent
, const wxGLContext 
*shared
, wxWindowID id
, 
 215                         const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 216                         int *attribList
, const wxPalette
& palette
) 
 218     m_macCanvasIsShown 
= false ; 
 220     wxWindow::Create( parent
, id
, pos
, size
, style
, name 
); 
 222     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 223     wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") ); 
 225     m_glContext 
= new wxGLContext(fmt
, this, palette
, shared
); 
 226     m_macCanvasIsShown 
= true ; 
 227     aglDestroyPixelFormat(fmt
); 
 232 void wxGLCanvas::SwapBuffers() 
 235         m_glContext
->SwapBuffers(); 
 238 void wxGLCanvas::UpdateContext() 
 241         m_glContext
->Update(); 
 244 void wxGLCanvas::SetViewport() 
 246     // viewport is initially set to entire port 
 247     // adjust glViewport to just this window 
 251     wxWindow
* iter 
= this ; 
 252     while( iter
->GetParent() ) 
 254         iter 
= iter
->GetParent() ; 
 257     if ( iter 
&& iter
->IsTopLevel() ) 
 259         MacClientToRootWindow( &x 
, &y 
) ; 
 261         GetClientSize(& width
, & height
); 
 264                 // TODO in case we adopt point vs pixel coordinates, this will make the conversion 
 265         GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds 
) ; 
 266         HIRect hiRect 
= CGRectMake( x
, y
, width
, height 
) ; 
 267         HIRectConvert( &hiRect
, kHICoordSpace72DPIGlobal
, NULL
, kHICoordSpaceScreenPixel
, NULL
) ; 
 268         HIRect hiBounds 
= CGRectMake( 0, 0, bounds
.right 
- bounds
.left 
, bounds
.bottom 
- bounds
.top 
) ; 
 269         HIRectConvert( &hiBounds
, kHICoordSpace72DPIGlobal
, NULL
, kHICoordSpaceScreenPixel
, NULL
) ; 
 271         parms
[0] = hiRect
.origin
.x 
; 
 272         parms
[1] = hiBounds
.size
.height 
- (hiRect
.origin
.y 
+ hiRect
.size
.height
) ; 
 273         parms
[2] = hiRect
.size
.width 
; 
 274         parms
[3] = hiRect
.size
.height 
; 
 276         GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds 
) ; 
 279         parms
[1] = bounds
.bottom 
- bounds
.top 
- ( y 
+ height 
) ; 
 283         if ( !m_macCanvasIsShown 
) 
 285         aglSetInteger( m_glContext
->m_glContext 
, AGL_BUFFER_RECT 
, parms 
) ; 
 289 void wxGLCanvas::OnSize(wxSizeEvent
& event
) 
 294 void wxGLCanvas::MacUpdateView() 
 299         m_glContext
->SetCurrent(); 
 304 void wxGLCanvas::MacSuperChangedPosition() 
 307     wxWindow::MacSuperChangedPosition() ; 
 310 void wxGLCanvas::MacTopLevelWindowChangedPosition() 
 313     wxWindow::MacTopLevelWindowChangedPosition() ; 
 316 void wxGLCanvas::SetCurrent() 
 320         m_glContext
->SetCurrent(); 
 324 void wxGLCanvas::SetColour(const wxChar 
*colour
) 
 327         m_glContext
->SetColour(colour
); 
 330 bool wxGLCanvas::Show(bool show
) 
 332     if ( !wxWindow::Show( show 
) ) 
 337         if ( m_macCanvasIsShown ) 
 339             m_macCanvasIsShown = false ; 
 345         if ( m_peer->IsVisible()&& !m_macCanvasIsShown ) 
 347             m_macCanvasIsShown = true ; 
 355 void wxGLCanvas::MacVisibilityChanged() 
 357     if ( !MacIsReallyShown() ) 
 359         if ( m_macCanvasIsShown 
) 
 361             m_macCanvasIsShown 
= false ; 
 367         if ( !m_macCanvasIsShown 
) 
 369             m_macCanvasIsShown 
= true ; 
 373     wxWindowMac::MacVisibilityChanged() ; 
 376 //--------------------------------------------------------------------------- 
 378 //--------------------------------------------------------------------------- 
 380 IMPLEMENT_CLASS(wxGLApp
, wxApp
) 
 382 bool wxGLApp::InitGLVisual(int *attribList
) 
 384     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 386         aglDestroyPixelFormat(fmt
); 
 392 wxGLApp::~wxGLApp(void) 
 396 #endif // wxUSE_GLCANVAS