]>
Commit | Line | Data |
---|---|---|
43c742d0 | 1 | /////////////////////////////////////////////////////////////////////////////// |
8b089c5e JS |
2 | // Name: cube.cpp |
3 | // Purpose: wxGLCanvas demo program | |
4 | // Author: Julian Smart | |
43c742d0 | 5 | // Modified by: Vadim Zeitlin to use new wxGLCanvas API (2007-04-09) |
8b089c5e JS |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
f4a7108f | 9 | // Licence: wxWindows licence |
43c742d0 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
8b089c5e | 19 | |
8b089c5e JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | ||
806e2f15 VZ |
31 | #if !wxUSE_GLCANVAS |
32 | #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library" | |
33 | #endif | |
34 | ||
8b089c5e | 35 | #include "cube.h" |
5cf036d0 | 36 | |
e7092398 | 37 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
43c742d0 VZ |
38 | #include "../../sample.xpm" |
39 | #endif | |
8b089c5e | 40 | |
e4545e01 VZ |
41 | // ---------------------------------------------------------------------------- |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // control ids | |
46 | enum | |
47 | { | |
48 | SpinTimer = wxID_HIGHEST + 1 | |
49 | }; | |
50 | ||
378a3872 VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // helper functions | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | static void CheckGLError() | |
56 | { | |
57 | GLenum errLast = GL_NO_ERROR; | |
58 | ||
59 | for ( ;; ) | |
60 | { | |
61 | GLenum err = glGetError(); | |
62 | if ( err == GL_NO_ERROR ) | |
63 | return; | |
64 | ||
65 | // normally the error is reset by the call to glGetError() but if | |
66 | // glGetError() itself returns an error, we risk looping forever here | |
67 | // so check that we get a different error than the last time | |
68 | if ( err == errLast ) | |
69 | { | |
9a83f860 | 70 | wxLogError(wxT("OpenGL error state couldn't be reset.")); |
378a3872 VZ |
71 | return; |
72 | } | |
73 | ||
74 | errLast = err; | |
75 | ||
9a83f860 | 76 | wxLogError(wxT("OpenGL error %d"), err); |
378a3872 VZ |
77 | } |
78 | } | |
79 | ||
80 | // function to draw the texture for cube faces | |
81 | static wxImage DrawDice(int size, unsigned num) | |
82 | { | |
9a83f860 | 83 | wxASSERT_MSG( num >= 1 && num <= 6, wxT("invalid dice index") ); |
378a3872 VZ |
84 | |
85 | const int dot = size/16; // radius of a single dot | |
86 | const int gap = 5*size/32; // gap between dots | |
87 | ||
88 | wxBitmap bmp(size, size); | |
89 | wxMemoryDC dc; | |
90 | dc.SelectObject(bmp); | |
91 | dc.SetBackground(*wxWHITE_BRUSH); | |
92 | dc.Clear(); | |
93 | dc.SetBrush(*wxBLACK_BRUSH); | |
94 | ||
95 | // the upper left and lower right points | |
96 | if ( num != 1 ) | |
97 | { | |
98 | dc.DrawCircle(gap + dot, gap + dot, dot); | |
99 | dc.DrawCircle(size - gap - dot, size - gap - dot, dot); | |
100 | } | |
101 | ||
102 | // draw the central point for odd dices | |
103 | if ( num % 2 ) | |
104 | { | |
105 | dc.DrawCircle(size/2, size/2, dot); | |
106 | } | |
107 | ||
108 | // the upper right and lower left points | |
109 | if ( num > 3 ) | |
110 | { | |
111 | dc.DrawCircle(size - gap - dot, gap + dot, dot); | |
112 | dc.DrawCircle(gap + dot, size - gap - dot, dot); | |
113 | } | |
114 | ||
115 | // finally those 2 are only for the last dice | |
116 | if ( num == 6 ) | |
117 | { | |
118 | dc.DrawCircle(gap + dot, size/2, dot); | |
119 | dc.DrawCircle(size - gap - dot, size/2, dot); | |
120 | } | |
121 | ||
122 | dc.SelectObject(wxNullBitmap); | |
123 | ||
124 | return bmp.ConvertToImage(); | |
125 | } | |
126 | ||
43c742d0 VZ |
127 | // ============================================================================ |
128 | // implementation | |
129 | // ============================================================================ | |
8b089c5e | 130 | |
43c742d0 | 131 | // ---------------------------------------------------------------------------- |
50f5d508 | 132 | // TestGLContext |
43c742d0 | 133 | // ---------------------------------------------------------------------------- |
8b089c5e | 134 | |
50f5d508 VZ |
135 | TestGLContext::TestGLContext(wxGLCanvas *canvas) |
136 | : wxGLContext(canvas) | |
8b089c5e | 137 | { |
378a3872 | 138 | SetCurrent(*canvas); |
8b089c5e | 139 | |
378a3872 | 140 | // set up the parameters we want to use |
948f48e7 | 141 | glEnable(GL_CULL_FACE); |
8b089c5e JS |
142 | glEnable(GL_DEPTH_TEST); |
143 | glEnable(GL_LIGHTING); | |
144 | glEnable(GL_LIGHT0); | |
378a3872 | 145 | glEnable(GL_TEXTURE_2D); |
8b089c5e | 146 | |
451c13c8 | 147 | // add slightly more light, the default lighting is rather dark |
bc521497 VZ |
148 | GLfloat ambient[] = { 0.5, 0.5, 0.5, 0.5 }; |
149 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); | |
150 | ||
378a3872 VZ |
151 | // set viewing projection |
152 | glMatrixMode(GL_PROJECTION); | |
153 | glLoadIdentity(); | |
154 | glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 3.0f); | |
8b089c5e | 155 | |
378a3872 VZ |
156 | // create the textures to use for cube sides: they will be reused by all |
157 | // canvases (which is probably not critical in the case of simple textures | |
158 | // we use here but could be really important for a real application where | |
159 | // each texture could take many megabytes) | |
160 | glGenTextures(WXSIZEOF(m_textures), m_textures); | |
8b089c5e | 161 | |
378a3872 VZ |
162 | for ( unsigned i = 0; i < WXSIZEOF(m_textures); i++ ) |
163 | { | |
164 | glBindTexture(GL_TEXTURE_2D, m_textures[i]); | |
8b089c5e | 165 | |
378a3872 VZ |
166 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
167 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); | |
168 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); | |
169 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
170 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
f4a7108f | 171 | |
378a3872 | 172 | const wxImage img(DrawDice(256, i + 1)); |
8b089c5e | 173 | |
378a3872 | 174 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
ce00f59b | 175 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), |
378a3872 VZ |
176 | 0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData()); |
177 | } | |
8b089c5e | 178 | |
378a3872 | 179 | CheckGLError(); |
8b089c5e JS |
180 | } |
181 | ||
50f5d508 VZ |
182 | void TestGLContext::DrawRotatedCube(float xangle, float yangle) |
183 | { | |
50f5d508 VZ |
184 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
185 | ||
186 | glMatrixMode(GL_MODELVIEW); | |
187 | glLoadIdentity(); | |
188 | glTranslatef(0.0f, 0.0f, -2.0f); | |
189 | glRotatef(xangle, 1.0f, 0.0f, 0.0f); | |
190 | glRotatef(yangle, 0.0f, 1.0f, 0.0f); | |
191 | ||
378a3872 VZ |
192 | // draw six faces of a cube of size 1 centered at (0, 0, 0) |
193 | glBindTexture(GL_TEXTURE_2D, m_textures[0]); | |
194 | glBegin(GL_QUADS); | |
195 | glNormal3f( 0.0f, 0.0f, 1.0f); | |
196 | glTexCoord2f(0, 0); glVertex3f( 0.5f, 0.5f, 0.5f); | |
197 | glTexCoord2f(1, 0); glVertex3f(-0.5f, 0.5f, 0.5f); | |
198 | glTexCoord2f(1, 1); glVertex3f(-0.5f,-0.5f, 0.5f); | |
199 | glTexCoord2f(0, 1); glVertex3f( 0.5f,-0.5f, 0.5f); | |
200 | glEnd(); | |
201 | ||
202 | glBindTexture(GL_TEXTURE_2D, m_textures[1]); | |
203 | glBegin(GL_QUADS); | |
204 | glNormal3f( 0.0f, 0.0f,-1.0f); | |
205 | glTexCoord2f(0, 0); glVertex3f(-0.5f,-0.5f,-0.5f); | |
206 | glTexCoord2f(1, 0); glVertex3f(-0.5f, 0.5f,-0.5f); | |
207 | glTexCoord2f(1, 1); glVertex3f( 0.5f, 0.5f,-0.5f); | |
208 | glTexCoord2f(0, 1); glVertex3f( 0.5f,-0.5f,-0.5f); | |
209 | glEnd(); | |
210 | ||
211 | glBindTexture(GL_TEXTURE_2D, m_textures[2]); | |
212 | glBegin(GL_QUADS); | |
213 | glNormal3f( 0.0f, 1.0f, 0.0f); | |
214 | glTexCoord2f(0, 0); glVertex3f( 0.5f, 0.5f, 0.5f); | |
215 | glTexCoord2f(1, 0); glVertex3f( 0.5f, 0.5f,-0.5f); | |
216 | glTexCoord2f(1, 1); glVertex3f(-0.5f, 0.5f,-0.5f); | |
217 | glTexCoord2f(0, 1); glVertex3f(-0.5f, 0.5f, 0.5f); | |
218 | glEnd(); | |
219 | ||
220 | glBindTexture(GL_TEXTURE_2D, m_textures[3]); | |
221 | glBegin(GL_QUADS); | |
222 | glNormal3f( 0.0f,-1.0f, 0.0f); | |
223 | glTexCoord2f(0, 0); glVertex3f(-0.5f,-0.5f,-0.5f); | |
224 | glTexCoord2f(1, 0); glVertex3f( 0.5f,-0.5f,-0.5f); | |
225 | glTexCoord2f(1, 1); glVertex3f( 0.5f,-0.5f, 0.5f); | |
226 | glTexCoord2f(0, 1); glVertex3f(-0.5f,-0.5f, 0.5f); | |
227 | glEnd(); | |
228 | ||
229 | glBindTexture(GL_TEXTURE_2D, m_textures[4]); | |
230 | glBegin(GL_QUADS); | |
231 | glNormal3f( 1.0f, 0.0f, 0.0f); | |
232 | glTexCoord2f(0, 0); glVertex3f( 0.5f, 0.5f, 0.5f); | |
233 | glTexCoord2f(1, 0); glVertex3f( 0.5f,-0.5f, 0.5f); | |
234 | glTexCoord2f(1, 1); glVertex3f( 0.5f,-0.5f,-0.5f); | |
235 | glTexCoord2f(0, 1); glVertex3f( 0.5f, 0.5f,-0.5f); | |
236 | glEnd(); | |
237 | ||
238 | glBindTexture(GL_TEXTURE_2D, m_textures[5]); | |
239 | glBegin(GL_QUADS); | |
240 | glNormal3f(-1.0f, 0.0f, 0.0f); | |
241 | glTexCoord2f(0, 0); glVertex3f(-0.5f,-0.5f,-0.5f); | |
242 | glTexCoord2f(1, 0); glVertex3f(-0.5f,-0.5f, 0.5f); | |
243 | glTexCoord2f(1, 1); glVertex3f(-0.5f, 0.5f, 0.5f); | |
244 | glTexCoord2f(0, 1); glVertex3f(-0.5f, 0.5f,-0.5f); | |
245 | glEnd(); | |
50f5d508 VZ |
246 | |
247 | glFlush(); | |
378a3872 VZ |
248 | |
249 | CheckGLError(); | |
50f5d508 VZ |
250 | } |
251 | ||
451c13c8 VZ |
252 | |
253 | // ---------------------------------------------------------------------------- | |
254 | // MyApp: the application object | |
255 | // ---------------------------------------------------------------------------- | |
256 | ||
257 | IMPLEMENT_APP(MyApp) | |
258 | ||
259 | bool MyApp::OnInit() | |
260 | { | |
261 | if ( !wxApp::OnInit() ) | |
262 | return false; | |
263 | ||
264 | new MyFrame(); | |
265 | ||
266 | return true; | |
267 | } | |
268 | ||
269 | int MyApp::OnExit() | |
270 | { | |
271 | delete m_glContext; | |
272 | ||
273 | return wxApp::OnExit(); | |
274 | } | |
275 | ||
276 | TestGLContext& MyApp::GetContext(wxGLCanvas *canvas) | |
277 | { | |
278 | if ( !m_glContext ) | |
279 | { | |
280 | // Create the OpenGL context for the first window which needs it: | |
281 | // subsequently created windows will all share the same context. | |
282 | m_glContext = new TestGLContext(canvas); | |
283 | } | |
284 | ||
285 | m_glContext->SetCurrent(*canvas); | |
286 | ||
287 | return *m_glContext; | |
288 | } | |
289 | ||
50f5d508 VZ |
290 | // ---------------------------------------------------------------------------- |
291 | // TestGLCanvas | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
294 | BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) | |
50f5d508 | 295 | EVT_PAINT(TestGLCanvas::OnPaint) |
50f5d508 | 296 | EVT_KEY_DOWN(TestGLCanvas::OnKeyDown) |
e4545e01 | 297 | EVT_TIMER(SpinTimer, TestGLCanvas::OnSpinTimer) |
50f5d508 VZ |
298 | END_EVENT_TABLE() |
299 | ||
300 | TestGLCanvas::TestGLCanvas(wxWindow *parent) | |
451c13c8 VZ |
301 | // With perspective OpenGL graphics, the wxFULL_REPAINT_ON_RESIZE style |
302 | // flag should always be set, because even making the canvas smaller should | |
303 | // be followed by a paint event that updates the entire canvas with new | |
304 | // viewport settings. | |
305 | : wxGLCanvas(parent, wxID_ANY, NULL /* attribs */, | |
306 | wxDefaultPosition, wxDefaultSize, | |
e4545e01 VZ |
307 | wxFULL_REPAINT_ON_RESIZE), |
308 | m_xangle(30.0), | |
309 | m_yangle(30.0), | |
310 | m_spinTimer(this,SpinTimer) | |
50f5d508 | 311 | { |
50f5d508 VZ |
312 | } |
313 | ||
314 | void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
315 | { | |
451c13c8 | 316 | // This is required even though dc is not used otherwise. |
50f5d508 VZ |
317 | wxPaintDC dc(this); |
318 | ||
451c13c8 VZ |
319 | // Set the OpenGL viewport according to the client size of this canvas. |
320 | // This is done here rather than in a wxSizeEvent handler because our | |
321 | // OpenGL rendering context (and thus viewport setting) is used with | |
322 | // multiple canvases: If we updated the viewport in the wxSizeEvent | |
323 | // handler, changing the size of one canvas causes a viewport setting that | |
324 | // is wrong when next another canvas is repainted. | |
325 | const wxSize ClientSize = GetClientSize(); | |
50f5d508 | 326 | |
97f85100 | 327 | TestGLContext& canvas = wxGetApp().GetContext(this); |
451c13c8 | 328 | glViewport(0, 0, ClientSize.x, ClientSize.y); |
50f5d508 | 329 | |
451c13c8 | 330 | // Render the graphics and swap the buffers. |
97f85100 | 331 | canvas.DrawRotatedCube(m_xangle, m_yangle); |
451c13c8 | 332 | SwapBuffers(); |
50f5d508 VZ |
333 | } |
334 | ||
e4545e01 | 335 | void TestGLCanvas::Spin(float xSpin, float ySpin) |
8b089c5e | 336 | { |
e4545e01 VZ |
337 | m_xangle += xSpin; |
338 | m_yangle += ySpin; | |
8b089c5e | 339 | |
e4545e01 VZ |
340 | Refresh(false); |
341 | } | |
342 | ||
343 | void TestGLCanvas::OnKeyDown(wxKeyEvent& event) | |
344 | { | |
345 | float angle = 5.0; | |
f4a7108f | 346 | |
43c742d0 | 347 | switch ( event.GetKeyCode() ) |
8b089c5e | 348 | { |
43c742d0 | 349 | case WXK_RIGHT: |
e4545e01 VZ |
350 | Spin( 0.0, -angle ); |
351 | break; | |
43c742d0 VZ |
352 | |
353 | case WXK_LEFT: | |
e4545e01 | 354 | Spin( 0.0, angle ); |
43c742d0 VZ |
355 | break; |
356 | ||
357 | case WXK_DOWN: | |
e4545e01 VZ |
358 | Spin( -angle, 0.0 ); |
359 | break; | |
43c742d0 VZ |
360 | |
361 | case WXK_UP: | |
e4545e01 VZ |
362 | Spin( angle, 0.0 ); |
363 | break; | |
364 | ||
365 | case WXK_SPACE: | |
366 | if ( m_spinTimer.IsRunning() ) | |
367 | m_spinTimer.Stop(); | |
368 | else | |
369 | m_spinTimer.Start( 25 ); | |
43c742d0 VZ |
370 | break; |
371 | ||
372 | default: | |
373 | event.Skip(); | |
374 | return; | |
8b089c5e | 375 | } |
e4545e01 | 376 | } |
8b089c5e | 377 | |
e4545e01 VZ |
378 | void TestGLCanvas::OnSpinTimer(wxTimerEvent& WXUNUSED(event)) |
379 | { | |
380 | Spin(0.0, 4.0); | |
43c742d0 | 381 | } |
8b089c5e | 382 | |
451c13c8 | 383 | |
43c742d0 VZ |
384 | // ---------------------------------------------------------------------------- |
385 | // MyFrame: main application window | |
386 | // ---------------------------------------------------------------------------- | |
8b089c5e JS |
387 | |
388 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
43c742d0 | 389 | EVT_MENU(wxID_NEW, MyFrame::OnNewWindow) |
1f602af6 | 390 | EVT_MENU(wxID_CLOSE, MyFrame::OnClose) |
8b089c5e JS |
391 | END_EVENT_TABLE() |
392 | ||
43c742d0 | 393 | MyFrame::MyFrame() |
9a83f860 | 394 | : wxFrame(NULL, wxID_ANY, wxT("wxWidgets OpenGL Cube Sample")) |
8b089c5e | 395 | { |
50f5d508 | 396 | new TestGLCanvas(this); |
5cf036d0 | 397 | |
43c742d0 | 398 | SetIcon(wxICON(sample)); |
5cf036d0 | 399 | |
5cf036d0 | 400 | // Make a menubar |
1f602af6 VZ |
401 | wxMenu *menu = new wxMenu; |
402 | menu->Append(wxID_NEW); | |
403 | menu->AppendSeparator(); | |
404 | menu->Append(wxID_CLOSE); | |
5cf036d0 | 405 | wxMenuBar *menuBar = new wxMenuBar; |
9a83f860 | 406 | menuBar->Append(menu, wxT("&Cube")); |
8b089c5e | 407 | |
43c742d0 | 408 | SetMenuBar(menuBar); |
5cf036d0 | 409 | |
1f602af6 VZ |
410 | CreateStatusBar(); |
411 | ||
50f5d508 | 412 | SetClientSize(400, 400); |
43c742d0 | 413 | Show(); |
3f20f7d8 VZ |
414 | |
415 | // test IsDisplaySupported() function: | |
416 | static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 }; | |
417 | wxLogStatus("Double-buffered display %s supported", | |
418 | wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not"); | |
8b089c5e JS |
419 | } |
420 | ||
1f602af6 | 421 | void MyFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
8b089c5e | 422 | { |
43c742d0 VZ |
423 | // true is to force the frame to close |
424 | Close(true); | |
8b089c5e | 425 | } |
2db98bf5 | 426 | |
43c742d0 | 427 | void MyFrame::OnNewWindow( wxCommandEvent& WXUNUSED(event) ) |
8b089c5e | 428 | { |
451c13c8 | 429 | new MyFrame(); |
8b089c5e JS |
430 | } |
431 |