]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/glcanvas.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxGLCanvas, for using OpenGL with wxWindows under Macintosh 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) Stefan Csomor 
   9 // Licence:       wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation "glcanvas.h" 
  16 #include "wx/wxprec.h" 
  18 #if defined(__BORLANDC__) 
  30 #include "wx/settings.h" 
  33 #include "wx/glcanvas.h" 
  34 #include "wx/mac/uma.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
->MacGetRootWindow())); 
  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
) 
  99     wxColour 
*col 
= wxTheColourDatabase
->FindColour(colour
); 
 102         r 
= (float)(col
->Red()/256.0); 
 103         g 
= (float)(col
->Green()/256.0); 
 104         b 
= (float)(col
->Blue()/256.0); 
 111 * wxGLCanvas implementation 
 114 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
) 
 116 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
) 
 117     EVT_SIZE(wxGLCanvas::OnSize
) 
 120 wxGLCanvas::wxGLCanvas(wxWindow 
*parent
, wxWindowID id
, 
 121                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 122                        int *attribList
, const wxPalette
& palette
) 
 124     Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 127 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, 
 128                        const wxGLContext 
*shared
, wxWindowID id
, 
 129                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 130                        int *attribList
, const wxPalette
& palette 
) 
 132     Create(parent
, shared
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 135 wxGLCanvas::wxGLCanvas( wxWindow 
*parent
, const wxGLCanvas 
*shared
, wxWindowID id
, 
 136                        const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 137                        int *attribList
, const wxPalette
& palette 
) 
 139     Create(parent
, shared 
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
); 
 142 wxGLCanvas::~wxGLCanvas() 
 144     if (m_glContext 
!= NULL
) { 
 150 static AGLPixelFormat 
ChoosePixelFormat(const int *attribList
) 
 153     GLint defaultAttribs
[] = { AGL_RGBA
,  
 156         AGL_DEPTH_SIZE
, 1,  // use largest available depth buffer 
 165         attribs 
= defaultAttribs
; 
 171         data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX 
 172         while( (attribList
[arg
]!=0) && (p
<512) ) 
 174             switch( attribList
[arg
++] ) 
 176             case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break; 
 177             case WX_GL_BUFFER_SIZE
: 
 178                 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 180                 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break; 
 181             case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break; 
 182             case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break; 
 183             case WX_GL_AUX_BUFFERS
: 
 184                 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break; 
 186                 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 187             case WX_GL_MIN_GREEN
: 
 188                 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 190                 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 191             case WX_GL_MIN_ALPHA
: 
 192                 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 193             case WX_GL_DEPTH_SIZE
:  
 194                 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 195             case WX_GL_STENCIL_SIZE
:  
 196                 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 197             case WX_GL_MIN_ACCUM_RED
: 
 198                 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 199             case WX_GL_MIN_ACCUM_GREEN
: 
 200                 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 201             case WX_GL_MIN_ACCUM_BLUE
: 
 202                 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 203             case WX_GL_MIN_ACCUM_ALPHA
: 
 204                 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break; 
 214     return aglChoosePixelFormat(NULL
, 0, attribs
); 
 217 bool wxGLCanvas::Create(wxWindow 
*parent
, const wxGLContext 
*shared
, wxWindowID id
, 
 218                         const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
, 
 219                         int *attribList
, const wxPalette
& palette
) 
 221     wxWindow::Create( parent
, id
, pos
, size
, style
, name 
); 
 223     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 224     wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") ); 
 226     m_glContext 
= new wxGLContext(fmt
, this, palette
, shared
); 
 227     m_macCanvasIsShown 
= true ; 
 228     aglDestroyPixelFormat(fmt
); 
 233 void wxGLCanvas::SwapBuffers() 
 236         m_glContext
->SwapBuffers(); 
 239 void wxGLCanvas::UpdateContext() 
 242         m_glContext
->Update(); 
 245 void wxGLCanvas::SetViewport() 
 247     // viewport is initially set to entire port 
 248     // adjust glViewport to just this window 
 252     wxWindow
* iter 
= this ; 
 253     while( iter
->GetParent() ) 
 255         iter 
= iter
->GetParent() ; 
 258     if ( iter 
&& iter
->IsTopLevel() ) 
 260             MacClientToRootWindow( &x 
, &y 
) ; 
 262             GetClientSize(& width
, & height
); 
 264             GetWindowPortBounds( MAC_WXHWND(MacGetRootWindow()) , &bounds 
) ; 
 267             parms
[1] = bounds
.bottom 
- bounds
.top 
- ( y 
+ height 
) ; 
 271             if ( !m_macCanvasIsShown 
) 
 273             aglSetInteger( m_glContext
->m_glContext 
, AGL_BUFFER_RECT 
, parms 
) ; 
 277 void wxGLCanvas::OnSize(wxSizeEvent
& event
) 
 282 void wxGLCanvas::MacUpdateView() 
 287         m_glContext
->SetCurrent(); 
 292 void wxGLCanvas::MacSuperChangedPosition() 
 295     wxWindow::MacSuperChangedPosition() ; 
 298 void wxGLCanvas::MacTopLevelWindowChangedPosition() 
 301     wxWindow::MacTopLevelWindowChangedPosition() ; 
 304 void wxGLCanvas::SetCurrent() 
 308         m_glContext
->SetCurrent(); 
 312 void wxGLCanvas::SetColour(const wxChar 
*colour
) 
 315         m_glContext
->SetColour(colour
); 
 318 bool wxGLCanvas::Show(bool show
)  
 320     if ( !wxWindow::Show( show 
) ) 
 325         if ( m_macCanvasIsShown 
) 
 327             m_macCanvasIsShown 
= false ; 
 333         if ( MacIsReallyShown() && !m_macCanvasIsShown 
) 
 335             m_macCanvasIsShown 
= true ; 
 342 void wxGLCanvas::MacSuperShown( bool show 
)  
 346         if ( m_macCanvasIsShown 
) 
 348             m_macCanvasIsShown 
= false ; 
 354         if ( MacIsReallyShown() && !m_macCanvasIsShown 
) 
 356             m_macCanvasIsShown 
= true ; 
 361     wxWindow::MacSuperShown( show 
) ; 
 364 //--------------------------------------------------------------------------- 
 366 //--------------------------------------------------------------------------- 
 368 IMPLEMENT_CLASS(wxGLApp
, wxApp
) 
 370 bool wxGLApp::InitGLVisual(int *attribList
) 
 372     AGLPixelFormat fmt 
= ChoosePixelFormat(attribList
); 
 374         aglDestroyPixelFormat(fmt
); 
 380 wxGLApp::~wxGLApp(void) 
 384 #endif // wxUSE_GLCANVAS