]> git.saurik.com Git - wxWidgets.git/blob - samples/opengl/isosurf/isosurf.cpp
Disable some wxWebView tests that fail on the buildbot but not locally.
[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 bool MyApp::OnInit()
55 {
56 if ( !wxApp::OnInit() )
57 return false;
58
59 // Create the main frame window
60 new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"));
61
62 return true;
63 }
64
65 void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
66 {
67 parser.AddSwitch("", "sb", "Do not use double buffering");
68 parser.AddSwitch("", "db", "Use double buffering");
69 parser.AddSwitch("", "va", "Use vertex arrays");
70
71 wxApp::OnInitCmdLine(parser);
72 }
73
74 bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
75 {
76 if (parser.Found("sb"))
77 g_doubleBuffer = GL_FALSE;
78 else if (parser.Found("db"))
79 g_doubleBuffer = GL_TRUE;
80
81 if (parser.Found("va"))
82 g_use_vertex_arrays = GL_TRUE;
83
84 return wxApp::OnCmdLineParsed(parser);
85 }
86
87 //---------------------------------------------------------------------------
88 // MyFrame
89 //---------------------------------------------------------------------------
90
91 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
92 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
93 END_EVENT_TABLE()
94
95 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
96 const wxSize& size, long style)
97 : wxFrame(frame, wxID_ANY, title, pos, size, style),
98 m_canvas(NULL)
99 {
100 SetIcon(wxICON(sample));
101
102
103 // Make a menubar
104 wxMenu *fileMenu = new wxMenu;
105
106 fileMenu->Append(wxID_EXIT, wxT("E&xit"));
107 wxMenuBar *menuBar = new wxMenuBar;
108 menuBar->Append(fileMenu, wxT("&File"));
109 SetMenuBar(menuBar);
110
111
112 // Make a TestGLCanvas
113
114 // JACS
115 #ifdef __WXMSW__
116 int *gl_attrib = NULL;
117 #else
118 int gl_attrib[20] =
119 { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
120 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
121 WX_GL_DOUBLEBUFFER,
122 # if defined(__WXMAC__) || defined(__WXCOCOA__)
123 GL_NONE };
124 # else
125 None };
126 # endif
127 #endif
128
129 if (!g_doubleBuffer)
130 {
131 wxLogWarning("Disabling double buffering");
132
133 #ifdef __WXGTK__
134 gl_attrib[9] = None;
135 #endif
136 g_doubleBuffer = GL_FALSE;
137 }
138
139 m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib);
140
141 // Show the frame
142 Show(true);
143 Raise();
144
145 m_canvas->InitGL();
146 }
147
148 MyFrame::~MyFrame()
149 {
150 delete m_canvas;
151 }
152
153 // Intercept menu commands
154 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
155 {
156 // true is to force the frame to close
157 Close(true);
158 }
159
160
161 //---------------------------------------------------------------------------
162 // TestGLCanvas
163 //---------------------------------------------------------------------------
164
165 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
166 EVT_SIZE(TestGLCanvas::OnSize)
167 EVT_PAINT(TestGLCanvas::OnPaint)
168 EVT_CHAR(TestGLCanvas::OnChar)
169 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
170 END_EVENT_TABLE()
171
172 TestGLCanvas::TestGLCanvas(wxWindow *parent,
173 wxWindowID id,
174 int* gl_attrib)
175 : wxGLCanvas(parent, id, gl_attrib)
176 {
177 m_xrot = 0;
178 m_yrot = 0;
179 m_numverts = 0;
180
181 // Explicitly create a new rendering context instance for this canvas.
182 m_glRC = new wxGLContext(this);
183 }
184
185 TestGLCanvas::~TestGLCanvas()
186 {
187 delete m_glRC;
188 }
189
190 void TestGLCanvas::LoadSurface(const wxString& filename)
191 {
192 // FIXME
193 // we need to set english locale to force wxTextInputStream's calls to
194 // wxStrtod to use the point and not the comma as decimal separator...
195 // (the isosurf.dat contains points and not commas)...
196 wxLocale l(wxLANGUAGE_ENGLISH);
197
198 wxZlibInputStream* stream =
199 new wxZlibInputStream(new wxFFileInputStream(filename));
200 if (!stream || !stream->IsOk())
201 {
202 wxLogError("Cannot load '%s' type of files!", filename.c_str());
203 delete stream;
204 return;
205 }
206
207 {
208 // we suppose to have in input a text file containing floating numbers
209 // space/newline-separated... first 3 numbers are the coordinates of a
210 // vertex and the following 3 are the relative vertex normal and so on...
211
212 wxTextInputStream inFile(*stream);
213 m_numverts = 0;
214
215 while (!stream->Eof() && m_numverts < MAXVERTS)// && m_numverts<MAXVERTS)
216 {
217 inFile >> m_verts[m_numverts][0] >> m_verts[m_numverts][1] >> m_verts[m_numverts][2];
218 inFile >> m_norms[m_numverts][0] >> m_norms[m_numverts][1] >> m_norms[m_numverts][2];
219
220 m_numverts++;
221 }
222
223 // discard last vertex; it is a zero caused by the EOF
224 m_numverts--;
225 }
226
227 delete stream;
228
229 wxLogMessage(wxT("Loaded %d vertices, %d triangles from '%s'"),
230 m_numverts, m_numverts-2, filename.c_str());
231
232 // NOTE: for some reason under wxGTK the following is required to avoid that
233 // the surface gets rendered in a small rectangle in the top-left corner of the frame
234 PostSizeEventToParent();
235 }
236
237 void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
238 {
239 // This is a dummy, to avoid an endless succession of paint messages.
240 // OnPaint handlers must always create a wxPaintDC.
241 wxPaintDC dc(this);
242
243 // This is normally only necessary if there is more than one wxGLCanvas
244 // or more than one wxGLContext in the application.
245 SetCurrent(*m_glRC);
246
247 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
248 glPushMatrix();
249 glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
250 glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
251
252 // draw the surface
253 if (g_use_vertex_arrays)
254 {
255 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
256 }
257 else
258 {
259 glBegin( GL_TRIANGLE_STRIP );
260
261 for (int i=0;i<m_numverts;i++)
262 {
263 glNormal3fv( m_norms[i] );
264 glVertex3fv( m_verts[i] );
265 }
266
267 glEnd();
268 }
269
270 glPopMatrix();
271 glFlush(); // Not really necessary: buffer swapping below implies glFlush()
272
273 SwapBuffers();
274 }
275
276 void TestGLCanvas::OnSize(wxSizeEvent& event)
277 {
278 if ( !IsShownOnScreen() )
279 return;
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 // Make the new context current (activate it for use) with this canvas.
408 SetCurrent(*m_glRC);
409
410 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
411
412 glShadeModel(GL_SMOOTH);
413 glEnable(GL_DEPTH_TEST);
414
415 InitMaterials();
416
417 glMatrixMode(GL_PROJECTION);
418 glLoadIdentity();
419 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
420
421 glMatrixMode(GL_MODELVIEW);
422 glLoadIdentity();
423 glTranslatef( 0.0, 0.0, -6.0 );
424
425 if (g_use_vertex_arrays)
426 {
427 glVertexPointer( 3, GL_FLOAT, 0, m_verts );
428 glNormalPointer( GL_FLOAT, 0, m_norms );
429 glEnable( GL_VERTEX_ARRAY );
430 glEnable( GL_NORMAL_ARRAY );
431 }
432
433 InitMaterials();
434 LoadSurface("isosurf.dat.gz");
435 }
436