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