r480: no message
[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.5 2001/02/02 00:46:38 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 #ifdef __GNUG__
29 #pragma implementation
30 #pragma interface
31 #endif
32
33 // For compilers that support precompilation, includes "wx.h".
34 #include "wx/wxprec.h"
35
36 #ifdef __BORLANDC__
37 #pragma hdrstop
38 #endif
39
40 #ifndef WX_PRECOMP
41 #include "wx/wx.h"
42 #endif
43
44 #if wxUSE_GLCANVAS
45
46 #if !wxUSE_GLCANVAS
47 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
48 #endif
49
50 #include "wx/timer.h"
51 #include "wx/glcanvas.h"
52
53 #include <GL/gl.h>
54 #include <GL/glu.h>
55
56 #include "ct.h"
57 #include "ctsim.h"
58 #include "docs.h"
59 #include "views.h"
60 #include "dialogs.h"
61 #include "dlgprojections.h"
62 #include "dlgreconstruct.h"
63 #include "backprojectors.h"
64 #include "reconstruct.h"
65 #include "timer.h"
66
67 #if defined(MSVC) || HAVE_SSTREAM
68 #include <sstream>
69 #else
70 #include <sstream_subst>
71 #endif
72
73 // Rainbow: Purple->Blue->Cyan->Green->Yellow->Red = (1,0,1)-(0,0,1)-(0,1,1)-(0,1,0)-(1,1,0)-(1,0,0)
74 static void 
75 intensityToColor (double dIntensity, float vecColor[3])
76 {
77   double dRange = dIntensity * 5;
78   int iRange = static_cast<int>(floor (dRange));
79   double dFrac = dRange - iRange;
80   
81   switch (iRange) {
82   case 0:
83     vecColor[0] = 1 - dFrac; vecColor[1] = 0; vecColor[2] = 1;
84     break;
85   case 1:
86     vecColor[0] = 0; vecColor[1] = dFrac; vecColor[2] = 1;
87     break;
88   case 2:
89     vecColor[0] = 0; vecColor[1] = 1; vecColor[2] = 1 - dFrac;
90     break;
91   case 3:
92     vecColor[0] = dFrac; vecColor[1] = 1; vecColor[2] = 0;
93     break;
94   case 4:
95     vecColor[0] = 0; vecColor[1] = 1 - dFrac; vecColor[2] = 0;
96     break;
97   case 5:
98     vecColor[0] = 1; vecColor[1] = 0; vecColor[2] = 0;
99     break;
100   }
101 }
102
103 //***********************************************************************
104 // Function: CalculateVectorNormal
105 //
106 // Purpose: Given three points of a 3D plane, this function calculates
107 //          the normal vector of that plane.
108 //
109 // Parameters:
110 //     fVert1[]   == array for 1st point (3 elements are x, y, and z).
111 //     fVert2[]   == array for 2nd point (3 elements are x, y, and z).
112 //     fVert3[]   == array for 3rd point (3 elements are x, y, and z).
113 //
114 // Returns:
115 //     fNormalX   == X vector for the normal vector
116 //     fNormalY   == Y vector for the normal vector
117 //     fNormalZ   == Z vector for the normal vector
118 //*************************************************************************
119
120
121 GLvoid CalculateVectorNormal (GLfloat fVert1[], GLfloat fVert2[], 
122                               GLfloat fVert3[], GLfloat *fNormalX,
123                               GLfloat *fNormalY, GLfloat *fNormalZ)
124 {
125   GLfloat Qx = fVert2[0] - fVert1[0];
126   GLfloat Qy = fVert2[1] - fVert1[1];
127   GLfloat Qz = fVert2[2] - fVert1[2];
128   GLfloat Px = fVert3[0] - fVert1[0];
129   GLfloat Py = fVert3[1] - fVert1[1];
130   GLfloat Pz = fVert3[2] - fVert1[2];
131   
132   *fNormalX = Py*Qz - Pz*Qy;
133   *fNormalY = Pz*Qx - Px*Qz;
134   *fNormalZ = Px*Qy - Py*Qx;
135   
136
137
138 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
139
140 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
141 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
142 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
143 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
144 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
145 EVT_MENU(GRAPH3D_VIEW_SURFACE, Graph3dFileView::OnSurface)
146 END_EVENT_TABLE()
147
148 Graph3dFileView::Graph3dFileView ()
149 : m_pFileMenu(NULL), m_pViewMenu(NULL)
150 {
151   m_bUseVertexArrays = false;
152   m_bDoubleBuffer = true;
153   m_bSmooth = true;
154   m_bLighting = true;
155   m_bSurface = true;
156   m_bLighting = true;
157   m_bColor = true;
158   m_dXRotate = 0;
159   m_dYRotate = 0;
160   m_dZRotate = 0;
161 }
162
163 Graph3dFileView::~Graph3dFileView()
164 {
165   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
166   GetDocumentManager()->ActivateView(this, FALSE, TRUE);
167 }
168
169 bool 
170 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
171 {
172   m_pFrame = CreateChildFrame(doc, this);
173   (m_pFrame);
174   
175   int width, height;
176   m_pFrame->GetClientSize (&width, &height);
177   m_pFrame->SetTitle("Graph3dFileView");
178   m_pCanvas = CreateCanvas (m_pFrame);
179   
180   m_pFrame->Show(TRUE);
181   m_pCanvas->SetCurrent();
182   
183   InitGL();
184   
185   int x, y;  // X requires a forced resize
186   m_pFrame->GetSize(&x, &y);
187   m_pFrame->SetSize(-1, -1, x, y);
188   m_pFrame->SetFocus();
189   m_pFrame->Show(true);
190   Activate(true);
191   
192   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
193   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
194   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
195   m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
196   return true;
197
198
199 Graph3dFileCanvas* 
200 Graph3dFileView::CreateCanvas (wxFrame* parent)
201 {
202   Graph3dFileCanvas* pCanvas;
203   int width, height;
204   parent->GetClientSize (&width, &height);
205   
206 #ifdef __WXMSW__
207   int *gl_attrib = NULL;
208 #else
209   int gl_attrib[20] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
210     GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, None };
211 #endif
212   
213   if(! m_bDoubleBuffer) {
214 #ifdef __WXGTK__
215     gl_attrib[9] = None;
216 #endif
217   }
218   
219   pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0, gl_attrib);
220   
221   pCanvas->SetBackgroundColour(*wxWHITE);
222   pCanvas->Clear();
223   
224   return pCanvas;
225 }
226
227
228
229 void
230 Graph3dFileView::DrawSurface()
231 {
232 #ifdef GL_EXT_vertex_array
233   if (m_bUseVertexArrays) {
234     //    glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, nVertices );
235   }
236   else {
237 #endif
238     double edge = 1.;   
239     unsigned int nx = GetDocument()->nx();
240     unsigned int ny = GetDocument()->ny();
241     const ImageFileArrayConst v = GetDocument()->getArray();
242     if (nx == 0 || ny == 0 || ! v)
243       return;
244     
245         glRotatef( m_dXRotate, 1.0, 0.0, 0.0 );
246         glRotatef( m_dZRotate, 0.0, 1.0, 0.0 );
247         glRotatef( m_dYRotate, 0.0, 0.0, 1.0 );
248         glTranslatef (-static_cast<double>(nx) / 2., 0, -static_cast<double>(ny) / 2.);
249         
250     InitMaterials(); 
251     
252     if (m_bSmooth) {
253       glShadeModel (GL_SMOOTH);
254     } else {
255       glShadeModel (GL_FLAT);
256     }
257     
258     if (m_bLighting) {
259       glEnable (GL_LIGHTING);
260     } else {
261       glDisable (GL_LIGHTING);
262     }
263     
264     double dMin = v[0][0];
265     double dMax = dMin;
266     unsigned int ix;
267     for (ix = 0; ix < nx; ix++)
268       for (unsigned int iy = 0; iy < ny; iy++)
269         if (v[ix][iy] < dMin)
270           dMin = v[ix][iy];
271         else if (v[ix][iy] > dMax)
272           dMax = v[ix][iy];
273         
274         double dIntensityScale = dMax - dMin;
275         double actOffset = dMin;
276         double actScale = 0.3 * sqrt(nx*nx+ny*ny) / (dMax - dMin);
277         
278         //      glNewList(opnListNum++,GL_COMPILE);             
279         if (! m_bColor)
280           glColor3f (1.0, 1.0, 1.0);
281         
282         glDisable (GL_CULL_FACE);
283         for (ix = 0; ix < nx-1; ix++) {                 
284           for (unsigned int iy = 0; iy < ny-1; iy++) {                  
285             
286             float p1[3], p2[3], p3[3], p4[3];
287             float n1[3], n2[3], n3[3], n4[3];
288             if (m_bSurface)
289               glBegin(GL_QUADS);
290             else
291               glBegin(GL_LINE_LOOP);
292             
293             p1[0] = ix;  p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = iy;
294             p2[0] = ix+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = iy; 
295             p3[0] = ix+1; p3[1] = actScale * (v[ix+1][iy+1] + actOffset); p3[2] = iy+1;
296             p4[0] = ix;  p4[1] = actScale * (v[ix][iy+1] + actOffset); p4[2] = iy+1;
297             
298             //                          n1[0] = -(p2[1] - p1[1])*(p3[2] - p1[2]) + (p2[2] - p1[2])*(p3[1] - p2[1]);
299             //                          n1[1] = -(p2[2] - p1[2])*(p3[0] - p2[0]) + (p2[0] - p1[0])*(p3[2] - p2[2]);
300             //                          n1[2] = -(p2[0] - p1[0])*(p3[1] - p2[1]) + (p2[1] - p1[1])*(p3[0] - p2[0]);
301             CalculateVectorNormal (p1, p2, p4, &n1[0], &n1[1], &n1[2]);
302             //CalculateVectorNormal (p2, p1, p3, &n2[0], &n2[1], &n2[2])
303             //CalculateVectorNormal (p3, p2, p4, &n1[0], &n1[1], &n1[2])
304             double dIntensity1, dIntensity2, dIntensity3, dIntensity4;
305             if (m_bColor) {
306               dIntensity1 =     (v[ix][iy] - dMin) / dIntensityScale;
307               dIntensity2 =     (v[ix+1][iy] - dMin) / dIntensityScale;
308               dIntensity3 =     (v[ix+1][iy+1] - dMin) / dIntensityScale;
309               dIntensity4 =     (v[ix][iy+1] - dMin) / dIntensityScale;
310             }
311             float vecColor[3];
312             if (m_bColor) {
313               intensityToColor (dIntensity1, vecColor);
314               glColor3fv (vecColor);
315             }
316             glVertex3fv (p1); glNormal3fv (n1);                                 
317             if (m_bColor) {
318               intensityToColor (dIntensity2, vecColor);
319               glColor3fv (vecColor);
320             }
321             glVertex3fv (p2); glNormal3fv (n1);                                 
322             if (m_bColor) {
323               intensityToColor (dIntensity3, vecColor);
324               glColor3fv (vecColor);
325             }
326             glVertex3fv (p3); glNormal3fv (n1);                                 
327             if (m_bColor) {
328               intensityToColor (dIntensity4, vecColor);
329               glColor3fv (vecColor);
330             }
331             glVertex3fv (p4); glNormal3fv (n1);                                                                                                                                         
332             glEnd();                            
333           }                     
334           
335         }
336         glEndList();
337         
338 #ifdef GL_EXT_vertex_array
339   }
340 #endif
341 }
342
343
344 void
345 Graph3dFileView::OnProperties (wxCommandEvent& event)
346 {
347   std::ostringstream os;
348   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
349   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
350   dialogMsg.ShowModal();
351 }
352
353 void
354 Graph3dFileView::OnLighting (wxCommandEvent& event)
355 {
356   m_bLighting = ! m_bLighting;
357   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
358   
359   m_pCanvas->Refresh();
360 }
361
362 void
363 Graph3dFileView::OnSurface (wxCommandEvent& event)
364 {
365   m_bSurface = ! m_bSurface;
366   m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
367   m_pCanvas->Refresh();
368 }
369
370 void
371 Graph3dFileView::OnColor (wxCommandEvent& event)
372 {
373   m_bColor = ! m_bColor;
374   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
375   m_pCanvas->Refresh();
376 }
377
378 void
379 Graph3dFileView::OnSmooth (wxCommandEvent& event)
380 {
381   m_bSmooth = ! m_bSmooth;
382   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
383   m_pCanvas->Refresh();
384 }
385
386
387
388 void 
389 Graph3dFileView::OnDraw (wxDC* dc)
390 {
391 #ifndef __WXMOTIF__
392   if (! m_pCanvas->GetContext()) return;
393 #endif
394   
395   Draw();
396   std::ostringstream os;
397   os << "Xangle=" << m_dXRotate << ", Yangle=" << m_dYRotate << ", Zangle=" << m_dZRotate;
398   m_statusBar.SetStatusText (os.str().c_str());
399   m_pCanvas->SwapBuffers();
400 }
401
402
403 void 
404 Graph3dFileView::Draw ()
405 {
406   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
407   glPushMatrix();
408   DrawSurface();
409   
410   glPopMatrix();
411   glFlush();
412 }
413
414
415 void
416 Graph3dFileView::InitMaterials()
417 {
418   if (! GetDocument())
419     return;
420   int nx = GetDocument()->nx();
421   int ny = GetDocument()->ny();
422   
423 #if 1
424   static float ambient[] = {0.1, 0.1, 0.1, 1.0};
425   static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
426   static float position0[] = {0, 0, -nx/4, 0, 0.0};
427   static float position1[] = {nx/2, ny/2, nx/4, 0.0};
428   //  static float position0[] = {0.0, 0.0, 20.0, 0.0};
429   //  static float position1[] = {0.0, 0.0, -20.0, 0.0};
430   static float front_mat_shininess[] = {5.0};
431   static float front_mat_specular[] = {0.1, 0.1, 0.1, 1.0};
432   static float front_mat_diffuse[] = {0.3, 0.3, 0.3, 1.0};
433   /*
434   static float back_mat_shininess[] = {60.0};
435   static float back_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
436   static float back_mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
437   */
438   static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
439   static float lmodel_twoside[] = {GL_FALSE};
440   
441   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
442   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
443   glLightfv (GL_LIGHT0, GL_POSITION, position0);
444   glEnable (GL_LIGHT0);
445   
446   glLightfv (GL_LIGHT1, GL_AMBIENT, ambient);
447   glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse);
448   glLightfv (GL_LIGHT1, GL_POSITION, position1);
449   glEnable (GL_LIGHT1);
450   
451   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
452   glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
453   
454   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
455   glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
456   glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
457   
458   glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
459   //  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
460   glEnable(GL_COLOR_MATERIAL);
461 #else
462   GLfloat impLPos[]  = {1.0, 1.0, 1.0, 0.0};
463   
464   GLfloat defaultLightAmb   [] = {.2, .2, .2, 1.0};
465   GLfloat defaultLightDiff  [] = {.2, .2, .2, 1.0};
466   GLfloat defaultLightSpec  [] = { .3, .3, .3, 1.0};
467   
468   GLfloat defaultGlobalAmb [] = {.3, .3, .3, 1.0};
469   GLfloat defaultGlobalDiff[] = {.3, .3, .3, 1.0};
470   
471   GLfloat defaultMatShine[] = {  30.0 };
472   GLfloat defaultMatSpec[]  = { .4, .4, .4, 1.0};
473   GLfloat defaultMatAmb[]   = { .3, .3, .3, 1.0};
474   GLfloat defaultMatDiff[]  = { .5, .5, .5, 1.0};
475   
476   GLfloat brassMatAmb[]   = { .33, .22, .03, 1.0};
477   GLfloat brassMatDiff[]  = { .78, .57, .11, 1.0};
478   GLfloat brassMatSpec[]  = { .99, .91, .81, 1.0};
479   GLfloat brassMatShine[] = {  27.8 };
480   
481   GLfloat emeraldMatAmb[]   = { .021, .1745 , .021, 1.0};
482   GLfloat emeraldMatDiff[]  = { .075, .6142 , .075, 1.0 };
483   GLfloat emeraldMatSpec[]  = { .633, .7278 , .633, 1.0 };
484   GLfloat emeraldMatShine[] = {  76.8 };
485   
486   GLfloat slateMatAmb[]   = { .02, .02 , .02, 1.0 };
487   GLfloat slateMatDiff[]  = { .02, .01 , .01, 1.0 };
488   GLfloat slateMatSpec[]  = { .4,  .4 ,  .4 , 1.0 };
489   GLfloat slateMatShine[] = { .768 };
490   
491   //       double opnX = nx, opnY = ny, opnZ = z;
492   //       eyeX = 1; eyeY = 1, eyeZ = 1;
493   
494   impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
495   //opnListNum = 1;
496   //impGraphicsFlag = IMP__3D;
497   
498   //       glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
499   //       glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
500   //  glutInitWindowPosition (100, 100);
501   //       glutCreateWindow ("- imp3D graphics -" );
502   
503   glClearColor(0.0, 0.0, 0.0, 0.0);
504   
505   glShadeModel (GL_SMOOTH);
506   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
507   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
508   glEnable(GL_NORMALIZE);
509   
510   
511   glEnable(GL_DEPTH_TEST);
512   
513   glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
514   glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
515   glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
516   
517   glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
518   glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
519   glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
520   
521   glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
522   glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
523   glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
524   
525   glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
526   glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
527   glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
528   
529   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT  , defaultMatAmb);
530   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE  , defaultMatDiff);
531   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
532   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
533   
534   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
535   
536   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
537   
538   glEnable(GL_COLOR_MATERIAL);
539   
540   glEnable(GL_LIGHTING);
541   glEnable(GL_LIGHT1);
542   glEnable(GL_LIGHT2);
543   glEnable(GL_LIGHT0);
544 #endif
545   
546 }
547
548
549 void 
550 Graph3dFileView::InitGL ()
551 {
552   glClearColor(0.0, 0.0, 0.0, 0.0);
553   
554   glEnable(GL_DEPTH_TEST);
555   
556 }
557
558 void 
559 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
560 {
561   int nVertices = GetDocument()->m_nVertices;
562   glTripleFloat* pVertices = GetDocument()->m_pVertices;
563   glTripleFloat* pNormals = GetDocument()->m_pNormals;
564   
565   glMatrixMode(GL_PROJECTION);
566   glLoadIdentity();
567   if (! GetDocument())
568     return;
569   int nx = GetDocument()->nx();
570   int ny = GetDocument()->ny();
571   int maxDim = maxValue<int> (nx, ny);
572   
573   glOrtho (-maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71, maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71);
574   glMatrixMode(GL_MODELVIEW);
575   glLoadIdentity();
576   
577   
578 #ifdef GL_EXT_vertex_array
579   if (m_bUseVertexArrays) {
580     //  glVertexPointerEXT( 3, GL_FLOAT, 0, nVertices, pVertices );
581     //  glNormalPointerEXT( GL_FLOAT, 0, nVertices, pNormals );
582     glEnable( GL_VERTEX_ARRAY_EXT );
583     glEnable( GL_NORMAL_ARRAY_EXT );
584   }
585 #endif
586   
587   if (m_pCanvas)
588     m_pCanvas->Refresh();
589 }
590
591 bool 
592 Graph3dFileView::OnClose (bool deleteWindow)
593 {
594   if (! GetDocument() || ! GetDocument()->Close())
595     return false;
596   
597   Activate (false);
598   if (m_pCanvas) {
599     m_pCanvas->setView(NULL);
600     m_pCanvas = NULL;
601   }
602   wxString s(theApp->GetAppName());
603   if (m_pFrame)
604     m_pFrame->SetTitle(s);
605   
606   SetFrame(NULL);
607   
608   if (deleteWindow) {
609     m_pFrame->Destroy();
610     m_pFrame = NULL;
611     if (GetDocument() && GetDocument()->getBadFileOpen())
612       ::wxYield();  // wxWindows bug workaround
613   }
614   
615   return true;
616 }
617
618 #if CTSIM_MDI
619 wxDocMDIChildFrame*
620 #else
621 wxDocChildFrame*
622 #endif
623 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
624 {
625 #if CTSIM_MDI
626   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
627 #else
628   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
629 #endif
630   theApp->setIconForFrame (subframe);
631   
632   m_statusBar.Create (subframe, -1);
633   subframe->SetStatusBar (&m_statusBar);
634   
635   m_pFileMenu = new wxMenu;
636   
637   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
638   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
639   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
640   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
641   
642   m_pFileMenu->AppendSeparator();
643   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
644   
645   m_pFileMenu->AppendSeparator();
646   m_pFileMenu->Append(wxID_PRINT, "&Print...");
647   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
648   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
649 #ifdef CTSIM_MDI
650   m_pFileMenu->AppendSeparator();
651   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
652 #endif
653   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
654   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
655   
656   m_pViewMenu = new wxMenu;
657   m_pViewMenu->Append(GRAPH3D_VIEW_SURFACE, "&Surface\tCtrl-U", "", true);
658   m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "&Smooth\tCtrl-M", "", true);
659   m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "&Color\tCtrl-R", "", true);
660   m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "&Lighting\tCtrl-L", "", true);
661   
662   wxMenu *help_menu = new wxMenu;
663   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
664   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
665   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
666   
667   wxMenuBar *menu_bar = new wxMenuBar;
668   
669   menu_bar->Append(m_pFileMenu, "&File");
670   menu_bar->Append(m_pViewMenu, "&View");
671   menu_bar->Append(help_menu, "&Help");
672   
673   subframe->SetMenuBar(menu_bar);
674   
675   subframe->Centre(wxBOTH);
676   
677   wxAcceleratorEntry accelEntries[10];
678   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
679   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
680   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
681   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
682   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
683   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SURFACE);
684   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_COLOR);
685   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_LIGHTING);
686   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
687   wxAcceleratorTable accelTable (9, accelEntries);
688   subframe->SetAcceleratorTable (accelTable);
689   
690   return subframe;
691 }
692
693
694
695 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
696 EVT_SIZE(Graph3dFileCanvas::OnSize)
697 EVT_PAINT(Graph3dFileCanvas::OnPaint)
698 EVT_CHAR(Graph3dFileCanvas::OnChar)
699 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
700 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
701 END_EVENT_TABLE()
702
703
704
705
706 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
707                                       const wxSize& size, long style, int* gl_attrib):
708 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas"), gl_attrib), m_pView(view)
709 {
710   parent->Show(TRUE);
711   SetCurrent();
712   /* Make sure server supports the vertex array extension */
713   char* extensions = (char *) glGetString( GL_EXTENSIONS );
714   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
715     m_pView->m_bUseVertexArrays = GL_FALSE;
716   }
717 }
718
719
720 Graph3dFileCanvas::~Graph3dFileCanvas(void)
721 {
722   m_pView = NULL;
723 }
724
725 void 
726 Graph3dFileCanvas::OnDraw(wxDC& dc)
727 {
728   if (m_pView)
729     m_pView->OnDraw(& dc);
730 }
731
732 void 
733 Graph3dFileCanvas::OnSize(wxSizeEvent& event)
734 {
735 #ifndef __WXMOTIF__
736   if (!GetContext()) return;
737 #endif
738   
739   SetCurrent();
740   int width, height;
741   GetClientSize (&width, &height);
742   Reshape (width, height);
743 }
744
745 void 
746 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
747 {
748   if (! m_pView)
749     return;
750   
751   wxCommandEvent dummyEvent;
752   switch (event.KeyCode()) {
753   case WXK_LEFT:
754         m_pView->m_dYRotate -= 15.0;
755     break;
756   case WXK_RIGHT:
757     m_pView->m_dYRotate += 15.0;
758     break;
759   case WXK_UP:
760     m_pView->m_dXRotate += 15.0;
761     break;
762   case WXK_DOWN:
763     m_pView->m_dXRotate -= 15.0;
764     break;
765   case 'z': case 'Z':
766     m_pView->m_dZRotate += 15.0;
767     break;
768   case 'x': case 'X':
769     m_pView->m_dZRotate -= 15.0;
770     break;
771   case 's': case 'S':
772     m_pView->OnSmooth (dummyEvent);
773     break;
774   case 'l': case 'L':
775     m_pView->OnLighting (dummyEvent);
776     break;
777   case 'c': case 'C':
778     m_pView->OnColor (dummyEvent);
779     break;
780   default:
781     {
782       event.Skip();
783       return;
784     }
785   }
786   
787   Refresh (false);
788 }
789
790 void
791 Graph3dFileCanvas::Reshape (int width, int height)
792 {
793   glViewport (0, 0, (GLint)width, (GLint)height);
794 }
795
796
797 void 
798 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
799 {
800   static int dragging = 0;
801   static float last_x, last_y;
802   
803   if (! m_pView)
804     return;
805   
806   if(event.LeftIsDown()) {
807     if(! dragging) {
808       dragging = 1;
809     } else {
810       m_pView->m_dXRotate += (event.GetX() - last_x)*1.0;
811       m_pView->m_dYRotate += (event.GetY() - last_y)*1.0;
812       Refresh(FALSE);
813     }
814     last_x = event.GetX();
815     last_y = event.GetY();
816   } else
817     dragging = 0;
818 }
819
820 void 
821 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
822 {
823   // Do nothing: avoid flashing.
824 }
825
826
827
828
829 #endif // wxUSE_GLCANVAS