]>
Commit | Line | Data |
---|---|---|
9d3221ab RR |
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 | ||
bbe0af5b | 16 | #include "glcanvas.h" |
9d3221ab RR |
17 | |
18 | #include "wx/frame.h" | |
19 | #include "wx/colour.h" | |
bbe0af5b RR |
20 | #include "wx/module.h" |
21 | #include "wx/app.h" | |
22 | ||
aae24d21 | 23 | extern "C" { |
bbe0af5b RR |
24 | #include "gtk/gtk.h" |
25 | #include "gdk/gdk.h" | |
bbe0af5b RR |
26 | #include "gdk/gdkx.h" |
27 | } | |
28 | ||
aae24d21 RR |
29 | #include "wx/gtk/win_gtk.h" |
30 | ||
bbe0af5b | 31 | //--------------------------------------------------------------------------- |
aae24d21 | 32 | // global data |
bbe0af5b RR |
33 | //--------------------------------------------------------------------------- |
34 | ||
aae24d21 | 35 | XVisualInfo *g_vi = (XVisualInfo*) NULL; |
9d3221ab | 36 | |
54517652 RR |
37 | //----------------------------------------------------------------------------- |
38 | // idle system | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | extern void wxapp_install_idle_handler(); | |
42 | extern bool g_isIdle; | |
43 | ||
9d3221ab RR |
44 | //--------------------------------------------------------------------------- |
45 | // wxGLContext | |
46 | //--------------------------------------------------------------------------- | |
47 | ||
48 | IMPLEMENT_CLASS(wxGLContext,wxObject) | |
49 | ||
50 | wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) ) | |
51 | { | |
bbe0af5b | 52 | m_window = win; |
aae24d21 | 53 | m_widget = ((wxGLCanvas*)win)->m_glWidget; |
9d3221ab | 54 | |
aae24d21 | 55 | wxCHECK_RET( g_vi, "invalid visual for OpenGl" ); |
bbe0af5b | 56 | |
aae24d21 | 57 | m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE ); |
9d3221ab | 58 | |
aae24d21 | 59 | wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" ); |
9d3221ab RR |
60 | } |
61 | ||
089e55d6 RR |
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 | ||
9d3221ab RR |
82 | wxGLContext::~wxGLContext() |
83 | { | |
aae24d21 | 84 | if (!m_glContext) return; |
9d3221ab | 85 | |
aae24d21 RR |
86 | if (m_glContext == glXGetCurrentContext()) |
87 | { | |
88 | glXMakeCurrent( GDK_DISPLAY(), None, NULL); | |
bbe0af5b | 89 | } |
aae24d21 RR |
90 | |
91 | glXDestroyContext( GDK_DISPLAY(), m_glContext ); | |
9d3221ab RR |
92 | } |
93 | ||
94 | void wxGLContext::SwapBuffers() | |
95 | { | |
bbe0af5b RR |
96 | if (m_glContext) |
97 | { | |
da048e3d | 98 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; |
147bc491 | 99 | glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( window ) ); |
bbe0af5b | 100 | } |
9d3221ab RR |
101 | } |
102 | ||
103 | void wxGLContext::SetCurrent() | |
104 | { | |
bbe0af5b RR |
105 | if (m_glContext) |
106 | { | |
da048e3d | 107 | GdkWindow *window = GTK_PIZZA(m_widget)->bin_window; |
147bc491 | 108 | glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(window), m_glContext ); |
bbe0af5b | 109 | } |
9d3221ab RR |
110 | } |
111 | ||
112 | void wxGLContext::SetColour(const char *colour) | |
113 | { | |
bbe0af5b RR |
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 | } | |
9d3221ab RR |
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 | { | |
bbe0af5b | 137 | return wxNullPalette; |
9d3221ab RR |
138 | } |
139 | ||
1d61ebac RR |
140 | //----------------------------------------------------------------------------- |
141 | // "realize" from m_wxwindow | |
142 | //----------------------------------------------------------------------------- | |
143 | ||
144 | static gint | |
145 | gtk_glwindow_realized_callback( GtkWidget * WXUNUSED(widget), wxGLCanvas *win ) | |
146 | { | |
147bc491 | 147 | win->m_glContext = new wxGLContext( TRUE, win, wxNullPalette, win->m_sharedContext ); |
1d61ebac | 148 | |
f7a11f8c RR |
149 | XFree( g_vi ); |
150 | g_vi = (XVisualInfo*) NULL; | |
151 | ||
1d61ebac RR |
152 | return FALSE; |
153 | } | |
154 | ||
147bc491 RR |
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 | { | |
54517652 RR |
162 | if (g_isIdle) |
163 | wxapp_install_idle_handler(); | |
164 | ||
147bc491 RR |
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 | { | |
54517652 RR |
180 | if (g_isIdle) |
181 | wxapp_install_idle_handler(); | |
182 | ||
147bc491 RR |
183 | win->m_exposed = TRUE; |
184 | ||
185 | win->GetUpdateRegion().Union( rect->x, rect->y, | |
186 | rect->width, rect->height ); | |
187 | } | |
188 | ||
30760ce7 RR |
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 | ||
9d3221ab RR |
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 | ||
aae24d21 RR |
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 | { | |
089e55d6 RR |
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 ); | |
aae24d21 RR |
235 | } |
236 | ||
089e55d6 RR |
237 | bool wxGLCanvas::Create( wxWindow *parent, |
238 | const wxGLContext *shared, | |
239 | wxWindowID id, | |
aae24d21 RR |
240 | const wxPoint& pos, const wxSize& size, |
241 | long style, const wxString& name, | |
242 | int *attribList, | |
089e55d6 | 243 | const wxPalette& palette) |
9d3221ab | 244 | { |
147bc491 RR |
245 | m_sharedContext = (wxGLContext*)shared; // const_cast |
246 | ||
247 | m_exposed = FALSE; | |
248 | m_noExpose = TRUE; | |
30760ce7 | 249 | m_nativeSizeEvent = TRUE; |
1d61ebac | 250 | |
aae24d21 RR |
251 | if (!attribList) |
252 | { | |
253 | int data[] = { GLX_RGBA, | |
254 | GLX_DOUBLEBUFFER, | |
1d61ebac | 255 | GLX_DEPTH_SIZE, 1, // use largest available depth buffer |
a533f5c1 RR |
256 | GLX_RED_SIZE, 1, |
257 | GLX_GREEN_SIZE, 1, | |
258 | GLX_BLUE_SIZE, 1, | |
265b4577 | 259 | GLX_ALPHA_SIZE, 0, |
aae24d21 RR |
260 | None }; |
261 | attribList = (int*) data; | |
089e55d6 RR |
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; | |
aae24d21 RR |
288 | } |
289 | ||
e8b04eb3 | 290 | |
aae24d21 RR |
291 | Display *dpy = GDK_DISPLAY(); |
292 | ||
293 | g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList ); | |
294 | ||
a20cd16c SB |
295 | wxCHECK_MSG( g_vi, FALSE, "required visual couldn't be found" ); |
296 | ||
aae24d21 RR |
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 ); | |
1d61ebac RR |
302 | |
303 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
304 | ||
305 | m_glWidget = m_wxwindow; | |
aae24d21 | 306 | |
da048e3d | 307 | gtk_pizza_set_clear( GTK_PIZZA(m_wxwindow), FALSE ); |
147bc491 RR |
308 | |
309 | gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize", | |
1d61ebac | 310 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this ); |
147bc491 RR |
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 | ||
30760ce7 RR |
318 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
319 | GTK_SIGNAL_FUNC(gtk_glcanvas_size_callback), (gpointer)this ); | |
320 | ||
aae24d21 RR |
321 | gtk_widget_pop_visual(); |
322 | gtk_widget_pop_colormap(); | |
323 | ||
aae24d21 | 324 | return TRUE; |
9d3221ab RR |
325 | } |
326 | ||
327 | wxGLCanvas::~wxGLCanvas() | |
328 | { | |
bbe0af5b | 329 | if (m_glContext) delete m_glContext; |
9d3221ab RR |
330 | } |
331 | ||
332 | void wxGLCanvas::SwapBuffers() | |
333 | { | |
bbe0af5b | 334 | if (m_glContext) m_glContext->SwapBuffers(); |
9d3221ab RR |
335 | } |
336 | ||
337 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
338 | { | |
bbe0af5b | 339 | int width, height; |
aae24d21 | 340 | GetClientSize( &width, &height ); |
5fd11f09 | 341 | |
aae24d21 | 342 | if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) ) |
bbe0af5b | 343 | { |
aae24d21 RR |
344 | SetCurrent(); |
345 | ||
346 | glViewport(0, 0, (GLint)width, (GLint)height ); | |
bbe0af5b RR |
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 | } | |
9d3221ab RR |
352 | } |
353 | ||
354 | void wxGLCanvas::SetCurrent() | |
355 | { | |
bbe0af5b | 356 | if (m_glContext) m_glContext->SetCurrent(); |
9d3221ab RR |
357 | } |
358 | ||
359 | void wxGLCanvas::SetColour( const char *colour ) | |
360 | { | |
bbe0af5b | 361 | if (m_glContext) m_glContext->SetColour( colour ); |
9d3221ab RR |
362 | } |
363 | ||
147bc491 | 364 | void wxGLCanvas::OnInternalIdle() |
bbe0af5b | 365 | { |
147bc491 RR |
366 | if (m_glContext && m_exposed) |
367 | { | |
368 | wxPaintEvent event( GetId() ); | |
369 | event.SetEventObject( this ); | |
370 | GetEventHandler()->ProcessEvent( event ); | |
9d3221ab | 371 | |
147bc491 RR |
372 | m_exposed = FALSE; |
373 | GetUpdateRegion().Clear(); | |
374 | } | |
54517652 RR |
375 | |
376 | wxWindow::OnInternalIdle(); | |
377 | } |