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