X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7881f83bed81ee6993f83a0903cce02cbf46e53b..8076df5d81549d5afe7afe392e3b531964e33d47:/samples/opengl/isosurf/isosurf.cpp diff --git a/samples/opengl/isosurf/isosurf.cpp b/samples/opengl/isosurf/isosurf.cpp index 37e3bb0cc1..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,9 +29,9 @@ #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" @@ -51,15 +51,13 @@ 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; } @@ -94,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) { @@ -106,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); @@ -131,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; @@ -142,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() @@ -172,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); @@ -198,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()) @@ -207,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) ) @@ -257,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 ); @@ -320,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: @@ -358,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; } @@ -436,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 ); } }