From: Francesco Montorsi Date: Fri, 12 Dec 2008 20:42:51 +0000 (+0000) Subject: modernize the sample removing global C-style functions; partially fix loading of... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/6c7f094959a591d034d593266471e0f3c6533f65 modernize the sample removing global C-style functions; partially fix loading of the .dat.gz file git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57294 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/samples/opengl/isosurf/isosurf.cpp b/samples/opengl/isosurf/isosurf.cpp index f89ba3f71f..c7f47eb8f5 100644 --- a/samples/opengl/isosurf/isosurf.cpp +++ b/samples/opengl/isosurf/isosurf.cpp @@ -27,232 +27,76 @@ #include "wx/timer.h" #include "wx/glcanvas.h" #include "wx/math.h" - -#if defined(__WXMAC__) || defined(__WXCOCOA__) -# ifdef __DARWIN__ -# include -# include -# else -# include -# include -# endif -#else -# include -# include -#endif - -// disabled because this has apparently changed in OpenGL 1.2, so doesn't link -// correctly if this is on... -#ifdef GL_EXT_vertex_array -#undef GL_EXT_vertex_array -#endif - -#include +#include "wx/log.h" +#include "wx/cmdline.h" +#include "wx/archive.h" +#include "wx/wfstream.h" +#include "wx/zstream.h" #include "isosurf.h" #include "../../sample.xpm" -// The following part is taken largely unchanged from the original C Version - -GLboolean speed_test = GL_FALSE; -GLboolean use_vertex_arrays = GL_FALSE; -GLboolean doubleBuffer = GL_TRUE; +// global options which can be set through command-line options +GLboolean g_speed_test = GL_FALSE; +GLboolean g_use_vertex_arrays = GL_FALSE; +GLboolean g_doubleBuffer = GL_TRUE; +GLboolean g_smooth = GL_TRUE; +GLboolean g_lighting = GL_TRUE; -GLboolean smooth = GL_TRUE; -GLboolean lighting = GL_TRUE; -#define MAXVERTS 10000 - -static GLfloat verts[MAXVERTS][3]; -static GLfloat norms[MAXVERTS][3]; -static GLint numverts; - -static GLfloat xrot; -static GLfloat yrot; +//--------------------------------------------------------------------------- +// MyApp +//--------------------------------------------------------------------------- +IMPLEMENT_APP(MyApp) -static void read_surface(const char *filename) +// `Main program' equivalent, creating windows and returning main app frame +bool MyApp::OnInit() { - std::ifstream inFile(filename); - numverts = 0; - - if ( !inFile ) - { - wxLogError("Couldn't read \"%s\"", filename); - return; - } + if ( !wxApp::OnInit() ) + return false; - while ((inFile >> verts[numverts][0] >> verts[numverts][1] >> verts[numverts][2] - >> norms[numverts][0] >> norms[numverts][1] >> norms[numverts][2]) && numvertsIsOk()) + { + wxLogError("Cannot load '%s' type of files!", filename.c_str()); + delete stream; + return; + } + + m_numverts = 0; + + const size_t sz = sizeof(GLfloat); + while (!stream->Eof() && m_numverts < MAXVERTS) + { + // read a vertex + for (int i=0; i<3; i++) + if (stream->Read(&m_verts[m_numverts][i], sz).LastRead() != sz) + { + wxLogError("Cannot read the %d-th vertex in '%s'!", + m_numverts, filename.c_str()); + delete stream; + return; + } + + // read its normal + for (int i=0; i<3; i++) + if (stream->Read(&m_norms[m_numverts][i], sz).LastRead() != sz) + { + wxLogError("Cannot read the %d-th vertex in '%s'!", + m_numverts, filename.c_str()); + delete stream; + return; + } + + m_numverts++; + } + + delete stream; + + wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"), + m_numverts, m_numverts-2, filename.c_str()); +} + void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) { // This is a dummy, to avoid an endless succession of paint messages. @@ -370,7 +259,32 @@ void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) // or more than one wxGLContext in the application. SetCurrent(*m_glRC); - draw1(); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glPushMatrix(); + glRotatef( m_yrot, 0.0f, 1.0f, 0.0f ); + glRotatef( m_xrot, 1.0f, 0.0f, 0.0f ); + + // draw the surface +/* if (g_use_vertex_arrays) + { + glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts ); + } + else*/ + { + glBegin( GL_TRIANGLE_STRIP ); + + for (int i=0;i +# include +# else +# include +# include +# endif +#else +# include +# include +#endif + +// the maximum number of vertex in the loaded .dat file +#define MAXVERTS 10000 + + // Define a new application type class MyApp : public wxApp { public: virtual bool OnInit(); + + virtual void OnInitCmdLine(wxCmdLineParser& parser); + virtual bool OnCmdLineParsed(wxCmdLineParser& parser); }; +// The OpenGL-enabled canvas class TestGLCanvas : public wxGLCanvas { public: @@ -38,15 +60,26 @@ public: void OnChar(wxKeyEvent& event); void OnMouseEvent(wxMouseEvent& event); + void LoadSurface(const wxString& filename); + void InitMaterials(); + void InitGL(); private: wxGLContext* m_glRC; + GLfloat m_verts[MAXVERTS][3]; + GLfloat m_norms[MAXVERTS][3]; + GLint m_numverts; + + GLfloat m_xrot; + GLfloat m_yrot; + DECLARE_NO_COPY_CLASS(TestGLCanvas) DECLARE_EVENT_TABLE() }; +// The frame containing the GL canvas class MyFrame : public wxFrame { public: @@ -66,5 +99,6 @@ private : DECLARE_EVENT_TABLE() }; + #endif // _WX_ISOSURF_H_