r484: 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.8 2001/02/03 18:38:42 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 = false;
158   m_dXRotate = -45;
159   m_dYRotate = 0;
160   m_dZRotate = -45;
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     InitMaterials(); 
246     
247     glRotatef( m_dXRotate, 1.0, 0.0, 0.0 );
248     glRotatef( m_dZRotate, 0.0, 1.0, 0.0 );
249     glRotatef( m_dYRotate, 0.0, 0.0, 1.0 );
250     glTranslatef (-static_cast<double>(nx) / 2., 0., -static_cast<double>(ny) / 2.);
251     
252     
253     if (m_bSmooth) {
254       glShadeModel (GL_SMOOTH);
255     } else {
256       glShadeModel (GL_FLAT);
257     }
258     
259     if (m_bLighting) {
260       glEnable (GL_LIGHTING);
261     } else {
262       glDisable (GL_LIGHTING);
263     }
264     
265     double dMin = v[0][0];
266     double dMax = dMin;
267     unsigned int ix;
268     for (ix = 0; ix < nx; ix++)
269       for (unsigned int iy = 0; iy < ny; iy++)
270         if (v[ix][iy] < dMin)
271           dMin = v[ix][iy];
272         else if (v[ix][iy] > dMax)
273           dMax = v[ix][iy];
274         
275         double dIntensityScale = dMax - dMin;
276         double actOffset = dMin;
277         double actScale = 0.3 * sqrt(nx*nx+ny*ny) / (dMax - dMin);
278         
279         //      glNewList(opnListNum++,GL_COMPILE);             
280         if (! m_bColor)
281           glColor3f (1.0, 1.0, 1.0);
282         
283         glDisable (GL_CULL_FACE);
284         for (ix = 0; ix < nx-1; ix++) {                 
285           for (unsigned int iy = 0; iy < ny-1; iy++) {                  
286             
287             float p1[3], p2[3], p3[3], p4[3];
288             float n1[3], n2[3], n3[3], n4[3];
289             if (m_bSurface)
290               glBegin(GL_QUADS);
291             else
292               glBegin(GL_LINE_LOOP);
293             
294             p1[0] = ix;  p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = iy;
295             p2[0] = ix+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = iy; 
296             p3[0] = ix+1; p3[1] = actScale * (v[ix+1][iy+1] + actOffset); p3[2] = iy+1;
297             p4[0] = ix;  p4[1] = actScale * (v[ix][iy+1] + actOffset); p4[2] = iy+1;
298             
299             //                          n1[0] = -(p2[1] - p1[1])*(p3[2] - p1[2]) + (p2[2] - p1[2])*(p3[1] - p2[1]);
300             //                          n1[1] = -(p2[2] - p1[2])*(p3[0] - p2[0]) + (p2[0] - p1[0])*(p3[2] - p2[2]);
301             //                          n1[2] = -(p2[0] - p1[0])*(p3[1] - p2[1]) + (p2[1] - p1[1])*(p3[0] - p2[0]);
302             CalculateVectorNormal (p1, p2, p4, &n1[0], &n1[1], &n1[2]);
303             //CalculateVectorNormal (p2, p1, p3, &n2[0], &n2[1], &n2[2])
304             //CalculateVectorNormal (p3, p2, p4, &n1[0], &n1[1], &n1[2])
305             double dIntensity1, dIntensity2, dIntensity3, dIntensity4;
306             if (m_bColor) {
307               dIntensity1 =     (v[ix][iy] - dMin) / dIntensityScale;
308               dIntensity2 =     (v[ix+1][iy] - dMin) / dIntensityScale;
309               dIntensity3 =     (v[ix+1][iy+1] - dMin) / dIntensityScale;
310               dIntensity4 =     (v[ix][iy+1] - dMin) / dIntensityScale;
311             }
312             float vecColor[3];
313             if (m_bColor) {
314               intensityToColor (dIntensity1, vecColor);
315               glColor3fv (vecColor);
316             }
317             glVertex3fv (p1); glNormal3fv (n1);                                 
318             if (m_bColor) {
319               intensityToColor (dIntensity2, vecColor);
320               glColor3fv (vecColor);
321             }
322             glVertex3fv (p2); glNormal3fv (n1);                                 
323             if (m_bColor) {
324               intensityToColor (dIntensity3, vecColor);
325               glColor3fv (vecColor);
326             }
327             glVertex3fv (p3); glNormal3fv (n1);                                 
328             if (m_bColor) {
329               intensityToColor (dIntensity4, vecColor);
330               glColor3fv (vecColor);
331             }
332             glVertex3fv (p4); glNormal3fv (n1);                                                                                                                                         
333             glEnd();                            
334           }                     
335           
336         }
337         glEndList();
338         
339 #ifdef GL_EXT_vertex_array
340   }
341 #endif
342 }
343
344
345 void
346 Graph3dFileView::OnProperties (wxCommandEvent& event)
347 {
348   std::ostringstream os;
349   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
350   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
351   dialogMsg.ShowModal();
352 }
353
354 void
355 Graph3dFileView::OnLighting (wxCommandEvent& event)
356 {
357   m_bLighting = ! m_bLighting;
358   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
359   
360   m_pCanvas->Refresh();
361 }
362
363 void
364 Graph3dFileView::OnSurface (wxCommandEvent& event)
365 {
366   m_bSurface = ! m_bSurface;
367   m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
368   m_pCanvas->Refresh();
369 }
370
371 void
372 Graph3dFileView::OnColor (wxCommandEvent& event)
373 {
374   m_bColor = ! m_bColor;
375   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
376   m_pCanvas->Refresh();
377 }
378
379 void
380 Graph3dFileView::OnSmooth (wxCommandEvent& event)
381 {
382   m_bSmooth = ! m_bSmooth;
383   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
384   m_pCanvas->Refresh();
385 }
386
387
388
389 void 
390 Graph3dFileView::OnDraw (wxDC* dc)
391 {
392 #ifndef __WXMOTIF__
393   if (! m_pCanvas->GetContext()) return;
394 #endif
395   
396   m_pCanvas->SwapBuffers();
397   Draw();
398   std::ostringstream os;
399   os << "Xangle=" << m_dXRotate << ", Yangle=" << m_dYRotate << ", Zangle=" << m_dZRotate;
400   m_pStatusBar->SetStatusText (os.str().c_str());
401   m_pCanvas->SwapBuffers();
402 }
403
404
405 void 
406 Graph3dFileView::Draw ()
407 {
408   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
409   glPushMatrix();
410   DrawSurface();
411   
412   glPopMatrix();
413   glFlush();
414 }
415
416
417 void
418 Graph3dFileView::InitMaterials()
419 {
420   if (! GetDocument())
421     return;
422   int nx = GetDocument()->nx();
423   int ny = GetDocument()->ny();
424   
425 #if 1
426   static float ambient[] = {0.1, 0.1, 0.1, 1.0};
427   static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
428   static float position0[] = {-nx/2, -ny/2, -ny/2, 0, 0.0};
429   static float position1[] = {-nx/2, -ny/2, ny/2, 0.0};
430   static float ambient1[] = {0.5, 0.5, 0.5, 1.0};
431   static float diffuse1[] = {1.0, 1.0, 1.0, 1.0};
432   //  static float position0[] = {0.0, 0.0, 20.0, 0.0};
433   //  static float position1[] = {0.0, 0.0, -20.0, 0.0};
434   static float front_mat_shininess[] = {5.0};
435   static float front_mat_specular[] = {0.1, 0.1, 0.1, 1.0};
436   static float front_mat_diffuse[] = {0.3, 0.3, 0.3, 1.0};
437   /*
438   static float back_mat_shininess[] = {60.0};
439   static float back_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
440   static float back_mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
441   */
442   static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
443   static float lmodel_twoside[] = {GL_FALSE};
444   
445   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
446   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
447   glEnable(GL_NORMALIZE);
448   
449   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
450   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
451   glLightfv (GL_LIGHT0, GL_POSITION, position0);
452   glEnable (GL_LIGHT0);
453   
454   glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
455   glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
456   glLightfv (GL_LIGHT1, GL_POSITION, position1);
457   glEnable (GL_LIGHT1);
458   
459   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
460   glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
461   
462   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
463   glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
464   glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
465   
466   glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
467   //  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
468   glEnable(GL_COLOR_MATERIAL);
469 #else
470   GLfloat impLPos[]  = {1.0, 1.0, 1.0, 0.0};
471   
472   GLfloat defaultLightAmb   [] = {.2, .2, .2, 1.0};
473   GLfloat defaultLightDiff  [] = {.2, .2, .2, 1.0};
474   GLfloat defaultLightSpec  [] = { .3, .3, .3, 1.0};
475   
476   GLfloat defaultGlobalAmb [] = {.3, .3, .3, 1.0};
477   GLfloat defaultGlobalDiff[] = {.3, .3, .3, 1.0};
478   
479   GLfloat defaultMatShine[] = {  30.0 };
480   GLfloat defaultMatSpec[]  = { .4, .4, .4, 1.0};
481   GLfloat defaultMatAmb[]   = { .3, .3, .3, 1.0};
482   GLfloat defaultMatDiff[]  = { .5, .5, .5, 1.0};
483   
484   GLfloat brassMatAmb[]   = { .33, .22, .03, 1.0};
485   GLfloat brassMatDiff[]  = { .78, .57, .11, 1.0};
486   GLfloat brassMatSpec[]  = { .99, .91, .81, 1.0};
487   GLfloat brassMatShine[] = {  27.8 };
488   
489   GLfloat emeraldMatAmb[]   = { .021, .1745 , .021, 1.0};
490   GLfloat emeraldMatDiff[]  = { .075, .6142 , .075, 1.0 };
491   GLfloat emeraldMatSpec[]  = { .633, .7278 , .633, 1.0 };
492   GLfloat emeraldMatShine[] = {  76.8 };
493   
494   GLfloat slateMatAmb[]   = { .02, .02 , .02, 1.0 };
495   GLfloat slateMatDiff[]  = { .02, .01 , .01, 1.0 };
496   GLfloat slateMatSpec[]  = { .4,  .4 ,  .4 , 1.0 };
497   GLfloat slateMatShine[] = { .768 };
498   
499   //       double opnX = nx, opnY = ny, opnZ = z;
500   //       eyeX = 1; eyeY = 1, eyeZ = 1;
501   
502   impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
503   //opnListNum = 1;
504   //impGraphicsFlag = IMP__3D;
505   
506   //       glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
507   //       glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
508   //  glutInitWindowPosition (100, 100);
509   //       glutCreateWindow ("- imp3D graphics -" );
510   
511   glClearColor(0.0, 0.0, 0.0, 0.0);
512   
513   glShadeModel (GL_SMOOTH);
514   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
515   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
516   glEnable(GL_NORMALIZE);
517   
518   
519   glEnable(GL_DEPTH_TEST);
520   
521   glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
522   glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
523   glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
524   
525   glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
526   glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
527   glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
528   
529   glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
530   glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
531   glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
532   
533   glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
534   glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
535   glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
536   
537   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT  , defaultMatAmb);
538   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE  , defaultMatDiff);
539   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
540   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
541   
542   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
543   
544   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
545   
546   glEnable(GL_COLOR_MATERIAL);
547   
548   glEnable(GL_LIGHTING);
549   glEnable(GL_LIGHT1);
550   glEnable(GL_LIGHT2);
551   glEnable(GL_LIGHT0);
552 #endif
553   
554 }
555
556
557 void 
558 Graph3dFileView::InitGL ()
559 {
560   glClearColor(0.0, 0.0, 0.0, 0.0);
561   
562   glEnable(GL_DEPTH_TEST);
563   
564 }
565
566 void 
567 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
568 {
569   int nVertices = GetDocument()->m_nVertices;
570   glTripleFloat* pVertices = GetDocument()->m_pVertices;
571   glTripleFloat* pNormals = GetDocument()->m_pNormals;
572   
573   glMatrixMode(GL_PROJECTION);
574   glLoadIdentity();
575   if (! GetDocument())
576     return;
577   int nx = GetDocument()->nx();
578   int ny = GetDocument()->ny();
579   int maxDim = maxValue<int> (nx, ny);
580   
581   glOrtho (-maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71, maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71);
582   GLfloat eyep[3], lookp[3], up[3];
583   eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
584   lookp[1] = 0; lookp[1] = 0, lookp[2] = 0;
585   up[0] = 0; up[1] = 1; up[2] = 0;
586   //gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
587   glMatrixMode(GL_MODELVIEW);
588   glLoadIdentity();
589   
590   
591 #ifdef GL_EXT_vertex_array
592   if (m_bUseVertexArrays) {
593     //  glVertexPointerEXT( 3, GL_FLOAT, 0, nVertices, pVertices );
594     //  glNormalPointerEXT( GL_FLOAT, 0, nVertices, pNormals );
595     glEnable( GL_VERTEX_ARRAY_EXT );
596     glEnable( GL_NORMAL_ARRAY_EXT );
597   }
598 #endif
599   
600   if (m_pCanvas)
601     m_pCanvas->Refresh();
602 }
603
604 bool 
605 Graph3dFileView::OnClose (bool deleteWindow)
606 {
607   if (! GetDocument() || ! GetDocument()->Close())
608     return false;
609   
610   Activate (false);
611   if (m_pCanvas) {
612     //    m_pCanvas->Show(false);
613     m_pCanvas->setView(NULL);
614     m_pCanvas = NULL;
615   }
616   wxString s(theApp->GetAppName());
617   if (m_pFrame)
618     m_pFrame->SetTitle(s);
619   
620   SetFrame(NULL);
621   
622   if (deleteWindow) {
623     //    m_pFrame->Show(false);
624     delete m_pFrame;
625     m_pFrame = NULL;
626     //    if (GetDocument() && GetDocument()->getBadFileOpen())
627     //      ::wxYield();  // wxWindows bug workaround
628   }
629   
630   return true;
631 }
632
633 #if CTSIM_MDI
634 wxDocMDIChildFrame*
635 #else
636 wxDocChildFrame*
637 #endif
638 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
639 {
640 #if CTSIM_MDI
641   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
642 #else
643   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
644 #endif
645   theApp->setIconForFrame (subframe);
646   
647   m_pStatusBar = new wxStatusBar (subframe, -1);
648   subframe->SetStatusBar (m_pStatusBar);
649   
650   m_pFileMenu = new wxMenu;
651   
652   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
653   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
654   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
655   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
656   
657   m_pFileMenu->AppendSeparator();
658   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
659   
660   m_pFileMenu->AppendSeparator();
661   m_pFileMenu->Append(wxID_PRINT, "&Print...");
662   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
663   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
664 #ifdef CTSIM_MDI
665   m_pFileMenu->AppendSeparator();
666   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
667 #endif
668   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
669   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
670   
671   m_pViewMenu = new wxMenu;
672   m_pViewMenu->Append(GRAPH3D_VIEW_SURFACE, "&Surface\tCtrl-U", "", true);
673   m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "&Smooth\tCtrl-M", "", true);
674   m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "&Color\tCtrl-R", "", true);
675   m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "&Lighting\tCtrl-L", "", true);
676   
677   wxMenu *help_menu = new wxMenu;
678   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
679   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
680   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
681   
682   wxMenuBar *menu_bar = new wxMenuBar;
683   
684   menu_bar->Append(m_pFileMenu, "&File");
685   menu_bar->Append(m_pViewMenu, "&View");
686   menu_bar->Append(help_menu, "&Help");
687   
688   subframe->SetMenuBar(menu_bar);
689   
690   subframe->Centre(wxBOTH);
691   
692   wxAcceleratorEntry accelEntries[10];
693   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
694   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
695   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
696   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
697   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
698   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SURFACE);
699   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_COLOR);
700   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_LIGHTING);
701   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
702   wxAcceleratorTable accelTable (9, accelEntries);
703   subframe->SetAcceleratorTable (accelTable);
704   
705   return subframe;
706 }
707
708
709
710 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
711 EVT_SIZE(Graph3dFileCanvas::OnSize)
712 EVT_PAINT(Graph3dFileCanvas::OnPaint)
713 EVT_CHAR(Graph3dFileCanvas::OnChar)
714 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
715 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
716 END_EVENT_TABLE()
717
718
719
720
721 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
722                                       const wxSize& size, long style, int* gl_attrib):
723 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas"), gl_attrib), m_pView(view)
724 {
725   parent->Show(TRUE);
726   SetCurrent();
727   /* Make sure server supports the vertex array extension */
728   char* extensions = (char *) glGetString( GL_EXTENSIONS );
729   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
730     m_pView->m_bUseVertexArrays = GL_FALSE;
731   }
732 }
733
734
735 Graph3dFileCanvas::~Graph3dFileCanvas(void)
736 {
737   m_pView = NULL;
738 }
739
740 void 
741 Graph3dFileCanvas::OnDraw(wxDC& dc)
742 {
743   if (m_pView)
744     m_pView->OnDraw(& dc);
745 }
746
747 void 
748 Graph3dFileCanvas::OnSize(wxSizeEvent& event)
749 {
750 #ifndef __WXMOTIF__
751   if (!GetContext()) return;
752 #endif
753   
754   SetCurrent();
755   int width, height;
756   GetClientSize (&width, &height);
757   Reshape (width, height);
758 }
759
760 void 
761 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
762 {
763   if (! m_pView)
764     return;
765   
766   wxCommandEvent dummyEvent;
767   switch (event.KeyCode()) {
768   case WXK_LEFT:
769         m_pView->m_dYRotate -= 15.0;
770     break;
771   case WXK_RIGHT:
772     m_pView->m_dYRotate += 15.0;
773     break;
774   case WXK_UP:
775     m_pView->m_dXRotate += 15.0;
776     break;
777   case WXK_DOWN:
778     m_pView->m_dXRotate -= 15.0;
779     break;
780   case 'z': case 'Z':
781     m_pView->m_dZRotate += 15.0;
782     break;
783   case 'x': case 'X':
784     m_pView->m_dZRotate -= 15.0;
785     break;
786   case 's': case 'S':
787     m_pView->OnSmooth (dummyEvent);
788     break;
789   case 'l': case 'L':
790     m_pView->OnLighting (dummyEvent);
791     break;
792   case 'c': case 'C':
793     m_pView->OnColor (dummyEvent);
794     break;
795   default:
796     {
797       event.Skip();
798       return;
799     }
800   }
801   
802   Refresh (false);
803 }
804
805 void
806 Graph3dFileCanvas::Reshape (int width, int height)
807 {
808   glViewport (0, 0, (GLint)width, (GLint)height);
809 }
810
811
812 void 
813 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
814 {
815   static int dragging = 0;
816   static float last_x, last_y;
817   
818   if (! m_pView)
819     return;
820   
821   if(event.LeftIsDown()) {
822     if(! dragging) {
823       dragging = 1;
824     } else {
825       m_pView->m_dXRotate += (event.GetX() - last_x)*1.0;
826       m_pView->m_dYRotate += (event.GetY() - last_y)*1.0;
827       Refresh(FALSE);
828     }
829     last_x = event.GetX();
830     last_y = event.GetY();
831   } else
832     dragging = 0;
833 }
834
835 void 
836 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
837 {
838   wxGLCanvas::OnEraseBackground(event); // Do nothing: avoid flashing.
839 }
840
841
842
843
844 #endif // wxUSE_GLCANVAS