]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/src/glcanvas.cpp
a668badcabc56ed0a0e8b8bb0b20a1f79ded7413
[wxWidgets.git] / utils / glcanvas / src / 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 #include "gtk/gtk.h"
24 #include "gdk/gdk.h"
25 extern "C" {
26 #include "gdk/gdkx.h"
27 }
28
29 //---------------------------------------------------------------------------
30 // global variables
31 //---------------------------------------------------------------------------
32
33 XVisualInfo *g_visual_info = (XVisualInfo*) NULL;
34
35 //---------------------------------------------------------------------------
36 // wxGLContext
37 //---------------------------------------------------------------------------
38
39 IMPLEMENT_CLASS(wxGLContext,wxObject)
40
41 wxGLContext::wxGLContext( bool WXUNUSED(isRGB), wxWindow *win, const wxPalette& WXUNUSED(palette) )
42 {
43 m_window = win;
44 m_widget = win->m_wxwindow;
45
46 wxCHECK_RET( g_visual_info != NULL, "invalid visual for OpenGl" );
47
48 m_glContext = glXCreateContext( GDK_DISPLAY(), g_visual_info, None, GL_TRUE );
49
50 wxCHECK_RET( m_glContext != NULL, "Couldn't create OpenGl context" );
51
52 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
53 }
54
55 wxGLContext::~wxGLContext()
56 {
57 if (m_glContext)
58 {
59 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
60
61 glXDestroyContext( GDK_DISPLAY(), m_glContext );
62 }
63 }
64
65 void wxGLContext::SwapBuffers()
66 {
67 if (m_glContext)
68 {
69 glXSwapBuffers( GDK_DISPLAY(), GDK_WINDOW_XWINDOW( m_widget->window ) );
70 }
71 }
72
73 void wxGLContext::SetCurrent()
74 {
75 if (m_glContext)
76 {
77 glXMakeCurrent( GDK_DISPLAY(), GDK_WINDOW_XWINDOW(m_widget->window), m_glContext );
78 }
79 }
80
81 void wxGLContext::SetColour(const char *colour)
82 {
83 float r = 0.0;
84 float g = 0.0;
85 float b = 0.0;
86 wxColour *col = wxTheColourDatabase->FindColour(colour);
87 if (col)
88 {
89 r = (float)(col->Red()/256.0);
90 g = (float)(col->Green()/256.0);
91 b = (float)(col->Blue()/256.0);
92 glColor3f( r, g, b);
93 }
94 }
95
96 void wxGLContext::SetupPixelFormat()
97 {
98 }
99
100 void wxGLContext::SetupPalette( const wxPalette& WXUNUSED(palette) )
101 {
102 }
103
104 wxPalette wxGLContext::CreateDefaultPalette()
105 {
106 return wxNullPalette;
107 }
108
109 //---------------------------------------------------------------------------
110 // wxGlCanvas
111 //---------------------------------------------------------------------------
112
113 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
114
115 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
116 EVT_SIZE(wxGLCanvas::OnSize)
117 END_EVENT_TABLE()
118
119 wxGLCanvas::wxGLCanvas(wxWindow *parent, wxWindowID id,
120 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
121 int *WXUNUSED(attribList), const wxPalette& palette):
122 wxScrolledWindow(parent, id, pos, size, style, name)
123 {
124 m_glContext = new wxGLContext( TRUE, this, palette );
125 }
126
127 wxGLCanvas::~wxGLCanvas()
128 {
129 if (m_glContext) delete m_glContext;
130 }
131
132 void wxGLCanvas::SwapBuffers()
133 {
134 if (m_glContext) m_glContext->SwapBuffers();
135 }
136
137 void wxGLCanvas::OnSize(wxSizeEvent& WXUNUSED(event))
138 {
139 int width, height;
140 GetClientSize(& width, & height);
141
142 if (m_glContext)
143 {
144 m_glContext->SetCurrent();
145
146 glViewport(0, 0, (GLint)width, (GLint)height);
147 glMatrixMode(GL_PROJECTION);
148 glLoadIdentity();
149 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
150 glMatrixMode(GL_MODELVIEW);
151 }
152 }
153
154 void wxGLCanvas::SetCurrent()
155 {
156 if (m_glContext) m_glContext->SetCurrent();
157 }
158
159 void wxGLCanvas::SetColour( const char *colour )
160 {
161 if (m_glContext) m_glContext->SetColour( colour );
162 }
163
164 //--------------------------------------------------------------------
165 // wxGLModule
166 //--------------------------------------------------------------------
167
168 class wxGLModule : public wxModule
169 {
170 public:
171 virtual bool OnInit();
172 virtual void OnExit();
173
174 private:
175 DECLARE_DYNAMIC_CLASS(wxGLModule)
176 };
177
178 IMPLEMENT_DYNAMIC_CLASS(wxGLModule, wxModule)
179
180 bool wxGLModule::OnInit()
181 {
182 int data[] = { GLX_RGBA,GLX_RED_SIZE,1,GLX_GREEN_SIZE,1,
183 GLX_BLUE_SIZE,1,GLX_DOUBLEBUFFER,None};
184
185 g_visual_info = glXChooseVisual( GDK_DISPLAY(), DefaultScreen(GDK_DISPLAY()), data );
186
187 wxCHECK_MSG( g_visual_info != NULL, FALSE, "Couldn't choose visual for OpenGl" );
188
189 wxVisualSetByExternal = gdkx_visual_get(g_visual_info->visualid);
190
191 wxColormapSetByExternal = gdk_colormap_new( gdkx_visual_get(g_visual_info->visualid), TRUE );
192
193 return TRUE;
194 }
195
196 void wxGLModule::OnExit()
197 {
198 }
199