]> git.saurik.com Git - wxWidgets.git/blame - samples/opengl/isosurf/isosurf.cpp
compate charset names case-insensitively in GetEncodingFromName()
[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"
b713f891 34#include "wx/math.h"
8b089c5e 35
509dcc4b 36#if defined(__WXMAC__) || defined(__WXCOCOA__)
5cf036d0
DS
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
cb712074 44#else
5cf036d0
DS
45# include <GL/gl.h>
46# include <GL/glu.h>
cb712074 47#endif
8b089c5e 48
9d705dfa
GD
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
8b089c5e
JS
55#include "isosurf.h"
56
3a992940
JS
57#include "../../sample.xpm"
58
8b089c5e
JS
59// The following part is taken largely unchanged from the original C Version
60
8b089c5e
JS
61GLboolean speed_test = GL_FALSE;
62GLboolean use_vertex_arrays = GL_FALSE;
63
64GLboolean doubleBuffer = GL_TRUE;
65
66GLboolean smooth = GL_TRUE;
67GLboolean lighting = GL_TRUE;
68
69
70#define MAXVERTS 10000
71
72static GLfloat verts[MAXVERTS][3];
73static GLfloat norms[MAXVERTS][3];
74static GLint numverts;
75
76static GLfloat xrot;
77static GLfloat yrot;
78
79
5cf036d0 80static void read_surface( const wxChar *filename )
8b089c5e 81{
994e41cb 82 FILE *f = wxFopen(filename,_T("r"));
5cf036d0
DS
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);
8b089c5e
JS
105}
106
107
5cf036d0 108static void draw_surface()
8b089c5e 109{
5cf036d0 110 GLint i;
8b089c5e
JS
111
112#ifdef GL_EXT_vertex_array
5cf036d0
DS
113 if (use_vertex_arrays)
114 {
115 glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
116 }
117 else
8b089c5e 118#endif
5cf036d0
DS
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 }
8b089c5e
JS
128}
129
130
5cf036d0 131static void draw1()
8b089c5e
JS
132{
133 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
134 glPushMatrix();
5cf036d0
DS
135 glRotatef( yrot, 0.0f, 1.0f, 0.0f );
136 glRotatef( xrot, 1.0f, 0.0f, 0.0f );
8b089c5e
JS
137
138 draw_surface();
139
140 glPopMatrix();
141
142 glFlush();
143}
144
145
5cf036d0 146static void InitMaterials()
8b089c5e 147{
5cf036d0
DS
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};
8b089c5e 155 /*
5cf036d0
DS
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};
8b089c5e 159 */
5cf036d0
DS
160 static const GLfloat lmodel_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f};
161 static const GLfloat lmodel_twoside[1] = {GL_FALSE};
8b089c5e
JS
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);
5cf036d0 167
8b089c5e
JS
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);
5cf036d0 172
8b089c5e
JS
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
183static void Init(void)
184{
5cf036d0 185 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
8b089c5e 186
5cf036d0
DS
187 glShadeModel(GL_SMOOTH);
188 glEnable(GL_DEPTH_TEST);
8b089c5e 189
5cf036d0 190 InitMaterials();
8b089c5e 191
5cf036d0
DS
192 glMatrixMode(GL_PROJECTION);
193 glLoadIdentity();
194 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
8b089c5e 195
5cf036d0
DS
196 glMatrixMode(GL_MODELVIEW);
197 glLoadIdentity();
198 glTranslatef( 0.0, 0.0, -6.0 );
8b089c5e
JS
199
200#ifdef GL_EXT_vertex_array
5cf036d0
DS
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 }
8b089c5e
JS
208#endif
209}
210
2db98bf5 211static GLenum Args(int argc, wxChar **argv)
8b089c5e 212{
5cf036d0
DS
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;
8b089c5e
JS
244}
245
be5a51fb 246// The following part was written for wxWidgets 1.66
8b089c5e
JS
247MyFrame *frame = NULL;
248
249IMPLEMENT_APP(MyApp)
250
251// `Main program' equivalent, creating windows and returning main app frame
5cf036d0 252bool MyApp::OnInit()
8b089c5e 253{
5cf036d0 254 Args(argc, argv);
8b089c5e 255
5cf036d0 256 // Create the main frame window
be5a51fb 257 frame = new MyFrame(NULL, wxT("wxWidgets OpenGL Isosurf Sample"),
5cf036d0 258 wxDefaultPosition, wxDefaultSize);
8b089c5e 259
5cf036d0
DS
260 // Give it an icon
261 frame->SetIcon(wxIcon(_T("mondrian")));
8b089c5e 262
5cf036d0
DS
263 // Make a menubar
264 wxMenu *fileMenu = new wxMenu;
8b089c5e 265
5cf036d0
DS
266 fileMenu->Append(wxID_EXIT, _T("E&xit"));
267 wxMenuBar *menuBar = new wxMenuBar;
268 menuBar->Append(fileMenu, _T("&File"));
269 frame->SetMenuBar(menuBar);
8b089c5e
JS
270
271 // Make a TestGLCanvas
272
273 // JACS
274#ifdef __WXMSW__
5cf036d0 275 int *gl_attrib = NULL;
8b089c5e 276#else
5cf036d0
DS
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,
cb712074 280# ifdef __WXMAC__
5cf036d0 281 GL_NONE };
cb712074 282# else
5cf036d0 283 None };
cb712074 284# endif
8b089c5e
JS
285#endif
286
5cf036d0
DS
287 if(!doubleBuffer)
288 {
289 printf("don't have double buffer, disabling\n");
8b089c5e 290#ifdef __WXGTK__
5cf036d0 291 gl_attrib[9] = None;
8b089c5e 292#endif
5cf036d0
DS
293 doubleBuffer = GL_FALSE;
294 }
295
5cf036d0
DS
296 frame->m_canvas = new TestGLCanvas(frame, wxID_ANY, wxDefaultPosition,
297 wxDefaultSize, 0, _T("TestGLCanvas"), gl_attrib );
8b089c5e
JS
298
299 // Show the frame
5cf036d0 300 frame->Show(true);
8b089c5e 301
5cf036d0
DS
302 frame->m_canvas->SetCurrent();
303 read_surface( _T("isosurf.dat") );
8b089c5e 304
5cf036d0 305 Init();
8b089c5e 306
5cf036d0 307 return true;
8b089c5e
JS
308}
309
310BEGIN_EVENT_TABLE(MyFrame, wxFrame)
311 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
312END_EVENT_TABLE()
313
314// My frame constructor
315MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
5cf036d0
DS
316 const wxSize& size, long style)
317 : wxFrame(frame, wxID_ANY, title, pos, size, style)
8b089c5e
JS
318{
319 m_canvas = NULL;
3a992940 320 SetIcon(wxIcon(sample_xpm));
8b089c5e
JS
321}
322
5cf036d0
DS
323MyFrame::~MyFrame()
324{
806e2f15 325 delete m_canvas;
5cf036d0
DS
326}
327
8b089c5e 328// Intercept menu commands
5cf036d0 329void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
8b089c5e 330{
5cf036d0
DS
331 // true is to force the frame to close
332 Close(true);
8b089c5e
JS
333}
334
335/*
336 * TestGLCanvas implementation
337 */
338
339BEGIN_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)
345END_EVENT_TABLE()
346
347TestGLCanvas::TestGLCanvas(wxWindow *parent, wxWindowID id,
5cf036d0
DS
348 const wxPoint& pos, const wxSize& size, long style,
349 const wxString& name, int* gl_attrib)
3a992940 350 : wxGLCanvas(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name, gl_attrib)
8b089c5e 351{
5cf036d0
DS
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 }
8b089c5e
JS
361}
362
363
2db98bf5 364void TestGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
8b089c5e
JS
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
c661ecca
RR
370#ifndef __WXMOTIF__
371 if (!GetContext()) return;
372#endif
373
919ae91a
UN
374 SetCurrent();
375
8b089c5e
JS
376 draw1();
377 SwapBuffers();
378}
379
380void TestGLCanvas::OnSize(wxSizeEvent& event)
381{
9d705dfa
GD
382 // this is also necessary to update the context on some platforms
383 wxGLCanvas::OnSize(event);
5cf036d0 384
9d705dfa 385 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
2f6c54eb
VZ
386 int w, h;
387 GetClientSize(&w, &h);
c661ecca 388#ifndef __WXMOTIF__
9d705dfa 389 if (GetContext())
c661ecca 390#endif
9d705dfa 391 {
2f6c54eb 392 SetCurrent();
9d705dfa
GD
393 glViewport(0, 0, (GLint) w, (GLint) h);
394 }
8b089c5e
JS
395}
396
397void TestGLCanvas::OnChar(wxKeyEvent& event)
398{
5cf036d0
DS
399 switch( event.GetKeyCode() )
400 {
8b089c5e 401 case WXK_ESCAPE:
5cf036d0
DS
402 wxTheApp->ExitMainLoop();
403 return;
404
8b089c5e 405 case WXK_LEFT:
5cf036d0
DS
406 yrot -= 15.0;
407 break;
408
8b089c5e 409 case WXK_RIGHT:
5cf036d0
DS
410 yrot += 15.0;
411 break;
412
8b089c5e 413 case WXK_UP:
5cf036d0
DS
414 xrot += 15.0;
415 break;
416
8b089c5e 417 case WXK_DOWN:
5cf036d0
DS
418 xrot -= 15.0;
419 break;
420
8b089c5e 421 case 's': case 'S':
5cf036d0
DS
422 smooth = !smooth;
423 if (smooth)
424 {
425 glShadeModel(GL_SMOOTH);
426 }
427 else
428 {
429 glShadeModel(GL_FLAT);
430 }
431 break;
432
8b089c5e 433 case 'l': case 'L':
5cf036d0
DS
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:
8b089c5e 446 event.Skip();
5cf036d0 447 return;
8b089c5e
JS
448 }
449
5cf036d0 450 Refresh(false);
8b089c5e
JS
451}
452
453void 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());
5cf036d0
DS
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();
2f6c54eb 473 }
5cf036d0
DS
474 else
475 dragging = 0;
476
8b089c5e
JS
477}
478
5cf036d0 479void TestGLCanvas::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
8b089c5e
JS
480{
481 // Do nothing, to avoid flashing.
482}
483