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