source: trunk/source/visualization/OpenGL/src/#G4OpenGLStoredViewer.cc# @ 1350

Last change on this file since 1350 was 1350, checked in by garnier, 13 years ago

update to last version 4.9.4

File size: 10.6 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4OpenGLStoredViewer.cc,v 1.29 2010/10/06 10:05:52 allison Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Andrew Walkden  7th February 1997
32// Class G4OpenGLStoredViewer : Encapsulates the `storedness' of
33//                            an OpenGL view, for inheritance by
34//                            derived (X, Xm...) classes.
35
36#ifdef G4VIS_BUILD_OPENGL_DRIVER
37
38#include "G4OpenGLStoredViewer.hh"
39
40#include "G4OpenGLStoredSceneHandler.hh"
41#include "G4Text.hh"
42#include "G4Circle.hh"
43#include "G4UnitsTable.hh"
44#include "G4Scene.hh"
45
46G4OpenGLStoredViewer::G4OpenGLStoredViewer
47(G4OpenGLStoredSceneHandler& sceneHandler):
48G4VViewer (sceneHandler, -1), 
49G4OpenGLViewer (sceneHandler),
50fG4OpenGLStoredSceneHandler (sceneHandler)
51{
52  fLastVP = fDefaultVP; // Not sure if this gets executed before or
53  // after G4VViewer::G4VViewer!!  Doesn't matter much.
54}
55
56G4OpenGLStoredViewer::~G4OpenGLStoredViewer () {}
57
58void G4OpenGLStoredViewer::KernelVisitDecision () {
59 
60  // If there's a significant difference with the last view parameters
61  // of either the scene handler or this viewer, trigger a rebuild.
62
63  if (!fG4OpenGLStoredSceneHandler.fTopPODL ||
64      CompareForKernelVisit(fLastVP)) {
65    NeedKernelVisit ();
66  }     
67  fLastVP = fVP;
68}
69
70G4bool G4OpenGLStoredViewer::CompareForKernelVisit(G4ViewParameters& lastVP) {
71
72  if (
73      (lastVP.GetDrawingStyle ()    != fVP.GetDrawingStyle ())    ||
74      (lastVP.IsAuxEdgeVisible ()   != fVP.IsAuxEdgeVisible ())   ||
75      (lastVP.GetRepStyle ()        != fVP.GetRepStyle ())        ||
76      (lastVP.IsCulling ()          != fVP.IsCulling ())          ||
77      (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
78      (lastVP.IsDensityCulling ()   != fVP.IsDensityCulling ())   ||
79      (lastVP.IsCullingCovered ()   != fVP.IsCullingCovered ())   ||
80      (lastVP.IsSection ()          != fVP.IsSection ())          ||
81      // Section (DCUT) implemented locally.  But still need to visit
82      // kernel if status changes so that back plane culling can be
83      // switched.
84      (lastVP.IsCutaway ()          != fVP.IsCutaway ())          ||
85      // Cutaways implemented locally.  But still need to visit kernel
86      // if status changes so that back plane culling can be switched.
87      (lastVP.IsExplode ()          != fVP.IsExplode ())          ||
88      (lastVP.GetNoOfSides ()       != fVP.GetNoOfSides ())       ||
89      (lastVP.IsMarkerNotHidden ()  != fVP.IsMarkerNotHidden ())  ||
90      (lastVP.GetDefaultVisAttributes()->GetColour() !=
91       fVP.GetDefaultVisAttributes()->GetColour())                ||
92      (lastVP.GetDefaultTextVisAttributes()->GetColour() !=
93       fVP.GetDefaultTextVisAttributes()->GetColour())            ||
94      (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
95      (lastVP.IsPicking ()          != fVP.IsPicking ())
96      )
97    return true;
98 
99  if (lastVP.IsDensityCulling () &&
100      (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
101    return true;
102
103  /**************************************************************
104  Section (DCUT) implemented locally.  No need to visit kernel if
105  section plane itself changes.
106  if (lastVP.IsSection () &&
107      (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
108    return true;
109  ***************************************************************/
110
111  /**************************************************************
112  Cutaways implemented locally.  No need to visit kernel if cutaway
113  planes themselves change.
114  if (lastVP.IsCutaway ()) {
115    if (lastVP.GetCutawayPlanes ().size () !=
116        fVP.GetCutawayPlanes ().size ()) return true;
117    for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
118      if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
119        return true;
120  }
121  ***************************************************************/
122
123  if (lastVP.IsExplode () &&
124      (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
125    return true;
126
127  return false;
128}
129
130void G4OpenGLStoredViewer::DrawDisplayLists () {
131#ifdef G4DEBUG_VIS_OGL
132      printf("G4OpenGLStoredViewer::DrawDisplayLists \n");
133#endif
134
135  const G4Planes& cutaways = fVP.GetCutawayPlanes();
136  G4bool cutawayUnion = fVP.IsCutaway() &&
137    fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion;
138  size_t nPasses = cutawayUnion? cutaways.size(): 1;
139#ifdef G4DEBUG_VIS_OGL
140  printf("G4OpenGLStoredViewer::DrawDisplayLists");
141#endif
142  for (size_t i = 0; i < nPasses; ++i) {
143#ifdef G4DEBUG_VIS_OGL
144    printf("+");
145#endif
146
147    if (cutawayUnion) {
148      double a[4];
149      a[0] = cutaways[i].a();
150      a[1] = cutaways[i].b();
151      a[2] = cutaways[i].c();
152      a[3] = cutaways[i].d();
153      glClipPlane (GL_CLIP_PLANE2, a);
154      glEnable (GL_CLIP_PLANE2);
155    }
156
157    if (fG4OpenGLStoredSceneHandler.fTopPODL) {
158      glCallList (fG4OpenGLStoredSceneHandler.fTopPODL);
159#ifdef G4DEBUG_VIS_OGL
160    printf("fTopODL ");
161#endif
162    }
163    for (size_t i = 0; i < fG4OpenGLStoredSceneHandler.fTOList.size(); ++i) {
164#ifdef G4DEBUG_VIS_OGL
165      //      printf("-");
166#endif
167      G4OpenGLStoredSceneHandler::TO& to =
168        fG4OpenGLStoredSceneHandler.fTOList[i];
169      if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
170        glPushMatrix();
171        G4OpenGLTransform3D oglt (to.fTransform);
172        glMultMatrixd (oglt.GetGLMatrix ());
173        if (fVP.IsPicking()) glLoadName(to.fPickName);
174        const G4Colour& c = to.fColour;
175        const G4Colour& bg = fVP.GetBackgroundColour();
176        G4double bsf = 1.;  // Brightness scaling factor.
177        if (fFadeFactor > 0. && to.fEndTime < fEndTime)
178          bsf = 1. - fFadeFactor *
179            ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
180        glColor3d
181          (bsf * c.GetRed() + (1. - bsf) * bg.GetRed(),
182           bsf * c.GetGreen() + (1. - bsf) * bg.GetGreen(),
183           bsf * c.GetBlue() + (1. - bsf) * bg.GetBlue());
184        glCallList (to.fDisplayListId);
185#ifdef G4DEBUG_VIS_OGL
186        printf("%d ",to.fDisplayListId);
187#endif
188        glPopMatrix();
189      }
190    }
191
192    if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
193  }
194#ifdef G4DEBUG_VIS_OGL
195      printf("\n");
196#endif
197
198  // Display time at "head" of time range, which is fEndTime...
199  if (fDisplayHeadTime && fEndTime < DBL_MAX) {
200    glMatrixMode (GL_PROJECTION);
201    glPushMatrix();
202    glLoadIdentity();
203    glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
204    glMatrixMode (GL_MODELVIEW);
205    glPushMatrix();
206    glLoadIdentity();
207    G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
208                        G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
209    headTimeText.SetScreenSize(fDisplayHeadTimeSize);
210    G4VisAttributes visAtts (G4Colour
211                             (fDisplayHeadTimeRed,
212                              fDisplayHeadTimeGreen,
213                              fDisplayHeadTimeBlue));
214    headTimeText.SetVisAttributes(&visAtts);
215    static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
216      G4OpenGLSceneHandler::AddPrimitive(headTimeText);
217    glMatrixMode (GL_PROJECTION);
218    glPopMatrix();
219    glMatrixMode (GL_MODELVIEW);
220    glPopMatrix();
221  }
222
223  // Display light front...
224  if (fDisplayLightFront && fEndTime < DBL_MAX) {
225    G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
226    if (lightFrontRadius > 0.) {
227      G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
228      G4Point3D circleCentre = lightFrontCentre;
229      G4double circleRadius = lightFrontRadius;
230      if (fVP.GetFieldHalfAngle() > 0.) {
231        // Perspective view.  Find horizon centre and radius...
232        G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
233          fVP.GetCurrentTargetPoint();
234        G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
235        if(sceneRadius <= 0.) sceneRadius = 1.;
236        G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
237        G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
238        G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
239        G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
240        /*
241        G4cout << "cameraPosition: " << cameraPosition
242               << ", lightFrontCentre: " << lightFrontCentre
243               << ", lightFrontRadius: " << lightFrontRadius
244               << ", lightFrontCentreDistance: " << lightFrontCentreDistance
245               << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
246               << G4endl;
247        */
248        if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
249          // Light front in front of camera...
250          G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
251          circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
252          circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
253          /*
254          G4cout << "sineHorizonAngle: " << sineHorizonAngle
255                 << ", circleCentre: " << circleCentre
256                 << ", circleRadius: " << circleRadius
257                 << G4endl;
258          */
259        } else {
260          circleRadius = -1.;
261        }
262      }
263      if (circleRadius > 0.) {
264        G4Circle lightFront(circleCentre);
265        lightFront.SetWorldRadius(circleRadius);
266        glColor3d(fDisplayLightFrontRed,
267                  fDisplayLightFrontGreen,
268                  fDisplayLightFrontBlue);
269        static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
270          G4OpenGLSceneHandler::AddPrimitive(lightFront);
271      }
272    }
273  }
274}
275
276#endif
Note: See TracBrowser for help on using the repository browser.