source: trunk/source/visualization/OpenGL/src/G4OpenGLStoredViewer.cc @ 893

Last change on this file since 893 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

  • Property svn:mime-type set to text/cpp
File size: 9.8 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.24 2008/04/04 13:32:22 allison Exp $
28// GEANT4 tag $Name: HEAD $
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.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
91      (lastVP.IsPicking ()          != fVP.IsPicking ())
92      )
93    return true;
94 
95  if (lastVP.IsDensityCulling () &&
96      (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
97    return true;
98
99  /**************************************************************
100  Section (DCUT) implemented locally.  No need to visit kernel if
101  section plane itself changes.
102  if (lastVP.IsSection () &&
103      (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
104    return true;
105  ***************************************************************/
106
107  /**************************************************************
108  Cutaways implemented locally.  No need to visit kernel if cutaway
109  planes themselves change.
110  if (lastVP.IsCutaway ()) {
111    if (lastVP.GetCutawayPlanes ().size () !=
112        fVP.GetCutawayPlanes ().size ()) return true;
113    for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
114      if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
115        return true;
116  }
117  ***************************************************************/
118
119  if (lastVP.IsExplode () &&
120      (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
121    return true;
122
123  return false;
124}
125
126void G4OpenGLStoredViewer::DrawDisplayLists () {
127
128  const G4Planes& cutaways = fVP.GetCutawayPlanes();
129  G4bool cutawayUnion = fVP.IsCutaway() &&
130    fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion;
131  size_t nPasses = cutawayUnion? cutaways.size(): 1;
132  for (size_t i = 0; i < nPasses; ++i) {
133
134    if (cutawayUnion) {
135      double a[4];
136      a[0] = cutaways[i].a();
137      a[1] = cutaways[i].b();
138      a[2] = cutaways[i].c();
139      a[3] = cutaways[i].d();
140      glClipPlane (GL_CLIP_PLANE2, a);
141      glEnable (GL_CLIP_PLANE2);
142    }
143
144    if (fG4OpenGLStoredSceneHandler.fTopPODL)
145      glCallList (fG4OpenGLStoredSceneHandler.fTopPODL);
146
147    for (size_t i = 0; i < fG4OpenGLStoredSceneHandler.fTOList.size(); ++i) {
148      G4OpenGLStoredSceneHandler::TO& to =
149        fG4OpenGLStoredSceneHandler.fTOList[i];
150      if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
151        glPushMatrix();
152        G4OpenGLTransform3D oglt (to.fTransform);
153        glMultMatrixd (oglt.GetGLMatrix ());
154        if (fVP.IsPicking()) glLoadName(to.fPickName);
155        G4Colour& c = to.fColour;
156        G4double bsf = 1.;  // Brightness scaling factor.
157        if (fFadeFactor > 0. && to.fEndTime < fEndTime)
158          bsf = 1. - fFadeFactor *
159            ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
160        glColor3d(bsf * c.GetRed (), bsf * c.GetGreen (), bsf * c.GetBlue ());
161        glCallList (to.fDisplayListId);
162        glPopMatrix();
163      }
164    }
165
166    if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
167  }
168
169  // Display time at "head" of time range, which is fEndTime...
170  if (fDisplayHeadTime && fEndTime < DBL_MAX) {
171    glMatrixMode (GL_PROJECTION);
172    glPushMatrix();
173    glLoadIdentity();
174    glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
175    glMatrixMode (GL_MODELVIEW);
176    glPushMatrix();
177    glLoadIdentity();
178    G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
179                        G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
180    headTimeText.SetScreenSize(fDisplayHeadTimeSize);
181    G4VisAttributes visAtts (G4Colour
182                             (fDisplayHeadTimeRed,
183                              fDisplayHeadTimeGreen,
184                              fDisplayHeadTimeBlue));
185    headTimeText.SetVisAttributes(&visAtts);
186    static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
187      G4OpenGLSceneHandler::AddPrimitive(headTimeText);
188    glMatrixMode (GL_PROJECTION);
189    glPopMatrix();
190    glMatrixMode (GL_MODELVIEW);
191    glPopMatrix();
192  }
193
194  // Display light front...
195  if (fDisplayLightFront && fEndTime < DBL_MAX) {
196    G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
197    if (lightFrontRadius > 0.) {
198      G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
199      G4Point3D circleCentre = lightFrontCentre;
200      G4double circleRadius = lightFrontRadius;
201      if (fVP.GetFieldHalfAngle() > 0.) {
202        // Perspective view.  Find horizon centre and radius...
203        G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
204          fVP.GetCurrentTargetPoint();
205        G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
206        if(sceneRadius <= 0.) sceneRadius = 1.;
207        G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
208        G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
209        G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
210        G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
211        /*
212        G4cout << "cameraPosition: " << cameraPosition
213               << ", lightFrontCentre: " << lightFrontCentre
214               << ", lightFrontRadius: " << lightFrontRadius
215               << ", lightFrontCentreDistance: " << lightFrontCentreDistance
216               << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
217               << G4endl;
218        */
219        if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
220          // Light front in front of camera...
221          G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
222          circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
223          circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
224          /*
225          G4cout << "sineHorizonAngle: " << sineHorizonAngle
226                 << ", circleCentre: " << circleCentre
227                 << ", circleRadius: " << circleRadius
228                 << G4endl;
229          */
230        } else {
231          circleRadius = -1.;
232        }
233      }
234      if (circleRadius > 0.) {
235        G4Circle lightFront(circleCentre);
236        lightFront.SetWorldRadius(circleRadius);
237        glColor3d(fDisplayLightFrontRed,
238                  fDisplayLightFrontGreen,
239                  fDisplayLightFrontBlue);
240        static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
241          G4OpenGLSceneHandler::AddPrimitive(lightFront);
242      }
243    }
244  }
245}
246
247#endif
Note: See TracBrowser for help on using the repository browser.