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