X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/451c13c85ce4338db0853cfc1f148f8c92eaf91c..c0e69d720d8b15f788fe0998806f18c46f7d5d8b:/samples/opengl/isosurf/isosurf.cpp?ds=sidebyside diff --git a/samples/opengl/isosurf/isosurf.cpp b/samples/opengl/isosurf/isosurf.cpp index f89ba3f71f..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 @@ -27,240 +27,73 @@ #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/wfstream.h" +#include "wx/zstream.h" +#include "wx/txtstrm.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; - -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; - - -static void read_surface(const char *filename) -{ - std::ifstream inFile(filename); - numverts = 0; - if ( !inFile ) - { - wxLogError("Couldn't read \"%s\"", filename); - return; - } +// global options which can be set through command-line options +GLboolean g_use_vertex_arrays = GL_FALSE; +GLboolean g_doubleBuffer = GL_TRUE; +GLboolean g_smooth = GL_TRUE; +GLboolean g_lighting = GL_TRUE; - while ((inFile >> verts[numverts][0] >> verts[numverts][1] >> verts[numverts][2] - >> norms[numverts][0] >> norms[numverts][1] >> norms[numverts][2]) && numvertsAppend(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); @@ -282,7 +115,8 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, #ifdef __WXMSW__ int *gl_attrib = NULL; #else - int gl_attrib[20] = { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1, + int gl_attrib[20] = + { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1, WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1, WX_GL_DOUBLEBUFFER, # if defined(__WXMAC__) || defined(__WXCOCOA__) @@ -292,20 +126,20 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, # endif #endif - if(!doubleBuffer) + if (!g_doubleBuffer) { - printf("don't have double buffer, disabling\n"); + wxLogWarning("Disabling double buffering"); + #ifdef __WXGTK__ gl_attrib[9] = None; #endif - doubleBuffer = GL_FALSE; + g_doubleBuffer = GL_FALSE; } // 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() @@ -320,9 +154,10 @@ void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) ) Close(true); } -/* - * TestGLCanvas implementation - */ + +//--------------------------------------------------------------------------- +// TestGLCanvas +//--------------------------------------------------------------------------- BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) EVT_SIZE(TestGLCanvas::OnSize) @@ -333,26 +168,22 @@ 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); // Make the new context current (activate it for use) with this canvas. SetCurrent(*m_glRC); - /* Make sure server supports the vertex array extension */ - char* extensions = (char *) glGetString( GL_EXTENSIONS ); - if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) - { - use_vertex_arrays = GL_FALSE; - } + InitGL(); + InitMaterials(); + LoadSurface("isosurf.dat.gz"); } TestGLCanvas::~TestGLCanvas() @@ -360,6 +191,53 @@ TestGLCanvas::~TestGLCanvas() delete m_glRC; } +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()) + { + wxLogError("Cannot load '%s' type of files!", filename.c_str()); + delete stream; + return; + } + + { + // 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(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) ) { // This is a dummy, to avoid an endless succession of paint messages. @@ -370,7 +248,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