]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows under Macintosh
8 // Copyright: (c) AUTHOR
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"
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(win
->GetMacRootWindow());
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 char *colour
)
101 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
104 r
= (float)(col
->Red()/256.0);
105 g
= (float)(col
->Green()/256.0);
106 b
= (float)(col
->Blue()/256.0);
113 * wxGLCanvas implementation
116 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
118 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
119 EVT_SIZE(wxGLCanvas::OnSize
)
122 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
123 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
124 int *attribList
, const wxPalette
& palette
)
126 Create(parent
, NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
129 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
130 const wxGLContext
*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
, id
, pos
, size
, style
, name
, attribList
, palette
);
137 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, const wxGLCanvas
*shared
, wxWindowID id
,
138 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
139 int *attribList
, const wxPalette
& palette
)
141 Create(parent
, shared
? shared
->GetContext() : NULL
, id
, pos
, size
, style
, name
, attribList
, palette
);
144 wxGLCanvas::~wxGLCanvas()
150 bool wxGLCanvas::Create(wxWindow
*parent
, const wxGLContext
*shared
, wxWindowID id
,
151 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
152 int *attribList
, const wxPalette
& palette
)
154 m_macEraseOnRedraw
= false ;
156 GLint defs
[] = { AGL_RGBA
,
159 AGL_DEPTH_SIZE
, 1, // use largest available depth buffer
172 int data
[512], arg
=0, p
=0;
174 data
[p
++] = AGL_MINIMUM_POLICY
; // make _SIZE tags behave more like GLX
175 while( (attribList
[arg
]!=0) && (p
<512) )
177 switch( attribList
[arg
++] )
179 case WX_GL_RGBA
: data
[p
++] = AGL_RGBA
; break;
180 case WX_GL_BUFFER_SIZE
:
181 data
[p
++]=AGL_BUFFER_SIZE
; data
[p
++]=attribList
[arg
++]; break;
183 data
[p
++]=AGL_LEVEL
; data
[p
++]=attribList
[arg
++]; break;
184 case WX_GL_DOUBLEBUFFER
: data
[p
++] = AGL_DOUBLEBUFFER
; break;
185 case WX_GL_STEREO
: data
[p
++] = AGL_STEREO
; break;
186 case WX_GL_AUX_BUFFERS
:
187 data
[p
++]=AGL_AUX_BUFFERS
; data
[p
++]=attribList
[arg
++]; break;
189 data
[p
++]=AGL_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
190 case WX_GL_MIN_GREEN
:
191 data
[p
++]=AGL_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
193 data
[p
++]=AGL_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
194 case WX_GL_MIN_ALPHA
:
195 data
[p
++]=AGL_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
196 case WX_GL_DEPTH_SIZE
:
197 data
[p
++]=AGL_DEPTH_SIZE
; data
[p
++]=attribList
[arg
++]; break;
198 case WX_GL_STENCIL_SIZE
:
199 data
[p
++]=AGL_STENCIL_SIZE
; data
[p
++]=attribList
[arg
++]; break;
200 case WX_GL_MIN_ACCUM_RED
:
201 data
[p
++]=AGL_ACCUM_RED_SIZE
; data
[p
++]=attribList
[arg
++]; break;
202 case WX_GL_MIN_ACCUM_GREEN
:
203 data
[p
++]=AGL_ACCUM_GREEN_SIZE
; data
[p
++]=attribList
[arg
++]; break;
204 case WX_GL_MIN_ACCUM_BLUE
:
205 data
[p
++]=AGL_ACCUM_BLUE_SIZE
; data
[p
++]=attribList
[arg
++]; break;
206 case WX_GL_MIN_ACCUM_ALPHA
:
207 data
[p
++]=AGL_ACCUM_ALPHA_SIZE
; data
[p
++]=attribList
[arg
++]; break;
216 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
218 AGLPixelFormat fmt
= aglChoosePixelFormat(NULL
, 0, attribs
);
219 wxCHECK_MSG( fmt
, false, wxT("Couldn't create OpenGl pixel format") );
221 m_glContext
= new wxGLContext(fmt
, this, palette
, shared
);
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 MacClientToRootWindow( &x
, &y
) ;
249 GetClientSize(& width
, & height
);
251 GetWindowPortBounds( GetMacRootWindow() , &bounds
) ;
254 parms
[1] = bounds
.bottom
- bounds
.top
- ( y
+ height
) ;
258 aglSetInteger( m_glContext
->m_glContext
, AGL_BUFFER_RECT
, parms
) ;
261 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
266 m_glContext
->SetCurrent();
269 glMatrixMode(GL_PROJECTION
);
271 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
272 glMatrixMode(GL_MODELVIEW
);
276 void wxGLCanvas::SetCurrent()
280 m_glContext
->SetCurrent();
284 void wxGLCanvas::SetColour(const char *colour
)
287 m_glContext
->SetColour(colour
);
290 #endif // wxUSE_GLCANVAS