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

Last change on this file since 1121 was 1121, checked in by garnier, 15 years ago

en test...ca marche encore...

  • Property svn:mime-type set to text/cpp
File size: 10.2 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.26 2009/04/08 16:55:44 lgarnier 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" // for G4BestUnit
44#include "G4Scene.hh"
45
46G4OpenGLStoredViewer::G4OpenGLStoredViewer
47(G4OpenGLStoredSceneHandler& sceneHandler):
48G4VViewer (sceneHandler, -1), 
49G4OpenGLViewer (sceneHandler),
50fG4OpenGLStoredSceneHandler (sceneHandler)
51{
52#ifdef G4DEBUG_VIS_OGL
53  printf("G4OpenGLStoredViewer::Creation ...............\n");
54#endif
55  fLastVP = fDefaultVP; // Not sure if this gets executed before or
56  // after G4VViewer::G4VViewer!!  Doesn't matter much.
57}
58
59G4OpenGLStoredViewer::~G4OpenGLStoredViewer () {}
60
61void G4OpenGLStoredViewer::KernelVisitDecision () {
62 
63  // If there's a significant difference with the last view parameters
64  // of either the scene handler or this viewer, trigger a rebuild.
65
66  if (!fG4OpenGLStoredSceneHandler.fTopPODL ||
67      CompareForKernelVisit(fLastVP)) {
68    NeedKernelVisit ();
69  }     
70  fLastVP = fVP;
71}
72
73G4bool G4OpenGLStoredViewer::CompareForKernelVisit(G4ViewParameters& lastVP) {
74
75  if (
76      (lastVP.GetDrawingStyle ()    != fVP.GetDrawingStyle ())    ||
77      (lastVP.IsAuxEdgeVisible ()   != fVP.IsAuxEdgeVisible ())   ||
78      (lastVP.GetRepStyle ()        != fVP.GetRepStyle ())        ||
79      (lastVP.IsCulling ()          != fVP.IsCulling ())          ||
80      (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
81      (lastVP.IsDensityCulling ()   != fVP.IsDensityCulling ())   ||
82      (lastVP.IsCullingCovered ()   != fVP.IsCullingCovered ())   ||
83      (lastVP.IsSection ()          != fVP.IsSection ())          ||
84      // Section (DCUT) implemented locally.  But still need to visit
85      // kernel if status changes so that back plane culling can be
86      // switched.
87      (lastVP.IsCutaway ()          != fVP.IsCutaway ())          ||
88      // Cutaways implemented locally.  But still need to visit kernel
89      // if status changes so that back plane culling can be switched.
90      (lastVP.IsExplode ()          != fVP.IsExplode ())          ||
91      (lastVP.GetNoOfSides ()       != fVP.GetNoOfSides ())       ||
92      (lastVP.IsMarkerNotHidden ()  != fVP.IsMarkerNotHidden ())  ||
93      (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
94      (lastVP.IsPicking ()          != fVP.IsPicking ())
95      )
96    return true;
97 
98  if (lastVP.IsDensityCulling () &&
99      (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
100    return true;
101
102  /**************************************************************
103  Section (DCUT) implemented locally.  No need to visit kernel if
104  section plane itself changes.
105  if (lastVP.IsSection () &&
106      (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
107    return true;
108  ***************************************************************/
109
110  /**************************************************************
111  Cutaways implemented locally.  No need to visit kernel if cutaway
112  planes themselves change.
113  if (lastVP.IsCutaway ()) {
114    if (lastVP.GetCutawayPlanes ().size () !=
115        fVP.GetCutawayPlanes ().size ()) return true;
116    for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
117      if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
118        return true;
119  }
120  ***************************************************************/
121
122  if (lastVP.IsExplode () &&
123      (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
124    return true;
125
126  return false;
127}
128
129void G4OpenGLStoredViewer::DrawDisplayLists () {
130#ifdef G4DEBUG_VIS_OGL
131      printf("G4OpenGLStoredViewer::DrawDisplayLists \n");
132#endif
133
134  const G4Planes& cutaways = fVP.GetCutawayPlanes();
135  G4bool cutawayUnion = fVP.IsCutaway() &&
136    fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion;
137  size_t nPasses = cutawayUnion? cutaways.size(): 1;
138#ifdef G4DEBUG_VIS_OGL
139  printf("G4OpenGLStoredViewer::DrawDisplayLists");
140#endif
141  for (size_t i = 0; i < nPasses; ++i) {
142#ifdef G4DEBUG_VIS_OGL
143    printf("+");
144#endif
145
146    if (cutawayUnion) {
147      double a[4];
148      a[0] = cutaways[i].a();
149      a[1] = cutaways[i].b();
150      a[2] = cutaways[i].c();
151      a[3] = cutaways[i].d();
152      glClipPlane (GL_CLIP_PLANE2, a);
153      glEnable (GL_CLIP_PLANE2);
154    }
155
156    if (fG4OpenGLStoredSceneHandler.fTopPODL)
157      glCallList (fG4OpenGLStoredSceneHandler.fTopPODL);
158
159    for (size_t i = 0; i < fG4OpenGLStoredSceneHandler.fTOList.size(); ++i) {
160#ifdef G4DEBUG_VIS_OGL
161      printf("-");
162#endif
163      G4OpenGLStoredSceneHandler::TO& to =
164        fG4OpenGLStoredSceneHandler.fTOList[i];
165      if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
166        glPushMatrix();
167        G4OpenGLTransform3D oglt (to.fTransform);
168        glMultMatrixd (oglt.GetGLMatrix ());
169        if (fVP.IsPicking()) glLoadName(to.fPickName);
170        G4Colour& c = to.fColour;
171        G4double bsf = 1.;  // Brightness scaling factor.
172        if (fFadeFactor > 0. && to.fEndTime < fEndTime)
173          bsf = 1. - fFadeFactor *
174            ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
175        glColor3d(bsf * c.GetRed (), bsf * c.GetGreen (), bsf * c.GetBlue ());
176        glCallList (to.fDisplayListId);
177        glPopMatrix();
178      }
179    }
180
181    if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
182  }
183#ifdef G4DEBUG_VIS_OGL
184      printf("\n");
185#endif
186
187  // Display time at "head" of time range, which is fEndTime...
188  if (fDisplayHeadTime && fEndTime < DBL_MAX) {
189    glMatrixMode (GL_PROJECTION);
190    glPushMatrix();
191    glLoadIdentity();
192    glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
193    glMatrixMode (GL_MODELVIEW);
194    glPushMatrix();
195    glLoadIdentity();
196    G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
197                        G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
198    headTimeText.SetScreenSize(fDisplayHeadTimeSize);
199    G4VisAttributes visAtts (G4Colour
200                             (fDisplayHeadTimeRed,
201                              fDisplayHeadTimeGreen,
202                              fDisplayHeadTimeBlue));
203    headTimeText.SetVisAttributes(&visAtts);
204    static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
205      G4OpenGLSceneHandler::AddPrimitive(headTimeText);
206    glMatrixMode (GL_PROJECTION);
207    glPopMatrix();
208    glMatrixMode (GL_MODELVIEW);
209    glPopMatrix();
210  }
211
212  // Display light front...
213  if (fDisplayLightFront && fEndTime < DBL_MAX) {
214    G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
215    if (lightFrontRadius > 0.) {
216      G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
217      G4Point3D circleCentre = lightFrontCentre;
218      G4double circleRadius = lightFrontRadius;
219      if (fVP.GetFieldHalfAngle() > 0.) {
220        // Perspective view.  Find horizon centre and radius...
221        G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
222          fVP.GetCurrentTargetPoint();
223        G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
224        if(sceneRadius <= 0.) sceneRadius = 1.;
225        G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
226        G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
227        G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
228        G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
229        /*
230        G4cout << "cameraPosition: " << cameraPosition
231               << ", lightFrontCentre: " << lightFrontCentre
232               << ", lightFrontRadius: " << lightFrontRadius
233               << ", lightFrontCentreDistance: " << lightFrontCentreDistance
234               << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
235               << G4endl;
236        */
237        if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
238          // Light front in front of camera...
239          G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
240          circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
241          circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
242          /*
243          G4cout << "sineHorizonAngle: " << sineHorizonAngle
244                 << ", circleCentre: " << circleCentre
245                 << ", circleRadius: " << circleRadius
246                 << G4endl;
247          */
248        } else {
249          circleRadius = -1.;
250        }
251      }
252      if (circleRadius > 0.) {
253        G4Circle lightFront(circleCentre);
254        lightFront.SetWorldRadius(circleRadius);
255        glColor3d(fDisplayLightFrontRed,
256                  fDisplayLightFrontGreen,
257                  fDisplayLightFrontBlue);
258        static_cast<G4OpenGLSceneHandler&>(fSceneHandler).
259          G4OpenGLSceneHandler::AddPrimitive(lightFront);
260      }
261    }
262  }
263}
264
265#endif
Note: See TracBrowser for help on using the repository browser.