isosurf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: isosurf.cpp
  3. // Purpose: wxGLCanvas demo program
  4. // Author: Brian Paul (original gltk version), Wolfram Gloger
  5. // Modified by: Julian Smart, Francesco Montorsi
  6. // Created: 04/01/98
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif
  18. #if !wxUSE_GLCANVAS
  19. #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
  20. #endif
  21. #include "wx/timer.h"
  22. #include "wx/glcanvas.h"
  23. #include "wx/math.h"
  24. #include "wx/log.h"
  25. #include "wx/cmdline.h"
  26. #include "wx/wfstream.h"
  27. #include "wx/zstream.h"
  28. #include "wx/txtstrm.h"
  29. #include "isosurf.h"
  30. #include "../../sample.xpm"
  31. // global options which can be set through command-line options
  32. GLboolean g_use_vertex_arrays = GL_FALSE;
  33. GLboolean g_doubleBuffer = GL_TRUE;
  34. GLboolean g_smooth = GL_TRUE;
  35. GLboolean g_lighting = GL_TRUE;
  36. //---------------------------------------------------------------------------
  37. // MyApp
  38. //---------------------------------------------------------------------------
  39. IMPLEMENT_APP(MyApp)
  40. bool MyApp::OnInit()
  41. {
  42. if ( !wxApp::OnInit() )
  43. return false;
  44. // Create the main frame window
  45. new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"));
  46. return true;
  47. }
  48. void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
  49. {
  50. parser.AddSwitch("", "sb", "Do not use double buffering");
  51. parser.AddSwitch("", "db", "Use double buffering");
  52. parser.AddSwitch("", "va", "Use vertex arrays");
  53. wxApp::OnInitCmdLine(parser);
  54. }
  55. bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
  56. {
  57. if (parser.Found("sb"))
  58. g_doubleBuffer = GL_FALSE;
  59. else if (parser.Found("db"))
  60. g_doubleBuffer = GL_TRUE;
  61. if (parser.Found("va"))
  62. g_use_vertex_arrays = GL_TRUE;
  63. return wxApp::OnCmdLineParsed(parser);
  64. }
  65. //---------------------------------------------------------------------------
  66. // MyFrame
  67. //---------------------------------------------------------------------------
  68. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  69. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  70. wxEND_EVENT_TABLE()
  71. MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
  72. const wxSize& size, long style)
  73. : wxFrame(frame, wxID_ANY, title, pos, size, style),
  74. m_canvas(NULL)
  75. {
  76. SetIcon(wxICON(sample));
  77. // Make a menubar
  78. wxMenu *fileMenu = new wxMenu;
  79. fileMenu->Append(wxID_EXIT, wxT("E&xit"));
  80. wxMenuBar *menuBar = new wxMenuBar;
  81. menuBar->Append(fileMenu, wxT("&File"));
  82. SetMenuBar(menuBar);
  83. // Make a TestGLCanvas
  84. // JACS
  85. #ifdef __WXMSW__
  86. int *gl_attrib = NULL;
  87. #else
  88. int gl_attrib[20] =
  89. { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
  90. WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
  91. WX_GL_DOUBLEBUFFER,
  92. # if defined(__WXMAC__) || defined(__WXCOCOA__)
  93. GL_NONE };
  94. # else
  95. None };
  96. # endif
  97. #endif
  98. if (!g_doubleBuffer)
  99. {
  100. wxLogWarning("Disabling double buffering");
  101. #ifdef __WXGTK__
  102. gl_attrib[9] = None;
  103. #endif
  104. g_doubleBuffer = GL_FALSE;
  105. }
  106. m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib);
  107. // Show the frame
  108. Show(true);
  109. Raise();
  110. m_canvas->InitGL();
  111. }
  112. MyFrame::~MyFrame()
  113. {
  114. delete m_canvas;
  115. }
  116. // Intercept menu commands
  117. void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
  118. {
  119. // true is to force the frame to close
  120. Close(true);
  121. }
  122. //---------------------------------------------------------------------------
  123. // TestGLCanvas
  124. //---------------------------------------------------------------------------
  125. wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
  126. EVT_SIZE(TestGLCanvas::OnSize)
  127. EVT_PAINT(TestGLCanvas::OnPaint)
  128. EVT_CHAR(TestGLCanvas::OnChar)
  129. EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
  130. wxEND_EVENT_TABLE()
  131. TestGLCanvas::TestGLCanvas(wxWindow *parent,
  132. wxWindowID id,
  133. int* gl_attrib)
  134. : wxGLCanvas(parent, id, gl_attrib)
  135. {
  136. m_xrot = 0;
  137. m_yrot = 0;
  138. m_numverts = 0;
  139. // Explicitly create a new rendering context instance for this canvas.
  140. m_glRC = new wxGLContext(this);
  141. }
  142. TestGLCanvas::~TestGLCanvas()
  143. {
  144. delete m_glRC;
  145. }
  146. void TestGLCanvas::LoadSurface(const wxString& filename)
  147. {
  148. // FIXME
  149. // we need to set english locale to force wxTextInputStream's calls to
  150. // wxStrtod to use the point and not the comma as decimal separator...
  151. // (the isosurf.dat contains points and not commas)...
  152. wxLocale l(wxLANGUAGE_ENGLISH);
  153. wxZlibInputStream* stream =
  154. new wxZlibInputStream(new wxFFileInputStream(filename));
  155. if (!stream || !stream->IsOk())
  156. {
  157. wxLogError("Cannot load '%s' type of files!", filename.c_str());
  158. delete stream;
  159. return;
  160. }
  161. {
  162. // we suppose to have in input a text file containing floating numbers
  163. // space/newline-separated... first 3 numbers are the coordinates of a
  164. // vertex and the following 3 are the relative vertex normal and so on...
  165. wxTextInputStream inFile(*stream);
  166. m_numverts = 0;
  167. while (!stream->Eof() && m_numverts < MAXVERTS)// && m_numverts<MAXVERTS)
  168. {
  169. inFile >> m_verts[m_numverts][0] >> m_verts[m_numverts][1] >> m_verts[m_numverts][2];
  170. inFile >> m_norms[m_numverts][0] >> m_norms[m_numverts][1] >> m_norms[m_numverts][2];
  171. m_numverts++;
  172. }
  173. // discard last vertex; it is a zero caused by the EOF
  174. m_numverts--;
  175. }
  176. delete stream;
  177. wxLogMessage(wxT("Loaded %d vertices, %d triangles from '%s'"),
  178. m_numverts, m_numverts-2, filename.c_str());
  179. // NOTE: for some reason under wxGTK the following is required to avoid that
  180. // the surface gets rendered in a small rectangle in the top-left corner of the frame
  181. PostSizeEventToParent();
  182. }
  183. void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
  184. {
  185. // This is a dummy, to avoid an endless succession of paint messages.
  186. // OnPaint handlers must always create a wxPaintDC.
  187. wxPaintDC dc(this);
  188. // This is normally only necessary if there is more than one wxGLCanvas
  189. // or more than one wxGLContext in the application.
  190. SetCurrent(*m_glRC);
  191. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  192. glPushMatrix();
  193. glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
  194. glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
  195. // draw the surface
  196. if (g_use_vertex_arrays)
  197. {
  198. glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
  199. }
  200. else
  201. {
  202. glBegin( GL_TRIANGLE_STRIP );
  203. for (int i=0;i<m_numverts;i++)
  204. {
  205. glNormal3fv( m_norms[i] );
  206. glVertex3fv( m_verts[i] );
  207. }
  208. glEnd();
  209. }
  210. glPopMatrix();
  211. glFlush(); // Not really necessary: buffer swapping below implies glFlush()
  212. SwapBuffers();
  213. }
  214. void TestGLCanvas::OnSize(wxSizeEvent& event)
  215. {
  216. if ( !IsShownOnScreen() )
  217. return;
  218. // This is normally only necessary if there is more than one wxGLCanvas
  219. // or more than one wxGLContext in the application.
  220. SetCurrent(*m_glRC);
  221. // It's up to the application code to update the OpenGL viewport settings.
  222. // This is OK here only because there is only one canvas that uses the
  223. // context. See the cube sample for that case that multiple canvases are
  224. // made current with one context.
  225. glViewport(0, 0, event.GetSize().x, event.GetSize().y);
  226. }
  227. void TestGLCanvas::OnChar(wxKeyEvent& event)
  228. {
  229. switch( event.GetKeyCode() )
  230. {
  231. case WXK_ESCAPE:
  232. wxTheApp->ExitMainLoop();
  233. return;
  234. case WXK_LEFT:
  235. m_yrot -= 15.0;
  236. break;
  237. case WXK_RIGHT:
  238. m_yrot += 15.0;
  239. break;
  240. case WXK_UP:
  241. m_xrot += 15.0;
  242. break;
  243. case WXK_DOWN:
  244. m_xrot -= 15.0;
  245. break;
  246. case 's': case 'S':
  247. g_smooth = !g_smooth;
  248. if (g_smooth)
  249. glShadeModel(GL_SMOOTH);
  250. else
  251. glShadeModel(GL_FLAT);
  252. break;
  253. case 'l': case 'L':
  254. g_lighting = !g_lighting;
  255. if (g_lighting)
  256. glEnable(GL_LIGHTING);
  257. else
  258. glDisable(GL_LIGHTING);
  259. break;
  260. default:
  261. event.Skip();
  262. return;
  263. }
  264. Refresh(false);
  265. }
  266. void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
  267. {
  268. static int dragging = 0;
  269. static float last_x, last_y;
  270. // Allow default processing to happen, or else the canvas cannot gain focus
  271. // (for key events).
  272. event.Skip();
  273. if (event.LeftIsDown())
  274. {
  275. if (!dragging)
  276. {
  277. dragging = 1;
  278. }
  279. else
  280. {
  281. m_yrot += (event.GetX() - last_x)*1.0;
  282. m_xrot += (event.GetY() - last_y)*1.0;
  283. Refresh(false);
  284. }
  285. last_x = event.GetX();
  286. last_y = event.GetY();
  287. }
  288. else
  289. {
  290. dragging = 0;
  291. }
  292. }
  293. void TestGLCanvas::InitMaterials()
  294. {
  295. static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
  296. static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
  297. static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
  298. static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
  299. static const GLfloat front_mat_shininess[1] = {60.0f};
  300. static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
  301. static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
  302. /*
  303. static const GLfloat back_mat_shininess[1] = {60.0f};
  304. static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
  305. static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
  306. */
  307. static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  308. static const GLfloat lmodel_twoside[1] = {GL_FALSE};
  309. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  310. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  311. glLightfv(GL_LIGHT0, GL_POSITION, position0);
  312. glEnable(GL_LIGHT0);
  313. glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
  314. glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
  315. glLightfv(GL_LIGHT1, GL_POSITION, position1);
  316. glEnable(GL_LIGHT1);
  317. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  318. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  319. glEnable(GL_LIGHTING);
  320. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
  321. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
  322. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
  323. }
  324. void TestGLCanvas::InitGL()
  325. {
  326. // Make the new context current (activate it for use) with this canvas.
  327. SetCurrent(*m_glRC);
  328. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  329. glShadeModel(GL_SMOOTH);
  330. glEnable(GL_DEPTH_TEST);
  331. InitMaterials();
  332. glMatrixMode(GL_PROJECTION);
  333. glLoadIdentity();
  334. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  335. glMatrixMode(GL_MODELVIEW);
  336. glLoadIdentity();
  337. glTranslatef( 0.0, 0.0, -6.0 );
  338. if (g_use_vertex_arrays)
  339. {
  340. glVertexPointer( 3, GL_FLOAT, 0, m_verts );
  341. glNormalPointer( GL_FLOAT, 0, m_norms );
  342. glEnable( GL_VERTEX_ARRAY );
  343. glEnable( GL_NORMAL_ARRAY );
  344. }
  345. InitMaterials();
  346. LoadSurface("isosurf.dat.gz");
  347. }