]>
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 | // 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 | return FALSE; | |
141 | } | |
142 | ||
143 | //--------------------------------------------------------------------------- | |
144 | // wxGlCanvas | |
145 | //--------------------------------------------------------------------------- | |
146 | ||
147 | IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow) | |
148 | ||
149 | BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow) | |
150 | EVT_SIZE(wxGLCanvas::OnSize) | |
151 | END_EVENT_TABLE() | |
152 | ||
153 | wxGLCanvas::wxGLCanvas( wxWindow *parent, wxWindowID id, | |
154 | const wxPoint& pos, const wxSize& size, | |
155 | long style, const wxString& name, | |
156 | int *attribList, | |
157 | const wxPalette& palette ) | |
158 | { | |
159 | Create( parent, NULL, id, pos, size, style, name, attribList, palette ); | |
160 | } | |
161 | ||
162 | wxGLCanvas::wxGLCanvas( wxWindow *parent, | |
163 | const wxGLContext *shared, | |
164 | wxWindowID id, | |
165 | const wxPoint& pos, const wxSize& size, | |
166 | long style, const wxString& name, | |
167 | int *attribList, | |
168 | const wxPalette& palette ) | |
169 | { | |
170 | Create( parent, shared, id, pos, size, style, name, attribList, palette ); | |
171 | } | |
172 | ||
173 | bool wxGLCanvas::Create( wxWindow *parent, | |
174 | const wxGLContext *shared, | |
175 | wxWindowID id, | |
176 | const wxPoint& pos, const wxSize& size, | |
177 | long style, const wxString& name, | |
178 | int *attribList, | |
179 | const wxPalette& palette) | |
180 | { | |
181 | m_glContext = (wxGLContext*)shared; // const_cast | |
182 | ||
183 | if (!attribList) | |
184 | { | |
185 | int data[] = { GLX_RGBA, | |
186 | GLX_DOUBLEBUFFER, | |
187 | GLX_DEPTH_SIZE, 1, // use largest available depth buffer | |
188 | GLX_RED_SIZE, 1, | |
189 | GLX_GREEN_SIZE, 1, | |
190 | GLX_BLUE_SIZE, 1, | |
191 | GLX_ALPHA_SIZE, 0, | |
192 | None }; | |
193 | attribList = (int*) data; | |
194 | } | |
195 | else | |
196 | { | |
197 | int data[512], arg=0, p=0; | |
198 | ||
199 | while( (attribList[arg]!=0) && (p<512) ) | |
200 | { | |
201 | switch( attribList[arg++] ) | |
202 | { | |
203 | case WX_GL_RGBA: data[p++] = GLX_RGBA; break; | |
204 | case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; break; | |
205 | case WX_GL_DEPTH_SIZE: | |
206 | data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break; | |
207 | case WX_GL_MIN_RED: | |
208 | data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break; | |
209 | case WX_GL_MIN_GREEN: | |
210 | data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break; | |
211 | case WX_GL_MIN_BLUE: | |
212 | data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break; | |
213 | default: | |
214 | break; | |
215 | } | |
216 | } | |
217 | data[p] = 0; | |
218 | ||
219 | attribList = (int*) data; | |
220 | } | |
221 | ||
222 | ||
223 | Display *dpy = GDK_DISPLAY(); | |
224 | ||
225 | g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList ); | |
226 | ||
227 | wxCHECK_MSG( g_vi, FALSE, "required visual couldn't be found" ); | |
228 | ||
229 | GdkVisual *visual = gdkx_visual_get( g_vi->visualid ); | |
230 | GdkColormap *colormap = gdk_colormap_new( gdkx_visual_get(g_vi->visualid), TRUE ); | |
231 | ||
232 | gtk_widget_push_colormap( colormap ); | |
233 | gtk_widget_push_visual( visual ); | |
234 | ||
235 | wxScrolledWindow::Create( parent, id, pos, size, style, name ); | |
236 | ||
237 | m_glWidget = m_wxwindow; | |
238 | ||
239 | gtk_signal_connect( GTK_OBJECT(m_glWidget), "realize", | |
240 | GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this ); | |
241 | gtk_widget_pop_visual(); | |
242 | gtk_widget_pop_colormap(); | |
243 | ||
244 | /* must be realized for OpenGl output */ | |
245 | gtk_widget_realize( m_glWidget ); | |
246 | ||
247 | XFree( g_vi ); | |
248 | g_vi = (XVisualInfo*) NULL; | |
249 | ||
250 | return TRUE; | |
251 | } | |
252 | ||
253 | wxGLCanvas::~wxGLCanvas() | |
254 | { | |
255 | if (m_glContext) delete m_glContext; | |
256 | } | |
257 | ||
258 | void wxGLCanvas::SwapBuffers() | |
259 | { | |
260 | if (m_glContext) m_glContext->SwapBuffers(); | |
261 | } | |
262 | ||
263 | void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event)) | |
264 | { | |
265 | int width, height; | |
266 | GetClientSize( &width, &height ); | |
267 | ||
268 | if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) ) | |
269 | { | |
270 | SetCurrent(); | |
271 | ||
272 | glViewport(0, 0, (GLint)width, (GLint)height ); | |
273 | glMatrixMode(GL_PROJECTION); | |
274 | glLoadIdentity(); | |
275 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 ); | |
276 | glMatrixMode(GL_MODELVIEW); | |
277 | } | |
278 | } | |
279 | ||
280 | void wxGLCanvas::SetCurrent() | |
281 | { | |
282 | if (m_glContext) m_glContext->SetCurrent(); | |
283 | } | |
284 | ||
285 | void wxGLCanvas::SetColour( const char *colour ) | |
286 | { | |
287 | if (m_glContext) m_glContext->SetColour( colour ); | |
288 | } | |
289 | ||
290 | GtkWidget *wxGLCanvas::GetConnectWidget() | |
291 | { | |
292 | return m_wxwindow; | |
293 | } | |
294 | ||
295 | bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window ) | |
296 | { | |
297 | return (window == m_wxwindow->window); | |
298 | } |