///////////////////////////////////////////////////////////////////////////// // Name: isosurf.cpp // Purpose: wxGLCanvas demo program // Author: Brian Paul (original gltk version), Wolfram Gloger // Modified by: Julian Smart // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ #pragma implementation #pragma interface #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wx/timer.h" #include "glcanvas.h" #include #include #include "isosurf.h" // The following part is taken largely unchanged from the original C Version #include 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( char *filename ) { FILE *f; f = fopen(filename,"r"); if (!f) { wxString msg("Couldn't read "); msg += filename; wxMessageBox(msg); return; } numverts = 0; while (!feof(f) && numvertsSetIcon(wxIcon("mondrian")); // Make a menubar wxMenu *fileMenu = new wxMenu; fileMenu->Append(wxID_EXIT, "E&xit"); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(fileMenu, "&File"); frame->SetMenuBar(menuBar); // Make a TestGLCanvas // JACS #ifdef __WXMSW__ int *gl_attrib = NULL; #else int gl_attrib[20] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, None }; #endif if(!doubleBuffer #ifdef __X__ // JACS || !wxGLCanvas::HaveVisual(gl_attrib) #endif ) { printf("don't have double buffer, disabling\n"); #ifndef __WXMSW__ gl_attrib[9] = None; #endif doubleBuffer = GL_FALSE; } frame->m_canvas = new TestGLCanvas(frame, -1, wxPoint(0, 0), wxSize(200, 200), 0, "TestGLCanvas", gl_attrib); // Show the frame frame->Show(TRUE); frame->m_canvas->SetCurrent(); read_surface( "isosurf.dat" ); Init(); return TRUE; } 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): wxFrame(frame, -1, title, pos, size, style) { m_canvas = NULL; } // Intercept menu commands void MyFrame::OnExit(wxCommandEvent& event) { Destroy(); } bool MyFrame::OnClose(void) { return TRUE; } /* * TestGLCanvas implementation */ BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) EVT_SIZE(TestGLCanvas::OnSize) EVT_PAINT(TestGLCanvas::OnPaint) EVT_CHAR(TestGLCanvas::OnChar) EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent) EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground) 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, pos, size, style, name, gl_attrib) { parent->Show(TRUE); SetCurrent(); /* 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; } } TestGLCanvas::~TestGLCanvas(void) { } void TestGLCanvas::OnPaint( wxPaintEvent& event ) { // This is a dummy, to avoid an endless succession of paint messages. // OnPaint handlers must always create a wxPaintDC. wxPaintDC dc(this); draw1(); SwapBuffers(); } void TestGLCanvas::OnSize(wxSizeEvent& event) { SetCurrent(); int width, height; GetClientSize(& width, & height); Reshape(width, height); } void TestGLCanvas::OnChar(wxKeyEvent& event) { switch(event.KeyCode()) { case WXK_ESCAPE: exit(0); case WXK_LEFT: yrot -= 15.0; break; case WXK_RIGHT: yrot += 15.0; break; case WXK_UP: xrot += 15.0; break; case WXK_DOWN: xrot -= 15.0; break; case 's': case 'S': smooth = !smooth; if (smooth) { glShadeModel(GL_SMOOTH); } else { glShadeModel(GL_FLAT); } break; case 'l': case 'L': lighting = !lighting; if (lighting) { glEnable(GL_LIGHTING); } else { glDisable(GL_LIGHTING); } break; } Refresh(); } void TestGLCanvas::OnMouseEvent(wxMouseEvent& event) { static int dragging = 0; static float last_x, last_y; //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown()); if(event.LeftIsDown()) { if(!dragging) { dragging = 1; } else { yrot += (event.GetX() - last_x)*1.0; xrot += (event.GetY() - last_y)*1.0; Refresh(); } last_x = event.GetX(); last_y = event.GetY(); } else dragging = 0; } void TestGLCanvas::OnEraseBackground(wxEraseEvent& event) { // Do nothing, to avoid flashing. }