#include "wx/timer.h"
#include "wx/glcanvas.h"
#include "wx/math.h"
-
-#if defined(__WXMAC__) || defined(__WXCOCOA__)
-# ifdef __DARWIN__
-# include <OpenGL/gl.h>
-# include <OpenGL/glu.h>
-# else
-# include <gl.h>
-# include <glu.h>
-# endif
-#else
-# include <GL/gl.h>
-# include <GL/glu.h>
-#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 <fstream>
+#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]) && numverts<MAXVERTS)
- {
- numverts++;
- }
+ // Create the main frame window
+ SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
+ wxDefaultPosition, wxDefaultSize));
- wxPrintf(_T("%d vertices, %d triangles\n"), numverts, numverts-2);
+ return true;
}
-
-static void draw_surface()
+void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
- GLint i;
+ parser.AddSwitch("", "-sb");
+ parser.AddSwitch("", "-db");
+ parser.AddSwitch("", "-speed");
+ parser.AddSwitch("", "-va");
-#ifdef GL_EXT_vertex_array
- if (use_vertex_arrays)
- {
- glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
- }
- else
-#endif
- {
- glBegin( GL_TRIANGLE_STRIP );
- for (i=0;i<numverts;i++)
- {
- glNormal3fv( norms[i] );
- glVertex3fv( verts[i] );
- }
- glEnd();
- }
+ wxApp::OnInitCmdLine(parser);
}
-
-static void draw1()
+bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glRotatef( yrot, 0.0f, 1.0f, 0.0f );
- glRotatef( xrot, 1.0f, 0.0f, 0.0f );
-
- draw_surface();
-
- glPopMatrix();
-
- glFlush(); // Not really necessary: buffer swapping below implies glFlush()
-}
+ if (parser.Found("-sb"))
+ g_doubleBuffer = GL_FALSE;
+ else if (parser.Found("-db"))
+ g_doubleBuffer = GL_TRUE;
-
-static void InitMaterials()
-{
- static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
- static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
- static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
- static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
- static const GLfloat front_mat_shininess[1] = {60.0f};
- static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
- static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
- /*
- static const GLfloat back_mat_shininess[1] = {60.0f};
- static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
- static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
- */
- static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
- static const GLfloat lmodel_twoside[1] = {GL_FALSE};
-
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, position0);
- glEnable(GL_LIGHT0);
-
- glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
- glLightfv(GL_LIGHT1, GL_POSITION, position1);
- glEnable(GL_LIGHT1);
-
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
- glEnable(GL_LIGHTING);
-
- glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
-}
-
-
-static void Init(void)
-{
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
-
- InitMaterials();
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -6.0 );
-
-#ifdef GL_EXT_vertex_array
- if (use_vertex_arrays)
+ if (parser.Found("-speed"))
{
- glVertexPointerEXT( 3, GL_FLOAT, 0, numverts, verts );
- glNormalPointerEXT( GL_FLOAT, 0, numverts, norms );
- glEnable( GL_VERTEX_ARRAY_EXT );
- glEnable( GL_NORMAL_ARRAY_EXT );
+ g_speed_test = GL_TRUE;
+ g_doubleBuffer = GL_TRUE;
}
-#endif
-}
-static GLenum Args(int argc, wxChar **argv)
-{
- GLint i;
+ if (parser.Found("-va"))
+ g_use_vertex_arrays = GL_TRUE;
- for (i = 1; i < argc; i++)
- {
- if (wxStrcmp(argv[i], _T("-sb")) == 0)
- {
- doubleBuffer = GL_FALSE;
- }
- else if (wxStrcmp(argv[i], _T("-db")) == 0)
- {
- doubleBuffer = GL_TRUE;
- }
- else if (wxStrcmp(argv[i], _T("-speed")) == 0)
- {
- speed_test = GL_TRUE;
- doubleBuffer = GL_TRUE;
- }
- else if (wxStrcmp(argv[i], _T("-va")) == 0)
- {
- use_vertex_arrays = GL_TRUE;
- }
- else
- {
- wxString msg = _T("Bad option: ");
- msg += argv[i];
- wxMessageBox(msg);
- return GL_FALSE;
- }
- }
-
- return GL_TRUE;
+ return wxApp::OnCmdLineParsed(parser);
}
-
-IMPLEMENT_APP(MyApp)
-
-// `Main program' equivalent, creating windows and returning main app frame
-bool MyApp::OnInit()
-{
- if ( !wxApp::OnInit() )
- return false;
-
- Args(argc, argv);
-
- // Create the main frame window
- new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
- wxDefaultPosition, wxDefaultSize);
-
- read_surface("isosurf.dat");
-
- Init();
-
- return true;
-}
+//---------------------------------------------------------------------------
+// MyFrame
+//---------------------------------------------------------------------------
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
#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__)
# endif
#endif
- if(!doubleBuffer)
+ if (!g_doubleBuffer)
{
- printf("don't have double buffer, disabling\n");
+ wxLogWarning("don't have double buffer, disabling\n");
+
#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 );
+ GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib);
}
MyFrame::~MyFrame()
Close(true);
}
-/*
- * TestGLCanvas implementation
- */
+
+//---------------------------------------------------------------------------
+// TestGLCanvas
+//---------------------------------------------------------------------------
BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
EVT_SIZE(TestGLCanvas::OnSize)
// 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()
delete m_glRC;
}
+void TestGLCanvas::LoadSurface(const wxString& filename)
+{
+ wxZlibInputStream* stream =
+ new wxZlibInputStream(new wxFFileInputStream(filename));
+ if (!stream || !stream->IsOk())
+ {
+ 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.
// 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<m_numverts;i++)
+ {
+ glNormal3fv( m_norms[i] );
+ glVertex3fv( m_verts[i] );
+ }
+
+ glEnd();
+ }
+
+ glPopMatrix();
+ glFlush(); // Not really necessary: buffer swapping below implies glFlush()
+
SwapBuffers();
}
return;
case WXK_LEFT:
- yrot -= 15.0;
+ m_yrot -= 15.0;
break;
case WXK_RIGHT:
- yrot += 15.0;
+ m_yrot += 15.0;
break;
case WXK_UP:
- xrot += 15.0;
+ m_xrot += 15.0;
break;
case WXK_DOWN:
- xrot -= 15.0;
+ m_xrot -= 15.0;
break;
case 's': case 'S':
- smooth = !smooth;
- if (smooth)
+ g_smooth = !g_smooth;
+ if (g_smooth)
{
glShadeModel(GL_SMOOTH);
}
break;
case 'l': case 'L':
- lighting = !lighting;
- if (lighting)
+ g_lighting = !g_lighting;
+ if (g_lighting)
{
glEnable(GL_LIGHTING);
}
}
else
{
- yrot += (event.GetX() - last_x)*1.0;
- xrot += (event.GetY() - last_y)*1.0;
+ m_yrot += (event.GetX() - last_x)*1.0;
+ m_xrot += (event.GetY() - last_y)*1.0;
Refresh(false);
}
last_x = event.GetX();
}
}
+void TestGLCanvas::InitMaterials()
+{
+ static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
+ static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
+ static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
+ static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
+ static const GLfloat front_mat_shininess[1] = {60.0f};
+ static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
+ static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
+ /*
+ static const GLfloat back_mat_shininess[1] = {60.0f};
+ static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
+ static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
+ */
+ static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+ static const GLfloat lmodel_twoside[1] = {GL_FALSE};
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
+ glLightfv(GL_LIGHT0, GL_POSITION, position0);
+ glEnable(GL_LIGHT0);
+
+ glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
+ glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
+ glLightfv(GL_LIGHT1, GL_POSITION, position1);
+ glEnable(GL_LIGHT1);
+
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
+ glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
+ glEnable(GL_LIGHTING);
+
+ glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
+}
+
+void TestGLCanvas::InitGL()
+{
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ glShadeModel(GL_SMOOTH);
+ glEnable(GL_DEPTH_TEST);
+
+ InitMaterials();
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glTranslatef( 0.0, 0.0, -6.0 );
+
+ if (g_use_vertex_arrays)
+ {
+ glVertexPointer( 3, GL_FLOAT, 0, m_verts );
+ glNormalPointer( GL_FLOAT, 0, m_norms );
+ glEnable( GL_VERTEX_ARRAY_EXT );
+ glEnable( GL_NORMAL_ARRAY_EXT );
+ }
+}
+