call OnInit() from all samples to allow using standard command line options with...
[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
31 #if defined(__WXMAC__) || defined(__WXCOCOA__)
32 # ifdef __DARWIN__
33 # include <OpenGL/gl.h>
34 # include <OpenGL/glu.h>
35 # else
36 # include <gl.h>
37 # include <glu.h>
38 # endif
39 #else
40 # include <GL/gl.h>
41 # include <GL/glu.h>
42 #endif
43
44 // disabled because this has apparently changed in OpenGL 1.2, so doesn't link
45 // correctly if this is on...
46 #ifdef GL_EXT_vertex_array
47 #undef GL_EXT_vertex_array
48 #endif
49
50 #include "isosurf.h"
51
52 #include "../../sample.xpm"
53
54 // The following part is taken largely unchanged from the original C Version
55
56 GLboolean speed_test = GL_FALSE;
57 GLboolean use_vertex_arrays = GL_FALSE;
58
59 GLboolean doubleBuffer = GL_TRUE;
60
61 GLboolean smooth = GL_TRUE;
62 GLboolean lighting = GL_TRUE;
63
64
65 #define MAXVERTS 10000
66
67 static GLfloat verts[MAXVERTS][3];
68 static GLfloat norms[MAXVERTS][3];
69 static GLint numverts;
70
71 static GLfloat xrot;
72 static GLfloat yrot;
73
74
75 static void read_surface( const wxChar *filename )
76 {
77 FILE *f = wxFopen(filename,_T("r"));
78 if (!f)
79 {
80 wxString msg = _T("Couldn't read ");
81 msg += filename;
82 wxMessageBox(msg);
83 return;
84 }
85
86 numverts = 0;
87 while (!feof(f) && numverts<MAXVERTS)
88 {
89 fscanf( f, "%f %f %f %f %f %f",
90 &verts[numverts][0], &verts[numverts][1], &verts[numverts][2],
91 &norms[numverts][0], &norms[numverts][1], &norms[numverts][2] );
92 numverts++;
93 }
94
95 numverts--;
96
97 wxPrintf(_T("%d vertices, %d triangles\n"), numverts, numverts-2);
98
99 fclose(f);
100 }
101
102
103 static void draw_surface()
104 {
105 GLint i;
106
107 #ifdef GL_EXT_vertex_array
108 if (use_vertex_arrays)
109 {
110 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
111 }
112 else
113 #endif
114 {
115 glBegin( GL_TRIANGLE_STRIP );
116 for (i=0;i<numverts;i++)
117 {
118 glNormal3fv( norms[i] );
119 glVertex3fv( verts[i] );
120 }
121 glEnd();
122 }
123 }
124
125
126 static void draw1()
127 {
128 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
129 glPushMatrix();
130 glRotatef( yrot, 0.0f, 1.0f, 0.0f );
131 glRotatef( xrot, 1.0f, 0.0f, 0.0f );
132
133 draw_surface();
134
135 glPopMatrix();
136
137 glFlush();
138 }
139
140
141 static void InitMaterials()
142 {
143 static const GLfloat ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f};
144 static const GLfloat diffuse[4] = {0.5f, 1.0f, 1.0f, 1.0f};
145 static const GLfloat position0[4] = {0.0f, 0.0f, 20.0f, 0.0f};
146 static const GLfloat position1[4] = {0.0f, 0.0f, -20.0f, 0.0f};
147 static const GLfloat front_mat_shininess[1] = {60.0f};
148 static const GLfloat front_mat_specular[4] = {0.2f, 0.2f, 0.2f, 1.0f};
149 static const GLfloat front_mat_diffuse[4] = {0.5f, 0.28f, 0.38f, 1.0f};
150 /*
151 static const GLfloat back_mat_shininess[1] = {60.0f};
152 static const GLfloat back_mat_specular[4] = {0.5f, 0.5f, 0.2f, 1.0f};
153 static const GLfloat back_mat_diffuse[4] = {1.0f, 1.0f, 0.2f, 1.0f};
154 */
155 static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
156 static const GLfloat lmodel_twoside[1] = {GL_FALSE};
157
158 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
159 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
160 glLightfv(GL_LIGHT0, GL_POSITION, position0);
161 glEnable(GL_LIGHT0);
162
163 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
164 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
165 glLightfv(GL_LIGHT1, GL_POSITION, position1);
166 glEnable(GL_LIGHT1);
167
168 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
169 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
170 glEnable(GL_LIGHTING);
171
172 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
173 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
174 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
175 }
176
177
178 static void Init(void)
179 {
180 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
181
182 glShadeModel(GL_SMOOTH);
183 glEnable(GL_DEPTH_TEST);
184
185 InitMaterials();
186
187 glMatrixMode(GL_PROJECTION);
188 glLoadIdentity();
189 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
190
191 glMatrixMode(GL_MODELVIEW);
192 glLoadIdentity();
193 glTranslatef( 0.0, 0.0, -6.0 );
194
195 #ifdef GL_EXT_vertex_array
196 if (use_vertex_arrays)
197 {
198 glVertexPointerEXT( 3, GL_FLOAT, 0, numverts, verts );
199 glNormalPointerEXT( GL_FLOAT, 0, numverts, norms );
200 glEnable( GL_VERTEX_ARRAY_EXT );
201 glEnable( GL_NORMAL_ARRAY_EXT );
202 }
203 #endif
204 }
205
206 static GLenum Args(int argc, wxChar **argv)
207 {
208 GLint i;
209
210 for (i = 1; i < argc; i++)
211 {
212 if (wxStrcmp(argv[i], _T("-sb")) == 0)
213 {
214 doubleBuffer = GL_FALSE;
215 }
216 else if (wxStrcmp(argv[i], _T("-db")) == 0)
217 {
218 doubleBuffer = GL_TRUE;
219 }
220 else if (wxStrcmp(argv[i], _T("-speed")) == 0)
221 {
222 speed_test = GL_TRUE;
223 doubleBuffer = GL_TRUE;
224 }
225 else if (wxStrcmp(argv[i], _T("-va")) == 0)
226 {
227 use_vertex_arrays = GL_TRUE;
228 }
229 else
230 {
231 wxString msg = _T("Bad option: ");
232 msg += argv[i];
233 wxMessageBox(msg);
234 return GL_FALSE;
235 }
236 }
237
238 return GL_TRUE;
239 }
240
241 // The following part was written for wxWidgets 1.66
242 MyFrame *frame = NULL;
243
244 IMPLEMENT_APP(MyApp)
245
246 // `Main program' equivalent, creating windows and returning main app frame
247 bool MyApp::OnInit()
248 {
249 if ( !wxApp::OnInit() )
250 return false;
251
252 Args(argc, argv);
253
254 // Create the main frame window
255 frame = new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
256 wxDefaultPosition, wxDefaultSize);
257
258 // Give it an icon
259 frame->SetIcon(wxIcon(_T("mondrian")));
260
261 // Make a menubar
262 wxMenu *fileMenu = new wxMenu;
263
264 fileMenu->Append(wxID_EXIT, _T("E&xit"));
265 wxMenuBar *menuBar = new wxMenuBar;
266 menuBar->Append(fileMenu, _T("&File"));
267 frame->SetMenuBar(menuBar);
268
269 // Make a TestGLCanvas
270
271 // JACS
272 #ifdef __WXMSW__
273 int *gl_attrib = NULL;
274 #else
275 int gl_attrib[20] = { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
276 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
277 WX_GL_DOUBLEBUFFER,
278 # if defined(__WXMAC__) || defined(__WXCOCOA__)
279 GL_NONE };
280 # else
281 None };
282 # endif
283 #endif
284
285 if(!doubleBuffer)
286 {
287 printf("don't have double buffer, disabling\n");
288 #ifdef __WXGTK__
289 gl_attrib[9] = None;
290 #endif
291 doubleBuffer = GL_FALSE;
292 }
293
294 frame->m_canvas = new TestGLCanvas(frame, wxID_ANY, wxDefaultPosition,
295 wxDefaultSize, 0, _T("TestGLCanvas"), gl_attrib );
296
297 // Show the frame
298 frame->Show(true);
299
300 frame->m_canvas->SetCurrent();
301 read_surface( _T("isosurf.dat") );
302
303 Init();
304
305 return true;
306 }
307
308 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
309 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
310 END_EVENT_TABLE()
311
312 // My frame constructor
313 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
314 const wxSize& size, long style)
315 : wxFrame(frame, wxID_ANY, title, pos, size, style)
316 {
317 m_canvas = NULL;
318 SetIcon(wxIcon(sample_xpm));
319 }
320
321 MyFrame::~MyFrame()
322 {
323 delete m_canvas;
324 }
325
326 // Intercept menu commands
327 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
328 {
329 // true is to force the frame to close
330 Close(true);
331 }
332
333 /*
334 * TestGLCanvas implementation
335 */
336
337 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
338 EVT_SIZE(TestGLCanvas::OnSize)
339 EVT_PAINT(TestGLCanvas::OnPaint)
340 EVT_CHAR(TestGLCanvas::OnChar)
341 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
342 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
343 END_EVENT_TABLE()
344
345 TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
346 const wxPoint& pos, const wxSize& size, long style,
347 const wxString& name, int* gl_attrib)
348 : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name, gl_attrib)
349 {
350 parent->Show(true);
351 SetCurrent();
352
353 /* Make sure server supports the vertex array extension */
354 char* extensions = (char *) glGetString( GL_EXTENSIONS );
355 if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" ))
356 {
357 use_vertex_arrays = GL_FALSE;
358 }
359 }
360
361
362 void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
363 {
364 // This is a dummy, to avoid an endless succession of paint messages.
365 // OnPaint handlers must always create a wxPaintDC.
366 wxPaintDC dc(this);
367
368 #ifndef __WXMOTIF__
369 if (!GetContext()) return;
370 #endif
371
372 SetCurrent();
373
374 draw1();
375 SwapBuffers();
376 }
377
378 void TestGLCanvas::OnSize(wxSizeEvent& event)
379 {
380 // this is also necessary to update the context on some platforms
381 wxGLCanvas::OnSize(event);
382
383 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
384 int w, h;
385 GetClientSize(&w, &h);
386 #ifndef __WXMOTIF__
387 if (GetContext())
388 #endif
389 {
390 SetCurrent();
391 glViewport(0, 0, (GLint) w, (GLint) h);
392 }
393 }
394
395 void TestGLCanvas::OnChar(wxKeyEvent& event)
396 {
397 switch( event.GetKeyCode() )
398 {
399 case WXK_ESCAPE:
400 wxTheApp->ExitMainLoop();
401 return;
402
403 case WXK_LEFT:
404 yrot -= 15.0;
405 break;
406
407 case WXK_RIGHT:
408 yrot += 15.0;
409 break;
410
411 case WXK_UP:
412 xrot += 15.0;
413 break;
414
415 case WXK_DOWN:
416 xrot -= 15.0;
417 break;
418
419 case 's': case 'S':
420 smooth = !smooth;
421 if (smooth)
422 {
423 glShadeModel(GL_SMOOTH);
424 }
425 else
426 {
427 glShadeModel(GL_FLAT);
428 }
429 break;
430
431 case 'l': case 'L':
432 lighting = !lighting;
433 if (lighting)
434 {
435 glEnable(GL_LIGHTING);
436 }
437 else
438 {
439 glDisable(GL_LIGHTING);
440 }
441 break;
442
443 default:
444 event.Skip();
445 return;
446 }
447
448 Refresh(false);
449 }
450
451 void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
452 {
453 static int dragging = 0;
454 static float last_x, last_y;
455
456 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
457 if(event.LeftIsDown())
458 {
459 if(!dragging)
460 {
461 dragging = 1;
462 }
463 else
464 {
465 yrot += (event.GetX() - last_x)*1.0;
466 xrot += (event.GetY() - last_y)*1.0;
467 Refresh(false);
468 }
469 last_x = event.GetX();
470 last_y = event.GetY();
471 }
472 else
473 dragging = 0;
474
475 }
476
477 void TestGLCanvas::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
478 {
479 // Do nothing, to avoid flashing.
480 }
481