]>
Commit | Line | Data |
---|---|---|
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 |
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; | |
8b089c5e | 45 | |
8b089c5e JS |
46 | |
47 | ||
6c7f0949 FM |
48 | //--------------------------------------------------------------------------- |
49 | // MyApp | |
50 | //--------------------------------------------------------------------------- | |
8b089c5e | 51 | |
6c7f0949 | 52 | IMPLEMENT_APP(MyApp) |
8b089c5e | 53 | |
6c7f0949 | 54 | bool MyApp::OnInit() |
8b089c5e | 55 | { |
6c7f0949 FM |
56 | if ( !wxApp::OnInit() ) |
57 | return false; | |
5cf036d0 | 58 | |
6c7f0949 | 59 | // Create the main frame window |
e2250d64 | 60 | new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample")); |
5cf036d0 | 61 | |
6c7f0949 | 62 | return true; |
8b089c5e JS |
63 | } |
64 | ||
6c7f0949 | 65 | void MyApp::OnInitCmdLine(wxCmdLineParser& parser) |
8b089c5e | 66 | { |
7881f83b FM |
67 | parser.AddSwitch("", "sb", "Do not use double buffering"); |
68 | parser.AddSwitch("", "db", "Use double buffering"); | |
69 | parser.AddSwitch("", "va", "Use vertex arrays"); | |
8b089c5e | 70 | |
6c7f0949 | 71 | wxApp::OnInitCmdLine(parser); |
8b089c5e JS |
72 | } |
73 | ||
6c7f0949 | 74 | bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser) |
8b089c5e | 75 | { |
7881f83b | 76 | if (parser.Found("sb")) |
6c7f0949 | 77 | g_doubleBuffer = GL_FALSE; |
7881f83b | 78 | else if (parser.Found("db")) |
6c7f0949 | 79 | g_doubleBuffer = GL_TRUE; |
8b089c5e | 80 | |
7881f83b | 81 | if (parser.Found("va")) |
6c7f0949 | 82 | g_use_vertex_arrays = GL_TRUE; |
5cf036d0 | 83 | |
6c7f0949 | 84 | return wxApp::OnCmdLineParsed(parser); |
8b089c5e JS |
85 | } |
86 | ||
6c7f0949 FM |
87 | //--------------------------------------------------------------------------- |
88 | // MyFrame | |
89 | //--------------------------------------------------------------------------- | |
451c13c8 VZ |
90 | |
91 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
92 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) | |
93 | END_EVENT_TABLE() | |
94 | ||
451c13c8 | 95 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, |
df344ba9 | 96 | const wxSize& size, long style) |
451c13c8 VZ |
97 | : wxFrame(frame, wxID_ANY, title, pos, size, style), |
98 | m_canvas(NULL) | |
99 | { | |
100 | SetIcon(wxICON(sample)); | |
101 | ||
102 | ||
5cf036d0 DS |
103 | // Make a menubar |
104 | wxMenu *fileMenu = new wxMenu; | |
8b089c5e | 105 | |
9a83f860 | 106 | fileMenu->Append(wxID_EXIT, wxT("E&xit")); |
5cf036d0 | 107 | wxMenuBar *menuBar = new wxMenuBar; |
9a83f860 | 108 | menuBar->Append(fileMenu, wxT("&File")); |
451c13c8 VZ |
109 | SetMenuBar(menuBar); |
110 | ||
8b089c5e JS |
111 | |
112 | // Make a TestGLCanvas | |
113 | ||
114 | // JACS | |
115 | #ifdef __WXMSW__ | |
5cf036d0 | 116 | int *gl_attrib = NULL; |
8b089c5e | 117 | #else |
6c7f0949 FM |
118 | int gl_attrib[20] = |
119 | { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1, | |
5cf036d0 DS |
120 | WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1, |
121 | WX_GL_DOUBLEBUFFER, | |
6716fce4 | 122 | # if defined(__WXMAC__) || defined(__WXCOCOA__) |
5cf036d0 | 123 | GL_NONE }; |
cb712074 | 124 | # else |
5cf036d0 | 125 | None }; |
cb712074 | 126 | # endif |
8b089c5e JS |
127 | #endif |
128 | ||
6c7f0949 | 129 | if (!g_doubleBuffer) |
5cf036d0 | 130 | { |
0c1c1c71 | 131 | wxLogWarning("Disabling double buffering"); |
6c7f0949 | 132 | |
8b089c5e | 133 | #ifdef __WXGTK__ |
5cf036d0 | 134 | gl_attrib[9] = None; |
8b089c5e | 135 | #endif |
6c7f0949 | 136 | g_doubleBuffer = GL_FALSE; |
5cf036d0 DS |
137 | } |
138 | ||
97f85100 VZ |
139 | m_canvas = new TestGLCanvas(this, wxID_ANY, gl_attrib); |
140 | ||
451c13c8 VZ |
141 | // Show the frame |
142 | Show(true); | |
97f85100 | 143 | Raise(); |
8b089c5e | 144 | |
97f85100 | 145 | m_canvas->InitGL(); |
8b089c5e JS |
146 | } |
147 | ||
5cf036d0 DS |
148 | MyFrame::~MyFrame() |
149 | { | |
806e2f15 | 150 | delete m_canvas; |
5cf036d0 DS |
151 | } |
152 | ||
8b089c5e | 153 | // Intercept menu commands |
5cf036d0 | 154 | void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) ) |
8b089c5e | 155 | { |
5cf036d0 DS |
156 | // true is to force the frame to close |
157 | Close(true); | |
8b089c5e JS |
158 | } |
159 | ||
6c7f0949 FM |
160 | |
161 | //--------------------------------------------------------------------------- | |
162 | // TestGLCanvas | |
163 | //--------------------------------------------------------------------------- | |
8b089c5e JS |
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) | |
8b089c5e JS |
170 | END_EVENT_TABLE() |
171 | ||
451c13c8 VZ |
172 | TestGLCanvas::TestGLCanvas(wxWindow *parent, |
173 | wxWindowID id, | |
451c13c8 | 174 | int* gl_attrib) |
0c1c1c71 | 175 | : wxGLCanvas(parent, id, gl_attrib) |
8b089c5e | 176 | { |
0c1c1c71 FM |
177 | m_xrot = 0; |
178 | m_yrot = 0; | |
179 | m_numverts = 0; | |
180 | ||
451c13c8 VZ |
181 | // Explicitly create a new rendering context instance for this canvas. |
182 | m_glRC = new wxGLContext(this); | |
8b089c5e JS |
183 | } |
184 | ||
451c13c8 VZ |
185 | TestGLCanvas::~TestGLCanvas() |
186 | { | |
187 | delete m_glRC; | |
188 | } | |
8b089c5e | 189 | |
6c7f0949 FM |
190 | void TestGLCanvas::LoadSurface(const wxString& filename) |
191 | { | |
0c1c1c71 FM |
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 | ||
6c7f0949 FM |
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 | ||
6c7f0949 | 207 | { |
0c1c1c71 | 208 | // we suppose to have in input a text file containing floating numbers |
4c51a665 | 209 | // space/newline-separated... first 3 numbers are the coordinates of a |
0c1c1c71 FM |
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--; | |
6c7f0949 FM |
225 | } |
226 | ||
227 | delete stream; | |
228 | ||
9a83f860 | 229 | wxLogMessage(wxT("Loaded %d vertices, %d triangles from '%s'"), |
6c7f0949 | 230 | m_numverts, m_numverts-2, filename.c_str()); |
ce00f59b | 231 | |
df344ba9 FM |
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(); | |
6c7f0949 FM |
235 | } |
236 | ||
2db98bf5 | 237 | void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
8b089c5e JS |
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 | ||
451c13c8 VZ |
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); | |
919ae91a | 246 | |
6c7f0949 FM |
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 | |
0c1c1c71 | 253 | if (g_use_vertex_arrays) |
6c7f0949 FM |
254 | { |
255 | glDrawArrays( GL_TRIANGLE_STRIP, 0, m_numverts ); | |
256 | } | |
0c1c1c71 | 257 | else |
6c7f0949 FM |
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 | ||
8b089c5e JS |
273 | SwapBuffers(); |
274 | } | |
275 | ||
276 | void TestGLCanvas::OnSize(wxSizeEvent& event) | |
277 | { | |
97f85100 VZ |
278 | if ( !IsShownOnScreen() ) |
279 | return; | |
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 | ||
291 | void 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 | ||
339 | void 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 | ||
df344ba9 | 348 | if (event.LeftIsDown()) |
5cf036d0 | 349 | { |
df344ba9 | 350 | if (!dragging) |
5cf036d0 DS |
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 |
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 | { | |
97f85100 VZ |
407 | // Make the new context current (activate it for use) with this canvas. |
408 | SetCurrent(*m_glRC); | |
409 | ||
6c7f0949 FM |
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 ); | |
0c1c1c71 FM |
429 | glEnable( GL_VERTEX_ARRAY ); |
430 | glEnable( GL_NORMAL_ARRAY ); | |
6c7f0949 | 431 | } |
97f85100 VZ |
432 | |
433 | InitMaterials(); | |
434 | LoadSurface("isosurf.dat.gz"); | |
6c7f0949 FM |
435 | } |
436 |