modernize the sample removing global C-style functions; partially fix loading of...
[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
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/archive.h"
33 #include "wx/wfstream.h"
34 #include "wx/zstream.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_speed_test = GL_FALSE;
42 GLboolean g_use_vertex_arrays = GL_FALSE;
43 GLboolean g_doubleBuffer = GL_TRUE;
44 GLboolean g_smooth = GL_TRUE;
45 GLboolean g_lighting = GL_TRUE;
46
47
48
49 //---------------------------------------------------------------------------
50 // MyApp
51 //---------------------------------------------------------------------------
52
53 IMPLEMENT_APP(MyApp)
54
55 // `Main program' equivalent, creating windows and returning main app frame
56 bool MyApp::OnInit()
57 {
58 if ( !wxApp::OnInit() )
59 return false;
60
61 // Create the main frame window
62 SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
63 wxDefaultPosition, wxDefaultSize));
64
65 return true;
66 }
67
68 void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
69 {
70 parser.AddSwitch("", "-sb");
71 parser.AddSwitch("", "-db");
72 parser.AddSwitch("", "-speed");
73 parser.AddSwitch("", "-va");
74
75 wxApp::OnInitCmdLine(parser);
76 }
77
78 bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
79 {
80 if (parser.Found("-sb"))
81 g_doubleBuffer = GL_FALSE;
82 else if (parser.Found("-db"))
83 g_doubleBuffer = GL_TRUE;
84
85 if (parser.Found("-speed"))
86 {
87 g_speed_test = GL_TRUE;
88 g_doubleBuffer = GL_TRUE;
89 }
90
91 if (parser.Found("-va"))
92 g_use_vertex_arrays = GL_TRUE;
93
94 return wxApp::OnCmdLineParsed(parser);
95 }
96
97 //---------------------------------------------------------------------------
98 // MyFrame
99 //---------------------------------------------------------------------------
100
101 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
102 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
103 END_EVENT_TABLE()
104
105 // My frame constructor
106 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
107 const wxSize& size, long style)
108 : wxFrame(frame, wxID_ANY, title, pos, size, style),
109 m_canvas(NULL)
110 {
111 SetIcon(wxICON(sample));
112
113
114 // Make a menubar
115 wxMenu *fileMenu = new wxMenu;
116
117 fileMenu->Append(wxID_EXIT, _T("E&xit"));
118 wxMenuBar *menuBar = new wxMenuBar;
119 menuBar->Append(fileMenu, _T("&File"));
120 SetMenuBar(menuBar);
121
122
123 // Make a TestGLCanvas
124
125 // JACS
126 #ifdef __WXMSW__
127 int *gl_attrib = NULL;
128 #else
129 int gl_attrib[20] =
130 { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
131 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
132 WX_GL_DOUBLEBUFFER,
133 # if defined(__WXMAC__) || defined(__WXCOCOA__)
134 GL_NONE };
135 # else
136 None };
137 # endif
138 #endif
139
140 if (!g_doubleBuffer)
141 {
142 wxLogWarning("don't have double buffer, disabling\n");
143
144 #ifdef __WXGTK__
145 gl_attrib[9] = None;
146 #endif
147 g_doubleBuffer = GL_FALSE;
148 }
149
150 // Show the frame
151 Show(true);
152
153 m_canvas = new TestGLCanvas(this, wxID_ANY, wxDefaultPosition,
154 GetClientSize(), 0, _T("TestGLCanvas"), gl_attrib);
155 }
156
157 MyFrame::~MyFrame()
158 {
159 delete m_canvas;
160 }
161
162 // Intercept menu commands
163 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
164 {
165 // true is to force the frame to close
166 Close(true);
167 }
168
169
170 //---------------------------------------------------------------------------
171 // TestGLCanvas
172 //---------------------------------------------------------------------------
173
174 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
175 EVT_SIZE(TestGLCanvas::OnSize)
176 EVT_PAINT(TestGLCanvas::OnPaint)
177 EVT_CHAR(TestGLCanvas::OnChar)
178 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
179 END_EVENT_TABLE()
180
181 TestGLCanvas::TestGLCanvas(wxWindow *parent,
182 wxWindowID id,
183 const wxPoint& pos,
184 const wxSize& size,
185 long style,
186 const wxString& name,
187 int* gl_attrib)
188 : wxGLCanvas(parent, id, gl_attrib, pos, size,
189 style | wxFULL_REPAINT_ON_RESIZE, name)
190 {
191 // Explicitly create a new rendering context instance for this canvas.
192 m_glRC = new wxGLContext(this);
193
194 // Make the new context current (activate it for use) with this canvas.
195 SetCurrent(*m_glRC);
196
197 InitGL();
198 InitMaterials();
199 LoadSurface("isosurf.dat.gz");
200 }
201
202 TestGLCanvas::~TestGLCanvas()
203 {
204 delete m_glRC;
205 }
206
207 void TestGLCanvas::LoadSurface(const wxString& filename)
208 {
209 wxZlibInputStream* stream =
210 new wxZlibInputStream(new wxFFileInputStream(filename));
211 if (!stream || !stream->IsOk())
212 {
213 wxLogError("Cannot load '%s' type of files!", filename.c_str());
214 delete stream;
215 return;
216 }
217
218 m_numverts = 0;
219
220 const size_t sz = sizeof(GLfloat);
221 while (!stream->Eof() && m_numverts < MAXVERTS)
222 {
223 // read a vertex
224 for (int i=0; i<3; i++)
225 if (stream->Read(&m_verts[m_numverts][i], sz).LastRead() != sz)
226 {
227 wxLogError("Cannot read the %d-th vertex in '%s'!",
228 m_numverts, filename.c_str());
229 delete stream;
230 return;
231 }
232
233 // read its normal
234 for (int i=0; i<3; i++)
235 if (stream->Read(&m_norms[m_numverts][i], sz).LastRead() != sz)
236 {
237 wxLogError("Cannot read the %d-th vertex in '%s'!",
238 m_numverts, filename.c_str());
239 delete stream;
240 return;
241 }
242
243 m_numverts++;
244 }
245
246 delete stream;
247
248 wxLogMessage(_T("Loaded %d vertices, %d triangles from '%s'"),
249 m_numverts, m_numverts-2, filename.c_str());
250 }
251
252 void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
253 {
254 // This is a dummy, to avoid an endless succession of paint messages.
255 // OnPaint handlers must always create a wxPaintDC.
256 wxPaintDC dc(this);
257
258 // This is normally only necessary if there is more than one wxGLCanvas
259 // or more than one wxGLContext in the application.
260 SetCurrent(*m_glRC);
261
262 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
263 glPushMatrix();
264 glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
265 glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
266
267 // draw the surface
268 /* if (g_use_vertex_arrays)
269 {
270 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
271 }
272 else*/
273 {
274 glBegin( GL_TRIANGLE_STRIP );
275
276 for (int i=0;i<m_numverts;i++)
277 {
278 glNormal3fv( m_norms[i] );
279 glVertex3fv( m_verts[i] );
280 }
281
282 glEnd();
283 }
284
285 glPopMatrix();
286 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
287
288 SwapBuffers();
289 }
290
291 void TestGLCanvas::OnSize(wxSizeEvent& event)
292 {
293 // This is normally only necessary if there is more than one wxGLCanvas
294 // or more than one wxGLContext in the application.
295 SetCurrent(*m_glRC);
296
297 // It's up to the application code to update the OpenGL viewport settings.
298 // This is OK here only because there is only one canvas that uses the
299 // context. See the cube sample for that case that multiple canvases are
300 // made current with one context.
301 glViewport(0, 0, event.GetSize().x, event.GetSize().y);
302 }
303
304 void TestGLCanvas::OnChar(wxKeyEvent& event)
305 {
306 switch( event.GetKeyCode() )
307 {
308 case WXK_ESCAPE:
309 wxTheApp->ExitMainLoop();
310 return;
311
312 case WXK_LEFT:
313 m_yrot -= 15.0;
314 break;
315
316 case WXK_RIGHT:
317 m_yrot += 15.0;
318 break;
319
320 case WXK_UP:
321 m_xrot += 15.0;
322 break;
323
324 case WXK_DOWN:
325 m_xrot -= 15.0;
326 break;
327
328 case 's': case 'S':
329 g_smooth = !g_smooth;
330 if (g_smooth)
331 {
332 glShadeModel(GL_SMOOTH);
333 }
334 else
335 {
336 glShadeModel(GL_FLAT);
337 }
338 break;
339
340 case 'l': case 'L':
341 g_lighting = !g_lighting;
342 if (g_lighting)
343 {
344 glEnable(GL_LIGHTING);
345 }
346 else
347 {
348 glDisable(GL_LIGHTING);
349 }
350 break;
351
352 default:
353 event.Skip();
354 return;
355 }
356
357 Refresh(false);
358 }
359
360 void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
361 {
362 static int dragging = 0;
363 static float last_x, last_y;
364
365 // Allow default processing to happen, or else the canvas cannot gain focus
366 // (for key events).
367 event.Skip();
368
369 if(event.LeftIsDown())
370 {
371 if(!dragging)
372 {
373 dragging = 1;
374 }
375 else
376 {
377 m_yrot += (event.GetX() - last_x)*1.0;
378 m_xrot += (event.GetY() - last_y)*1.0;
379 Refresh(false);
380 }
381 last_x = event.GetX();
382 last_y = event.GetY();
383 }
384 else
385 {
386 dragging = 0;
387 }
388 }
389
390 void TestGLCanvas::InitMaterials()
391 {
392 static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
393 static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
394 static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
395 static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
396 static const GLfloat front_mat_shininess[1] = {60.0f};
397 static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
398 static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
399 /*
400 static const GLfloat back_mat_shininess[1] = {60.0f};
401 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
402 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
403 */
404 static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
405 static const GLfloat lmodel_twoside[1] = {GL_FALSE};
406
407 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
408 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
409 glLightfv(GL_LIGHT0, GL_POSITION, position0);
410 glEnable(GL_LIGHT0);
411
412 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
413 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
414 glLightfv(GL_LIGHT1, GL_POSITION, position1);
415 glEnable(GL_LIGHT1);
416
417 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
418 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
419 glEnable(GL_LIGHTING);
420
421 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
422 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
423 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
424 }
425
426 void TestGLCanvas::InitGL()
427 {
428 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
429
430 glShadeModel(GL_SMOOTH);
431 glEnable(GL_DEPTH_TEST);
432
433 InitMaterials();
434
435 glMatrixMode(GL_PROJECTION);
436 glLoadIdentity();
437 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
438
439 glMatrixMode(GL_MODELVIEW);
440 glLoadIdentity();
441 glTranslatef( 0.0, 0.0, -6.0 );
442
443 if (g_use_vertex_arrays)
444 {
445 glVertexPointer( 3, GL_FLOAT, 0, m_verts );
446 glNormalPointer( GL_FLOAT, 0, m_norms );
447 glEnable( GL_VERTEX_ARRAY_EXT );
448 glEnable( GL_NORMAL_ARRAY_EXT );
449 }
450 }
451