r1023: 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.24 2001/09/24 11:34:06 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 #include "wx/wxprec.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #endif
38
39 #if wxUSE_GLCANVAS
40
41 #if !wxUSE_GLCANVAS
42 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
43 #endif
44
45 #include "wx/timer.h"
46 #include "wx/glcanvas.h"
47
48 #include <GL/gl.h>
49 #include <GL/glu.h>
50
51 #include "ct.h"
52 #include "ctsim.h"
53 #include "docs.h"
54 #include "views.h"
55 #include "dialogs.h"
56 #include "dlgprojections.h"
57 #include "dlgreconstruct.h"
58 #include "backprojectors.h"
59 #include "reconstruct.h"
60 #include "timer.h"
61
62 #if defined(MSVC) || HAVE_SSTREAM
63 #include <sstream>
64 #else
65 #include <sstream_subst>
66 #endif
67
68 void 
69 Graph3dFileView::intensityToColor (double dIntensity, GLfloat* vecColor)
70 {
71   if (dIntensity < 0 || dIntensity > 1) {
72     vecColor[0] = vecColor[1] = vecColor[2] = 1;
73     return;
74   }
75   
76   float fRange = dIntensity * 5;
77   int iRange = static_cast<int>(floor (fRange));
78   float fFrac = fRange - iRange;
79   
80   // 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)
81   switch (iRange) {
82   case 0:
83     vecColor[0] = 1.0f - fFrac; vecColor[1] = 0.0f; vecColor[2] = 1.0f;
84     break;
85   case 1:
86     vecColor[0] = 0.0f; vecColor[1] = fFrac; vecColor[2] = 1.0f;
87     break;
88   case 2:
89     vecColor[0] = 0.0f; vecColor[1] = 1.0f; vecColor[2] = 1.0f - fFrac;
90     break;
91   case 3:
92     vecColor[0] = fFrac; vecColor[1] = 1.0f; vecColor[2] = 0.0f;
93     break;
94   case 4:
95     vecColor[0] = 1.0f; vecColor[1] = 1.0f - fFrac; vecColor[2] = 0.0f;
96     break;
97   case 5:
98     vecColor[0] = 1.0f; vecColor[1] = 0.0f; vecColor[2] = 0.0f;
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 template<class T>
121 static void 
122 CalculateVectorNormal (T fVert1[], T fVert2[], T fVert3[], T *fNormalX, T *fNormalY, T *fNormalZ)
123 {
124   T Qx = fVert2[0] - fVert1[0];
125   T Qy = fVert2[1] - fVert1[1];
126   T Qz = fVert2[2] - fVert1[2];
127   T Px = fVert3[0] - fVert1[0];
128   T Py = fVert3[1] - fVert1[1];
129   T Pz = fVert3[2] - fVert1[2];
130   
131   *fNormalX = Py*Qz - Pz*Qy;
132   *fNormalY = Pz*Qx - Px*Qz;
133   *fNormalZ = Px*Qy - Py*Qx;
134
135
136 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
137
138 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
139 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
140 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
141 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
142 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
143 EVT_MENU(GRAPH3D_VIEW_WIREFRAME, Graph3dFileView::OnWireframe)
144 EVT_MENU(GRAPH3D_VIEW_SCALE_MINMAX, Graph3dFileView::OnScaleSet)
145 EVT_MENU(GRAPH3D_VIEW_SCALE_AUTO, Graph3dFileView::OnScaleAuto)
146 EVT_MENU(GRAPH3D_VIEW_SCALE_FULL, Graph3dFileView::OnScaleFull)
147 END_EVENT_TABLE()
148
149 Graph3dFileView::Graph3dFileView ()
150   : m_pFileMenu(NULL), m_pViewMenu(NULL), m_pCanvas(NULL), m_pFrame(NULL)
151 {
152   m_bDoubleBuffer = true;
153   m_bSmooth = true;
154   m_bLighting = true;
155   m_bWireframe = false;
156   m_bLighting = true;
157   m_bColor = true;
158   m_dXRotate = -180;
159   m_dYRotate = -210;
160   m_dZRotate = -195;
161   m_bColorScaleMinSet = false;
162   m_bColorScaleMaxSet = false;
163 }
164
165 Graph3dFileView::~Graph3dFileView()
166 {
167   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
168   GetDocumentManager()->ActivateView(this, false, true);
169 }
170
171 bool 
172 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
173 {
174   m_pFrame = CreateChildFrame(doc, this);
175   
176   int width, height;
177   m_pFrame->GetClientSize (&width, &height);
178   m_pFrame->SetTitle("Graph3dFileView");
179   m_pCanvas = CreateCanvas (m_pFrame);
180   
181   m_pFrame->Show (true);
182   m_pCanvas->SetCurrent();
183   
184   InitGL();
185   
186   int x, y;  // X requires a forced resize
187   m_pFrame->GetSize(&x, &y);
188   m_pFrame->SetSize(-1, -1, x, y);
189   m_pFrame->SetFocus();
190   m_pFrame->Show(true);
191   Activate(true);
192   
193   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
194   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
195   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
196   m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
197   return true;
198
199
200 Graph3dFileCanvas* 
201 Graph3dFileView::CreateCanvas (wxFrame* parent)
202 {
203   Graph3dFileCanvas* pCanvas;
204   int width, height;
205   parent->GetClientSize (&width, &height);
206   
207   pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0);
208   
209   pCanvas->SetBackgroundColour(*wxWHITE);
210   pCanvas->Clear();
211   
212   return pCanvas;
213 }
214
215
216 void
217 Graph3dFileView::DrawSurface()
218 {
219   if (! GetDocument())
220     return;
221   
222   if (m_bSmooth) {
223     glShadeModel (GL_SMOOTH);
224   } else {
225     glShadeModel (GL_FLAT);
226   }
227   
228   if (m_bLighting) {
229     glEnable (GL_LIGHTING);
230   } else {
231     glDisable (GL_LIGHTING);
232   }
233   
234   unsigned int nx = GetDocument()->nx();
235   unsigned int ny = GetDocument()->ny();
236
237   glRotated (m_dZRotate, 0.0, 1.0, 0.0);
238   glRotated (m_dXRotate, 0.0, 0.0, 1.0);
239   glRotated (m_dYRotate, 1.0, 0.0, 0.0);
240   glTranslated (-static_cast<double>(nx - 1) / 2, 0.0, -static_cast<double>(ny - 1) / 2);
241
242   InitMaterials();   
243   if (m_bWireframe) {
244     if (! m_bColor)
245       glColor3f (1.0f, 1.0f, 1.0f);
246     glPolygonOffset (1.0f, 1.0f);
247     glEnable (GL_POLYGON_OFFSET_FILL);
248     glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
249     glCallList (DISPLAYLIST_COLOR);
250
251     glColor3f (0.0f, 0.0f, 0.0f);
252     glPolygonOffset (0.0f, 0.0f);
253     glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
254     glCallList (DISPLAYLIST_NO_COLOR);
255   } else {
256     glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
257     if (! m_bColor) {
258       glColor3f (1.0f, 1.0f, 1.0f);
259       glCallList (DISPLAYLIST_NO_COLOR);
260     } else
261       glCallList (DISPLAYLIST_COLOR);
262   }
263   
264 }
265
266 void
267 Graph3dFileView::CreateDisplayList()
268 {
269   if (! GetDocument())
270     return;
271   
272   unsigned int nx = GetDocument()->nx();
273   unsigned int ny = GetDocument()->ny();
274   const ImageFileArrayConst v = GetDocument()->getArray();
275   if (nx == 0 || ny == 0 || ! v)
276     return;
277   
278   glNewList (DISPLAYLIST_COLOR, GL_COMPILE);
279   
280   double dMin = m_dColorScaleMin;
281   double dIntensityScale = m_dColorScaleMax - m_dColorScaleMin;
282   double actOffset = m_dGraphMin;
283   double actScale = 0.4 * sqrt(nx*nx+ny*ny) / (m_dGraphMax - m_dGraphMin);
284   double dXOffset = -(static_cast<double>(nx) - 1) / 2.;
285   double dYOffset = -(static_cast<double>(ny) - 1) / 2.;
286   dXOffset = 0;
287   dYOffset = 0;
288
289   double dXPos = -dXOffset;
290   unsigned int ix;
291   for (ix = 0; ix < nx - 1; ix++, dXPos++) {
292     double dYPos = -dYOffset;
293     glBegin(GL_QUAD_STRIP);
294     double p1[3], p2[3], p3[3], n1[3]; 
295     p1[0] = dXPos;  p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
296     p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos; 
297     p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
298     CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
299
300     double dIntensity1, dIntensity2;
301     if (m_bColor) {
302       dIntensity1 = (v[ix][0] - dMin) / dIntensityScale;
303       dIntensity2 = (v[ix+1][0] - dMin) / dIntensityScale;
304     }
305     float vecColor[3];
306     if (m_bColor) {
307       intensityToColor (dIntensity1, vecColor);
308       glColor3fv (vecColor);
309     }
310     glVertex3dv (p1); 
311     glNormal3dv (n1);                                   
312       intensityToColor (dIntensity2, vecColor);
313       glColor3fv (vecColor);
314     glVertex3dv (p2); 
315     glNormal3dv (n1);                                   
316     double lastP[3];
317     lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0; 
318     for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {       
319       p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
320       p2[0] = dXPos+1;  p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
321       CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
322       lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
323         dIntensity1 = (v[ix][iy] - dMin) / dIntensityScale;
324         dIntensity2 = (v[ix+1][iy] - dMin) / dIntensityScale;
325         intensityToColor (dIntensity1, vecColor);
326         glColor3fv (vecColor);
327       glVertex3dv (p1); glNormal3dv (n1);                                       
328         intensityToColor (dIntensity2, vecColor);
329         glColor3fv (vecColor);
330       glVertex3dv (p2); glNormal3dv (n1);                                       
331     }                   
332     glEnd(); // QUAD_STRIP
333   }
334   glEndList();
335
336
337   glNewList (DISPLAYLIST_NO_COLOR, GL_COMPILE);
338   dXPos = -dXOffset;
339   for (ix = 0; ix < nx - 1; ix++, dXPos++) {
340     double dYPos = -dYOffset;
341     glBegin(GL_QUAD_STRIP);
342     double p1[3], p2[3], p3[3], n1[3]; 
343     p1[0] = dXPos;  p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
344     p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos; 
345     p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
346     CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
347
348     glVertex3dv (p1); 
349     glNormal3dv (n1);                                   
350     glVertex3dv (p2); 
351     glNormal3dv (n1);                                   
352     double lastP[3];
353     lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0; 
354     for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {       
355       p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
356       p2[0] = dXPos+1;  p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
357       CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
358       lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
359       glVertex3dv (p1); glNormal3dv (n1);                                       
360       glVertex3dv (p2); glNormal3dv (n1);                                       
361     }                   
362     glEnd(); // QUAD_STRIP
363   }
364   glEndList();
365 }
366
367
368 void
369 Graph3dFileView::OnProperties (wxCommandEvent& event)
370 {
371   std::ostringstream os;
372   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
373   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
374   dialogMsg.ShowModal();
375 }
376
377 void
378 Graph3dFileView::OnLighting (wxCommandEvent& event)
379 {
380   m_bLighting = ! m_bLighting;
381   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
382   
383   m_pCanvas->Refresh();
384 }
385
386 void
387 Graph3dFileView::OnWireframe (wxCommandEvent& event)
388 {
389   m_bWireframe = ! m_bWireframe;
390   m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
391   m_pCanvas->Refresh();
392 }
393
394 void
395 Graph3dFileView::OnColor (wxCommandEvent& event)
396 {
397   m_bColor = ! m_bColor;
398   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
399   m_pCanvas->Refresh();
400 }
401
402 void
403 Graph3dFileView::OnSmooth (wxCommandEvent& event)
404 {
405   m_bSmooth = ! m_bSmooth;
406   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
407   m_pCanvas->Refresh();
408 }
409
410
411
412 void 
413 Graph3dFileView::OnDraw (wxDC* dc)
414 {
415   if (! m_pCanvas)
416     return;
417   m_pCanvas->SetCurrent();
418
419 #ifndef __WXMOTIF__
420   //  if (! m_pCanvas->GetContext()) return;
421 #endif
422   
423   Draw();
424   std::ostringstream os;
425   os << "Xangle=" << m_dXRotate << ", Yangle=" << m_dYRotate << ", Zangle=" << m_dZRotate;
426   m_pStatusBar->SetStatusText (os.str().c_str());
427   m_pCanvas->SwapBuffers();
428 }
429
430
431 void 
432 Graph3dFileView::Draw ()
433 {
434   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
435   glPushMatrix();
436   DrawSurface();  
437   glPopMatrix();
438   glFlush();
439 }
440
441
442 void
443 Graph3dFileView::InitMaterials()
444 {
445   if (! GetDocument())
446     return;
447   int nx = GetDocument()->nx();
448   int ny = GetDocument()->ny();
449   
450 #if 1
451   static float ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
452   static float diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
453   static float position0[] = {-nx/2, -ny/2, ny/2, 0.0f, 0.0f};
454   static float position1[] = {-nx/2, -ny/2, -ny/2, 0.0f};
455   static float ambient1[] = {0.5f, 0.5f, 0.5f, 1.0f};
456   static float diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};
457   //  static float position0[] = {0.0f, 0.0f, 20.0f, 0.0f};
458   //  static float position1[] = {0.0f, 0.0f, -20.0f, 0.0f};
459   static float front_mat_shininess[] = {5.0f};
460   static float front_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
461   static float front_mat_diffuse[] = {0.3f, 0.3f, 0.3f, 1.0f};
462   /*
463   static float back_mat_shininess[] = {60.0f};
464   static float back_mat_specular[] = {0.2f, 0.2f, 0.2f, 1.0f};
465   static float back_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
466   */
467   static float lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
468   static float lmodel_twoside[] = {GL_FALSE};
469   
470   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
471   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
472   glEnable(GL_NORMALIZE);
473   
474   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
475   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
476   glLightfv (GL_LIGHT0, GL_POSITION, position0);
477   glEnable (GL_LIGHT0);
478   
479   glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
480   glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
481   glLightfv (GL_LIGHT1, GL_POSITION, position1);
482   glEnable (GL_LIGHT1);
483   
484   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
485   glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
486   
487   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
488   glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
489   glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
490   
491   glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
492   //  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
493   glEnable(GL_COLOR_MATERIAL);
494 #else
495   GLfloat impLPos[]  = {1.0f, 1.0f, 1.0f, 0.0f};
496   
497   GLfloat defaultLightAmb   [] = {.2f, .2f, .2f, 1.0f};
498   GLfloat defaultLightDiff  [] = {.2f, .2f, .2f, 1.0f};
499   GLfloat defaultLightSpec  [] = { .3f, .3f, .3f, 1.0f};
500   
501   GLfloat defaultGlobalAmb [] = {.3f, .3f, .3f, 1.0f};
502   GLfloat defaultGlobalDiff[] = {.3f, .3f, .3f, 1.0f};
503   
504   GLfloat defaultMatShine[] = {  30.0f };
505   GLfloat defaultMatSpec[]  = { .4f, .4f, .4f, 1.0f};
506   GLfloat defaultMatAmb[]   = { .3f, .3f, .3f, 1.0f};
507   GLfloat defaultMatDiff[]  = { .5f, .5f, .5f, 1.0f};
508   
509   GLfloat brassMatAmb[]   = { .33f, .22f, .03f, 1.0f};
510   GLfloat brassMatDiff[]  = { .78f, .57f, .11f, 1.0f};
511   GLfloat brassMatSpec[]  = { .99f, .91f, .81f, 1.0f};
512   GLfloat brassMatShine[] = {  27.8f };
513   
514   GLfloat emeraldMatAmb[]   = { .02f1, .1745f , .021f, 1.0f };
515   GLfloat emeraldMatDiff[]  = { .075f, .6142f , .075f, 1.0f };
516   GLfloat emeraldMatSpec[]  = { .633f, .7278f , .633f, 1.0f };
517   GLfloat emeraldMatShine[] = {  76.8f };
518   
519   GLfloat slateMatAmb[]   = { .02f, .02f , .02f, 1.0f };
520   GLfloat slateMatDiff[]  = { .02f, .01f , .01f, 1.0f };
521   GLfloat slateMatSpec[]  = { .4f,  .4f ,  .4f , 1.0f };
522   GLfloat slateMatShine[] = { .768f };
523   
524   //       double opnX = nx, opnY = ny, opnZ = z;
525   //       eyeX = 1; eyeY = 1, eyeZ = 1;
526   
527   impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
528   //opnListNum = 1;
529   //impGraphicsFlag = IMP__3D;
530   
531   //       glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
532   //       glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
533   //  glutInitWindowPosition (100, 100);
534   //       glutCreateWindow ("- imp3D graphics -" );
535   
536   glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
537   
538   glShadeModel (GL_SMOOTH);
539   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
540   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
541   glEnable(GL_NORMALIZE);
542   
543   
544   glEnable(GL_DEPTH_TEST);
545   
546   glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
547   glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
548   glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
549   
550   glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
551   glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
552   glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
553   
554   glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
555   glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
556   glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
557   
558   glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
559   glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
560   glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
561   
562   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT  , defaultMatAmb);
563   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE  , defaultMatDiff);
564   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
565   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
566   
567   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
568   
569   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
570   
571   glEnable(GL_COLOR_MATERIAL);
572   
573   glEnable(GL_LIGHTING);
574   glEnable(GL_LIGHT1);
575   glEnable(GL_LIGHT2);
576   glEnable(GL_LIGHT0);
577 #endif
578   
579 }
580
581
582 void 
583 Graph3dFileView::InitGL ()
584 {
585   glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
586   
587   glDisable (GL_CULL_FACE);
588   glEnable (GL_DEPTH_TEST);
589   
590 }
591
592 void 
593 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
594 {
595   if (! GetDocument())
596     return;
597   
598   unsigned int nx = GetDocument()->nx();
599   unsigned int ny = GetDocument()->ny();
600   const ImageFileArrayConst v = GetDocument()->getArray();
601   if (v != NULL && nx != 0 && ny != 0) {
602     double min = v[0][0];
603     double max = min;
604     for (unsigned int ix = 0; ix < nx; ix++)
605       for (unsigned int iy = 0; iy < ny; iy++) {
606         double dVal = v[ix][iy];
607         if (min > dVal)
608           min = dVal;
609         else if (max < dVal)
610           max = dVal;
611       }
612       m_dGraphMin = min;
613       m_dGraphMax = max;
614       if (! m_bColorScaleMinSet)
615         m_dColorScaleMin = min;
616       if (! m_bColorScaleMaxSet)
617         m_dColorScaleMax = max;  
618   }
619   double dRadius = maxValue<int> (nx, ny) * SQRT2 / 2;
620   glMatrixMode(GL_PROJECTION);
621   glLoadIdentity();
622   glOrtho (-dRadius, dRadius, -dRadius, dRadius, dRadius*5, -dRadius*5);
623   
624   glMatrixMode(GL_MODELVIEW);
625   glLoadIdentity();
626 #if 0
627   GLfloat eyep[3], lookp[3], up[3];
628   eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
629   lookp[0] = 0; lookp[1] = 0, lookp[2] = 0;
630   up[0] = 0; up[1] = 1; up[2] = 0;
631   gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
632 #endif
633   
634   CreateDisplayList();
635   
636   if (m_pCanvas)
637     m_pCanvas->Refresh();
638 }
639
640 bool 
641 Graph3dFileView::OnClose (bool deleteWindow)
642 {
643   if (! GetDocument() || ! GetDocument()->Close())
644     return false;
645   
646   Activate (false);
647   if (m_pCanvas) {
648     m_pCanvas->setView(NULL);
649     m_pCanvas = NULL;
650   }
651   wxString s(theApp->GetAppName());
652   if (m_pFrame)
653     m_pFrame->SetTitle(s);
654   
655   SetFrame(NULL);
656   
657   if (deleteWindow) {
658     delete m_pFrame;
659     m_pFrame = NULL;
660   }
661   
662   return true;
663 }
664
665 void 
666 Graph3dFileView::OnScaleAuto (wxCommandEvent& event)
667 {
668 #if 0
669   const ImageFile& rIF = GetDocument()->getImageFile();
670   double min, max, mean, mode, median, stddev;
671   rIF.statistics(min, max, mean, mode, median, stddev);
672   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
673   int iRetVal = dialogAutoScale.ShowModal();
674   if (iRetVal == wxID_OK) {
675     m_bMinSpecified = true;
676     m_bMaxSpecified = true;
677     double dMin, dMax;
678     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
679       m_dMinPixel = dMin;
680       m_dMaxPixel = dMax;
681       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
682       OnUpdate (this, NULL);
683     }
684   }
685 #endif
686 }
687
688 void 
689 Graph3dFileView::OnScaleSet (wxCommandEvent& event)
690 {
691   if (! GetDocument())
692     return;
693   
694   unsigned int nx = GetDocument()->nx();
695   unsigned int ny = GetDocument()->ny();
696   const ImageFileArrayConst v = GetDocument()->getArray();
697   double dMin, dMax;
698   if (! m_bColorScaleMinSet && ! m_bColorScaleMaxSet) {
699     dMax = dMin = v[0][0];
700     for (unsigned ix = 0; ix < nx; ix++)
701       for (unsigned int iy = 0; iy < ny; iy++)
702         if (v[ix][iy] < dMin)
703           dMin = v[ix][iy];
704         else if (v[ix][iy] > dMax)
705           dMax = v[ix][iy];
706   }
707   if (m_bColorScaleMinSet)
708     dMin = m_dColorScaleMin;
709   if (m_bColorScaleMaxSet)
710     dMax = m_dColorScaleMax;
711   
712   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Color Scale Minimum & Maximum", dMin, dMax);
713   int retVal = dialogMinMax.ShowModal();
714   if (retVal == wxID_OK) {
715     m_bColorScaleMinSet = true;
716     m_bColorScaleMaxSet = true;
717     m_dColorScaleMin = dialogMinMax.getMinimum();
718     m_dColorScaleMax = dialogMinMax.getMaximum();
719     OnUpdate (this, NULL);
720   }
721 }
722
723 void 
724 Graph3dFileView::OnScaleFull (wxCommandEvent& event)
725 {
726   if (m_bColorScaleMinSet || m_bColorScaleMaxSet) {
727     m_bColorScaleMinSet = false;
728     m_bColorScaleMaxSet = false;
729     OnUpdate (this, NULL);
730   }
731 }
732
733 #if CTSIM_MDI
734 wxDocMDIChildFrame*
735 #else
736 wxDocChildFrame*
737 #endif
738 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
739 {
740 #if CTSIM_MDI
741   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
742 #else
743   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
744 #endif
745   theApp->setIconForFrame (subframe);
746   
747   m_pStatusBar = new wxStatusBar (subframe, -1);
748   subframe->SetStatusBar (m_pStatusBar);
749   
750   m_pFileMenu = new wxMenu;
751   
752   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
753   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
754   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
755   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
756   
757   m_pFileMenu->AppendSeparator();
758   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
759   
760   m_pFileMenu->AppendSeparator();
761   m_pFileMenu->Append(wxID_PRINT, "&Print...");
762   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
763   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
764 #ifdef CTSIM_MDI
765   m_pFileMenu->AppendSeparator();
766   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
767 #endif
768   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
769   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
770   
771   m_pViewMenu = new wxMenu;
772   m_pViewMenu->Append(GRAPH3D_VIEW_WIREFRAME, "Wi&reframe\tCtrl-R", "", true);
773   m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "S&mooth\tCtrl-M", "", true);
774   m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "Co&lor\tCtrl-L", "", true);
775   m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "Li&ghting\tCtrl-G", "", true);
776   m_pViewMenu->AppendSeparator();
777   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_MINMAX, "Color Scale S&et Min/Max...\tCtrl-E");
778   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_AUTO, "Color Scale &Auto...\tCtrl-A");
779   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_FULL, "Color F&ull Scale\tCtrl-U");
780   
781   
782   wxMenu *help_menu = new wxMenu;
783   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
784   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
785   
786   wxMenuBar *menu_bar = new wxMenuBar;
787   
788   menu_bar->Append(m_pFileMenu, "&File");
789   menu_bar->Append(m_pViewMenu, "&View");
790   menu_bar->Append(help_menu, "&Help");
791   
792   subframe->SetMenuBar(menu_bar);
793   
794   subframe->Centre(wxBOTH);
795
796   wxAcceleratorEntry accelEntries[7];
797   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_WIREFRAME);
798   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_COLOR);
799   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('G'), GRAPH3D_VIEW_LIGHTING);
800   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
801   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('E'), GRAPH3D_VIEW_SCALE_MINMAX);
802   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('A'), GRAPH3D_VIEW_SCALE_AUTO);
803   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SCALE_FULL);
804   wxAcceleratorTable accelTable (7, accelEntries);
805   subframe->SetAcceleratorTable (accelTable);
806
807   return subframe;
808 }
809
810
811
812 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
813 EVT_SIZE(Graph3dFileCanvas::OnSize)
814 EVT_PAINT(Graph3dFileCanvas::OnPaint)
815 EVT_CHAR(Graph3dFileCanvas::OnChar)
816 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
817 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
818 END_EVENT_TABLE()
819
820
821
822
823 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
824                                       const wxSize& size, long style):
825 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas")
826                         ), m_pView(view)
827 {
828   parent->Show (true);
829   SetCurrent();
830 #if 0
831   // Make sure server supports the vertex array extension 
832   char* extensions = (char *) glGetString( GL_EXTENSIONS );
833   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
834     m_pView->m_bUseVertexArrays = GL_FALSE;
835   }
836 #endif
837 }
838
839
840 Graph3dFileCanvas::~Graph3dFileCanvas()
841 {
842 }
843
844 void 
845 Graph3dFileCanvas::OnDraw (wxDC& dc)
846 {
847   if (m_pView)
848     m_pView->OnDraw(& dc);
849 }
850
851 void 
852 Graph3dFileCanvas::OnSize (wxSizeEvent& event)
853 {
854 #ifndef __WXMOTIF__
855   if (!GetContext()) return;
856 #endif
857   
858   SetCurrent();
859   int width, height;
860   GetClientSize (&width, &height);
861   Reshape (width, height);
862 }
863
864 void 
865 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
866 {
867   if (! m_pView)
868     return;
869   
870   wxCommandEvent dummyEvent;
871   switch (event.KeyCode()) {
872   case WXK_LEFT:
873         m_pView->m_dZRotate += 15.0;
874     Refresh (false);
875     break;
876   case WXK_RIGHT:
877     m_pView->m_dZRotate -= 15.0;
878     Refresh (false);
879     break;
880   case WXK_UP:
881     m_pView->m_dXRotate += 15.0;
882     Refresh (false);
883     break;
884   case WXK_DOWN:
885     m_pView->m_dXRotate -= 15.0;
886     Refresh (false);
887     break;
888   case 'y': case 'Y':
889     m_pView->m_dYRotate += 15.0;
890     Refresh (false);
891     break;
892   case 't': case 'T':
893     m_pView->m_dYRotate -= 15.0;
894     Refresh (false);
895     break;
896   case 'w': case 'W':
897     m_pView->OnWireframe (dummyEvent);
898     break;
899   case 's': case 'S':
900     m_pView->OnSmooth (dummyEvent);
901     break;
902   case 'l': case 'L':
903     m_pView->OnLighting (dummyEvent);
904     break;
905   case 'c': case 'C':
906     m_pView->OnColor (dummyEvent);
907     break;
908   default:
909     event.Skip();
910     return;
911   }
912 }
913
914 void
915 Graph3dFileCanvas::Reshape (int width, int height)
916 {
917   glViewport (0, 0, (GLint)width, (GLint)height);
918 }
919
920
921 void 
922 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
923 {
924   static int dragging = 0;
925   static float last_x, last_y;
926   
927   if (! m_pView)
928     return;
929   
930   if(event.LeftIsDown()) {
931     if(! dragging) {
932       dragging = 1;
933     } else {
934       m_pView->m_dXRotate -= (event.GetY() - last_y)*1.0;
935       m_pView->m_dZRotate += (event.GetX() - last_x)*1.0;
936       Refresh (false);
937     }
938     last_x = event.GetX();
939     last_y = event.GetY();
940   } else
941     dragging = 0;
942 }
943
944 void 
945 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
946 {
947   // Do nothing: avoid flashing.
948 }
949
950
951 #endif // wxUSE_GLCANVAS