]>
Commit | Line | Data |
---|---|---|
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 | // idle system | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | extern void wxapp_install_idle_handler(); | |
42 | extern bool g_isIdle; | |
43 | ||
44 | //--------------------------------------------------------------------------- | |
45 | // wxGLContext | |
46 | //--------------------------------------------------------------------------- | |
47 | ||
48 | IMPLEMENT_CLASS(wxGLContext,wxObject) | |
49 | ||
50 | wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) ) | |
51 | { | |
52 | m_window = win; | |
53 | m_widget = win->m_wxwindow; | |
54 | ||
55 | wxGLCanvas *gc = (wxGLCanvas*) win; | |
56 | XVisualInfo *vi = (XVisualInfo *) gc->m_vi; | |
57 | ||
58 | wxCHECK_RET( vi, "invalid visual for OpenGl" ); | |
59 | ||
60 | m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE ); | |
61 | ||
62 | wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" ); | |
63 | } | |
64 | ||
65 | wxGLContext::wxGLContext( | |
66 | bool WXUNUSED(isRGB), wxWindow *win, | |
67 | const wxPalette& WXUNUSED(palette), | |
68 | const wxGLContext *other /* for sharing display lists */ | |
69 | ) | |
70 | { | |
71 | m_window = win; | |
72 | m_widget = win->m_wxwindow; | |
73 | ||
74 | wxGLCanvas *gc = (wxGLCanvas*) win; | |
75 | XVisualInfo *vi = (XVisualInfo *) gc->m_vi; | |
76 | ||
77 | wxCHECK_RET( vi, "invalid visual for OpenGl" ); | |
78 | ||
79 | if( other != 0 ) | |
80 | m_glContext = glXCreateContext( GDK_DISPLAY(), vi, other->m_glContext, GL_TRUE ); | |
81 | else | |
82 | m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE ); | |
83 | ||
84 | wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" ); | |
85 | } | |
86 | ||
87 | wxGLContext::~wxGLContext() | |
88 | { | |
89 | if (!m_glContext) return; | |
90 | ||
91 | if (m_glContext == glXGetCurrentContext()) | |
92 | { | |
93 | glXMakeCurrent( GDK_DISPLAY(), None, NULL); | |
94 | } | |
95 | ||
96 | glXDestroyContext( GDK_DISPLAY(), m_glContext ); | |
97 | } | |
98 | ||
99 | void wxGLContext::SwapBuffers() | |
100 | { | |
101 | if (m_glContext) | |
102 | { | |
103 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; | |
104 | glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) ); | |
105 | } | |
106 | } | |
107 | ||
108 | void wxGLContext::SetCurrent() | |
109 | { | |
110 | if (m_glContext) | |
111 | { | |
112 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; | |
113 | glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext ); | |
114 | } | |
115 | } | |
116 | ||
117 | void wxGLContext::SetColour(const char *colour) | |
118 | { | |
119 | float r = 0.0; | |
120 | float g = 0.0; | |
121 | float b = 0.0; | |
122 | wxColour *col = wxTheColourDatabase->FindColour(colour); | |
123 | if (col) | |
124 | { | |
125 | r = (float)(col->Red()/256.0); | |
126 | g = (float)(col->Green()/256.0); | |
127 | b = (float)(col->Blue()/256.0); | |
128 | glColor3f( r, g, b); | |
129 | } | |
130 | } | |
131 | ||
132 | void wxGLContext::SetupPixelFormat() | |
133 | { | |
134 | } | |
135 | ||
136 | void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) ) | |
137 | { | |
138 | } | |
139 | ||
140 | wxPalette wxGLContext::CreateDefaultPalette() | |
141 | { | |
142 | return wxNullPalette; | |
143 | } | |
144 | ||
145 | //----------------------------------------------------------------------------- | |
146 | // "realize" from m_wxwindow | |
147 | //----------------------------------------------------------------------------- | |
148 | ||
149 | static gint | |
150 | gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
151 | { | |
152 | wxGLContext *share= win->m_sharedContext; | |
153 | if (share==NULL && win->m_sharedContextOf) share=win->m_sharedContextOf->GetContext(); | |
154 | ||
155 | win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share ); | |
156 | ||
157 | return FALSE; | |
158 | } | |
159 | ||
160 | //----------------------------------------------------------------------------- | |
161 | // "map" from m_wxwindow | |
162 | //----------------------------------------------------------------------------- | |
163 | ||
164 | static gint | |
165 | gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
166 | { | |
167 | if (win->m_glContext/* && win->m_exposed*/) | |
168 | { | |
169 | wxPaintEvent event( win->GetId() ); | |
170 | event.SetEventObject( win ); | |
171 | win->GetEventHandler()->ProcessEvent( event ); | |
172 | ||
173 | win->m_exposed = FALSE; | |
174 | win->GetUpdateRegion().Clear(); | |
175 | } | |
176 | ||
177 | return FALSE; | |
178 | } | |
179 | ||
180 | //----------------------------------------------------------------------------- | |
181 | // "expose_event" of m_wxwindow | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | static void | |
185 | gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win ) | |
186 | { | |
187 | if (g_isIdle) | |
188 | wxapp_install_idle_handler(); | |
189 | ||
190 | win->m_exposed = TRUE; | |
191 | ||
192 | win->GetUpdateRegion().Union( gdk_event->area.x, | |
193 | gdk_event->area.y, | |
194 | gdk_event->area.width, | |
195 | gdk_event->area.height ); | |
196 | } | |
197 | ||
198 | //----------------------------------------------------------------------------- | |
199 | // "draw" of m_wxwindow | |
200 | //----------------------------------------------------------------------------- | |
201 | ||
202 | static void | |
203 | gtk_glwindow_draw_callback( GtkWidget *WXUNUSED(widget), GdkRectangle *rect, wxGLCanvas *win ) | |
204 | { | |
205 | if (g_isIdle) | |
206 | wxapp_install_idle_handler(); | |
207 | ||
208 | win->m_exposed = TRUE; | |
209 | ||
210 | win->GetUpdateRegion().Union( rect->x, rect->y, | |
211 | rect->width, rect->height ); | |
212 | } | |
213 | ||
214 | //----------------------------------------------------------------------------- | |
215 | // "size_allocate" of m_wxwindow | |
216 | //----------------------------------------------------------------------------- | |
217 | ||
218 | static void | |
219 | gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win ) | |
220 | { | |
221 | if (g_isIdle) | |
222 | wxapp_install_idle_handler(); | |
223 | ||
224 | if (!win->m_hasVMT) | |
225 | return; | |
226 | ||
227 | wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() ); | |
228 | event.SetEventObject( win ); | |
229 | win->GetEventHandler()->ProcessEvent( event ); | |
230 | } | |
231 | ||
232 | //--------------------------------------------------------------------------- | |
233 | // wxGlCanvas | |
234 | //--------------------------------------------------------------------------- | |
235 | ||
236 | IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow) | |
237 | ||
238 | BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow) | |
239 | EVT_SIZE(wxGLCanvas::OnSize) | |
240 | END_EVENT_TABLE() | |
241 | ||
242 | wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id, | |
243 | const wxPoint& pos, const wxSize& size, | |
244 | long style, const wxString& name, | |
245 | int *attribList, | |
246 | const wxPalette& palette ) | |
247 | { | |
248 | Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette ); | |
249 | } | |
250 | ||
251 | wxGLCanvas::wxGLCanvas( wxWindow *parent, | |
252 | const wxGLContext *shared, | |
253 | wxWindowID id, | |
254 | const wxPoint& pos, const wxSize& size, | |
255 | long style, const wxString& name, | |
256 | int *attribList, | |
257 | const wxPalette& palette ) | |
258 | { | |
259 | Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette ); | |
260 | } | |
261 | ||
262 | wxGLCanvas::wxGLCanvas( wxWindow *parent, | |
263 | const wxGLCanvas *shared, | |
264 | wxWindowID id, | |
265 | const wxPoint& pos, const wxSize& size, | |
266 | long style, const wxString& name, | |
267 | int *attribList, | |
268 | const wxPalette& palette ) | |
269 | { | |
270 | Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette ); | |
271 | } | |
272 | ||
273 | bool wxGLCanvas::Create( wxWindow *parent, | |
274 | const wxGLContext *shared, | |
275 | const wxGLCanvas *shared_context_of, | |
276 | wxWindowID id, | |
277 | const wxPoint& pos, const wxSize& size, | |
278 | long style, const wxString& name, | |
279 | int *attribList, | |
280 | const wxPalette& palette) | |
281 | { | |
282 | m_sharedContext = (wxGLContext*)shared; // const_cast | |
283 | m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast | |
284 | m_glContext = (wxGLContext*) NULL; | |
285 | ||
286 | m_exposed = FALSE; | |
287 | m_noExpose = TRUE; | |
288 | m_nativeSizeEvent = TRUE; | |
289 | ||
290 | if (!attribList) | |
291 | { | |
292 | int data[] = { GLX_RGBA, | |
293 | GLX_DOUBLEBUFFER, | |
294 | GLX_DEPTH_SIZE, 1, // use largest available depth buffer | |
295 | GLX_RED_SIZE, 1, | |
296 | GLX_GREEN_SIZE, 1, | |
297 | GLX_BLUE_SIZE, 1, | |
298 | GLX_ALPHA_SIZE, 0, | |
299 | None }; | |
300 | attribList = (int*) data; | |
301 | } | |
302 | else | |
303 | { | |
304 | int data[512], arg=0, p=0; | |
305 | ||
306 | while( (attribList[arg]!=0) && (p<512) ) | |
307 | { | |
308 | switch( attribList[arg++] ) | |
309 | { | |
310 | case WX_GL_RGBA: data[p++] = GLX_RGBA; break; | |
311 | case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break; | |
312 | case WX_GL_DEPTH_SIZE: | |
313 | data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break; | |
314 | case WX_GL_MIN_RED: | |
315 | data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break; | |
316 | case WX_GL_MIN_GREEN: | |
317 | data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break; | |
318 | case WX_GL_MIN_BLUE: | |
319 | data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break; | |
320 | default: | |
321 | break; | |
322 | } | |
323 | } | |
324 | data[p] = 0; | |
325 | ||
326 | attribList = (int*) data; | |
327 | } | |
328 | ||
329 | ||
330 | Display *dpy = GDK_DISPLAY(); | |
331 | ||
332 | XVisualInfo *vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList ); | |
333 | ||
334 | m_vi = vi; // safe for later use | |
335 | ||
336 | wxCHECK_MSG( m_vi, FALSE, "required visual couldn't be found" ); | |
337 | ||
338 | GdkVisual *visual = gdkx_visual_get( vi->visualid ); | |
339 | GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(vi->visualid), TRUE ); | |
340 | ||
341 | gtk_widget_push_colormap( colormap ); | |
342 | gtk_widget_push_visual( visual ); | |
343 | ||
344 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
345 | ||
346 | m_glWidget = m_wxwindow; | |
347 | ||
348 | gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE ); | |
349 | ||
350 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize", | |
351 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this ); | |
352 | ||
353 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "map", | |
354 | GTK_SIGNAL_FUNC(gtk_glwindow_map_callback), (gpointer) this ); | |
355 | ||
356 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event", | |
357 | GTK_SIGNAL_FUNC(gtk_glwindow_expose_callback), (gpointer)this ); | |
358 | ||
359 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw", | |
360 | GTK_SIGNAL_FUNC(gtk_glwindow_draw_callback), (gpointer)this ); | |
361 | ||
362 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", | |
363 | GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this ); | |
364 | ||
365 | gtk_widget_pop_visual(); | |
366 | gtk_widget_pop_colormap(); | |
367 | ||
368 | return TRUE; | |
369 | } | |
370 | ||
371 | wxGLCanvas::~wxGLCanvas() | |
372 | { | |
373 | XVisualInfo *vi = (XVisualInfo *) m_vi; | |
374 | ||
375 | if (vi) XFree( vi ); | |
376 | if (m_glContext) delete m_glContext; | |
377 | } | |
378 | ||
379 | void wxGLCanvas::SwapBuffers() | |
380 | { | |
381 | if (m_glContext) m_glContext->SwapBuffers(); | |
382 | } | |
383 | ||
384 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
385 | { | |
386 | int width, height; | |
387 | GetClientSize( &width, &height ); | |
388 | ||
389 | if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) ) | |
390 | { | |
391 | SetCurrent(); | |
392 | ||
393 | glViewport(0, 0, (GLint)width, (GLint)height ); | |
394 | glMatrixMode(GL_PROJECTION); | |
395 | glLoadIdentity(); | |
396 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 ); | |
397 | glMatrixMode(GL_MODELVIEW); | |
398 | } | |
399 | } | |
400 | ||
401 | void wxGLCanvas::SetCurrent() | |
402 | { | |
403 | if (m_glContext) m_glContext->SetCurrent(); | |
404 | } | |
405 | ||
406 | void wxGLCanvas::SetColour( const char *colour ) | |
407 | { | |
408 | if (m_glContext) m_glContext->SetColour( colour ); | |
409 | } | |
410 | ||
411 | void wxGLCanvas::OnInternalIdle() | |
412 | { | |
413 | if (m_glContext && m_exposed) | |
414 | { | |
415 | wxPaintEvent event( GetId() ); | |
416 | event.SetEventObject( this ); | |
417 | GetEventHandler()->ProcessEvent( event ); | |
418 | ||
419 | m_exposed = FALSE; | |
420 | GetUpdateRegion().Clear(); | |
421 | } | |
422 | ||
423 | wxWindow::OnInternalIdle(); | |
424 | } |