cube.cpp 15 KB

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