]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/glcanvas.cpp
cleanup - reformatting; simplified OnPaint
[wxWidgets.git] / src / gtk / glcanvas.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/glcanvas.cpp
3// Purpose: wxGLCanvas, for using OpenGL/Mesa with wxWidgets 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// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if wxUSE_GLCANVAS
16
17#include "wx/glcanvas.h"
18
19#include "wx/frame.h"
20#include "wx/colour.h"
21#include "wx/module.h"
22#include "wx/app.h"
23
24extern "C"
25{
26#include "gtk/gtk.h"
27#include "gdk/gdk.h"
28#include "gdk/gdkx.h"
29}
30
31#include "wx/gtk/win_gtk.h"
32
33// DLL options compatibility check:
34#include "wx/build.h"
35WX_CHECK_BUILD_OPTIONS("wxGL")
36
37
38//---------------------------------------------------------------------------
39// static variables
40//---------------------------------------------------------------------------
41int wxGLCanvas::m_glxVersion = 0;
42
43//---------------------------------------------------------------------------
44// global data
45//---------------------------------------------------------------------------
46
47XVisualInfo *g_vi = (XVisualInfo*) NULL;
48//-----------------------------------------------------------------------------
49// idle system
50//-----------------------------------------------------------------------------
51
52extern void wxapp_install_idle_handler();
53extern bool g_isIdle;
54
55//---------------------------------------------------------------------------
56// wxGLContext
57//---------------------------------------------------------------------------
58
59IMPLEMENT_CLASS(wxGLContext,wxObject)
60
61wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
62{
63 m_window = win;
64 m_widget = win->m_wxwindow;
65
66 wxGLCanvas *gc = (wxGLCanvas*) win;
67
68 if (wxGLCanvas::GetGLXVersion() >= 13)
69 {
70 // GLX >= 1.3
71 GLXFBConfig *fbc = gc->m_fbc;
72 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
73 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE, None, GL_TRUE );
74 }
75 else
76 {
77 // GLX <= 1.2
78 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
79 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
80 m_glContext = glXCreateContext( GDK_DISPLAY(), vi, None, GL_TRUE );
81 }
82
83 wxCHECK_RET( m_glContext, _T("Couldn't create OpenGl context") );
84}
85
86wxGLContext::wxGLContext(
87 bool WXUNUSED(isRGB), wxWindow *win,
88 const wxPalette& WXUNUSED(palette),
89 const wxGLContext *other /* for sharing display lists */
90)
91{
92 m_window = win;
93 m_widget = win->m_wxwindow;
94
95 wxGLCanvas *gc = (wxGLCanvas*) win;
96
97 if (wxGLCanvas::GetGLXVersion() >= 13)
98 {
99 // GLX >= 1.3
100 GLXFBConfig *fbc = gc->m_fbc;
101 wxCHECK_RET( fbc, _T("invalid GLXFBConfig for OpenGl") );
102 m_glContext = glXCreateNewContext( GDK_DISPLAY(), fbc[0], GLX_RGBA_TYPE,
103 other ? other->m_glContext : None,
104 GL_TRUE );
105 }
106 else
107 {
108 // GLX <= 1.2
109 XVisualInfo *vi = (XVisualInfo *) gc->m_vi;
110 wxCHECK_RET( vi, _T("invalid visual for OpenGl") );
111 m_glContext = glXCreateContext( GDK_DISPLAY(), vi,
112 other ? other->m_glContext : None,
113 GL_TRUE );
114 }
115
116 if ( !m_glContext )
117 {
118 wxFAIL_MSG( _T("Couldn't create OpenGl context") );
119 }
120}
121
122wxGLContext::~wxGLContext()
123{
124 if (!m_glContext) return;
125
126 if (m_glContext == glXGetCurrentContext())
127 {
128 if (wxGLCanvas::GetGLXVersion() >= 13)
129 // GLX >= 1.3
130 glXMakeContextCurrent( GDK_DISPLAY(), None, None, NULL);
131 else
132 // GLX <= 1.2
133 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
134 }
135
136 glXDestroyContext( GDK_DISPLAY(), m_glContext );
137}
138
139void wxGLContext::SwapBuffers()
140{
141 if (m_glContext)
142 {
143 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
144 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) );
145 }
146}
147
148void wxGLContext::SetCurrent()
149{
150 if (m_glContext)
151 {
152 GdkWindow *window = GTK_PIZZA(m_widget)->bin_window;
153
154 if (wxGLCanvas::GetGLXVersion() >= 13)
155 // GLX >= 1.3
156 glXMakeContextCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window),
157 GDK_WINDOW_XWINDOW(window), m_glContext );
158 else
159 // GLX <= 1.2
160 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext );
161 }
162}
163
164void wxGLContext::SetColour(const wxChar *colour)
165{
166 wxColour col = wxTheColourDatabase->Find(colour);
167 if (col.Ok())
168 {
169 float r = (float)(col.Red()/256.0);
170 float g = (float)(col.Green()/256.0);
171 float b = (float)(col.Blue()/256.0);
172 glColor3f( r, g, b);
173 }
174}
175
176void wxGLContext::SetupPixelFormat()
177{
178}
179
180void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
181{
182}
183
184wxPalette wxGLContext::CreateDefaultPalette()
185{
186 return wxNullPalette;
187}
188
189//-----------------------------------------------------------------------------
190// "realize" from m_wxwindow
191//-----------------------------------------------------------------------------
192
193extern "C" {
194static gint
195gtk_glwindow_realized_callback( GtkWidget *WXUNUSED(widget), wxGLCanvas *win )
196{
197 if ( !win->m_glContext )
198 {
199 wxGLContext *share = win->m_sharedContext;
200 if ( !share && win->m_sharedContextOf )
201 share = win->m_sharedContextOf->GetContext();
202
203 win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, share );
204 }
205
206 return FALSE;
207}
208}
209
210//-----------------------------------------------------------------------------
211// "map" from m_wxwindow
212//-----------------------------------------------------------------------------
213
214extern "C" {
215static gint
216gtk_glwindow_map_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win )
217{
218 if (win->m_glContext/* && win->m_exposed*/)
219 {
220 wxPaintEvent event( win->GetId() );
221 event.SetEventObject( win );
222 win->GetEventHandler()->ProcessEvent( event );
223
224 win->m_exposed = FALSE;
225 win->GetUpdateRegion().Clear();
226 }
227
228 return FALSE;
229}
230}
231
232//-----------------------------------------------------------------------------
233// "expose_event" of m_wxwindow
234//-----------------------------------------------------------------------------
235
236extern "C" {
237static void
238gtk_glwindow_expose_callback( GtkWidget *WXUNUSED(widget), GdkEventExpose *gdk_event, wxGLCanvas *win )
239{
240 if (g_isIdle)
241 wxapp_install_idle_handler();
242
243 win->m_exposed = TRUE;
244
245 win->GetUpdateRegion().Union( gdk_event->area.x,
246 gdk_event->area.y,
247 gdk_event->area.width,
248 gdk_event->area.height );
249}
250}
251
252//-----------------------------------------------------------------------------
253// "size_allocate" of m_wxwindow
254//-----------------------------------------------------------------------------
255
256extern "C" {
257static void
258gtk_glcanvas_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxGLCanvas *win )
259{
260 if (g_isIdle)
261 wxapp_install_idle_handler();
262
263 if (!win->m_hasVMT)
264 return;
265
266 wxSizeEvent event( wxSize(win->m_width,win->m_height), win->GetId() );
267 event.SetEventObject( win );
268 win->GetEventHandler()->ProcessEvent( event );
269}
270}
271
272//---------------------------------------------------------------------------
273// wxGlCanvas
274//---------------------------------------------------------------------------
275
276IMPLEMENT_CLASS(wxGLCanvas, wxWindow)
277
278BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow)
279 EVT_SIZE(wxGLCanvas::OnSize)
280END_EVENT_TABLE()
281
282wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id,
283 const wxPoint& pos, const wxSize& size,
284 long style, const wxString& name,
285 int *attribList,
286 const wxPalette& palette )
287{
288 Create( parent, NULL, NULL, id, pos, size, style, name, attribList, palette );
289}
290
291wxGLCanvas::wxGLCanvas( wxWindow *parent,
292 const wxGLContext *shared,
293 wxWindowID id,
294 const wxPoint& pos, const wxSize& size,
295 long style, const wxString& name,
296 int *attribList,
297 const wxPalette& palette )
298{
299 Create( parent, shared, NULL, id, pos, size, style, name, attribList, palette );
300}
301
302wxGLCanvas::wxGLCanvas( wxWindow *parent,
303 const wxGLCanvas *shared,
304 wxWindowID id,
305 const wxPoint& pos, const wxSize& size,
306 long style, const wxString& name,
307 int *attribList,
308 const wxPalette& palette )
309{
310 Create( parent, NULL, shared, id, pos, size, style, name, attribList, palette );
311}
312
313bool wxGLCanvas::Create( wxWindow *parent,
314 const wxGLContext *shared,
315 const wxGLCanvas *shared_context_of,
316 wxWindowID id,
317 const wxPoint& pos, const wxSize& size,
318 long style, const wxString& name,
319 int *attribList,
320 const wxPalette& palette)
321{
322 m_sharedContext = (wxGLContext*)shared; // const_cast
323 m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast
324 m_glContext = (wxGLContext*) NULL;
325
326 m_exposed = FALSE;
327 m_noExpose = TRUE;
328 m_nativeSizeEvent = TRUE;
329 m_fbc = NULL;
330 m_vi = NULL;
331
332 // to be sure the glx version is known
333 wxGLCanvas::QueryGLXVersion();
334
335 if (wxGLCanvas::GetGLXVersion() >= 13)
336 {
337 // GLX >= 1.3 uses a GLXFBConfig
338 GLXFBConfig * fbc = NULL;
339 if (wxTheApp->m_glFBCInfo != NULL)
340 {
341 fbc = (GLXFBConfig *) wxTheApp->m_glFBCInfo;
342 m_canFreeFBC = FALSE; // owned by wxTheApp - don't free upon destruction
343 }
344 else
345 {
346 fbc = (GLXFBConfig *) wxGLCanvas::ChooseGLFBC(attribList);
347 m_canFreeFBC = TRUE;
348 }
349 m_fbc = fbc; // save for later use
350 wxCHECK_MSG( m_fbc, FALSE, _T("required FBConfig couldn't be found") );
351 }
352
353 XVisualInfo *vi = NULL;
354 if (wxTheApp->m_glVisualInfo != NULL)
355 {
356 vi = (XVisualInfo *)wxTheApp->m_glVisualInfo;
357 m_canFreeVi = FALSE; // owned by wxTheApp - don't free upon destruction
358 }
359 else
360 {
361 if (wxGLCanvas::GetGLXVersion() >= 13)
362 // GLX >= 1.3
363 vi = glXGetVisualFromFBConfig(GDK_DISPLAY(), m_fbc[0]);
364 else
365 // GLX <= 1.2
366 vi = (XVisualInfo *) ChooseGLVisual(attribList);
367
368 m_canFreeVi = TRUE;
369 }
370
371 m_vi = vi; // save for later use
372
373 wxCHECK_MSG( m_vi, FALSE, _T("required visual couldn't be found") );
374 GdkVisual *visual;
375 GdkColormap *colormap;
376
377 // MR: This needs a fix for lower gtk+ versions too. Might need to rethink logic (FIXME)
378#if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
379 if (!gtk_check_version(2,2,0))
380 {
381 wxWindow::Create( parent, id, pos, size, style, name );
382
383 m_glWidget = m_wxwindow;
384
385 GdkScreen *screen = gtk_widget_get_screen( m_glWidget );
386 colormap = gdk_screen_get_default_colormap(screen);
387 visual = gdk_colormap_get_visual(colormap);
388
389 if (GDK_VISUAL_XVISUAL(visual)->visualid != vi->visualid)
390 {
391 visual = gdk_x11_screen_lookup_visual( screen, vi->visualid );
392 colormap = gdk_colormap_new(visual, FALSE);
393 }
394
395 gtk_widget_set_colormap( m_glWidget, colormap );
396 }
397 else
398#endif
399 {
400 visual = gdkx_visual_get( vi->visualid );
401 colormap = gdk_colormap_new( visual, TRUE );
402
403 gtk_widget_push_colormap( colormap );
404
405 wxWindow::Create( parent, id, pos, size, style, name );
406 m_glWidget = m_wxwindow;
407 }
408
409 gtk_widget_set_double_buffered( m_glWidget, FALSE );
410
411 gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE );
412
413 g_signal_connect (m_wxwindow, "realize",
414 G_CALLBACK (gtk_glwindow_realized_callback),
415 this);
416 g_signal_connect (m_wxwindow, "map",
417 G_CALLBACK (gtk_glwindow_map_callback),
418 this);
419 g_signal_connect (m_wxwindow, "expose_event",
420 G_CALLBACK (gtk_glwindow_expose_callback),
421 this);
422 g_signal_connect (m_widget, "size_allocate",
423 G_CALLBACK (gtk_glcanvas_size_callback),
424 this);
425
426 if (gtk_check_version(2,2,0) != NULL)
427 {
428 gtk_widget_pop_colormap();
429 }
430
431 // if our parent window is already visible, we had been realized before we
432 // connected to the "realize" signal and hence our m_glContext hasn't been
433 // initialized yet and we have to do it now
434 if (GTK_WIDGET_REALIZED(m_wxwindow))
435 gtk_glwindow_realized_callback( m_wxwindow, this );
436
437 if (GTK_WIDGET_MAPPED(m_wxwindow))
438 gtk_glwindow_map_callback( m_wxwindow, this );
439
440 return TRUE;
441}
442
443wxGLCanvas::~wxGLCanvas()
444{
445 GLXFBConfig * fbc = (GLXFBConfig *) m_fbc;
446 if (fbc && m_canFreeFBC)
447 XFree( fbc );
448
449 XVisualInfo *vi = (XVisualInfo *) m_vi;
450 if (vi && m_canFreeVi)
451 XFree( vi );
452
453 delete m_glContext;
454}
455
456void* wxGLCanvas::ChooseGLVisual(int *attribList)
457{
458 int data[512];
459 GetGLAttribListFromWX( attribList, data );
460 attribList = (int*) data;
461
462 Display *dpy = GDK_DISPLAY();
463
464 return glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
465}
466
467void* wxGLCanvas::ChooseGLFBC(int *attribList)
468{
469 int data[512];
470 GetGLAttribListFromWX( attribList, data );
471 attribList = (int*) data;
472
473 int returned;
474 return glXChooseFBConfig( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()),
475 attribList, &returned );
476}
477
478
479void wxGLCanvas::GetGLAttribListFromWX(int *wx_attribList, int *gl_attribList )
480{
481 if (!wx_attribList)
482 {
483 if (wxGLCanvas::GetGLXVersion() >= 13)
484 // leave GLX >= 1.3 choose the default attributes
485 gl_attribList[0] = 0;
486 else
487 {
488 int i = 0;
489 // default settings if attriblist = 0
490 gl_attribList[i++] = GLX_RGBA;
491 gl_attribList[i++] = GLX_DOUBLEBUFFER;
492 gl_attribList[i++] = GLX_DEPTH_SIZE; gl_attribList[i++] = 1;
493 gl_attribList[i++] = GLX_RED_SIZE; gl_attribList[i++] = 1;
494 gl_attribList[i++] = GLX_GREEN_SIZE; gl_attribList[i++] = 1;
495 gl_attribList[i++] = GLX_BLUE_SIZE; gl_attribList[i++] = 1;
496 gl_attribList[i++] = GLX_ALPHA_SIZE; gl_attribList[i++] = 0;
497 gl_attribList[i++] = None;
498 }
499 }
500 else
501 {
502 int arg=0, p=0;
503 while( (wx_attribList[arg]!=0) && (p<510) )
504 {
505 switch( wx_attribList[arg++] )
506 {
507 case WX_GL_RGBA:
508 if (wxGLCanvas::GetGLXVersion() <= 12)
509 // for GLX >= 1.3, GLX_RGBA is useless (setting this flags will crash on most opengl implm)
510 gl_attribList[p++] = GLX_RGBA;
511 break;
512 case WX_GL_BUFFER_SIZE:
513 gl_attribList[p++] = GLX_BUFFER_SIZE;
514 gl_attribList[p++] = wx_attribList[arg++];
515 break;
516 case WX_GL_LEVEL:
517 gl_attribList[p++] = GLX_LEVEL;
518 gl_attribList[p++] = wx_attribList[arg++];
519 break;
520 case WX_GL_DOUBLEBUFFER:
521 if (wxGLCanvas::GetGLXVersion() <= 12)
522 gl_attribList[p++] = GLX_DOUBLEBUFFER;
523 else
524 // for GLX >= 1.3, GLX_DOUBLEBUFFER format is different (1 <=> True)
525 // it seems this flag is useless for some hardware opengl implementation.
526 // but for Mesa 6.2.1, this flag is used so don't ignore it.
527 gl_attribList[p++] = GLX_DOUBLEBUFFER;
528 gl_attribList[p++] = 1;
529 break;
530 case WX_GL_STEREO:
531 gl_attribList[p++] = GLX_STEREO;
532 break;
533 case WX_GL_AUX_BUFFERS:
534 gl_attribList[p++] = GLX_AUX_BUFFERS;
535 gl_attribList[p++] = wx_attribList[arg++];
536 break;
537 case WX_GL_MIN_RED:
538 gl_attribList[p++] = GLX_RED_SIZE;
539 gl_attribList[p++] = wx_attribList[arg++];
540 break;
541 case WX_GL_MIN_GREEN:
542 gl_attribList[p++] = GLX_GREEN_SIZE;
543 gl_attribList[p++] = wx_attribList[arg++];
544 break;
545 case WX_GL_MIN_BLUE:
546 gl_attribList[p++] = GLX_BLUE_SIZE;
547 gl_attribList[p++] = wx_attribList[arg++];
548 break;
549 case WX_GL_MIN_ALPHA:
550 gl_attribList[p++] = GLX_ALPHA_SIZE;
551 gl_attribList[p++] = wx_attribList[arg++];
552 break;
553 case WX_GL_DEPTH_SIZE:
554 gl_attribList[p++] = GLX_DEPTH_SIZE;
555 gl_attribList[p++] = wx_attribList[arg++];
556 break;
557 case WX_GL_STENCIL_SIZE:
558 gl_attribList[p++] = GLX_STENCIL_SIZE;
559 gl_attribList[p++] = wx_attribList[arg++];
560 break;
561 case WX_GL_MIN_ACCUM_RED:
562 gl_attribList[p++] = GLX_ACCUM_RED_SIZE;
563 gl_attribList[p++] = wx_attribList[arg++];
564 break;
565 case WX_GL_MIN_ACCUM_GREEN:
566 gl_attribList[p++] = GLX_ACCUM_GREEN_SIZE;
567 gl_attribList[p++] = wx_attribList[arg++];
568 break;
569 case WX_GL_MIN_ACCUM_BLUE:
570 gl_attribList[p++] = GLX_ACCUM_BLUE_SIZE;
571 gl_attribList[p++] = wx_attribList[arg++];
572 break;
573 case WX_GL_MIN_ACCUM_ALPHA:
574 gl_attribList[p++] = GLX_ACCUM_ALPHA_SIZE;
575 gl_attribList[p++] = wx_attribList[arg++];
576 break;
577 default:
578 break;
579 }
580 }
581
582 gl_attribList[p] = 0;
583 }
584}
585
586void wxGLCanvas::QueryGLXVersion()
587{
588 if (m_glxVersion == 0)
589 {
590 // check the GLX version
591 int glxMajorVer, glxMinorVer;
592 bool ok = glXQueryVersion(GDK_DISPLAY(), &glxMajorVer, &glxMinorVer);
593 wxASSERT_MSG( ok, _T("GLX version not found") );
594 if (!ok)
595 m_glxVersion = 10; // 1.0 by default
596 else
597 m_glxVersion = glxMajorVer*10 + glxMinorVer;
598 }
599}
600
601int wxGLCanvas::GetGLXVersion()
602{
603 wxASSERT_MSG( m_glxVersion>0, _T("GLX version has not been initialized with wxGLCanvas::QueryGLXVersion()") );
604 return m_glxVersion;
605}
606
607
608void wxGLCanvas::SwapBuffers()
609{
610 if (m_glContext)
611 m_glContext->SwapBuffers();
612}
613
614void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
615{
616}
617
618void wxGLCanvas::SetCurrent()
619{
620 if (m_glContext)
621 m_glContext->SetCurrent();
622}
623
624void wxGLCanvas::SetColour( const wxChar *colour )
625{
626 if (m_glContext)
627 m_glContext->SetColour( colour );
628}
629
630void wxGLCanvas::OnInternalIdle()
631{
632 if (m_glContext && m_exposed)
633 {
634 wxPaintEvent event( GetId() );
635 event.SetEventObject( this );
636 GetEventHandler()->ProcessEvent( event );
637
638 m_exposed = FALSE;
639 GetUpdateRegion().Clear();
640 }
641
642 wxWindow::OnInternalIdle();
643}
644
645
646
647//---------------------------------------------------------------------------
648// wxGLApp
649//---------------------------------------------------------------------------
650
651IMPLEMENT_CLASS(wxGLApp, wxApp)
652
653wxGLApp::~wxGLApp()
654{
655 if (m_glFBCInfo)
656 XFree(m_glFBCInfo);
657 if (m_glVisualInfo)
658 XFree(m_glVisualInfo);
659}
660
661bool wxGLApp::InitGLVisual(int *attribList)
662{
663 wxGLCanvas::QueryGLXVersion();
664
665 if (wxGLCanvas::GetGLXVersion() >= 13)
666 {
667 // GLX >= 1.3
668 if (m_glFBCInfo)
669 XFree(m_glFBCInfo);
670 m_glFBCInfo = wxGLCanvas::ChooseGLFBC(attribList);
671
672 if (m_glFBCInfo)
673 {
674 if (m_glVisualInfo)
675 XFree(m_glVisualInfo);
676 m_glVisualInfo = glXGetVisualFromFBConfig(GDK_DISPLAY(), ((GLXFBConfig *)m_glFBCInfo)[0]);
677 }
678 return (m_glFBCInfo != NULL) && (m_glVisualInfo != NULL);
679 }
680 else
681 {
682 // GLX <= 1.2
683 if (m_glVisualInfo)
684 XFree(m_glVisualInfo);
685 m_glVisualInfo = wxGLCanvas::ChooseGLVisual(attribList);
686 return m_glVisualInfo != NULL;
687 }
688}
689
690#endif
691 // wxUSE_GLCANVAS