00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // 00027 // $Id$ 00028 // 00029 // 00030 // John Allison 10th March 2006 00031 // A template for a sophisticated graphics driver with a scene graph. 00032 //?? Lines beginning like this require specialisation for your driver. 00033 00034 #include "G4XXXSGViewer.hh" 00035 00036 #include "G4VSceneHandler.hh" 00037 #include "G4XXXSGSceneHandler.hh" 00038 00039 #include <fstream> 00040 00041 G4XXXSGViewer::G4XXXSGViewer 00042 (G4VSceneHandler& sceneHandler, const G4String& name): 00043 G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name) 00044 {} 00045 00046 G4XXXSGViewer::~G4XXXSGViewer() {} 00047 00048 void G4XXXSGViewer::SetView() { 00049 //#ifdef G4XXXSGDEBUG 00050 G4cout << "G4XXXSGViewer::SetView() called." << G4endl; 00051 //#endif 00052 } 00053 00054 void G4XXXSGViewer::ClearView() { 00055 //#ifdef G4XXXSGDEBUG 00056 G4cout << "G4XXXSGViewer::ClearView() called." << G4endl; 00057 //#endif 00058 } 00059 00060 void G4XXXSGViewer::DrawView() { 00061 //#ifdef G4XXXSGDEBUG 00062 G4cout << "G4XXXSGViewer::DrawView() called." << G4endl; 00063 //#endif 00064 00065 // First, a view should decide when to re-visit the G4 kernel. 00066 // Sometimes it might not be necessary, e.g., if the scene is stored 00067 // in a graphical database (e.g., OpenGL's display lists) and only 00068 // the viewing angle has changed. But graphics systems without a 00069 // graphical database will always need to visit the G4 kernel. 00070 00071 // The fNeedKernelVisit flag might have been set by the user in 00072 // /vis/viewer/rebuild, but if not, make decision and set flag only 00073 // if necessary... 00074 if (!fNeedKernelVisit) KernelVisitDecision(); 00075 G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets). 00076 00077 ProcessView (); // Clears store and processes scene only if necessary. 00078 00079 if (kernelVisitWasNeeded) { 00080 // Some systems, notably OpenGL, can draw while re-building, so 00081 // there might not be a need to draw from store again here. But 00082 // in this case... 00083 DrawFromStore("G4XXXSGViewer::DrawView"); 00084 } else { 00085 DrawFromStore("G4XXXSGViewer::DrawView"); 00086 } 00087 00088 // ...before finally... 00089 FinishView (); // Flush streams and/or swap buffers. 00090 } 00091 00092 void G4XXXSGViewer::ShowView() { 00093 //#ifdef G4XXXSGDEBUG 00094 G4cout << "G4XXXSGViewer::ShowView() called." << G4endl; 00095 //#endif 00096 // This is what you should see... 00097 DrawFromStore("G4XXXSGViewer::ShowView"); 00098 } 00099 00100 void G4XXXSGViewer::KernelVisitDecision () { 00101 00102 // If there's a significant difference with the last view parameters 00103 // of either the scene handler or this viewer, trigger a rebuild. 00104 00105 SceneGraph& sceneGraph = 00106 static_cast<G4XXXSGSceneHandler&>(fSceneHandler).fSceneGraph; 00107 if (sceneGraph.fDaughters.size() == 3 // I.e., only the root nodes. 00108 // (The above needs re-thinking.) 00109 || CompareForKernelVisit(fLastVP)) { 00110 NeedKernelVisit (); // Sets fNeedKernelVisit. 00111 } 00112 fLastVP = fVP; 00113 } 00114 00115 G4bool G4XXXSGViewer::CompareForKernelVisit(G4ViewParameters& lastVP) 00116 { 00117 // Typical comparison. Taken from OpenGL. 00118 if ( 00119 (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) || 00120 (lastVP.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) || 00121 (lastVP.GetRepStyle () != fVP.GetRepStyle ()) || 00122 (lastVP.IsCulling () != fVP.IsCulling ()) || 00123 (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) || 00124 (lastVP.IsDensityCulling () != fVP.IsDensityCulling ()) || 00125 (lastVP.IsCullingCovered () != fVP.IsCullingCovered ()) || 00126 // No need to visit kernel if section plane changes. 00127 // No need to visit kernel if cutaway planes change. 00128 (lastVP.IsExplode () != fVP.IsExplode ()) || 00129 (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) || 00130 (lastVP.IsMarkerNotHidden () != fVP.IsMarkerNotHidden ()) || 00131 (lastVP.GetDefaultVisAttributes()->GetColour() != 00132 fVP.GetDefaultVisAttributes()->GetColour()) || 00133 (lastVP.GetDefaultTextVisAttributes()->GetColour() != 00134 fVP.GetDefaultTextVisAttributes()->GetColour()) || 00135 (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())|| 00136 (lastVP.GetVisAttributesModifiers().size() != 00137 fVP.GetVisAttributesModifiers().size()) 00138 ) { 00139 return true; 00140 } 00141 00142 if (lastVP.IsDensityCulling () && 00143 (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ())) 00144 return true; 00145 00146 if (lastVP.IsExplode () && 00147 (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ())) 00148 return true; 00149 00150 return false; 00151 } 00152 00153 void G4XXXSGViewer::DrawFromStore(const G4String& source) { 00154 SceneGraph& sceneGraph = 00155 static_cast<G4XXXSGSceneHandler&>(fSceneHandler).fSceneGraph; 00156 // Write to a file for testing... 00157 static G4int iCount = 0; 00158 std::ostringstream oss; 00159 oss << source << '.' << fName << '.' << iCount++ << ".out"; 00160 G4cout << "Writing " << oss.str() << G4endl; 00161 std::ofstream ofs(oss.str().c_str()); 00162 JA::PrintTree(ofs,&sceneGraph); 00163 ofs.close(); 00164 }