r472: Initial import of Graph3d Doc/View
[ctsim.git] / src / graph3dview.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          graph3dview.cpp
5 **   Purpose:       3d graph view classes
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Jan 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: graph3dview.cpp,v 1.1 2001/01/30 07:32:13 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28
29 #ifdef __GNUG__
30 #pragma implementation
31 #pragma interface
32 #endif
33
34 // For compilers that support precompilation, includes "wx.h".
35 #include "wx/wxprec.h"
36
37 #ifdef __BORLANDC__
38 #pragma hdrstop
39 #endif
40
41 #ifndef WX_PRECOMP
42 #include "wx/wx.h"
43 #endif
44
45 #if !wxUSE_GLCANVAS
46 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
47 #endif
48
49 #include "wx/timer.h"
50 #include "wx/glcanvas.h"
51
52 #include <GL/gl.h>
53 #include <GL/glu.h>
54
55 #include "ct.h"
56 #include "ctsim.h"
57 #include "docs.h"
58 #include "views.h"
59 #include "dialogs.h"
60 #include "dlgprojections.h"
61 #include "dlgreconstruct.h"
62 #include "backprojectors.h"
63 #include "reconstruct.h"
64 #include "timer.h"
65
66 #if defined(MSVC) || HAVE_SSTREAM
67 #include <sstream>
68 #else
69 #include <sstream_subst>
70 #endif
71
72
73 //***********************************************************************
74 // Function: CalculateVectorNormal
75 //
76 // Purpose: Given three points of a 3D plane, this function calculates
77 //          the normal vector of that plane.
78 //
79 // Parameters:
80 //     fVert1[]   == array for 1st point (3 elements are x, y, and z).
81 //     fVert2[]   == array for 2nd point (3 elements are x, y, and z).
82 //     fVert3[]   == array for 3rd point (3 elements are x, y, and z).
83 //
84 // Returns:
85 //     fNormalX   == X vector for the normal vector
86 //     fNormalY   == Y vector for the normal vector
87 //     fNormalZ   == Z vector for the normal vector
88 //*************************************************************************
89
90
91 GLvoid CalculateVectorNormal (GLfloat fVert1[], GLfloat fVert2[], 
92                               GLfloat fVert3[], GLfloat *fNormalX,
93                               GLfloat *fNormalY, GLfloat *fNormalZ)
94 {
95   GLfloat Qx = fVert2[0] - fVert1[0];
96   GLfloat Qy = fVert2[1] - fVert1[1];
97   GLfloat Qz = fVert2[2] - fVert1[2];
98   GLfloat Px = fVert3[0] - fVert1[0];
99   GLfloat Py = fVert3[1] - fVert1[1];
100   GLfloat Pz = fVert3[2] - fVert1[2];
101   
102   *fNormalX = Py*Qz - Pz*Qy;
103   *fNormalY = Pz*Qx - Px*Qz;
104   *fNormalZ = Px*Qy - Py*Qx;
105   
106
107
108 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
109
110 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
111 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
112 END_EVENT_TABLE()
113
114 Graph3dFileView::Graph3dFileView ()
115 : m_pFileMenu(NULL)
116 {
117   m_bUseVertexArrays = GL_FALSE;
118   m_bDoubleBuffer = GL_TRUE;
119   m_bSmooth = GL_TRUE;
120   m_bLighting = GL_TRUE;
121   
122 }
123
124 Graph3dFileView::~Graph3dFileView()
125 {
126   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
127   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
128 }
129
130 bool 
131 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
132 {
133   m_pFrame = CreateChildFrame(doc, this);
134   (m_pFrame);
135   
136   int width, height;
137   m_pFrame->GetClientSize (&width, &height);
138   m_pFrame->SetTitle("Graph3dFileView");
139   m_pCanvas = CreateCanvas (m_pFrame);
140   
141   m_pFrame->Show(TRUE);
142   m_pCanvas->SetCurrent();
143   
144   InitGL();
145   
146   int x, y;  // X requires a forced resize
147   m_pFrame->GetSize(&x, &y);
148   m_pFrame->SetSize(-1, -1, x, y);
149   m_pFrame->SetFocus();
150   m_pFrame->Show(true);
151   Activate(true);
152   
153   return true;
154
155
156 Graph3dFileCanvas* 
157 Graph3dFileView::CreateCanvas (wxFrame* parent)
158 {
159   Graph3dFileCanvas* pCanvas;
160   int width, height;
161   parent->GetClientSize(&width, &height);
162   
163 #ifdef __WXMSW__
164   int *gl_attrib = NULL;
165 #else
166   int gl_attrib[20] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
167     GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, None };
168 #endif
169   
170   if(! m_bDoubleBuffer) {
171 #ifdef __WXGTK__
172     gl_attrib[9] = None;
173 #endif
174   }
175   
176   pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0, gl_attrib);
177   
178   pCanvas->SetScrollbars(20, 20, 50, 50);
179   pCanvas->SetBackgroundColour(*wxWHITE);
180   pCanvas->Clear();
181   
182   return pCanvas;
183 }
184
185
186
187 void
188 Graph3dFileView::DrawSurface()
189 {
190 #ifdef GL_EXT_vertex_array
191   if (m_bUseVertexArrays) {
192     glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, m_nVerts );
193   }
194   else {
195 #endif
196     glBegin( GL_TRIANGLE_STRIP );
197     for (GLint i = 0;i < m_nVerts; i++) {
198       //         glNormal3fv( &m_vecNorms[i] );
199       //         glVertex3fv( &m_vecVerts[i] );
200     }
201     glEnd();
202 #ifdef GL_EXT_vertex_array
203   }
204 #endif
205 }
206
207
208 void
209 Graph3dFileView::OnProperties (wxCommandEvent& event)
210 {
211   const ImageFile& rIF = GetDocument()->getImageFile();
212   if (rIF.nx() == 0 || rIF.ny() == 0)
213     *theApp->getLog() << "Properties: empty imagefile\n";
214   else {
215     const std::string& rFilename = rIF.getFilename();
216     std::ostringstream os;
217     double min, max, mean, mode, median, stddev;
218     rIF.statistics (rIF.getArray(), min, max, mean, mode, median, stddev);
219     os << "Filename: " << rFilename << "\n";
220     os << "Size: (" << rIF.nx() << "," << rIF.ny() << ")\n";
221     os << "Data type: ";
222     if (rIF.isComplex())
223       os << "Complex\n";
224     else
225       os << "Real\n";
226     os << "Minimum: "<<min<<"\nMaximum: "<<max<<"\nMean: "<<mean<<"\nMedian: "<<median<<"\nMode: "<<mode<<"\nStandard Deviation: "<<stddev << "\n";
227     if (rIF.isComplex()) {
228       rIF.statistics (rIF.getImaginaryArray(), min, max, mean, mode, median, stddev);
229       os << "Imaginary: min: "<<min<<"\nmax: "<<max<<"\nmean: "<<mean<<"\nmedian: "<<median<<"\nmode: "<<mode<<"\nstddev: "<<stddev << "\n";
230     }
231     if (rIF.nLabels() > 0) {
232       rIF.printLabelsBrief (os);
233     }
234     *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
235     wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
236     dialogMsg.ShowModal();
237   }
238 }
239
240
241
242 void 
243 Graph3dFileView::OnDraw (wxDC* dc)
244 {
245 #ifndef __WXMOTIF__
246   if (! m_pCanvas->GetContext()) return;
247 #endif
248   
249   Draw();
250   m_pCanvas->SwapBuffers();
251 }
252
253
254 void 
255 Graph3dFileView::Draw ()
256 {
257   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
258   glPushMatrix();
259   glRotatef( m_dYRotate, 0.0, 1.0, 0.0 );
260   glRotatef( m_dXRotate, 1.0, 0.0, 0.0 );
261   
262   DrawSurface();
263   
264   glPopMatrix();
265   glFlush();
266 }
267
268
269 void
270 Graph3dFileView::InitMaterials()
271 {
272   static float ambient[] = {0.1, 0.1, 0.1, 1.0};
273   static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
274   static float position0[] = {0.0, 0.0, 20.0, 0.0};
275   static float position1[] = {0.0, 0.0, -20.0, 0.0};
276   static float front_mat_shininess[] = {60.0};
277   static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
278   static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
279   /*
280   static float back_mat_shininess[] = {60.0};
281   static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
282   static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
283   */
284   static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
285   static float lmodel_twoside[] = {GL_FALSE};
286   
287   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
288   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
289   glLightfv(GL_LIGHT0, GL_POSITION, position0);
290   glEnable(GL_LIGHT0);
291   
292   glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
293   glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
294   glLightfv(GL_LIGHT1, GL_POSITION, position1);
295   glEnable(GL_LIGHT1);
296   
297   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
298   glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
299   glEnable(GL_LIGHTING);
300   
301   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
302   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
303   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
304 }
305
306
307 void 
308 Graph3dFileView::InitGL ()
309 {
310   glClearColor(0.0, 0.0, 0.0, 0.0);
311   
312   glShadeModel(GL_SMOOTH);
313   glEnable(GL_DEPTH_TEST);
314   
315   InitMaterials();
316   
317   glMatrixMode(GL_PROJECTION);
318   glLoadIdentity();
319   glFrustum( -1.0, 1.0, -1.0, 1.0, 5, 25 );
320   
321   glMatrixMode(GL_MODELVIEW);
322   glLoadIdentity();
323   glTranslatef( 0.0, 0.0, -6.0 );
324   
325 #ifdef GL_EXT_vertex_array
326   if (m_bUseVertexArrays) {
327     //  glVertexPointerEXT( 3, GL_FLOAT, 0, m_nVerts, verts );
328     //  glNormalPointerEXT( GL_FLOAT, 0, m_nVerts, norms );
329     glEnable( GL_VERTEX_ARRAY_EXT );
330     glEnable( GL_NORMAL_ARRAY_EXT );
331   }
332 #endif
333 }
334
335 void 
336 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
337 {
338   const ImageFile& rIF = GetDocument()->getImageFile();
339   ImageFileArrayConst v = rIF.getArray();
340   int nx = rIF.nx();
341   int ny = rIF.ny();
342   if (v != NULL && nx != 0 && ny != 0) {
343 #if 0    
344     unsigned char* imageData = new unsigned char [nx * ny * 3];
345     for (int ix = 0; ix < nx; ix++) {
346       for (int iy = 0; iy < ny; iy++) {
347         double scaleValue = ((v[ix][iy] - m_dMinPixel) / scaleWidth) * 255;
348         int intensity = static_cast<int>(scaleValue + 0.5);
349         intensity = clamp (intensity, 0, 255);
350         int baseAddr = ((ny - 1 - iy) * nx + ix) * 3;
351         imageData[baseAddr] = imageData[baseAddr+1] = imageData[baseAddr+2] = intensity;
352       }
353     }
354     wxImage image (nx, ny, imageData, true);
355     m_bitmap = image.ConvertToBitmap();
356     delete imageData;
357     int xSize = nx;
358     int ySize = ny;
359     ySize = clamp (ySize, 0, 800);
360     m_pFrame->SetClientSize (xSize, ySize);
361     m_pCanvas->SetScrollbars(20, 20, nx/20, ny/20);
362     m_pCanvas->SetBackgroundColour(*wxWHITE);
363 #endif
364   } 
365   
366   if (m_pCanvas)
367     m_pCanvas->Refresh();
368 }
369
370 bool 
371 Graph3dFileView::OnClose (bool deleteWindow)
372 {
373   if (! GetDocument() || ! GetDocument()->Close())
374     return false;
375   
376   Activate (false);
377   if (m_pCanvas) {
378     m_pCanvas->setView(NULL);
379     m_pCanvas = NULL;
380   }
381   wxString s(theApp->GetAppName());
382   if (m_pFrame)
383     m_pFrame->SetTitle(s);
384   
385   SetFrame(NULL);
386   
387   if (deleteWindow) {
388     m_pFrame->Destroy();
389     m_pFrame = NULL;
390     if (GetDocument() && GetDocument()->getBadFileOpen())
391       ::wxYield();  // wxWindows bug workaround
392   }
393   
394   return true;
395 }
396
397 #if CTSIM_MDI
398 wxDocMDIChildFrame*
399 #else
400 wxDocChildFrame*
401 #endif
402 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
403 {
404 #if CTSIM_MDI
405   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
406 #else
407   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
408 #endif
409   theApp->setIconForFrame (subframe);
410   
411   m_pFileMenu = new wxMenu;
412   
413   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
414   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
415   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
416   m_pFileMenu->Append(wxID_SAVE, "&Save\tCtrl-S");
417   m_pFileMenu->Append(wxID_SAVEAS, "Save &As...");
418   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
419   m_pFileMenu->Append(wxID_REVERT, "Re&vert");
420   
421   m_pFileMenu->AppendSeparator();
422   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
423   m_pFileMenu->Append(IFMENU_FILE_EXPORT, "&Export...");
424   
425   m_pFileMenu->AppendSeparator();
426   m_pFileMenu->Append(wxID_PRINT, "&Print...");
427   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
428   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
429 #ifdef CTSIM_MDI
430   m_pFileMenu->AppendSeparator();
431   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
432 #endif
433   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
434   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
435   
436   wxMenu *view_menu = new wxMenu;
437   view_menu->Append(IFMENU_VIEW_SCALE_MINMAX, "Display Scale S&et...\tCtrl-E");
438   view_menu->Append(IFMENU_VIEW_SCALE_AUTO, "Display Scale &Auto...\tCtrl-A");
439   view_menu->Append(IFMENU_VIEW_SCALE_FULL, "Display F&ull Scale\tCtrl-U");
440   
441   wxMenu* filter_menu = new wxMenu;
442   filter_menu->Append (IFMENU_FILTER_INVERTVALUES, "&Invert Values");
443   filter_menu->Append (IFMENU_FILTER_SQUARE, "&Square");
444   filter_menu->Append (IFMENU_FILTER_SQRT, "Square &Root");
445   filter_menu->Append (IFMENU_FILTER_LOG, "&Log");
446   filter_menu->Append (IFMENU_FILTER_EXP, "&Exp");
447   filter_menu->AppendSeparator();
448 #ifdef HAVE_FFT
449   filter_menu->Append (IFMENU_FILTER_FFT, "2D &FFT");
450   filter_menu->Append (IFMENU_FILTER_IFFT, "2D &IFFT");
451   filter_menu->Append (IFMENU_FILTER_FFT_ROWS, "FFT Rows");
452   filter_menu->Append (IFMENU_FILTER_IFFT_ROWS, "IFFT Rows");
453   filter_menu->Append (IFMENU_FILTER_FFT_COLS, "FFT Columns");
454   filter_menu->Append (IFMENU_FILTER_IFFT_COLS, "IFFT Columns");
455   filter_menu->Append (IFMENU_FILTER_FOURIER, "F&ourier");
456   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "Inverse Fo&urier");
457 #else
458   filter_menu->Append (IFMENU_FILTER_FOURIER, "&Fourier");
459   filter_menu->Append (IFMENU_FILTER_INVERSE_FOURIER, "&Inverse Fourier");
460 #endif
461   filter_menu->Append (IFMENU_FILTER_SHUFFLEFOURIERTONATURALORDER, "S&huffle Fourier to Natural Order");
462   filter_menu->Append (IFMENU_FILTER_SHUFFLENATURALTOFOURIERORDER, "Shu&ffle Natural to Fourier Order");
463   filter_menu->Append (IFMENU_FILTER_MAGNITUDE, "&Magnitude");
464   filter_menu->Append (IFMENU_FILTER_PHASE, "&Phase");
465   
466   wxMenu *help_menu = new wxMenu;
467   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
468   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
469   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
470   
471   wxMenuBar *menu_bar = new wxMenuBar;
472   
473   menu_bar->Append(m_pFileMenu, "&File");
474   menu_bar->Append(view_menu, "&View");
475   menu_bar->Append(filter_menu, "Fi&lter");
476   menu_bar->Append(help_menu, "&Help");
477   
478   subframe->SetMenuBar(menu_bar);
479   
480   subframe->Centre(wxBOTH);
481   
482   wxAcceleratorEntry accelEntries[10];
483   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
484   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('S'), wxID_SAVE);
485   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('W'), wxID_CLOSE);
486   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
487   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
488   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
489   accelEntries[6].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
490   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('A'), IFMENU_VIEW_SCALE_AUTO);
491   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('U'), IFMENU_VIEW_SCALE_FULL);
492   accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('E'), IFMENU_VIEW_SCALE_MINMAX);
493   wxAcceleratorTable accelTable (10, accelEntries);
494   subframe->SetAcceleratorTable (accelTable);
495   
496   return subframe;
497 }
498
499
500
501 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
502 EVT_SIZE(Graph3dFileCanvas::OnSize)
503 EVT_PAINT(Graph3dFileCanvas::OnPaint)
504 EVT_CHAR(Graph3dFileCanvas::OnChar)
505 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
506 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
507 END_EVENT_TABLE()
508
509
510
511
512 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
513                                       const wxSize& size, long style, int* gl_attrib):
514 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas"), gl_attrib), m_pView(view)
515 {
516   parent->Show(TRUE);
517   SetCurrent();
518   /* Make sure server supports the vertex array extension */
519   char* extensions = (char *) glGetString( GL_EXTENSIONS );
520   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
521     m_pView->m_bUseVertexArrays = GL_FALSE;
522   }
523 }
524
525
526 Graph3dFileCanvas::~Graph3dFileCanvas(void)
527 {
528   m_pView = NULL;
529 }
530
531 void 
532 Graph3dFileCanvas::OnDraw(wxDC& dc)
533 {
534   if (m_pView)
535     m_pView->OnDraw(& dc);
536 }
537
538 void 
539 Graph3dFileCanvas::OnSize(wxSizeEvent& event)
540 {
541 #ifndef __WXMOTIF__
542   if (!GetContext()) return;
543 #endif
544   
545   SetCurrent();
546   int width, height;
547   GetClientSize (&width, &height);
548   Reshape (width, height);
549 }
550
551 void 
552 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
553 {
554   if (! m_pView)
555     return;
556   
557   switch(event.KeyCode()) {
558   case WXK_ESCAPE:
559     exit(0);
560   case WXK_LEFT:
561         m_pView->m_dYRotate -= 15.0;
562     break;
563   case WXK_RIGHT:
564     m_pView->m_dYRotate += 15.0;
565     break;
566   case WXK_UP:
567     m_pView->m_dXRotate += 15.0;
568     break;
569   case WXK_DOWN:
570     m_pView->m_dXRotate -= 15.0;
571     break;
572   case 's': case 'S':
573     m_pView->m_bSmooth = !m_pView->m_bSmooth;
574     if (m_pView->m_bSmooth) {
575       glShadeModel(GL_SMOOTH);
576     } else {
577       glShadeModel(GL_FLAT);
578     }
579     break;
580   case 'l': case 'L':
581     m_pView->m_bLighting = !m_pView->m_bLighting;
582     if (m_pView->m_bLighting) {
583       glEnable(GL_LIGHTING);
584     } else {
585       glDisable(GL_LIGHTING);
586     }
587     break;
588   default:
589     {
590       event.Skip();
591       return;
592     }
593   }
594   
595   Refresh(FALSE);
596 }
597
598 void
599 Graph3dFileCanvas::Reshape (int width, int height)
600 {
601   glViewport(0, 0, (GLint)width, (GLint)height);
602 }
603
604
605 void 
606 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
607 {
608   static int dragging = 0;
609   static float last_x, last_y;
610   
611   if(event.LeftIsDown()) {
612     if(!dragging) {
613       dragging = 1;
614     } else {
615       m_pView->m_dXRotate += (event.GetX() - last_x)*1.0;
616       m_pView->m_dYRotate += (event.GetY() - last_y)*1.0;
617       Refresh(FALSE);
618     }
619     last_x = event.GetX();
620     last_y = event.GetY();
621   } else
622     dragging = 0;
623 }
624
625 void 
626 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
627 {
628   // Do nothing: avoid flashing.
629 }
630
631
632
633