]>
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__) 
  24 #include "wx/settings.h" 
  27 #include "wx/glcanvas.h" 
  28 #include "wx/mac/uma.h" 
  30 // DLL options compatibility check: 
  32 WX_CHECK_BUILD_OPTIONS("wxGL") 
  35 * GLContext implementation 
  38 wxGLContext::wxGLContext( 
  39                          AGLPixelFormat fmt
, wxGLCanvas 
*win
, 
  40                          const wxPalette
& palette
, 
  41                          const wxGLContext 
*other        
/* for sharing display lists */ 
  46     m_drawable 
= (AGLDrawable
) UMAGetWindowPort(MAC_WXHWND(win
->MacGetRootWindow())); 
  48     m_glContext 
= aglCreateContext(fmt
, other 
? other
->m_glContext 
: NULL
); 
  49     wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") ); 
  52     b 
= aglSetDrawable(m_glContext
, m_drawable
); 
  53     wxCHECK_RET( b
, wxT("Couldn't bind OpenGl context") ); 
  54     aglEnable(m_glContext 
, AGL_BUFFER_RECT 
) ; 
  55     b 
= aglSetCurrentContext(m_glContext
); 
  56     wxCHECK_RET( b
, wxT("Couldn't activate OpenGl context") ); 
  59 wxGLContext::~wxGLContext() 
  63         aglSetCurrentContext(NULL
); 
  64         aglDestroyContext(m_glContext
); 
  68 void wxGLContext::SwapBuffers() 
  72         aglSwapBuffers(m_glContext
); 
  76 void wxGLContext::SetCurrent() 
  80         aglSetCurrentContext(m_glContext
); 
  84 void wxGLContext::Update() 
  88         aglUpdateContext(m_glContext
); 
  92 void wxGLContext::SetColour(const wxChar 
*colour
) 
  94     wxColour col 
= wxTheColourDatabase
->Find(colour
); 
  97         float r 
= (float)(col
.Red()/256.0); 
  98         float g 
= (float)(col
.Green()/256.0); 
  99         float b 
= (float)(col
.Blue()/256.0); 
 106 * wxGLCanvas implementation 
 109 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
) 
 111 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
) 
 112     EVT_SIZE(wxGLCanvas::OnSize
) 
 115 wxGLCanvas::wxGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 116                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 117                        int *attribList
, const wxPalette
& palette
) 
 119     Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 122 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, 
 123                        const wxGLContext 
*shared
, wxWindowID id
, 
 124                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 125                        int *attribList
, const wxPalette
& palette 
) 
 127     Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 130 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, const wxGLCanvas 
*shared
, wxWindowID id
, 
 131                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 132                        int *attribList
, const wxPalette
& palette 
) 
 134     Create(parent
, shared 
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 137 wxGLCanvas::~wxGLCanvas() 
 139     if (m_glContext 
!= NULL
) { 
 145 static AGLPixelFormat 
ChoosePixelFormat(const int *attribList
) 
 148     GLint defaultAttribs
[] = { AGL_RGBA
, 
 151         AGL_DEPTH_SIZE
, 1,  // use largest available depth buffer 
 160         attribs 
= defaultAttribs
; 
 166         data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX 
 167         while( (attribList
[arg
]!=0) && (p
<512) ) 
 169             switch( attribList
[arg
++] ) 
 171             case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break; 
 172             case WX_GL_BUFFER_SIZE
: 
 173                 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 175                 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break; 
 176             case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break; 
 177             case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break; 
 178             case WX_GL_AUX_BUFFERS
: 
 179                 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break; 
 181                 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 182             case WX_GL_MIN_GREEN
: 
 183                 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 185                 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 186             case WX_GL_MIN_ALPHA
: 
 187                 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 188             case WX_GL_DEPTH_SIZE
: 
 189                 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 190             case WX_GL_STENCIL_SIZE
: 
 191                 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 192             case WX_GL_MIN_ACCUM_RED
: 
 193                 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 194             case WX_GL_MIN_ACCUM_GREEN
: 
 195                 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 196             case WX_GL_MIN_ACCUM_BLUE
: 
 197                 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 198             case WX_GL_MIN_ACCUM_ALPHA
: 
 199                 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 209     return aglChoosePixelFormat(NULL
, 0, attribs
); 
 212 bool wxGLCanvas::Create(wxWindow 
*parent
, const wxGLContext 
*shared
, wxWindowID id
, 
 213                         const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 214                         int *attribList
, const wxPalette
& palette
) 
 216     wxWindow::Create( parent
, id
, pos
, size
, style
, name 
); 
 218     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 219     wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") ); 
 221     m_glContext 
= new wxGLContext(fmt
, this, palette
, shared
); 
 222     m_macCanvasIsShown 
= true ; 
 223     aglDestroyPixelFormat(fmt
); 
 228 void wxGLCanvas::SwapBuffers() 
 231         m_glContext
->SwapBuffers(); 
 234 void wxGLCanvas::UpdateContext() 
 237         m_glContext
->Update(); 
 240 void wxGLCanvas::SetViewport() 
 242     // viewport is initially set to entire port 
 243     // adjust glViewport to just this window 
 247     wxWindow
* iter 
= this ; 
 248     while( iter
->GetParent() ) 
 250         iter 
= iter
->GetParent() ; 
 253     if ( iter 
&& iter
->IsTopLevel() ) 
 255         MacClientToRootWindow( &x 
, &y 
) ; 
 257         GetClientSize(& width
, & height
); 
 259         GetWindowPortBounds( MAC_WXHWND(MacGetRootWindow()) , &bounds 
) ; 
 262         parms
[1] = bounds
.bottom 
- bounds
.top 
- ( y 
+ height 
) ; 
 266         if ( !m_macCanvasIsShown 
) 
 268         aglSetInteger( m_glContext
->m_glContext 
, AGL_BUFFER_RECT 
, parms 
) ; 
 272 void wxGLCanvas::OnSize(wxSizeEvent
& event
) 
 277 void wxGLCanvas::MacUpdateView() 
 282         m_glContext
->SetCurrent(); 
 287 void wxGLCanvas::MacSuperChangedPosition() 
 290     wxWindow::MacSuperChangedPosition() ; 
 293 void wxGLCanvas::MacTopLevelWindowChangedPosition() 
 296     wxWindow::MacTopLevelWindowChangedPosition() ; 
 299 void wxGLCanvas::SetCurrent() 
 303         m_glContext
->SetCurrent(); 
 307 void wxGLCanvas::SetColour(const wxChar 
*colour
) 
 310         m_glContext
->SetColour(colour
); 
 313 bool wxGLCanvas::Show(bool show
) 
 315     if ( !wxWindow::Show( show 
) ) 
 320         if ( m_macCanvasIsShown 
) 
 322             m_macCanvasIsShown 
= false ; 
 328         if ( MacIsReallyShown() && !m_macCanvasIsShown 
) 
 330             m_macCanvasIsShown 
= true ; 
 337 void wxGLCanvas::MacSuperShown( bool show 
) 
 341         if ( m_macCanvasIsShown 
) 
 343             m_macCanvasIsShown 
= false ; 
 349         if ( MacIsReallyShown() && !m_macCanvasIsShown 
) 
 351             m_macCanvasIsShown 
= true ; 
 356     wxWindow::MacSuperShown( show 
) ; 
 359 //--------------------------------------------------------------------------- 
 361 //--------------------------------------------------------------------------- 
 363 IMPLEMENT_CLASS(wxGLApp
, wxApp
) 
 365 bool wxGLApp::InitGLVisual(int *attribList
) 
 367     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 369         aglDestroyPixelFormat(fmt
); 
 375 wxGLApp::~wxGLApp(void) 
 379 #endif // wxUSE_GLCANVAS