]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/isosurf/isosurf.cpp
reopen app according to HIG
[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
5// Modified by: Julian Smart
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows licence
8b089c5e
JS
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#include "wx/timer.h"
29#include "wx/glcanvas.h"
30
cb712074 31#ifdef __WXMAC__
5cf036d0
DS
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
cb712074 39#else
5cf036d0
DS
40# include <GL/gl.h>
41# include <GL/glu.h>
cb712074 42#endif
8b089c5e 43
9d705dfa
GD
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
8b089c5e
JS
50#include "isosurf.h"
51
52// The following part is taken largely unchanged from the original C Version
53
54#include <math.h>
55
56GLboolean speed_test = GL_FALSE;
57GLboolean use_vertex_arrays = GL_FALSE;
58
59GLboolean doubleBuffer = GL_TRUE;
60
61GLboolean smooth = GL_TRUE;
62GLboolean lighting = GL_TRUE;
63
64
65#define MAXVERTS 10000
66
67static GLfloat verts[MAXVERTS][3];
68static GLfloat norms[MAXVERTS][3];
69static GLint numverts;
70
71static GLfloat xrot;
72static GLfloat yrot;
73
74
5cf036d0 75static void read_surface( const wxChar *filename )
8b089c5e 76{
5cf036d0
DS
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);
8b089c5e
JS
100}
101
102
5cf036d0 103static void draw_surface()
8b089c5e 104{
5cf036d0 105 GLint i;
8b089c5e
JS
106
107#ifdef GL_EXT_vertex_array
5cf036d0
DS
108 if (use_vertex_arrays)
109 {
110 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
111 }
112 else
8b089c5e 113#endif
5cf036d0
DS
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 }
8b089c5e
JS
123}
124
125
5cf036d0 126static void draw1()
8b089c5e
JS
127{
128 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
129 glPushMatrix();
5cf036d0
DS
130 glRotatef( yrot, 0.0f, 1.0f, 0.0f );
131 glRotatef( xrot, 1.0f, 0.0f, 0.0f );
8b089c5e
JS
132
133 draw_surface();
134
135 glPopMatrix();
136
137 glFlush();
138}
139
140
5cf036d0 141static void InitMaterials()
8b089c5e 142{
5cf036d0
DS
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};
8b089c5e 150 /*
5cf036d0
DS
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};
8b089c5e 154 */
5cf036d0
DS
155 static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
156 static const GLfloat lmodel_twoside[1] = {GL_FALSE};
8b089c5e
JS
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);
5cf036d0 162
8b089c5e
JS
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);
5cf036d0 167
8b089c5e
JS
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
178static void Init(void)
179{
5cf036d0 180 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
8b089c5e 181
5cf036d0
DS
182 glShadeModel(GL_SMOOTH);
183 glEnable(GL_DEPTH_TEST);
8b089c5e 184
5cf036d0 185 InitMaterials();
8b089c5e 186
5cf036d0
DS
187 glMatrixMode(GL_PROJECTION);
188 glLoadIdentity();
189 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
8b089c5e 190
5cf036d0
DS
191 glMatrixMode(GL_MODELVIEW);
192 glLoadIdentity();
193 glTranslatef( 0.0, 0.0, -6.0 );
8b089c5e
JS
194
195#ifdef GL_EXT_vertex_array
5cf036d0
DS
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 }
8b089c5e
JS
203#endif
204}
205
2db98bf5 206static GLenum Args(int argc, wxChar **argv)
8b089c5e 207{
5cf036d0
DS
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;
8b089c5e
JS
239}
240
be5a51fb 241// The following part was written for wxWidgets 1.66
8b089c5e
JS
242MyFrame *frame = NULL;
243
244IMPLEMENT_APP(MyApp)
245
246// `Main program' equivalent, creating windows and returning main app frame
5cf036d0 247bool MyApp::OnInit()
8b089c5e 248{
5cf036d0 249 Args(argc, argv);
8b089c5e 250
5cf036d0 251 // Create the main frame window
be5a51fb 252 frame = new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
5cf036d0 253 wxDefaultPosition, wxDefaultSize);
8b089c5e 254
5cf036d0
DS
255 // Give it an icon
256 frame->SetIcon(wxIcon(_T("mondrian")));
8b089c5e 257
5cf036d0
DS
258 // Make a menubar
259 wxMenu *fileMenu = new wxMenu;
8b089c5e 260
5cf036d0
DS
261 fileMenu->Append(wxID_EXIT, _T("E&xit"));
262 wxMenuBar *menuBar = new wxMenuBar;
263 menuBar->Append(fileMenu, _T("&File"));
264 frame->SetMenuBar(menuBar);
8b089c5e
JS
265
266 // Make a TestGLCanvas
267
268 // JACS
269#ifdef __WXMSW__
5cf036d0 270 int *gl_attrib = NULL;
8b089c5e 271#else
5cf036d0
DS
272 int gl_attrib[20] = { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
273 WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
274 WX_GL_DOUBLEBUFFER,
cb712074 275# ifdef __WXMAC__
5cf036d0 276 GL_NONE };
cb712074 277# else
5cf036d0 278 None };
cb712074 279# endif
8b089c5e
JS
280#endif
281
5cf036d0
DS
282 if(!doubleBuffer)
283 {
284 printf("don't have double buffer, disabling\n");
8b089c5e 285#ifdef __WXGTK__
5cf036d0 286 gl_attrib[9] = None;
8b089c5e 287#endif
5cf036d0
DS
288 doubleBuffer = GL_FALSE;
289 }
290
2db98bf5
JS
291#if wxUSE_GLCANVAS
292
5cf036d0
DS
293 frame->m_canvas = new TestGLCanvas(frame, wxID_ANY, wxDefaultPosition,
294 wxDefaultSize, 0, _T("TestGLCanvas"), gl_attrib );
8b089c5e
JS
295
296 // Show the frame
5cf036d0 297 frame->Show(true);
8b089c5e 298
5cf036d0
DS
299 frame->m_canvas->SetCurrent();
300 read_surface( _T("isosurf.dat") );
8b089c5e 301
5cf036d0 302 Init();
8b089c5e 303
5cf036d0 304 return true;
2db98bf5
JS
305
306#else
307
5cf036d0 308 wxMessageBox( _T("This sample has to be compiled with wxUSE_GLCANVAS"), _T("Building error"), wxOK);
2db98bf5 309
5cf036d0 310 return false;
2db98bf5 311#endif
8b089c5e
JS
312}
313
314BEGIN_EVENT_TABLE(MyFrame, wxFrame)
315 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
316END_EVENT_TABLE()
317
318// My frame constructor
319MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
5cf036d0
DS
320 const wxSize& size, long style)
321 : wxFrame(frame, wxID_ANY, title, pos, size, style)
8b089c5e 322{
2db98bf5 323#if wxUSE_GLCANVAS
8b089c5e 324 m_canvas = NULL;
2db98bf5 325#endif
8b089c5e
JS
326}
327
5cf036d0
DS
328MyFrame::~MyFrame()
329{
330#if wxUSE_GLCANVAS
331 if (m_canvas)
332 {
333 delete m_canvas; m_canvas = NULL;
334 }
335#endif
336}
337
8b089c5e 338// Intercept menu commands
5cf036d0 339void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 340{
5cf036d0
DS
341 // true is to force the frame to close
342 Close(true);
8b089c5e
JS
343}
344
345/*
346 * TestGLCanvas implementation
347 */
348
2db98bf5
JS
349#if wxUSE_GLCANVAS
350
8b089c5e
JS
351BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
352 EVT_SIZE(TestGLCanvas::OnSize)
353 EVT_PAINT(TestGLCanvas::OnPaint)
354 EVT_CHAR(TestGLCanvas::OnChar)
355 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
356 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
357END_EVENT_TABLE()
358
359TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
5cf036d0
DS
360 const wxPoint& pos, const wxSize& size, long style,
361 const wxString& name, int* gl_attrib)
362 : wxGLCanvas(parent, id, pos, size, style, name, gl_attrib)
8b089c5e 363{
5cf036d0
DS
364 parent->Show(true);
365 SetCurrent();
366
367 /* Make sure server supports the vertex array extension */
368 char* extensions = (char *) glGetString( GL_EXTENSIONS );
369 if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" ))
370 {
371 use_vertex_arrays = GL_FALSE;
372 }
8b089c5e
JS
373}
374
375
5cf036d0 376TestGLCanvas::~TestGLCanvas()
8b089c5e
JS
377{
378}
379
2db98bf5 380void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e
JS
381{
382 // This is a dummy, to avoid an endless succession of paint messages.
383 // OnPaint handlers must always create a wxPaintDC.
384 wxPaintDC dc(this);
385
c661ecca
RR
386#ifndef __WXMOTIF__
387 if (!GetContext()) return;
388#endif
389
919ae91a
UN
390 SetCurrent();
391
8b089c5e
JS
392 draw1();
393 SwapBuffers();
394}
395
396void TestGLCanvas::OnSize(wxSizeEvent& event)
397{
9d705dfa
GD
398 // this is also necessary to update the context on some platforms
399 wxGLCanvas::OnSize(event);
5cf036d0 400
9d705dfa 401 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
2f6c54eb
VZ
402 int w, h;
403 GetClientSize(&w, &h);
c661ecca 404#ifndef __WXMOTIF__
9d705dfa 405 if (GetContext())
c661ecca 406#endif
9d705dfa 407 {
2f6c54eb 408 SetCurrent();
9d705dfa
GD
409 glViewport(0, 0, (GLint) w, (GLint) h);
410 }
8b089c5e
JS
411}
412
413void TestGLCanvas::OnChar(wxKeyEvent& event)
414{
5cf036d0
DS
415 switch( event.GetKeyCode() )
416 {
8b089c5e 417 case WXK_ESCAPE:
5cf036d0
DS
418 wxTheApp->ExitMainLoop();
419 return;
420
8b089c5e 421 case WXK_LEFT:
5cf036d0
DS
422 yrot -= 15.0;
423 break;
424
8b089c5e 425 case WXK_RIGHT:
5cf036d0
DS
426 yrot += 15.0;
427 break;
428
8b089c5e 429 case WXK_UP:
5cf036d0
DS
430 xrot += 15.0;
431 break;
432
8b089c5e 433 case WXK_DOWN:
5cf036d0
DS
434 xrot -= 15.0;
435 break;
436
8b089c5e 437 case 's': case 'S':
5cf036d0
DS
438 smooth = !smooth;
439 if (smooth)
440 {
441 glShadeModel(GL_SMOOTH);
442 }
443 else
444 {
445 glShadeModel(GL_FLAT);
446 }
447 break;
448
8b089c5e 449 case 'l': case 'L':
5cf036d0
DS
450 lighting = !lighting;
451 if (lighting)
452 {
453 glEnable(GL_LIGHTING);
454 }
455 else
456 {
457 glDisable(GL_LIGHTING);
458 }
459 break;
460
461 default:
8b089c5e 462 event.Skip();
5cf036d0 463 return;
8b089c5e
JS
464 }
465
5cf036d0 466 Refresh(false);
8b089c5e
JS
467}
468
469void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
470{
471 static int dragging = 0;
472 static float last_x, last_y;
473
474 //printf("%f %f %d\n", event.GetX(), event.GetY(), (int)event.LeftIsDown());
5cf036d0
DS
475 if(event.LeftIsDown())
476 {
477 if(!dragging)
478 {
479 dragging = 1;
480 }
481 else
482 {
483 yrot += (event.GetX() - last_x)*1.0;
484 xrot += (event.GetY() - last_y)*1.0;
485 Refresh(false);
486 }
487 last_x = event.GetX();
488 last_y = event.GetY();
2f6c54eb 489 }
5cf036d0
DS
490 else
491 dragging = 0;
492
8b089c5e
JS
493}
494
5cf036d0 495void TestGLCanvas::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8b089c5e
JS
496{
497 // Do nothing, to avoid flashing.
498}
499
5cf036d0
DS
500#endif // #if wxUSE_GLCANVAS
501