]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/gtk/glcanvas.cpp
2095f0fbc2a59833272b1d60a8fe703d30b66bf4
[wxWidgets.git] / utils / glcanvas / gtk / glcanvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: glcanvas.cpp
3 // Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWindows and GTK
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 17/08/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "glcanvas.h"
14 #endif
15
16 #include "glcanvas.h"
17
18 #include "wx/frame.h"
19 #include "wx/colour.h"
20 #include "wx/module.h"
21 #include "wx/app.h"
22
23 extern "C" {
24 #include "gtk/gtk.h"
25 #include "gdk/gdk.h"
26 #include "gdk/gdkx.h"
27 }
28
29 #include "wx/gtk/win_gtk.h"
30
31 //---------------------------------------------------------------------------
32 // global data
33 //---------------------------------------------------------------------------
34
35 XVisualInfo *g_vi = (XVisualInfo*) NULL;
36
37 //---------------------------------------------------------------------------
38 // wxGLContext
39 //---------------------------------------------------------------------------
40
41 IMPLEMENT_CLASS(wxGLContext,wxObject)
42
43 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
44 {
45 m_window = win;
46 m_widget = ((wxGLCanvas*)win)->m_glWidget;
47
48 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
49
50 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
51
52 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
53 }
54
55 wxGLContext::wxGLContext(
56 bool WXUNUSED(isRGB), wxWindow *win,
57 const wxPalette& WXUNUSED(palette),
58 const wxGLContext *other /* for sharing display lists */
59 )
60 {
61 m_window = win;
62 m_widget = ((wxGLCanvas*)win)->m_glWidget;
63
64 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
65
66 if( other != 0 )
67 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, other->m_glContext,
68 GL_TRUE );
69 else
70 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
71
72 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
73 }
74
75 wxGLContext::~wxGLContext()
76 {
77 if (!m_glContext) return;
78
79 if (m_glContext == glXGetCurrentContext())
80 {
81 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
82 }
83
84 glXDestroyContext( GDK_DISPLAY(), m_glContext );
85 }
86
87 void wxGLContext::SwapBuffers()
88 {
89 if (m_glContext)
90 {
91 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
92 }
93 }
94
95 void wxGLContext::SetCurrent()
96 {
97 if (m_glContext)
98 {
99 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
100 }
101 }
102
103 void wxGLContext::SetColour(const char *colour)
104 {
105 float r = 0.0;
106 float g = 0.0;
107 float b = 0.0;
108 wxColour *col = wxTheColourDatabase->FindColour(colour);
109 if (col)
110 {
111 r = (float)(col->Red()/256.0);
112 g = (float)(col->Green()/256.0);
113 b = (float)(col->Blue()/256.0);
114 glColor3f( r, g, b);
115 }
116 }
117
118 void wxGLContext::SetupPixelFormat()
119 {
120 }
121
122 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
123 {
124 }
125
126 wxPalette wxGLContext::CreateDefaultPalette()
127 {
128 return wxNullPalette;
129 }
130
131 //-----------------------------------------------------------------------------
132 // "realize" from m_wxwindow
133 //-----------------------------------------------------------------------------
134
135 static gint
136 gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
137 {
138 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, win->m_glContext );
139
140 XFree( g_vi );
141 g_vi = (XVisualInfo*) NULL;
142
143 return FALSE;
144 }
145
146 //---------------------------------------------------------------------------
147 // wxGlCanvas
148 //---------------------------------------------------------------------------
149
150 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
151
152 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
153 EVT_SIZE(wxGLCanvas::OnSize)
154 END_EVENT_TABLE()
155
156 wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
157 const wxPoint& pos, const wxSize& size,
158 long style, const wxString& name,
159 int *attribList,
160 const wxPalette& palette )
161 {
162 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
163 }
164
165 wxGLCanvas::wxGLCanvas( wxWindow *parent,
166 const wxGLContext *shared,
167 wxWindowID id,
168 const wxPoint& pos, const wxSize& size,
169 long style, const wxString& name,
170 int *attribList,
171 const wxPalette& palette )
172 {
173 Create( parent, shared, id, pos, size, style, name, attribList, palette );
174 }
175
176 bool wxGLCanvas::Create( wxWindow *parent,
177 const wxGLContext *shared,
178 wxWindowID id,
179 const wxPoint& pos, const wxSize& size,
180 long style, const wxString& name,
181 int *attribList,
182 const wxPalette& palette)
183 {
184 m_glContext = (wxGLContext*)shared; // const_cast
185
186 if (!attribList)
187 {
188 int data[] = { GLX_RGBA,
189 GLX_DOUBLEBUFFER,
190 GLX_DEPTH_SIZE, 1, // use largest available depth buffer
191 GLX_RED_SIZE, 1,
192 GLX_GREEN_SIZE, 1,
193 GLX_BLUE_SIZE, 1,
194 GLX_ALPHA_SIZE, 0,
195 None };
196 attribList = (int*) data;
197 }
198 else
199 {
200 int data[512], arg=0, p=0;
201
202 while( (attribList[arg]!=0) && (p<512) )
203 {
204 switch( attribList[arg++] )
205 {
206 case WX_GL_RGBA: data[p++] = GLX_RGBA; break;
207 case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break;
208 case WX_GL_DEPTH_SIZE:
209 data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break;
210 case WX_GL_MIN_RED:
211 data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break;
212 case WX_GL_MIN_GREEN:
213 data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break;
214 case WX_GL_MIN_BLUE:
215 data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break;
216 default:
217 break;
218 }
219 }
220 data[p] = 0;
221
222 attribList = (int*) data;
223 }
224
225
226 Display *dpy = GDK_DISPLAY();
227
228 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
229
230 wxCHECK_MSG( g_vi, FALSE, "required visual couldn't be found" );
231
232 GdkVisual *visual = gdkx_visual_get( g_vi->visualid );
233 GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE );
234
235 gtk_widget_push_colormap( colormap );
236 gtk_widget_push_visual( visual );
237
238 wxScrolledWindow::Create( parent, id, pos, size, style, name );
239
240 m_glWidget = m_wxwindow;
241
242 gtk_signal_connect( GTK_OBJECT(m_glWidget), "realize",
243 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
244 gtk_widget_pop_visual();
245 gtk_widget_pop_colormap();
246
247 return TRUE;
248 }
249
250 wxGLCanvas::~wxGLCanvas()
251 {
252 if (m_glContext) delete m_glContext;
253 }
254
255 void wxGLCanvas::SwapBuffers()
256 {
257 if (m_glContext) m_glContext->SwapBuffers();
258 }
259
260 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
261 {
262 int width, height;
263 GetClientSize( &width, &height );
264
265 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
266 {
267 SetCurrent();
268
269 glViewport(0, 0, (GLint)width, (GLint)height );
270 glMatrixMode(GL_PROJECTION);
271 glLoadIdentity();
272 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
273 glMatrixMode(GL_MODELVIEW);
274 }
275 }
276
277 void wxGLCanvas::SetCurrent()
278 {
279 if (m_glContext) m_glContext->SetCurrent();
280 }
281
282 void wxGLCanvas::SetColour( const char *colour )
283 {
284 if (m_glContext) m_glContext->SetColour( colour );
285 }
286
287 GtkWidget *wxGLCanvas::GetConnectWidget()
288 {
289 return m_wxwindow;
290 }
291
292 bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
293 {
294 return (window == m_wxwindow->window);
295 }