r481: 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.6 2001/02/02 21:50:18 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         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/2, 0, 0.0};
427   static float position1[] = {0, 0, nx/2, 0.0};
428   static float ambient1[] = {0.5, 0.5, 0.5, 1.0};
429   static float diffuse1[] = {1.0, 1.0, 1.0, 1.0};
430   //  static float position0[] = {0.0, 0.0, 20.0, 0.0};
431   //  static float position1[] = {0.0, 0.0, -20.0, 0.0};
432   static float front_mat_shininess[] = {5.0};
433   static float front_mat_specular[] = {0.1, 0.1, 0.1, 1.0};
434   static float front_mat_diffuse[] = {0.3, 0.3, 0.3, 1.0};
435   /*
436   static float back_mat_shininess[] = {60.0};
437   static float back_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
438   static float back_mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
439   */
440   static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
441   static float lmodel_twoside[] = {GL_FALSE};
442   
443   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
444   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
445   glEnable(GL_NORMALIZE);
446   
447   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
448   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
449   glLightfv (GL_LIGHT0, GL_POSITION, position0);
450   glEnable (GL_LIGHT0);
451   
452   glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
453   glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
454   glLightfv (GL_LIGHT1, GL_POSITION, position1);
455   glEnable (GL_LIGHT1);
456   
457   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
458   glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
459   
460   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
461   glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
462   glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
463   
464   glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
465   //  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
466   glEnable(GL_COLOR_MATERIAL);
467 #else
468   GLfloat impLPos[]  = {1.0, 1.0, 1.0, 0.0};
469   
470   GLfloat defaultLightAmb   [] = {.2, .2, .2, 1.0};
471   GLfloat defaultLightDiff  [] = {.2, .2, .2, 1.0};
472   GLfloat defaultLightSpec  [] = { .3, .3, .3, 1.0};
473   
474   GLfloat defaultGlobalAmb [] = {.3, .3, .3, 1.0};
475   GLfloat defaultGlobalDiff[] = {.3, .3, .3, 1.0};
476   
477   GLfloat defaultMatShine[] = {  30.0 };
478   GLfloat defaultMatSpec[]  = { .4, .4, .4, 1.0};
479   GLfloat defaultMatAmb[]   = { .3, .3, .3, 1.0};
480   GLfloat defaultMatDiff[]  = { .5, .5, .5, 1.0};
481   
482   GLfloat brassMatAmb[]   = { .33, .22, .03, 1.0};
483   GLfloat brassMatDiff[]  = { .78, .57, .11, 1.0};
484   GLfloat brassMatSpec[]  = { .99, .91, .81, 1.0};
485   GLfloat brassMatShine[] = {  27.8 };
486   
487   GLfloat emeraldMatAmb[]   = { .021, .1745 , .021, 1.0};
488   GLfloat emeraldMatDiff[]  = { .075, .6142 , .075, 1.0 };
489   GLfloat emeraldMatSpec[]  = { .633, .7278 , .633, 1.0 };
490   GLfloat emeraldMatShine[] = {  76.8 };
491   
492   GLfloat slateMatAmb[]   = { .02, .02 , .02, 1.0 };
493   GLfloat slateMatDiff[]  = { .02, .01 , .01, 1.0 };
494   GLfloat slateMatSpec[]  = { .4,  .4 ,  .4 , 1.0 };
495   GLfloat slateMatShine[] = { .768 };
496   
497   //       double opnX = nx, opnY = ny, opnZ = z;
498   //       eyeX = 1; eyeY = 1, eyeZ = 1;
499   
500   impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
501   //opnListNum = 1;
502   //impGraphicsFlag = IMP__3D;
503   
504   //       glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
505   //       glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
506   //  glutInitWindowPosition (100, 100);
507   //       glutCreateWindow ("- imp3D graphics -" );
508   
509   glClearColor(0.0, 0.0, 0.0, 0.0);
510   
511   glShadeModel (GL_SMOOTH);
512   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
513   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
514   glEnable(GL_NORMALIZE);
515   
516   
517   glEnable(GL_DEPTH_TEST);
518   
519   glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
520   glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
521   glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
522   
523   glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
524   glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
525   glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
526   
527   glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
528   glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
529   glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
530   
531   glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
532   glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
533   glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
534   
535   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT  , defaultMatAmb);
536   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE  , defaultMatDiff);
537   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
538   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
539   
540   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
541   
542   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
543   
544   glEnable(GL_COLOR_MATERIAL);
545   
546   glEnable(GL_LIGHTING);
547   glEnable(GL_LIGHT1);
548   glEnable(GL_LIGHT2);
549   glEnable(GL_LIGHT0);
550 #endif
551   
552 }
553
554
555 void 
556 Graph3dFileView::InitGL ()
557 {
558   glClearColor(0.0, 0.0, 0.0, 0.0);
559   
560   glEnable(GL_DEPTH_TEST);
561   
562 }
563
564 void 
565 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
566 {
567   int nVertices = GetDocument()->m_nVertices;
568   glTripleFloat* pVertices = GetDocument()->m_pVertices;
569   glTripleFloat* pNormals = GetDocument()->m_pNormals;
570   
571   glMatrixMode(GL_PROJECTION);
572   glLoadIdentity();
573   if (! GetDocument())
574     return;
575   int nx = GetDocument()->nx();
576   int ny = GetDocument()->ny();
577   int maxDim = maxValue<int> (nx, ny);
578   
579   glOrtho (-maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71, maxDim * 0.71, maxDim * 0.71, -maxDim * 0.71);
580   glMatrixMode(GL_MODELVIEW);
581   glLoadIdentity();
582   
583   
584 #ifdef GL_EXT_vertex_array
585   if (m_bUseVertexArrays) {
586     //  glVertexPointerEXT( 3, GL_FLOAT, 0, nVertices, pVertices );
587     //  glNormalPointerEXT( GL_FLOAT, 0, nVertices, pNormals );
588     glEnable( GL_VERTEX_ARRAY_EXT );
589     glEnable( GL_NORMAL_ARRAY_EXT );
590   }
591 #endif
592   
593   if (m_pCanvas)
594     m_pCanvas->Refresh();
595 }
596
597 bool 
598 Graph3dFileView::OnClose (bool deleteWindow)
599 {
600   if (! GetDocument() || ! GetDocument()->Close())
601     return false;
602   
603   Activate (false);
604   if (m_pCanvas) {
605 //    m_pCanvas->Show(false);
606     m_pCanvas->setView(NULL);
607     m_pCanvas = NULL;
608   }
609   wxString s(theApp->GetAppName());
610   if (m_pFrame)
611     m_pFrame->SetTitle(s);
612   
613   SetFrame(NULL);
614   
615   if (deleteWindow) {
616 //    m_pFrame->Show(false);
617     delete m_pFrame; //->Destroy();
618     m_pFrame = NULL;
619 //    if (GetDocument() && GetDocument()->getBadFileOpen())
620 //      ::wxYield();  // wxWindows bug workaround
621   }
622   
623   return true;
624 }
625
626 #if CTSIM_MDI
627 wxDocMDIChildFrame*
628 #else
629 wxDocChildFrame*
630 #endif
631 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
632 {
633 #if CTSIM_MDI
634   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
635 #else
636   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
637 #endif
638   theApp->setIconForFrame (subframe);
639   
640   m_statusBar.Create (subframe, -1);
641   subframe->SetStatusBar (&m_statusBar);
642   
643   m_pFileMenu = new wxMenu;
644   
645   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
646   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
647   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
648   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
649   
650   m_pFileMenu->AppendSeparator();
651   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
652   
653   m_pFileMenu->AppendSeparator();
654   m_pFileMenu->Append(wxID_PRINT, "&Print...");
655   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
656   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
657 #ifdef CTSIM_MDI
658   m_pFileMenu->AppendSeparator();
659   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
660 #endif
661   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
662   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
663   
664   m_pViewMenu = new wxMenu;
665   m_pViewMenu->Append(GRAPH3D_VIEW_SURFACE, "&Surface\tCtrl-U", "", true);
666   m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "&Smooth\tCtrl-M", "", true);
667   m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "&Color\tCtrl-R", "", true);
668   m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "&Lighting\tCtrl-L", "", true);
669   
670   wxMenu *help_menu = new wxMenu;
671   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
672   help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
673   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
674   
675   wxMenuBar *menu_bar = new wxMenuBar;
676   
677   menu_bar->Append(m_pFileMenu, "&File");
678   menu_bar->Append(m_pViewMenu, "&View");
679   menu_bar->Append(help_menu, "&Help");
680   
681   subframe->SetMenuBar(menu_bar);
682   
683   subframe->Centre(wxBOTH);
684   
685   wxAcceleratorEntry accelEntries[10];
686   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
687   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
688   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
689   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
690   accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
691   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SURFACE);
692   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_COLOR);
693   accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_LIGHTING);
694   accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
695   wxAcceleratorTable accelTable (9, accelEntries);
696   subframe->SetAcceleratorTable (accelTable);
697   
698   return subframe;
699 }
700
701
702
703 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
704 EVT_SIZE(Graph3dFileCanvas::OnSize)
705 EVT_PAINT(Graph3dFileCanvas::OnPaint)
706 EVT_CHAR(Graph3dFileCanvas::OnChar)
707 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
708 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
709 END_EVENT_TABLE()
710
711
712
713
714 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
715                                       const wxSize& size, long style, int* gl_attrib):
716 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas"), gl_attrib), m_pView(view)
717 {
718   parent->Show(TRUE);
719   SetCurrent();
720   /* Make sure server supports the vertex array extension */
721   char* extensions = (char *) glGetString( GL_EXTENSIONS );
722   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
723     m_pView->m_bUseVertexArrays = GL_FALSE;
724   }
725 }
726
727
728 Graph3dFileCanvas::~Graph3dFileCanvas(void)
729 {
730   m_pView = NULL;
731 }
732
733 void 
734 Graph3dFileCanvas::OnDraw(wxDC& dc)
735 {
736   if (m_pView)
737     m_pView->OnDraw(& dc);
738 }
739
740 void 
741 Graph3dFileCanvas::OnSize(wxSizeEvent& event)
742 {
743 #ifndef __WXMOTIF__
744   if (!GetContext()) return;
745 #endif
746   
747   SetCurrent();
748   int width, height;
749   GetClientSize (&width, &height);
750   Reshape (width, height);
751 }
752
753 void 
754 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
755 {
756   if (! m_pView)
757     return;
758   
759   wxCommandEvent dummyEvent;
760   switch (event.KeyCode()) {
761   case WXK_LEFT:
762         m_pView->m_dYRotate -= 15.0;
763     break;
764   case WXK_RIGHT:
765     m_pView->m_dYRotate += 15.0;
766     break;
767   case WXK_UP:
768     m_pView->m_dXRotate += 15.0;
769     break;
770   case WXK_DOWN:
771     m_pView->m_dXRotate -= 15.0;
772     break;
773   case 'z': case 'Z':
774     m_pView->m_dZRotate += 15.0;
775     break;
776   case 'x': case 'X':
777     m_pView->m_dZRotate -= 15.0;
778     break;
779   case 's': case 'S':
780     m_pView->OnSmooth (dummyEvent);
781     break;
782   case 'l': case 'L':
783     m_pView->OnLighting (dummyEvent);
784     break;
785   case 'c': case 'C':
786     m_pView->OnColor (dummyEvent);
787     break;
788   default:
789     {
790       event.Skip();
791       return;
792     }
793   }
794   
795   Refresh (false);
796 }
797
798 void
799 Graph3dFileCanvas::Reshape (int width, int height)
800 {
801   glViewport (0, 0, (GLint)width, (GLint)height);
802 }
803
804
805 void 
806 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
807 {
808   static int dragging = 0;
809   static float last_x, last_y;
810   
811   if (! m_pView)
812     return;
813   
814   if(event.LeftIsDown()) {
815     if(! dragging) {
816       dragging = 1;
817     } else {
818       m_pView->m_dXRotate += (event.GetX() - last_x)*1.0;
819       m_pView->m_dYRotate += (event.GetY() - last_y)*1.0;
820       Refresh(FALSE);
821     }
822     last_x = event.GetX();
823     last_y = event.GetY();
824   } else
825     dragging = 0;
826 }
827
828 void 
829 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
830 {
831   // Do nothing: avoid flashing.
832 }
833
834
835
836
837 #endif // wxUSE_GLCANVAS