1 /*****************************************************************************
4 ** Name: graph3dview.cpp
5 ** Purpose: 3d graph view classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Jan 2001
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: graph3dview.cpp,v 1.9 2001/02/03 18:43:43 kevin Exp $
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.
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.
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 ******************************************************************************/
29 #pragma implementation
33 // For compilers that support precompilation, includes "wx.h".
34 #include "wx/wxprec.h"
47 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
51 #include "wx/glcanvas.h"
61 #include "dlgprojections.h"
62 #include "dlgreconstruct.h"
63 #include "backprojectors.h"
64 #include "reconstruct.h"
67 #if defined(MSVC) || HAVE_SSTREAM
70 #include <sstream_subst>
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)
75 intensityToColor (double dIntensity, float vecColor[3])
77 double dRange = dIntensity * 5;
78 int iRange = static_cast<int>(floor (dRange));
79 double dFrac = dRange - iRange;
83 vecColor[0] = 1 - dFrac; vecColor[1] = 0; vecColor[2] = 1;
86 vecColor[0] = 0; vecColor[1] = dFrac; vecColor[2] = 1;
89 vecColor[0] = 0; vecColor[1] = 1; vecColor[2] = 1 - dFrac;
92 vecColor[0] = dFrac; vecColor[1] = 1; vecColor[2] = 0;
95 vecColor[0] = 0; vecColor[1] = 1 - dFrac; vecColor[2] = 0;
98 vecColor[0] = 1; vecColor[1] = 0; vecColor[2] = 0;
103 //***********************************************************************
104 // Function: CalculateVectorNormal
106 // Purpose: Given three points of a 3D plane, this function calculates
107 // the normal vector of that plane.
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).
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 //*************************************************************************
121 GLvoid CalculateVectorNormal (GLfloat fVert1[], GLfloat fVert2[],
122 GLfloat fVert3[], GLfloat *fNormalX,
123 GLfloat *fNormalY, GLfloat *fNormalZ)
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];
132 *fNormalX = Py*Qz - Pz*Qy;
133 *fNormalY = Pz*Qx - Px*Qz;
134 *fNormalZ = Px*Qy - Py*Qx;
138 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
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)
148 Graph3dFileView::Graph3dFileView ()
149 : m_pFileMenu(NULL), m_pViewMenu(NULL)
151 m_bUseVertexArrays = false;
152 m_bDoubleBuffer = true;
163 Graph3dFileView::~Graph3dFileView()
165 GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
166 GetDocumentManager()->ActivateView(this, FALSE, TRUE);
170 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
172 m_pFrame = CreateChildFrame(doc, this);
176 m_pFrame->GetClientSize (&width, &height);
177 m_pFrame->SetTitle("Graph3dFileView");
178 m_pCanvas = CreateCanvas (m_pFrame);
180 m_pFrame->Show(TRUE);
181 m_pCanvas->SetCurrent();
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);
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);
200 Graph3dFileView::CreateCanvas (wxFrame* parent)
202 Graph3dFileCanvas* pCanvas;
204 parent->GetClientSize (&width, &height);
207 int *gl_attrib = NULL;
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 };
213 if(! m_bDoubleBuffer) {
219 pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0, gl_attrib);
221 pCanvas->SetBackgroundColour(*wxWHITE);
230 Graph3dFileView::DrawSurface()
232 #ifdef GL_EXT_vertex_array
233 if (m_bUseVertexArrays) {
234 // glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, nVertices );
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)
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.);
254 glShadeModel (GL_SMOOTH);
256 glShadeModel (GL_FLAT);
260 glEnable (GL_LIGHTING);
262 glDisable (GL_LIGHTING);
265 double dMin = v[0][0];
268 for (ix = 0; ix < nx; ix++)
269 for (unsigned int iy = 0; iy < ny; iy++)
270 if (v[ix][iy] < dMin)
272 else if (v[ix][iy] > dMax)
275 double dIntensityScale = dMax - dMin;
276 double actOffset = dMin;
277 double actScale = 0.3 * sqrt(nx*nx+ny*ny) / (dMax - dMin);
279 // glNewList(opnListNum++,GL_COMPILE);
281 glColor3f (1.0, 1.0, 1.0);
283 glDisable (GL_CULL_FACE);
284 for (ix = 0; ix < nx-1; ix++) {
285 for (unsigned int iy = 0; iy < ny-1; iy++) {
287 float p1[3], p2[3], p3[3], p4[3];
288 float n1[3], n2[3], n3[3], n4[3];
292 glBegin(GL_LINE_LOOP);
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;
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;
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;
314 intensityToColor (dIntensity1, vecColor);
315 glColor3fv (vecColor);
317 glVertex3fv (p1); glNormal3fv (n1);
319 intensityToColor (dIntensity2, vecColor);
320 glColor3fv (vecColor);
322 glVertex3fv (p2); glNormal3fv (n1);
324 intensityToColor (dIntensity3, vecColor);
325 glColor3fv (vecColor);
327 glVertex3fv (p3); glNormal3fv (n1);
329 intensityToColor (dIntensity4, vecColor);
330 glColor3fv (vecColor);
332 glVertex3fv (p4); glNormal3fv (n1);
339 #ifdef GL_EXT_vertex_array
346 Graph3dFileView::OnProperties (wxCommandEvent& event)
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();
355 Graph3dFileView::OnLighting (wxCommandEvent& event)
357 m_bLighting = ! m_bLighting;
358 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
360 m_pCanvas->Refresh();
364 Graph3dFileView::OnSurface (wxCommandEvent& event)
366 m_bSurface = ! m_bSurface;
367 m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
368 m_pCanvas->Refresh();
372 Graph3dFileView::OnColor (wxCommandEvent& event)
374 m_bColor = ! m_bColor;
375 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
376 m_pCanvas->Refresh();
380 Graph3dFileView::OnSmooth (wxCommandEvent& event)
382 m_bSmooth = ! m_bSmooth;
383 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
384 m_pCanvas->Refresh();
390 Graph3dFileView::OnDraw (wxDC* dc)
393 if (! m_pCanvas->GetContext()) return;
396 m_pCanvas->SwapBuffers();
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();
406 Graph3dFileView::Draw ()
408 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
418 Graph3dFileView::InitMaterials()
422 int nx = GetDocument()->nx();
423 int ny = GetDocument()->ny();
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};
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};
442 static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
443 static float lmodel_twoside[] = {GL_FALSE};
445 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
446 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
447 glEnable(GL_NORMALIZE);
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);
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);
459 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
460 glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
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);
466 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
467 // glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
468 glEnable(GL_COLOR_MATERIAL);
470 GLfloat impLPos[] = {1.0, 1.0, 1.0, 0.0};
472 GLfloat defaultLightAmb [] = {.2, .2, .2, 1.0};
473 GLfloat defaultLightDiff [] = {.2, .2, .2, 1.0};
474 GLfloat defaultLightSpec [] = { .3, .3, .3, 1.0};
476 GLfloat defaultGlobalAmb [] = {.3, .3, .3, 1.0};
477 GLfloat defaultGlobalDiff[] = {.3, .3, .3, 1.0};
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};
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 };
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 };
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 };
499 // double opnX = nx, opnY = ny, opnZ = z;
500 // eyeX = 1; eyeY = 1, eyeZ = 1;
502 impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
504 //impGraphicsFlag = IMP__3D;
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 -" );
511 glClearColor(0.0, 0.0, 0.0, 0.0);
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);
519 glEnable(GL_DEPTH_TEST);
521 glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
522 glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
523 glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
525 glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
526 glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
527 glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
529 glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
530 glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
531 glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
533 glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
534 glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
535 glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
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);
542 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
544 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
546 glEnable(GL_COLOR_MATERIAL);
548 glEnable(GL_LIGHTING);
558 Graph3dFileView::InitGL ()
560 glClearColor(0.0, 0.0, 0.0, 0.0);
562 glEnable(GL_DEPTH_TEST);
567 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
569 int nVertices = GetDocument()->m_nVertices;
570 glTripleFloat* pVertices = GetDocument()->m_pVertices;
571 glTripleFloat* pNormals = GetDocument()->m_pNormals;
573 glMatrixMode(GL_PROJECTION);
577 int nx = GetDocument()->nx();
578 int ny = GetDocument()->ny();
579 int maxDim = maxValue<int> (nx, ny);
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);
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 );
601 m_pCanvas->Refresh();
605 Graph3dFileView::OnClose (bool deleteWindow)
607 if (! GetDocument() || ! GetDocument()->Close())
612 // m_pCanvas->Show(false);
613 m_pCanvas->setView(NULL);
616 wxString s(theApp->GetAppName());
618 m_pFrame->SetTitle(s);
623 // m_pFrame->Show(false);
626 // if (GetDocument() && GetDocument()->getBadFileOpen())
627 // ::wxYield(); // wxWindows bug workaround
638 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
641 wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
643 wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
645 theApp->setIconForFrame (subframe);
647 m_pStatusBar = new wxStatusBar (subframe, -1);
648 subframe->SetStatusBar (m_pStatusBar);
650 m_pFileMenu = new wxMenu;
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");
657 m_pFileMenu->AppendSeparator();
658 m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
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");
665 m_pFileMenu->AppendSeparator();
666 m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
668 GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
669 GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
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);
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");
682 wxMenuBar *menu_bar = new wxMenuBar;
684 menu_bar->Append(m_pFileMenu, "&File");
685 menu_bar->Append(m_pViewMenu, "&View");
686 menu_bar->Append(help_menu, "&Help");
688 subframe->SetMenuBar(menu_bar);
690 subframe->Centre(wxBOTH);
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);
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)
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)
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;
735 Graph3dFileCanvas::~Graph3dFileCanvas(void)
741 Graph3dFileCanvas::OnDraw(wxDC& dc)
744 m_pView->OnDraw(& dc);
748 Graph3dFileCanvas::OnSize(wxSizeEvent& event)
751 if (!GetContext()) return;
756 GetClientSize (&width, &height);
757 Reshape (width, height);
761 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
766 wxCommandEvent dummyEvent;
767 switch (event.KeyCode()) {
769 m_pView->m_dYRotate -= 15.0;
772 m_pView->m_dYRotate += 15.0;
775 m_pView->m_dXRotate += 15.0;
778 m_pView->m_dXRotate -= 15.0;
781 m_pView->m_dZRotate += 15.0;
784 m_pView->m_dZRotate -= 15.0;
787 m_pView->OnSmooth (dummyEvent);
790 m_pView->OnLighting (dummyEvent);
793 m_pView->OnColor (dummyEvent);
806 Graph3dFileCanvas::Reshape (int width, int height)
808 glViewport (0, 0, (GLint)width, (GLint)height);
813 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
815 static int dragging = 0;
816 static float last_x, last_y;
821 if(event.LeftIsDown()) {
825 m_pView->m_dXRotate += (event.GetX() - last_x)*1.0;
826 m_pView->m_dYRotate += (event.GetY() - last_y)*1.0;
829 last_x = event.GetX();
830 last_y = event.GetY();
836 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
838 // wxGLCanvas::OnEraseBackground(event); // Do nothing: avoid flashing.
844 #endif // wxUSE_GLCANVAS