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