G4OpenGLStoredViewer.cc

Go to the documentation of this file.
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 // Andrew Walkden  7th February 1997
00031 // Class G4OpenGLStoredViewer : Encapsulates the `storedness' of
00032 //                            an OpenGL view, for inheritance by
00033 //                            derived (X, Xm...) classes.
00034 
00035 #ifdef G4VIS_BUILD_OPENGL_DRIVER
00036 
00037 #include "G4OpenGLStoredViewer.hh"
00038 
00039 #include "G4PhysicalConstants.hh"
00040 #include "G4OpenGLStoredSceneHandler.hh"
00041 #include "G4Text.hh"
00042 #include "G4Circle.hh"
00043 #include "G4UnitsTable.hh"
00044 #include "G4Scene.hh"
00045 #include "G4OpenGLTransform3D.hh"
00046 
00047 G4OpenGLStoredViewer::G4OpenGLStoredViewer
00048 (G4OpenGLStoredSceneHandler& sceneHandler):
00049 G4VViewer (sceneHandler, -1),  
00050 G4OpenGLViewer (sceneHandler), 
00051 fG4OpenGLStoredSceneHandler (sceneHandler)
00052 {
00053   fLastVP = fDefaultVP; // Not sure if this gets executed before or
00054   // after G4VViewer::G4VViewer!!  Doesn't matter much.
00055 }
00056 
00057 G4OpenGLStoredViewer::~G4OpenGLStoredViewer () {}
00058 
00059 void G4OpenGLStoredViewer::KernelVisitDecision () {
00060   
00061   // If there's a significant difference with the last view parameters
00062   // of either the scene handler or this viewer, trigger a rebuild.
00063 
00064   if (!fG4OpenGLStoredSceneHandler.fTopPODL ||
00065       CompareForKernelVisit(fLastVP)) {
00066     NeedKernelVisit ();
00067   }      
00068   fLastVP = fVP;
00069 }
00070 
00071 G4bool G4OpenGLStoredViewer::CompareForKernelVisit(G4ViewParameters& lastVP) {
00072 
00073   if (
00074       (lastVP.GetDrawingStyle ()    != fVP.GetDrawingStyle ())    ||
00075       (lastVP.IsAuxEdgeVisible ()   != fVP.IsAuxEdgeVisible ())   ||
00076       (lastVP.GetRepStyle ()        != fVP.GetRepStyle ())        ||
00077       (lastVP.IsCulling ()          != fVP.IsCulling ())          ||
00078       (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
00079       (lastVP.IsDensityCulling ()   != fVP.IsDensityCulling ())   ||
00080       (lastVP.IsCullingCovered ()   != fVP.IsCullingCovered ())   ||
00081       (lastVP.IsSection ()          != fVP.IsSection ())          ||
00082       // Section (DCUT) implemented locally.  But still need to visit
00083       // kernel if status changes so that back plane culling can be
00084       // switched.
00085       (lastVP.IsCutaway ()          != fVP.IsCutaway ())          ||
00086       // Cutaways implemented locally.  But still need to visit kernel
00087       // if status changes so that back plane culling can be switched.
00088       (lastVP.IsExplode ()          != fVP.IsExplode ())          ||
00089       (lastVP.GetNoOfSides ()       != fVP.GetNoOfSides ())       ||
00090       (lastVP.GetDefaultVisAttributes()->GetColour() !=
00091        fVP.GetDefaultVisAttributes()->GetColour())                ||
00092       (lastVP.GetDefaultTextVisAttributes()->GetColour() !=
00093        fVP.GetDefaultTextVisAttributes()->GetColour())            ||
00094       (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
00095       (lastVP.IsPicking ()          != fVP.IsPicking ())          ||
00096       (lastVP.GetVisAttributesModifiers().size() !=
00097        fVP.GetVisAttributesModifiers().size())
00098       )
00099     return true;
00100   
00101   if (lastVP.IsDensityCulling () &&
00102       (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
00103     return true;
00104 
00105   /**************************************************************
00106   Section (DCUT) implemented locally.  No need to visit kernel if
00107   section plane itself changes.
00108   if (lastVP.IsSection () &&
00109       (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
00110     return true;
00111   ***************************************************************/
00112 
00113   /**************************************************************
00114   Cutaways implemented locally.  No need to visit kernel if cutaway
00115   planes themselves change.
00116   if (lastVP.IsCutaway ()) {
00117     if (lastVP.GetCutawayPlanes ().size () !=
00118         fVP.GetCutawayPlanes ().size ()) return true;
00119     for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
00120       if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
00121         return true;
00122   }
00123   ***************************************************************/
00124 
00125   if (lastVP.IsExplode () &&
00126       (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
00127     return true;
00128 
00129   return false;
00130 }
00131 
00132 void G4OpenGLStoredViewer::DrawDisplayLists () {
00133 #ifdef G4DEBUG_VIS_OGL
00134   printf("G4OpenGLStoredViewer::DrawDisplayLists \n");
00135 #endif
00136 
00137   const G4Planes& cutaways = fVP.GetCutawayPlanes();
00138   G4bool cutawayUnion = fVP.IsCutaway() &&
00139     fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion;
00140   const size_t nCutaways = cutawayUnion? cutaways.size(): 1;
00141 #ifdef G4DEBUG_VIS_OGL
00142   printf("G4OpenGLStoredViewer::DrawDisplayLists");
00143 #endif
00144   G4int iPass = 1;
00145   G4bool secondPassForTransparencyRequested = false;
00146   G4bool thirdPassForNonHiddenMarkersRequested = false;
00147   do {
00148     for (size_t iCutaway = 0; iCutaway < nCutaways; ++iCutaway) {
00149 
00150       if (cutawayUnion) {
00151         double a[4];
00152         a[0] = cutaways[iCutaway].a();
00153         a[1] = cutaways[iCutaway].b();
00154         a[2] = cutaways[iCutaway].c();
00155         a[3] = cutaways[iCutaway].d();
00156         glClipPlane (GL_CLIP_PLANE2, a);
00157         glEnable (GL_CLIP_PLANE2);
00158       }
00159 
00160       G4bool isPicking = fVP.IsPicking();
00161 
00162       for (size_t iPO = 0;
00163            iPO < fG4OpenGLStoredSceneHandler.fPOList.size(); ++iPO) {
00164         if (POSelected(iPO)) {
00165           G4OpenGLStoredSceneHandler::PO& po =
00166             fG4OpenGLStoredSceneHandler.fPOList[iPO];
00167           G4Colour c = po.fColour;
00168           DisplayTimePOColourModification(c,iPO);
00169           const G4bool isTransparent = c.GetAlpha() < 1.;
00170           if ( iPass == 1) {
00171             if (isTransparent && transparency_enabled) {
00172               secondPassForTransparencyRequested = true;
00173               continue;
00174             }
00175             if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
00176               thirdPassForNonHiddenMarkersRequested = true;
00177               continue;
00178             }
00179           } else if (iPass == 2) {  // Second pass for transparency.
00180             if (!isTransparent) {
00181               continue;
00182             }
00183           } else {  // Third pass for non-hidden markers
00184             if (!po.fMarkerOrPolyline) {
00185               continue;
00186             }            
00187           }
00188           if (isPicking) glLoadName(po.fPickName);
00189           if (transparency_enabled) {
00190             glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
00191           } else {
00192             glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
00193           }
00194           if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden())
00195             glDisable (GL_DEPTH_TEST);
00196           else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
00197           if (po.fpG4TextPlus) {
00198             if (po.fpG4TextPlus->fProcessing2D) {
00199               glMatrixMode (GL_PROJECTION);
00200               glPushMatrix();
00201               glLoadIdentity();
00202               glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
00203               glMatrixMode (GL_MODELVIEW);
00204               glPushMatrix();
00205               glLoadIdentity();
00206             }
00207             G4OpenGLTransform3D oglt (po.fTransform);
00208             glMultMatrixd (oglt.GetGLMatrix ());
00209             fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
00210               (po.fpG4TextPlus->fG4Text);
00211             if (po.fpG4TextPlus->fProcessing2D) {
00212               glMatrixMode (GL_PROJECTION);
00213               glPopMatrix();
00214               glMatrixMode (GL_MODELVIEW);
00215               glPopMatrix();
00216             }
00217           } else {
00218             glPushMatrix();
00219             G4OpenGLTransform3D oglt (po.fTransform);
00220             glMultMatrixd (oglt.GetGLMatrix ());
00221             glCallList (po.fDisplayListId);
00222             glPopMatrix();
00223           }
00224         }
00225       }
00226 
00227       G4Transform3D lastMatrixTransform;
00228       G4bool first = true;
00229 
00230       for (size_t iTO = 0;
00231            iTO < fG4OpenGLStoredSceneHandler.fTOList.size(); ++iTO) {
00232         if (TOSelected(iTO)) {
00233           G4OpenGLStoredSceneHandler::TO& to =
00234             fG4OpenGLStoredSceneHandler.fTOList[iTO];
00235           const G4Colour& c = to.fColour;
00236           const G4bool isTransparent = c.GetAlpha() < 1.;
00237           if ( iPass == 1) {
00238             if (isTransparent && transparency_enabled) {
00239               secondPassForTransparencyRequested = true;
00240               continue;
00241             }
00242             if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
00243               thirdPassForNonHiddenMarkersRequested = true;
00244               continue;
00245             }
00246           } else if (iPass == 2) {  // Second pass for transparency.
00247             if (!isTransparent) {
00248               continue;
00249             }
00250           } else {  // Third pass for non-hidden markers
00251             if (!to.fMarkerOrPolyline) {
00252               continue;
00253             }
00254           }
00255           if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden())
00256             glDisable (GL_DEPTH_TEST);
00257           else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
00258           if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
00259             if (fVP.IsPicking()) glLoadName(to.fPickName);
00260             if (to.fpG4TextPlus) {
00261               if (to.fpG4TextPlus->fProcessing2D) {
00262                 glMatrixMode (GL_PROJECTION);
00263                 glPushMatrix();
00264                 glLoadIdentity();
00265                 glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
00266                 glMatrixMode (GL_MODELVIEW);
00267                 glPushMatrix();
00268                 glLoadIdentity();
00269               }
00270               G4OpenGLTransform3D oglt (to.fTransform);
00271               glMultMatrixd (oglt.GetGLMatrix ());
00272               if (transparency_enabled) {
00273                 glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
00274               } else {
00275                 glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
00276               }
00277               fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
00278                 (to.fpG4TextPlus->fG4Text);
00279               if (to.fpG4TextPlus->fProcessing2D) {
00280                 glMatrixMode (GL_PROJECTION);
00281                 glPopMatrix();
00282                 glMatrixMode (GL_MODELVIEW);
00283                 glPopMatrix();
00284               }
00285             } else {
00286               if (to.fTransform != lastMatrixTransform) {
00287                 if (! first) {
00288                   glPopMatrix();
00289                 }
00290                 first = false;
00291                 glPushMatrix();
00292                 G4OpenGLTransform3D oglt (to.fTransform);
00293                 glMultMatrixd (oglt.GetGLMatrix ());
00294               }
00295               const G4Colour& cc = to.fColour;
00296               if (fFadeFactor > 0. && to.fEndTime < fEndTime) {
00297                 // Brightness scaling factor
00298                 G4double bsf = 1. - fFadeFactor *
00299                   ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
00300                 const G4Colour& bg = fVP.GetBackgroundColour();
00301                 if (transparency_enabled) {
00302                   glColor4d
00303                   (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
00304                    bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
00305                    bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue(),
00306                    bsf * cc.GetAlpha() + (1. - bsf) * bg.GetAlpha());
00307                 } else {
00308                   glColor3d
00309                   (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
00310                    bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
00311                    bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue());
00312                 }
00313               } else {
00314                 if (transparency_enabled) {
00315                   glColor4d(cc.GetRed(),cc.GetGreen(),cc.GetBlue(),cc.GetAlpha());
00316                 } else {
00317                   glColor3d(cc.GetRed(),cc.GetGreen(),cc.GetBlue());
00318                 }
00319               }
00320               glCallList (to.fDisplayListId);
00321             }
00322             if (to.fTransform != lastMatrixTransform) {
00323               lastMatrixTransform = to.fTransform;
00324             }
00325           }
00326         }
00327       }
00328       if (! first) {
00329         glPopMatrix();
00330       }
00331 
00332       if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
00333     }  // iCutaway
00334     
00335     if (iPass == 2) secondPassForTransparencyRequested = false;  // Done.
00336     if (iPass == 3) thirdPassForNonHiddenMarkersRequested = false;  // Done.
00337     
00338     if (secondPassForTransparencyRequested) iPass = 2;
00339     else if (thirdPassForNonHiddenMarkersRequested) iPass = 3;
00340     else break;
00341 
00342   } while (true);
00343 
00344   // Display time at "head" of time range, which is fEndTime...
00345   if (fDisplayHeadTime && fEndTime < DBL_MAX) {
00346     glMatrixMode (GL_PROJECTION);
00347     glPushMatrix();
00348     glLoadIdentity();
00349     glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
00350     glMatrixMode (GL_MODELVIEW);
00351     glPushMatrix();
00352     glLoadIdentity();
00353     G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
00354                         G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
00355     headTimeText.SetScreenSize(fDisplayHeadTimeSize);
00356     G4VisAttributes visAtts (G4Colour
00357                              (fDisplayHeadTimeRed,
00358                               fDisplayHeadTimeGreen,
00359                               fDisplayHeadTimeBlue));
00360     headTimeText.SetVisAttributes(&visAtts);
00361     fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(headTimeText);
00362     glMatrixMode (GL_PROJECTION);
00363     glPopMatrix();
00364     glMatrixMode (GL_MODELVIEW);
00365     glPopMatrix();
00366   }
00367 
00368   // Display light front...
00369   if (fDisplayLightFront && fEndTime < DBL_MAX) {
00370     G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
00371     if (lightFrontRadius > 0.) {
00372       G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
00373       G4Point3D circleCentre = lightFrontCentre;
00374       G4double circleRadius = lightFrontRadius;
00375       if (fVP.GetFieldHalfAngle() > 0.) {
00376         // Perspective view.  Find horizon centre and radius...
00377         G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
00378           fVP.GetCurrentTargetPoint();
00379         G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
00380         if(sceneRadius <= 0.) sceneRadius = 1.;
00381         G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
00382         G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
00383         G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
00384         G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
00385         /*
00386         G4cout << "cameraPosition: " << cameraPosition
00387                << ", lightFrontCentre: " << lightFrontCentre
00388                << ", lightFrontRadius: " << lightFrontRadius
00389                << ", lightFrontCentreDistance: " << lightFrontCentreDistance
00390                << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
00391                << G4endl;
00392         */
00393         if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
00394           // Light front in front of camera...
00395           G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
00396           circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
00397           circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
00398           /*
00399           G4cout << "sineHorizonAngle: " << sineHorizonAngle
00400                  << ", circleCentre: " << circleCentre
00401                  << ", circleRadius: " << circleRadius
00402                  << G4endl;
00403           */
00404         } else {
00405           circleRadius = -1.;
00406         }
00407       }
00408       if (circleRadius > 0.) {
00409         G4Circle lightFront(circleCentre);
00410         lightFront.SetWorldRadius(circleRadius);
00411         glColor3d(fDisplayLightFrontRed,
00412                   fDisplayLightFrontGreen,
00413                   fDisplayLightFrontBlue);
00414         fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(lightFront);
00415       }
00416     }
00417   }
00418 }
00419 
00420 #endif

Generated on Mon May 27 17:49:10 2013 for Geant4 by  doxygen 1.4.7