X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6c7f094959a591d034d593266471e0f3c6533f65..c0e69d720d8b15f788fe0998806f18c46f7d5d8b:/samples/opengl/isosurf/isosurf.cpp diff --git a/samples/opengl/isosurf/isosurf.cpp b/samples/opengl/isosurf/isosurf.cpp index c7f47eb8f5..471942b637 100644 --- a/samples/opengl/isosurf/isosurf.cpp +++ b/samples/opengl/isosurf/isosurf.cpp @@ -2,7 +2,7 @@ // Name: isosurf.cpp // Purpose: wxGLCanvas demo program // Author: Brian Paul (original gltk version), Wolfram Gloger -// Modified by: Julian Smart +// Modified by: Julian Smart, Francesco Montorsi // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart @@ -29,16 +29,15 @@ #include "wx/math.h" #include "wx/log.h" #include "wx/cmdline.h" -#include "wx/archive.h" #include "wx/wfstream.h" #include "wx/zstream.h" +#include "wx/txtstrm.h" #include "isosurf.h" #include "../../sample.xpm" // 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; @@ -52,43 +51,34 @@ GLboolean g_lighting = GL_TRUE; IMPLEMENT_APP(MyApp) -// `Main program' equivalent, creating windows and returning main app frame bool MyApp::OnInit() { if ( !wxApp::OnInit() ) return false; // Create the main frame window - SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"), - wxDefaultPosition, wxDefaultSize)); + SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"))); return true; } void MyApp::OnInitCmdLine(wxCmdLineParser& parser) { - parser.AddSwitch("", "-sb"); - parser.AddSwitch("", "-db"); - parser.AddSwitch("", "-speed"); - parser.AddSwitch("", "-va"); + parser.AddSwitch("", "sb", "Do not use double buffering"); + parser.AddSwitch("", "db", "Use double buffering"); + parser.AddSwitch("", "va", "Use vertex arrays"); wxApp::OnInitCmdLine(parser); } bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser) { - if (parser.Found("-sb")) + if (parser.Found("sb")) g_doubleBuffer = GL_FALSE; - else if (parser.Found("-db")) + else if (parser.Found("db")) g_doubleBuffer = GL_TRUE; - if (parser.Found("-speed")) - { - g_speed_test = GL_TRUE; - g_doubleBuffer = GL_TRUE; - } - - if (parser.Found("-va")) + if (parser.Found("va")) g_use_vertex_arrays = GL_TRUE; return wxApp::OnCmdLineParsed(parser); @@ -102,9 +92,8 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_EXIT, MyFrame::OnExit) END_EVENT_TABLE() -// My frame constructor MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, - const wxSize& size, long style) + const wxSize& size, long style) : wxFrame(frame, wxID_ANY, title, pos, size, style), m_canvas(NULL) { @@ -114,9 +103,9 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, // Make a menubar wxMenu *fileMenu = new wxMenu; - fileMenu->Append(wxID_EXIT, _T("E&xit")); + fileMenu->Append(wxID_EXIT, wxT("E&xit")); wxMenuBar *menuBar = new wxMenuBar; - menuBar->Append(fileMenu, _T("&File")); + menuBar->Append(fileMenu, wxT("&File")); SetMenuBar(menuBar); @@ -139,7 +128,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, if (!g_doubleBuffer) { - wxLogWarning("don't have double buffer, disabling\n"); + wxLogWarning("Disabling double buffering"); #ifdef __WXGTK__ gl_attrib[9] = None; @@ -150,8 +139,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, // Show the frame Show(true); - m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition, - GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib); + m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib); } MyFrame::~MyFrame() @@ -180,14 +168,13 @@ END_EVENT_TABLE() TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id, - const wxPoint& pos, - const wxSize& size, - long style, - const wxString& name, int* gl_attrib) - : wxGLCanvas(parent, id, gl_attrib, pos, size, - style | wxFULL_REPAINT_ON_RESIZE, name) + : wxGLCanvas(parent, id, gl_attrib) { + m_xrot = 0; + m_yrot = 0; + m_numverts = 0; + // Explicitly create a new rendering context instance for this canvas. m_glRC = new wxGLContext(this); @@ -206,6 +193,12 @@ TestGLCanvas::~TestGLCanvas() void TestGLCanvas::LoadSurface(const wxString& filename) { + // FIXME + // we need to set english locale to force wxTextInputStream's calls to + // wxStrtod to use the point and not the comma as decimal separator... + // (the isosurf.dat contains points and not commas)... + wxLocale l(wxLANGUAGE_ENGLISH); + wxZlibInputStream* stream = new wxZlibInputStream(new wxFFileInputStream(filename)); if (!stream || !stream->IsOk()) @@ -215,38 +208,34 @@ void TestGLCanvas::LoadSurface(const wxString& filename) 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++; + // we suppose to have in input a text file containing floating numbers + // space/newline-separed... first 3 numbers are the coordinates of a + // vertex and the following 3 are the relative vertex normal and so on... + + wxTextInputStream inFile(*stream); + m_numverts = 0; + + while (!stream->Eof() && m_numverts < MAXVERTS)// && m_numverts> m_verts[m_numverts][0] >> m_verts[m_numverts][1] >> m_verts[m_numverts][2]; + inFile >> m_norms[m_numverts][0] >> m_norms[m_numverts][1] >> m_norms[m_numverts][2]; + + m_numverts++; + } + + // discard last vertex; it is a zero caused by the EOF + m_numverts--; } delete stream; - wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"), + wxLogMessage(wxT("Loaded %d vertices, %d triangles from '%s'"), m_numverts, m_numverts-2, filename.c_str()); + + // NOTE: for some reason under wxGTK the following is required to avoid that + // the surface gets rendered in a small rectangle in the top-left corner of the frame + PostSizeEventToParent(); } void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) @@ -265,11 +254,11 @@ void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) glRotatef( m_xrot, 1.0f, 0.0f, 0.0f ); // draw the surface -/* if (g_use_vertex_arrays) + if (g_use_vertex_arrays) { glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts ); } - else*/ + else { glBegin( GL_TRIANGLE_STRIP ); @@ -328,25 +317,17 @@ void TestGLCanvas::OnChar(wxKeyEvent& event) case 's': case 'S': g_smooth = !g_smooth; if (g_smooth) - { glShadeModel(GL_SMOOTH); - } else - { glShadeModel(GL_FLAT); - } break; case 'l': case 'L': g_lighting = !g_lighting; if (g_lighting) - { glEnable(GL_LIGHTING); - } else - { glDisable(GL_LIGHTING); - } break; default: @@ -366,9 +347,9 @@ void TestGLCanvas::OnMouseEvent(wxMouseEvent& event) // (for key events). event.Skip(); - if(event.LeftIsDown()) + if (event.LeftIsDown()) { - if(!dragging) + if (!dragging) { dragging = 1; } @@ -444,8 +425,8 @@ void TestGLCanvas::InitGL() { glVertexPointer( 3, GL_FLOAT, 0, m_verts ); glNormalPointer( GL_FLOAT, 0, m_norms ); - glEnable( GL_VERTEX_ARRAY_EXT ); - glEnable( GL_NORMAL_ARRAY_EXT ); + glEnable( GL_VERTEX_ARRAY ); + glEnable( GL_NORMAL_ARRAY ); } }