]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/isosurf/isosurf.cpp
build a 2.9.x windows setup file and chm and htb sets of docs
[wxWidgets.git] / samples / opengl / isosurf / isosurf.cpp
CommitLineData
8b089c5e
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: isosurf.cpp
3// Purpose: wxGLCanvas demo program
4// Author: Brian Paul (original gltk version), Wolfram Gloger
0c1c1c71 5// Modified by: Julian Smart, Francesco Montorsi
8b089c5e
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows licence
8b089c5e
JS
10/////////////////////////////////////////////////////////////////////////////
11
8b089c5e
JS
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
806e2f15
VZ
23#if !wxUSE_GLCANVAS
24 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
25#endif
26
8b089c5e
JS
27#include "wx/timer.h"
28#include "wx/glcanvas.h"
b713f891 29#include "wx/math.h"
6c7f0949
FM
30#include "wx/log.h"
31#include "wx/cmdline.h"
6c7f0949
FM
32#include "wx/wfstream.h"
33#include "wx/zstream.h"
0c1c1c71 34#include "wx/txtstrm.h"
8b089c5e 35
451c13c8 36#include "isosurf.h"
3a992940
JS
37#include "../../sample.xpm"
38
8b089c5e 39
6c7f0949 40// global options which can be set through command-line options
6c7f0949
FM
41GLboolean g_use_vertex_arrays = GL_FALSE;
42GLboolean g_doubleBuffer = GL_TRUE;
43GLboolean g_smooth = GL_TRUE;
44GLboolean g_lighting = GL_TRUE;
8b089c5e 45
8b089c5e
JS
46
47
6c7f0949
FM
48//---------------------------------------------------------------------------
49// MyApp
50//---------------------------------------------------------------------------
8b089c5e 51
6c7f0949 52IMPLEMENT_APP(MyApp)
8b089c5e 53
6c7f0949
FM
54// `Main program' equivalent, creating windows and returning main app frame
55bool MyApp::OnInit()
8b089c5e 56{
6c7f0949
FM
57 if ( !wxApp::OnInit() )
58 return false;
5cf036d0 59
6c7f0949 60 // Create the main frame window
0c1c1c71 61 SetTopWindow(new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample")));
5cf036d0 62
6c7f0949 63 return true;
8b089c5e
JS
64}
65
6c7f0949 66void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
8b089c5e 67{
7881f83b
FM
68 parser.AddSwitch("", "sb", "Do not use double buffering");
69 parser.AddSwitch("", "db", "Use double buffering");
70 parser.AddSwitch("", "va", "Use vertex arrays");
8b089c5e 71
6c7f0949 72 wxApp::OnInitCmdLine(parser);
8b089c5e
JS
73}
74
6c7f0949 75bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
8b089c5e 76{
7881f83b 77 if (parser.Found("sb"))
6c7f0949 78 g_doubleBuffer = GL_FALSE;
7881f83b 79 else if (parser.Found("db"))
6c7f0949 80 g_doubleBuffer = GL_TRUE;
8b089c5e 81
7881f83b 82 if (parser.Found("va"))
6c7f0949 83 g_use_vertex_arrays = GL_TRUE;
5cf036d0 84
6c7f0949 85 return wxApp::OnCmdLineParsed(parser);
8b089c5e
JS
86}
87
6c7f0949
FM
88//---------------------------------------------------------------------------
89// MyFrame
90//---------------------------------------------------------------------------
451c13c8
VZ
91
92BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
94END_EVENT_TABLE()
95
96// My frame constructor
97MyFrame::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
5cf036d0
DS
105 // Make a menubar
106 wxMenu *fileMenu = new wxMenu;
8b089c5e 107
5cf036d0
DS
108 fileMenu->Append(wxID_EXIT, _T("E&xit"));
109 wxMenuBar *menuBar = new wxMenuBar;
110 menuBar->Append(fileMenu, _T("&File"));
451c13c8
VZ
111 SetMenuBar(menuBar);
112
8b089c5e
JS
113
114 // Make a TestGLCanvas
115
116 // JACS
117#ifdef __WXMSW__
5cf036d0 118 int *gl_attrib = NULL;
8b089c5e 119#else
6c7f0949
FM
120 int gl_attrib[20] =
121 { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
5cf036d0
DS
122 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
123 WX_GL_DOUBLEBUFFER,
6716fce4 124# if defined(__WXMAC__) || defined(__WXCOCOA__)
5cf036d0 125 GL_NONE };
cb712074 126# else
5cf036d0 127 None };
cb712074 128# endif
8b089c5e
JS
129#endif
130
6c7f0949 131 if (!g_doubleBuffer)
5cf036d0 132 {
0c1c1c71 133 wxLogWarning("Disabling double buffering");
6c7f0949 134
8b089c5e 135#ifdef __WXGTK__
5cf036d0 136 gl_attrib[9] = None;
8b089c5e 137#endif
6c7f0949 138 g_doubleBuffer = GL_FALSE;
5cf036d0
DS
139 }
140
451c13c8
VZ
141 // Show the frame
142 Show(true);
8b089c5e 143
0c1c1c71 144 m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib);
8b089c5e
JS
145}
146
5cf036d0
DS
147MyFrame::~MyFrame()
148{
806e2f15 149 delete m_canvas;
5cf036d0
DS
150}
151
8b089c5e 152// Intercept menu commands
5cf036d0 153void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 154{
5cf036d0
DS
155 // true is to force the frame to close
156 Close(true);
8b089c5e
JS
157}
158
6c7f0949
FM
159
160//---------------------------------------------------------------------------
161// TestGLCanvas
162//---------------------------------------------------------------------------
8b089c5e
JS
163
164BEGIN_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)
8b089c5e
JS
169END_EVENT_TABLE()
170
451c13c8
VZ
171TestGLCanvas::TestGLCanvas(wxWindow *parent,
172 wxWindowID id,
451c13c8 173 int* gl_attrib)
0c1c1c71 174 : wxGLCanvas(parent, id, gl_attrib)
8b089c5e 175{
0c1c1c71
FM
176 m_xrot = 0;
177 m_yrot = 0;
178 m_numverts = 0;
179
451c13c8
VZ
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);
5cf036d0 185
6c7f0949
FM
186 InitGL();
187 InitMaterials();
188 LoadSurface("isosurf.dat.gz");
8b089c5e
JS
189}
190
451c13c8
VZ
191TestGLCanvas::~TestGLCanvas()
192{
193 delete m_glRC;
194}
8b089c5e 195
6c7f0949
FM
196void TestGLCanvas::LoadSurface(const wxString& filename)
197{
0c1c1c71
FM
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
6c7f0949
FM
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
6c7f0949 213 {
0c1c1c71
FM
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--;
6c7f0949
FM
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
2db98bf5 239void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e
JS
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
451c13c8
VZ
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);
919ae91a 248
6c7f0949
FM
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
0c1c1c71 255 if (g_use_vertex_arrays)
6c7f0949
FM
256 {
257 glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts );
258 }
0c1c1c71 259 else
6c7f0949
FM
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
8b089c5e
JS
275 SwapBuffers();
276}
277
278void TestGLCanvas::OnSize(wxSizeEvent& event)
279{
451c13c8
VZ
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);
8b089c5e
JS
289}
290
291void TestGLCanvas::OnChar(wxKeyEvent& event)
292{
5cf036d0
DS
293 switch( event.GetKeyCode() )
294 {
8b089c5e 295 case WXK_ESCAPE:
5cf036d0
DS
296 wxTheApp->ExitMainLoop();
297 return;
298
8b089c5e 299 case WXK_LEFT:
6c7f0949 300 m_yrot -= 15.0;
5cf036d0
DS
301 break;
302
8b089c5e 303 case WXK_RIGHT:
6c7f0949 304 m_yrot += 15.0;
5cf036d0
DS
305 break;
306
8b089c5e 307 case WXK_UP:
6c7f0949 308 m_xrot += 15.0;
5cf036d0
DS
309 break;
310
8b089c5e 311 case WXK_DOWN:
6c7f0949 312 m_xrot -= 15.0;
5cf036d0
DS
313 break;
314
8b089c5e 315 case 's': case 'S':
6c7f0949
FM
316 g_smooth = !g_smooth;
317 if (g_smooth)
5cf036d0 318 glShadeModel(GL_SMOOTH);
5cf036d0 319 else
5cf036d0 320 glShadeModel(GL_FLAT);
5cf036d0
DS
321 break;
322
8b089c5e 323 case 'l': case 'L':
6c7f0949
FM
324 g_lighting = !g_lighting;
325 if (g_lighting)
5cf036d0 326 glEnable(GL_LIGHTING);
5cf036d0 327 else
5cf036d0 328 glDisable(GL_LIGHTING);
5cf036d0
DS
329 break;
330
331 default:
8b089c5e 332 event.Skip();
5cf036d0 333 return;
8b089c5e
JS
334 }
335
5cf036d0 336 Refresh(false);
8b089c5e
JS
337}
338
339void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
340{
341 static int dragging = 0;
342 static float last_x, last_y;
343
451c13c8
VZ
344 // Allow default processing to happen, or else the canvas cannot gain focus
345 // (for key events).
346 event.Skip();
347
5cf036d0
DS
348 if(event.LeftIsDown())
349 {
350 if(!dragging)
351 {
352 dragging = 1;
353 }
354 else
355 {
6c7f0949
FM
356 m_yrot += (event.GetX() - last_x)*1.0;
357 m_xrot += (event.GetY() - last_y)*1.0;
5cf036d0
DS
358 Refresh(false);
359 }
360 last_x = event.GetX();
361 last_y = event.GetY();
2f6c54eb 362 }
5cf036d0 363 else
451c13c8 364 {
5cf036d0 365 dragging = 0;
451c13c8 366 }
8b089c5e
JS
367}
368
6c7f0949
FM
369void 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
405void 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 );
0c1c1c71
FM
426 glEnable( GL_VERTEX_ARRAY );
427 glEnable( GL_NORMAL_ARRAY );
6c7f0949
FM
428 }
429}
430