]> git.saurik.com Git - wxWidgets.git/blame - utils/glcanvas/gtk/glcanvas.cpp
after a seek, reset error if error==EOF
[wxWidgets.git] / utils / glcanvas / gtk / glcanvas.cpp
CommitLineData
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 23extern "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 35XVisualInfo *g_vi = (XVisualInfo*) NULL;
9d3221ab
RR
36
37//---------------------------------------------------------------------------
38// wxGLContext
39//---------------------------------------------------------------------------
40
41IMPLEMENT_CLASS(wxGLContext,wxObject)
42
43wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
44{
bbe0af5b 45 m_window = win;
aae24d21 46 m_widget = ((wxGLCanvas*)win)->m_glWidget;
9d3221ab 47
aae24d21 48 wxCHECK_RET( g_vi, "invalid visual for OpenGl" );
bbe0af5b 49
aae24d21 50 m_glContext = glXCreateContext( GDK_DISPLAY(), g_vi, None, GL_TRUE );
9d3221ab 51
aae24d21 52 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
9d3221ab
RR
53}
54
089e55d6
RR
55wxGLContext::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
9d3221ab
RR
75wxGLContext::~wxGLContext()
76{
aae24d21 77 if (!m_glContext) return;
9d3221ab 78
aae24d21
RR
79 if (m_glContext == glXGetCurrentContext())
80 {
81 glXMakeCurrent( GDK_DISPLAY(), None, NULL);
bbe0af5b 82 }
aae24d21
RR
83
84 glXDestroyContext( GDK_DISPLAY(), m_glContext );
9d3221ab
RR
85}
86
87void wxGLContext::SwapBuffers()
88{
bbe0af5b
RR
89 if (m_glContext)
90 {
91 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
92 }
9d3221ab
RR
93}
94
95void wxGLContext::SetCurrent()
96{
bbe0af5b
RR
97 if (m_glContext)
98 {
99 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
100 }
9d3221ab
RR
101}
102
103void wxGLContext::SetColour(const char *colour)
104{
bbe0af5b
RR
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 }
9d3221ab
RR
116}
117
118void wxGLContext::SetupPixelFormat()
119{
120}
121
122void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
123{
124}
125
126wxPalette wxGLContext::CreateDefaultPalette()
127{
bbe0af5b 128 return wxNullPalette;
9d3221ab
RR
129}
130
1d61ebac
RR
131//-----------------------------------------------------------------------------
132// "realize" from m_wxwindow
133//-----------------------------------------------------------------------------
134
135static gint
136gtk_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
9d3221ab
RR
143//---------------------------------------------------------------------------
144// wxGlCanvas
145//---------------------------------------------------------------------------
146
147IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
148
149BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
150 EVT_SIZE(wxGLCanvas::OnSize)
151END_EVENT_TABLE()
152
aae24d21
RR
153wxGLCanvas::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{
089e55d6
RR
159 Create( parent, NULL, id, pos, size, style, name, attribList, palette );
160}
161
162wxGLCanvas::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 );
aae24d21
RR
171}
172
089e55d6
RR
173bool wxGLCanvas::Create( wxWindow *parent,
174 const wxGLContext *shared,
175 wxWindowID id,
aae24d21
RR
176 const wxPoint& pos, const wxSize& size,
177 long style, const wxString& name,
178 int *attribList,
089e55d6 179 const wxPalette& palette)
9d3221ab 180{
1d61ebac
RR
181 m_glContext = (wxGLContext*)shared; // const_cast
182
aae24d21
RR
183 if (!attribList)
184 {
185 int data[] = { GLX_RGBA,
186 GLX_DOUBLEBUFFER,
1d61ebac 187 GLX_DEPTH_SIZE, 1, // use largest available depth buffer
a533f5c1
RR
188 GLX_RED_SIZE, 1,
189 GLX_GREEN_SIZE, 1,
190 GLX_BLUE_SIZE, 1,
265b4577 191 GLX_ALPHA_SIZE, 0,
aae24d21
RR
192 None };
193 attribList = (int*) data;
089e55d6
RR
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;
aae24d21
RR
220 }
221
e8b04eb3 222
aae24d21
RR
223 Display *dpy = GDK_DISPLAY();
224
225 g_vi = glXChooseVisual( dpy, DefaultScreen(dpy), attribList );
226
a20cd16c
SB
227 wxCHECK_MSG( g_vi, FALSE, "required visual couldn't be found" );
228
aae24d21
RR
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 );
1d61ebac
RR
234
235 wxScrolledWindow::Create( parent, id, pos, size, style, name );
236
237 m_glWidget = m_wxwindow;
aae24d21 238
1d61ebac
RR
239 gtk_signal_connect( GTK_OBJECT(m_glWidget), "realize",
240 GTK_SIGNAL_FUNC(gtk_glwindow_realized_callback), (gpointer) this );
aae24d21
RR
241 gtk_widget_pop_visual();
242 gtk_widget_pop_colormap();
243
3cdda6cd
RR
244 /* must be realized for OpenGl output */
245 gtk_widget_realize( m_glWidget );
089e55d6 246
aae24d21
RR
247 XFree( g_vi );
248 g_vi = (XVisualInfo*) NULL;
089e55d6 249
aae24d21 250 return TRUE;
9d3221ab
RR
251}
252
253wxGLCanvas::~wxGLCanvas()
254{
bbe0af5b 255 if (m_glContext) delete m_glContext;
9d3221ab
RR
256}
257
258void wxGLCanvas::SwapBuffers()
259{
bbe0af5b 260 if (m_glContext) m_glContext->SwapBuffers();
9d3221ab
RR
261}
262
263void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
264{
bbe0af5b 265 int width, height;
aae24d21 266 GetClientSize( &width, &height );
5fd11f09 267
aae24d21 268 if (m_glContext && GTK_WIDGET_REALIZED(m_glWidget) )
bbe0af5b 269 {
aae24d21
RR
270 SetCurrent();
271
272 glViewport(0, 0, (GLint)width, (GLint)height );
bbe0af5b
RR
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 }
9d3221ab
RR
278}
279
280void wxGLCanvas::SetCurrent()
281{
bbe0af5b 282 if (m_glContext) m_glContext->SetCurrent();
9d3221ab
RR
283}
284
285void wxGLCanvas::SetColour( const char *colour )
286{
bbe0af5b 287 if (m_glContext) m_glContext->SetColour( colour );
9d3221ab
RR
288}
289
aae24d21 290GtkWidget *wxGLCanvas::GetConnectWidget()
bbe0af5b 291{
1d61ebac 292 return m_wxwindow;
bbe0af5b 293}
9d3221ab 294
aae24d21
RR
295bool wxGLCanvas::IsOwnGtkWindow( GdkWindow *window )
296{
1d61ebac 297 return (window == m_wxwindow->window);
aae24d21 298}