fix the loading of the isosurf data; it was not loaded correctly on my system because...
[wxWidgets.git] / samples / opengl / isosurf / isosurf.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: isosurf.cpp
3 // Purpose: wxGLCanvas demo program
4 // Author: Brian Paul (original gltk version), Wolfram Gloger
5 // Modified by: Julian Smart, Francesco Montorsi
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !wxUSE_GLCANVAS
24 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
25 #endif
26
27 #include "wx/timer.h"
28 #include "wx/glcanvas.h"
29 #include "wx/math.h"
30 #include "wx/log.h"
31 #include "wx/cmdline.h"
32 #include "wx/wfstream.h"
33 #include "wx/zstream.h"
34 #include "wx/txtstrm.h"
35
36 #include "isosurf.h"
37 #include "../../sample.xpm"
38
39
40 // global options which can be set through command-line options
41 GLboolean g_use_vertex_arrays = GL_FALSE;
42 GLboolean g_doubleBuffer = GL_TRUE;
43 GLboolean g_smooth = GL_TRUE;
44 GLboolean g_lighting = GL_TRUE;
45
46
47
48 //---------------------------------------------------------------------------
49 // MyApp
50 //---------------------------------------------------------------------------
51
52 IMPLEMENT_APP(MyApp)
53
54 // `Main program' equivalent, creating windows and returning main app frame
55 bool MyApp::OnInit()
56 {
57 if ( !wxApp::OnInit() )
58 return false;
59
60 // Create the main frame window
61 SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample")));
62
63 return true;
64 }
65
66 void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
67 {
68 parser.AddSwitch("", "sb", "Do not use double buffering");
69 parser.AddSwitch("", "db", "Use double buffering");
70 parser.AddSwitch("", "va", "Use vertex arrays");
71
72 wxApp::OnInitCmdLine(parser);
73 }
74
75 bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
76 {
77 if (parser.Found("sb"))
78 g_doubleBuffer = GL_FALSE;
79 else if (parser.Found("db"))
80 g_doubleBuffer = GL_TRUE;
81
82 if (parser.Found("va"))
83 g_use_vertex_arrays = GL_TRUE;
84
85 return wxApp::OnCmdLineParsed(parser);
86 }
87
88 //---------------------------------------------------------------------------
89 // MyFrame
90 //---------------------------------------------------------------------------
91
92 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
94 END_EVENT_TABLE()
95
96 // My frame constructor
97 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
98 const wxSize& size, long style)
99 : wxFrame(frame, wxID_ANY, title, pos, size, style),
100 m_canvas(NULL)
101 {
102 SetIcon(wxICON(sample));
103
104
105 // Make a menubar
106 wxMenu *fileMenu = new wxMenu;
107
108 fileMenu->Append(wxID_EXIT, _T("E&xit"));
109 wxMenuBar *menuBar = new wxMenuBar;
110 menuBar->Append(fileMenu, _T("&File"));
111 SetMenuBar(menuBar);
112
113
114 // Make a TestGLCanvas
115
116 // JACS
117 #ifdef __WXMSW__
118 int *gl_attrib = NULL;
119 #else
120 int gl_attrib[20] =
121 { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
122 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
123 WX_GL_DOUBLEBUFFER,
124 # if defined(__WXMAC__) || defined(__WXCOCOA__)
125 GL_NONE };
126 # else
127 None };
128 # endif
129 #endif
130
131 if (!g_doubleBuffer)
132 {
133 wxLogWarning("Disabling double buffering");
134
135 #ifdef __WXGTK__
136 gl_attrib[9] = None;
137 #endif
138 g_doubleBuffer = GL_FALSE;
139 }
140
141 // Show the frame
142 Show(true);
143
144 m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib);
145 }
146
147 MyFrame::~MyFrame()
148 {
149 delete m_canvas;
150 }
151
152 // Intercept menu commands
153 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
154 {
155 // true is to force the frame to close
156 Close(true);
157 }
158
159
160 //---------------------------------------------------------------------------
161 // TestGLCanvas
162 //---------------------------------------------------------------------------
163
164 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
165 EVT_SIZE(TestGLCanvas::OnSize)
166 EVT_PAINT(TestGLCanvas::OnPaint)
167 EVT_CHAR(TestGLCanvas::OnChar)
168 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
169 END_EVENT_TABLE()
170
171 TestGLCanvas::TestGLCanvas(wxWindow *parent,
172 wxWindowID id,
173 int* gl_attrib)
174 : wxGLCanvas(parent, id, gl_attrib)
175 {
176 m_xrot = 0;
177 m_yrot = 0;
178 m_numverts = 0;
179
180 // Explicitly create a new rendering context instance for this canvas.
181 m_glRC = new wxGLContext(this);
182
183 // Make the new context current (activate it for use) with this canvas.
184 SetCurrent(*m_glRC);
185
186 InitGL();
187 InitMaterials();
188 LoadSurface("isosurf.dat.gz");
189 }
190
191 TestGLCanvas::~TestGLCanvas()
192 {
193 delete m_glRC;
194 }
195
196 void TestGLCanvas::LoadSurface(const wxString& filename)
197 {
198 // FIXME
199 // we need to set english locale to force wxTextInputStream's calls to
200 // wxStrtod to use the point and not the comma as decimal separator...
201 // (the isosurf.dat contains points and not commas)...
202 wxLocale l(wxLANGUAGE_ENGLISH);
203
204 wxZlibInputStream* stream =
205 new wxZlibInputStream(new wxFFileInputStream(filename));
206 if (!stream || !stream->IsOk())
207 {
208 wxLogError("Cannot load '%s' type of files!", filename.c_str());
209 delete stream;
210 return;
211 }
212
213 {
214 // we suppose to have in input a text file containing floating numbers
215 // space/newline-separed... first 3 numbers are the coordinates of a
216 // vertex and the following 3 are the relative vertex normal and so on...
217
218 wxTextInputStream inFile(*stream);
219 m_numverts = 0;
220
221 while (!stream->Eof() && m_numverts < MAXVERTS)// && m_numverts<MAXVERTS)
222 {
223 inFile >> m_verts[m_numverts][0] >> m_verts[m_numverts][1] >> m_verts[m_numverts][2];
224 inFile >> m_norms[m_numverts][0] >> m_norms[m_numverts][1] >> m_norms[m_numverts][2];
225
226 m_numverts++;
227 }
228
229 // discard last vertex; it is a zero caused by the EOF
230 m_numverts--;
231 }
232
233 delete stream;
234
235 wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"),
236 m_numverts, m_numverts-2, filename.c_str());
237 }
238
239 void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
240 {
241 // This is a dummy, to avoid an endless succession of paint messages.
242 // OnPaint handlers must always create a wxPaintDC.
243 wxPaintDC dc(this);
244
245 // This is normally only necessary if there is more than one wxGLCanvas
246 // or more than one wxGLContext in the application.
247 SetCurrent(*m_glRC);
248
249 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
250 glPushMatrix();
251 glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
252 glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
253
254 // draw the surface
255 if (g_use_vertex_arrays)
256 {
257 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
258 }
259 else
260 {
261 glBegin( GL_TRIANGLE_STRIP );
262
263 for (int i=0;i<m_numverts;i++)
264 {
265 glNormal3fv( m_norms[i] );
266 glVertex3fv( m_verts[i] );
267 }
268
269 glEnd();
270 }
271
272 glPopMatrix();
273 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
274
275 SwapBuffers();
276 }
277
278 void TestGLCanvas::OnSize(wxSizeEvent& event)
279 {
280 // This is normally only necessary if there is more than one wxGLCanvas
281 // or more than one wxGLContext in the application.
282 SetCurrent(*m_glRC);
283
284 // It's up to the application code to update the OpenGL viewport settings.
285 // This is OK here only because there is only one canvas that uses the
286 // context. See the cube sample for that case that multiple canvases are
287 // made current with one context.
288 glViewport(0, 0, event.GetSize().x, event.GetSize().y);
289 }
290
291 void TestGLCanvas::OnChar(wxKeyEvent& event)
292 {
293 switch( event.GetKeyCode() )
294 {
295 case WXK_ESCAPE:
296 wxTheApp->ExitMainLoop();
297 return;
298
299 case WXK_LEFT:
300 m_yrot -= 15.0;
301 break;
302
303 case WXK_RIGHT:
304 m_yrot += 15.0;
305 break;
306
307 case WXK_UP:
308 m_xrot += 15.0;
309 break;
310
311 case WXK_DOWN:
312 m_xrot -= 15.0;
313 break;
314
315 case 's': case 'S':
316 g_smooth = !g_smooth;
317 if (g_smooth)
318 glShadeModel(GL_SMOOTH);
319 else
320 glShadeModel(GL_FLAT);
321 break;
322
323 case 'l': case 'L':
324 g_lighting = !g_lighting;
325 if (g_lighting)
326 glEnable(GL_LIGHTING);
327 else
328 glDisable(GL_LIGHTING);
329 break;
330
331 default:
332 event.Skip();
333 return;
334 }
335
336 Refresh(false);
337 }
338
339 void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
340 {
341 static int dragging = 0;
342 static float last_x, last_y;
343
344 // Allow default processing to happen, or else the canvas cannot gain focus
345 // (for key events).
346 event.Skip();
347
348 if(event.LeftIsDown())
349 {
350 if(!dragging)
351 {
352 dragging = 1;
353 }
354 else
355 {
356 m_yrot += (event.GetX() - last_x)*1.0;
357 m_xrot += (event.GetY() - last_y)*1.0;
358 Refresh(false);
359 }
360 last_x = event.GetX();
361 last_y = event.GetY();
362 }
363 else
364 {
365 dragging = 0;
366 }
367 }
368
369 void TestGLCanvas::InitMaterials()
370 {
371 static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
372 static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
373 static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
374 static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
375 static const GLfloat front_mat_shininess[1] = {60.0f};
376 static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
377 static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
378 /*
379 static const GLfloat back_mat_shininess[1] = {60.0f};
380 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
381 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
382 */
383 static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
384 static const GLfloat lmodel_twoside[1] = {GL_FALSE};
385
386 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
387 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
388 glLightfv(GL_LIGHT0, GL_POSITION, position0);
389 glEnable(GL_LIGHT0);
390
391 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
392 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
393 glLightfv(GL_LIGHT1, GL_POSITION, position1);
394 glEnable(GL_LIGHT1);
395
396 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
397 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
398 glEnable(GL_LIGHTING);
399
400 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
401 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
402 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
403 }
404
405 void TestGLCanvas::InitGL()
406 {
407 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
408
409 glShadeModel(GL_SMOOTH);
410 glEnable(GL_DEPTH_TEST);
411
412 InitMaterials();
413
414 glMatrixMode(GL_PROJECTION);
415 glLoadIdentity();
416 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
417
418 glMatrixMode(GL_MODELVIEW);
419 glLoadIdentity();
420 glTranslatef( 0.0, 0.0, -6.0 );
421
422 if (g_use_vertex_arrays)
423 {
424 glVertexPointer( 3, GL_FLOAT, 0, m_verts );
425 glNormalPointer( GL_FLOAT, 0, m_norms );
426 glEnable( GL_VERTEX_ARRAY );
427 glEnable( GL_NORMAL_ARRAY );
428 }
429 }
430