]>
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 ///////////////////////////////////////////////////////////////////////////// 
  12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  13 #pragma implementation "glcanvas.h" 
  16 #include "wx/wxprec.h" 
  18 #if defined(__BORLANDC__) 
  28 #include "wx/settings.h" 
  31 #include "wx/glcanvas.h" 
  32 #include "wx/mac/uma.h" 
  34 // DLL options compatibility check: 
  36 WX_CHECK_BUILD_OPTIONS("wxGL") 
  39 * GLContext implementation 
  42 wxGLContext::wxGLContext( 
  43                          AGLPixelFormat fmt
, wxGLCanvas 
*win
, 
  44                          const wxPalette
& palette
, 
  45                          const wxGLContext 
*other        
/* for sharing display lists */ 
  50     m_drawable 
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetTopLevelWindowRef())); 
  52     m_glContext 
= aglCreateContext(fmt
, other 
? other
->m_glContext 
: NULL
); 
  53     wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") ); 
  56     b 
= aglSetDrawable(m_glContext
, m_drawable
); 
  57     wxCHECK_RET( b
, wxT("Couldn't bind OpenGl context") ); 
  58     aglEnable(m_glContext 
, AGL_BUFFER_RECT 
) ; 
  59     b 
= aglSetCurrentContext(m_glContext
); 
  60     wxCHECK_RET( b
, wxT("Couldn't activate OpenGl context") ); 
  63 wxGLContext::~wxGLContext() 
  67         aglSetCurrentContext(NULL
); 
  68         aglDestroyContext(m_glContext
); 
  72 void wxGLContext::SwapBuffers() 
  76         aglSwapBuffers(m_glContext
); 
  80 void wxGLContext::SetCurrent() 
  84         aglSetCurrentContext(m_glContext
); 
  88 void wxGLContext::Update() 
  92         aglUpdateContext(m_glContext
); 
  96 void wxGLContext::SetColour(const wxChar 
*colour
) 
  98     wxColour col 
= wxTheColourDatabase
->Find(colour
); 
 101         float r 
= (float)(col
.Red()/256.0); 
 102         float g 
= (float)(col
.Green()/256.0); 
 103         float b 
= (float)(col
.Blue()/256.0); 
 110 * wxGLCanvas implementation 
 113 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
) 
 115 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
) 
 116     EVT_SIZE(wxGLCanvas::OnSize
) 
 119 wxGLCanvas::wxGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 120                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 121                        int *attribList
, const wxPalette
& palette
) 
 123     Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 126 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, 
 127                        const wxGLContext 
*shared
, wxWindowID id
, 
 128                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 129                        int *attribList
, const wxPalette
& palette 
) 
 131     Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 134 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, const wxGLCanvas 
*shared
, wxWindowID id
, 
 135                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 136                        int *attribList
, const wxPalette
& palette 
) 
 138     Create(parent
, shared 
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 141 wxGLCanvas::~wxGLCanvas() 
 143     if (m_glContext 
!= NULL
) { 
 149 static AGLPixelFormat 
ChoosePixelFormat(const int *attribList
) 
 152     GLint defaultAttribs
[] = { AGL_RGBA
,  
 155         AGL_DEPTH_SIZE
, 1,  // use largest available depth buffer 
 164         attribs 
= defaultAttribs
; 
 170         data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX 
 171         while( (attribList
[arg
]!=0) && (p
<512) ) 
 173             switch( attribList
[arg
++] ) 
 175             case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break; 
 176             case WX_GL_BUFFER_SIZE
: 
 177                 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 179                 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break; 
 180             case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break; 
 181             case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break; 
 182             case WX_GL_AUX_BUFFERS
: 
 183                 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break; 
 185                 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 186             case WX_GL_MIN_GREEN
: 
 187                 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 189                 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 190             case WX_GL_MIN_ALPHA
: 
 191                 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 192             case WX_GL_DEPTH_SIZE
:  
 193                 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 194             case WX_GL_STENCIL_SIZE
:  
 195                 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 196             case WX_GL_MIN_ACCUM_RED
: 
 197                 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 198             case WX_GL_MIN_ACCUM_GREEN
: 
 199                 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 200             case WX_GL_MIN_ACCUM_BLUE
: 
 201                 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 202             case WX_GL_MIN_ACCUM_ALPHA
: 
 203                 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 213     return aglChoosePixelFormat(NULL
, 0, attribs
); 
 216 bool wxGLCanvas::Create(wxWindow 
*parent
, const wxGLContext 
*shared
, wxWindowID id
, 
 217                         const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 218                         int *attribList
, const wxPalette
& palette
) 
 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
); 
 263             GetWindowPortBounds( MAC_WXHWND(MacGetTopLevelWindowRef()) , &bounds 
) ; 
 266             parms
[1] = bounds
.bottom 
- bounds
.top 
- ( y 
+ height 
) ; 
 270             if ( !m_macCanvasIsShown 
) 
 272             aglSetInteger( m_glContext
->m_glContext 
, AGL_BUFFER_RECT 
, parms 
) ; 
 276 void wxGLCanvas::OnSize(wxSizeEvent
& event
) 
 281 void wxGLCanvas::MacUpdateView() 
 286         m_glContext
->SetCurrent(); 
 291 void wxGLCanvas::MacSuperChangedPosition() 
 294     wxWindow::MacSuperChangedPosition() ; 
 297 void wxGLCanvas::MacTopLevelWindowChangedPosition() 
 300     wxWindow::MacTopLevelWindowChangedPosition() ; 
 303 void wxGLCanvas::SetCurrent() 
 307         m_glContext
->SetCurrent(); 
 311 void wxGLCanvas::SetColour(const wxChar 
*colour
) 
 314         m_glContext
->SetColour(colour
); 
 317 bool wxGLCanvas::Show(bool show
)  
 319     if ( !wxWindow::Show( show 
) ) 
 324         if ( m_macCanvasIsShown ) 
 326             m_macCanvasIsShown = false ; 
 332         if ( m_peer->IsVisible()&& !m_macCanvasIsShown ) 
 334             m_macCanvasIsShown = true ; 
 342 void wxGLCanvas::MacVisibilityChanged()  
 344     if ( !MacIsReallyShown() ) 
 346         if ( m_macCanvasIsShown 
) 
 348             m_macCanvasIsShown 
= false ; 
 354         if ( !m_macCanvasIsShown 
) 
 356             m_macCanvasIsShown 
= true ; 
 360     wxWindowMac::MacVisibilityChanged() ; 
 363 //--------------------------------------------------------------------------- 
 365 //--------------------------------------------------------------------------- 
 367 IMPLEMENT_CLASS(wxGLApp
, wxApp
) 
 369 bool wxGLApp::InitGLVisual(int *attribList
) 
 371     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 373         aglDestroyPixelFormat(fmt
); 
 379 wxGLApp::~wxGLApp(void) 
 383 #endif // wxUSE_GLCANVAS