#include #include #include #include #include class MyMainFrame: public TGMainFrame { private: TGHorizontalFrame* fHor; TGTextButton* fBtnToggle; TGTextButton* fBtnQuit; TRootEmbeddedCanvas* fEC; bool is_GL; public: MyMainFrame( const TGWindow *p, UInt_t w, UInt_t h ); virtual ~MyMainFrame(); void DrawCube(); void CloseWindow(); }; MyMainFrame::MyMainFrame( const TGWindow *p, UInt_t w, UInt_t h ) : TGMainFrame(p,w,h) { //embedded GL view should work either by setting gStyle: gStyle->SetCanvasPreferGL(kTRUE); //or by setting the name of the TRootEmbeddedCanvas to a string containing "gl", but commenting out the above line //results in a crash is_GL = false; // The Canvas fEC = new TRootEmbeddedCanvas("glTest",this, 300, 270); fEC->SetBackgroundColor(1); AddFrame( fEC, new TGLayoutHints( kLHintsExpandX | kLHintsExpandY ) ); //Frame for Buttons fHor = new TGHorizontalFrame(this, 300, 30 ); fBtnToggle = new TGTextButton(fHor, "&Switch to GL view"); fHor->AddFrame(fBtnToggle, new TGLayoutHints( kLHintsCenterX, 0, 0, 5, 5 )); fBtnToggle->Connect("Clicked()", "MyMainFrame", this, "DrawCube()"); fBtnQuit = new TGTextButton(fHor, "&Quit"); fHor->AddFrame(fBtnQuit, new TGLayoutHints( kLHintsCenterX, 0, 0, 5, 5 )); fBtnQuit->Connect("Clicked()", "MyMainFrame", this, "CloseWindows()"); AddFrame(fHor, new TGLayoutHints( kLHintsTop | kLHintsExpandX )); Layout(); // Create GeoManager singleton new TGeoManager("Cube", "Cube"); //Create and draw a simple geometry //define some materials TGeoMaterial *matVacuum = new TGeoMaterial("Vacuum", 0,0,0); TGeoMaterial *matAl = new TGeoMaterial("Al", 26.98,13,2.7); //define some media TGeoMedium *Vacuum = new TGeoMedium("Vacuum", 1, matVacuum); TGeoMedium *Al = new TGeoMedium("Aluminium", 3, matAl); //define top volume TGeoVolume *top = gGeoManager->MakeBox( "Top", Vacuum, 100,100,100 ); // set as root for TGeoManager volume gGeoManager->SetTopVolume( top ); // make invisible top->SetVisibility( kFALSE ); // not needed for TopVolume! //draw red box TGeoVolume* box = gGeoManager->MakeBox( "Box", Al, 50,50,50 ); box->SetLineColor( kRed ); top->AddNode( box, 1, new TGeoTranslation( 0, 0, 0 ) ); // close geometry and draw gGeoManager->CloseGeometry(); fEC->GetCanvas()->cd(); gGeoManager->GetTopVolume()->Draw(); //calling Draw("gl") here crashes fEC->GetCanvas()->Update(); // init mainwindow SetWindowName("Toggling embedded GL View"); MapSubwindows(); Resize(300, 300); MapWindow(); } void MyMainFrame::DrawCube() { if (is_GL) { fEC->GetCanvas()->GetViewer3D()->Delete(); //I hoped, this would help, but it doesn't fEC->Clear(); //neiter this gGeoManager->GetTopVolume()->Draw("pad"); fEC->GetCanvas()->Modified( kTRUE ); //nor that fEC->GetCanvas()->Update(); is_GL = false; fBtnToggle->SetText("Switch to GL view"); } else { fEC->Clear(); gGeoManager->GetTopVolume()->Draw("gl"); fEC->GetCanvas()->Modified( kTRUE ); fEC->GetCanvas()->Update(); is_GL = true; fBtnToggle->SetText("Switch to Pad view"); } } MyMainFrame::~MyMainFrame() { //delete fTree; // DELETE OR NOT? Cleanup(); } void MyMainFrame::CloseWindows() { DeleteWindow(); gApplication->Terminate(0); } // named macro void toggleGL() { new MyMainFrame( 0, 300, 300 ); }