Geant4  10.03
G4OpenGLStoredViewer.cc
Go to the documentation of this file.
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 101714 2016-11-22 08:53:13Z gcosmo $
28 //
29 //
30 // Andrew Walkden 7th February 1997
31 // Class G4OpenGLStoredViewer : Encapsulates the `storedness' of
32 // an OpenGL view, for inheritance by
33 // derived (X, Xm...) classes.
34 
35 #ifdef G4VIS_BUILD_OPENGL_DRIVER
36 
37 #include "G4OpenGLStoredViewer.hh"
38 
39 #include "G4PhysicalConstants.hh"
41 #include "G4Text.hh"
42 #include "G4Circle.hh"
43 #include "G4UnitsTable.hh"
44 #include "G4Scene.hh"
45 #include "G4OpenGLTransform3D.hh"
46 
47 G4OpenGLStoredViewer::G4OpenGLStoredViewer
48 (G4OpenGLStoredSceneHandler& sceneHandler):
49 G4VViewer (sceneHandler, -1),
50 G4OpenGLViewer (sceneHandler),
51 fG4OpenGLStoredSceneHandler (sceneHandler),
52 fDepthTestEnable(true)
53 {
54  fLastVP = fDefaultVP; // Not sure if this gets executed before or
55  // after G4VViewer::G4VViewer!! Doesn't matter much.
56 }
57 
58 G4OpenGLStoredViewer::~G4OpenGLStoredViewer () {}
59 
60 void G4OpenGLStoredViewer::KernelVisitDecision () {
61 
62  // If there's a significant difference with the last view parameters
63  // of either the scene handler or this viewer, trigger a rebuild.
64 
65  if (!fG4OpenGLStoredSceneHandler.fTopPODL ||
66  CompareForKernelVisit(fLastVP)) {
67  NeedKernelVisit ();
68  }
69  fLastVP = fVP;
70 }
71 
72 G4bool G4OpenGLStoredViewer::CompareForKernelVisit(G4ViewParameters& lastVP) {
73 
74  if (
75  (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) ||
76  (lastVP.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) ||
77  (lastVP.IsCulling () != fVP.IsCulling ()) ||
78  (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
79  (lastVP.IsDensityCulling () != fVP.IsDensityCulling ()) ||
80  (lastVP.IsCullingCovered () != fVP.IsCullingCovered ()) ||
81  (lastVP.IsSection () != fVP.IsSection ()) ||
82  // Section (DCUT) implemented locally. But still need to visit
83  // kernel if status changes so that back plane culling can be
84  // switched.
85  (lastVP.IsCutaway () != fVP.IsCutaway ()) ||
86  // Cutaways implemented locally. But still need to visit kernel
87  // if status changes so that back plane culling can be switched.
88  (lastVP.IsExplode () != fVP.IsExplode ()) ||
89  (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) ||
90  (lastVP.GetDefaultVisAttributes()->GetColour() !=
91  fVP.GetDefaultVisAttributes()->GetColour()) ||
93  fVP.GetDefaultTextVisAttributes()->GetColour()) ||
94  (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
95  (lastVP.IsPicking () != fVP.IsPicking ()) ||
96  (lastVP.GetVisAttributesModifiers() !=
97  fVP.GetVisAttributesModifiers())
98  )
99  return true;
100 
101  if (lastVP.IsDensityCulling () &&
102  (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
103  return true;
104 
105  /**************************************************************
106  Section (DCUT) implemented locally. No need to visit kernel if
107  section plane itself changes.
108  if (lastVP.IsSection () &&
109  (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
110  return true;
111  ***************************************************************/
112 
113  /**************************************************************
114  Cutaways implemented locally. No need to visit kernel if cutaway
115  planes themselves change.
116  if (lastVP.IsCutaway ()) {
117  if (lastVP.GetCutawayPlanes ().size () !=
118  fVP.GetCutawayPlanes ().size ()) return true;
119  for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
120  if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
121  return true;
122  }
123  ***************************************************************/
124 
125  if (lastVP.IsExplode () &&
126  (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
127  return true;
128 
129  return false;
130 }
131 
132 void G4OpenGLStoredViewer::DrawDisplayLists () {
133 
134  const G4Planes& cutaways = fVP.GetCutawayPlanes();
135  G4bool cutawayUnion = fVP.IsCutaway() &&
136  fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion;
137  const size_t nCutaways = cutawayUnion? cutaways.size(): 1;
138  G4int iPass = 1;
139  G4bool secondPassForTransparencyRequested = false;
140  G4bool thirdPassForNonHiddenMarkersRequested = false;
141  fDepthTestEnable = true;
142  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
143  do {
144  for (size_t iCutaway = 0; iCutaway < nCutaways; ++iCutaway) {
145 
146  if (cutawayUnion) {
147  double a[4];
148  a[0] = cutaways[iCutaway].a();
149  a[1] = cutaways[iCutaway].b();
150  a[2] = cutaways[iCutaway].c();
151  a[3] = cutaways[iCutaway].d();
152  glClipPlane (GL_CLIP_PLANE2, a);
153  glEnable (GL_CLIP_PLANE2);
154  }
155 
156  G4bool isPicking = fVP.IsPicking();
157 
158  for (size_t iPO = 0;
159  iPO < fG4OpenGLStoredSceneHandler.fPOList.size(); ++iPO) {
160  if (POSelected(iPO)) {
161  G4OpenGLStoredSceneHandler::PO& po =
162  fG4OpenGLStoredSceneHandler.fPOList[iPO];
163  G4Colour c = po.fColour;
164  DisplayTimePOColourModification(c,iPO);
165  const G4bool isTransparent = c.GetAlpha() < 1.;
166  if ( iPass == 1) {
167  if (isTransparent && transparency_enabled) {
168  secondPassForTransparencyRequested = true;
169  continue;
170  }
171  if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
172  thirdPassForNonHiddenMarkersRequested = true;
173  continue;
174  }
175  } else if (iPass == 2) { // Second pass for transparency.
176  if (!isTransparent) {
177  continue;
178  }
179  } else { // Third pass for non-hidden markers
180  if (!po.fMarkerOrPolyline) {
181  continue;
182  }
183  }
184  if (isPicking) glLoadName(po.fPickName);
185  if (transparency_enabled) {
186  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
187  } else {
188  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
189  }
190  if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
191  if (fDepthTestEnable !=false) {
192  glDisable (GL_DEPTH_TEST);
193  fDepthTestEnable = false;
194  }
195  } else {
196  if (fDepthTestEnable !=true) {
197  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
198  fDepthTestEnable = true;
199  }
200  }
201  if (po.fpG4TextPlus) {
202  if (po.fpG4TextPlus->fProcessing2D) {
203  glMatrixMode (GL_PROJECTION);
204  glPushMatrix();
205  glLoadIdentity();
206  g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
207  glMatrixMode (GL_MODELVIEW);
208  glPushMatrix();
209  glLoadIdentity();
210  G4OpenGLTransform3D oglt (po.fTransform);
211  glMultMatrixd (oglt.GetGLMatrix ());
212  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
213  (po.fpG4TextPlus->fG4Text);
214  } else {
215  glPushMatrix();
216  G4OpenGLTransform3D oglt (po.fTransform);
217  glMultMatrixd (oglt.GetGLMatrix ());
218  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
219  (po.fpG4TextPlus->fG4Text);
220  glPopMatrix();
221  }
222 
223  if (po.fpG4TextPlus->fProcessing2D) {
224  glMatrixMode (GL_PROJECTION);
225  glPopMatrix();
226  glMatrixMode (GL_MODELVIEW);
227  glPopMatrix();
228  }
229  } else {
230  glPushMatrix();
231  G4OpenGLTransform3D oglt (po.fTransform);
232  glMultMatrixd (oglt.GetGLMatrix ());
233  glCallList (po.fDisplayListId);
234  glPopMatrix();
235  }
236  }
237  }
238 
239  G4Transform3D lastMatrixTransform;
240  G4bool first = true;
241 
242  for (size_t iTO = 0;
243  iTO < fG4OpenGLStoredSceneHandler.fTOList.size(); ++iTO) {
244  if (TOSelected(iTO)) {
245  G4OpenGLStoredSceneHandler::TO& to =
246  fG4OpenGLStoredSceneHandler.fTOList[iTO];
247  const G4Colour& c = to.fColour;
248  const G4bool isTransparent = c.GetAlpha() < 1.;
249  if ( iPass == 1) {
250  if (isTransparent && transparency_enabled) {
251  secondPassForTransparencyRequested = true;
252  continue;
253  }
254  if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
255  thirdPassForNonHiddenMarkersRequested = true;
256  continue;
257  }
258  } else if (iPass == 2) { // Second pass for transparency.
259  if (!isTransparent) {
260  continue;
261  }
262  } else { // Third pass for non-hidden markers
263  if (!to.fMarkerOrPolyline) {
264  continue;
265  }
266  }
267  if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
268  if (fDepthTestEnable !=false) {
269  glDisable (GL_DEPTH_TEST);
270  fDepthTestEnable = false;
271  }
272  } else {
273  if (fDepthTestEnable !=true) {
274  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
275  fDepthTestEnable = true;
276  }
277  }
278  if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
279  if (fVP.IsPicking()) glLoadName(to.fPickName);
280  if (to.fpG4TextPlus) {
281  if (to.fpG4TextPlus->fProcessing2D) {
282  glMatrixMode (GL_PROJECTION);
283  glPushMatrix();
284  glLoadIdentity();
285  g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
286  glMatrixMode (GL_MODELVIEW);
287  glPushMatrix();
288  glLoadIdentity();
289  }
290  G4OpenGLTransform3D oglt (to.fTransform);
291  glMultMatrixd (oglt.GetGLMatrix ());
292  if (transparency_enabled) {
293  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
294  } else {
295  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
296  }
297  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
298  (to.fpG4TextPlus->fG4Text);
299  if (to.fpG4TextPlus->fProcessing2D) {
300  glMatrixMode (GL_PROJECTION);
301  glPopMatrix();
302  glMatrixMode (GL_MODELVIEW);
303  glPopMatrix();
304  }
305  } else {
306  if (to.fTransform != lastMatrixTransform) {
307  if (! first) {
308  glPopMatrix();
309  }
310  first = false;
311  glPushMatrix();
312  G4OpenGLTransform3D oglt (to.fTransform);
313  glMultMatrixd (oglt.GetGLMatrix ());
314  }
315  const G4Colour& cc = to.fColour;
316  if (fFadeFactor > 0. && to.fEndTime < fEndTime) {
317  // Brightness scaling factor
318  G4double bsf = 1. - fFadeFactor *
319  ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
320  const G4Colour& bg = fVP.GetBackgroundColour();
321  if (transparency_enabled) {
322  glColor4d
323  (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
324  bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
325  bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue(),
326  bsf * cc.GetAlpha() + (1. - bsf) * bg.GetAlpha());
327  } else {
328  glColor3d
329  (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
330  bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
331  bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue());
332  }
333  } else {
334  if (transparency_enabled) {
335  glColor4d(cc.GetRed(),cc.GetGreen(),cc.GetBlue(),cc.GetAlpha());
336  } else {
337  glColor3d(cc.GetRed(),cc.GetGreen(),cc.GetBlue());
338  }
339  }
340  glCallList (to.fDisplayListId);
341  }
342  if (to.fTransform != lastMatrixTransform) {
343  lastMatrixTransform = to.fTransform;
344  }
345  }
346  }
347  }
348  if (! first) {
349  glPopMatrix();
350  }
351 
352  if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
353  } // iCutaway
354 
355  if (iPass == 2) secondPassForTransparencyRequested = false; // Done.
356  if (iPass == 3) thirdPassForNonHiddenMarkersRequested = false; // Done.
357 
358  if (secondPassForTransparencyRequested) iPass = 2;
359  else if (thirdPassForNonHiddenMarkersRequested) iPass = 3;
360  else break;
361 
362  } while (true);
363 
364  // Display time at "head" of time range, which is fEndTime...
365  if (fDisplayHeadTime && fEndTime < DBL_MAX) {
366  glMatrixMode (GL_PROJECTION);
367  glPushMatrix();
368  glLoadIdentity();
369  g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
370  glMatrixMode (GL_MODELVIEW);
371  glPushMatrix();
372  glLoadIdentity();
373  G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
374  G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
375  headTimeText.SetScreenSize(fDisplayHeadTimeSize);
376  G4VisAttributes visAtts (G4Colour
377  (fDisplayHeadTimeRed,
378  fDisplayHeadTimeGreen,
379  fDisplayHeadTimeBlue));
380  headTimeText.SetVisAttributes(&visAtts);
381  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(headTimeText);
382  glMatrixMode (GL_PROJECTION);
383  glPopMatrix();
384  glMatrixMode (GL_MODELVIEW);
385  glPopMatrix();
386  }
387 
388  // Display light front...
389  if (fDisplayLightFront && fEndTime < DBL_MAX) {
390  G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
391  if (lightFrontRadius > 0.) {
392  G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
393  G4Point3D circleCentre = lightFrontCentre;
394  G4double circleRadius = lightFrontRadius;
395  if (fVP.GetFieldHalfAngle() > 0.) {
396  // Perspective view. Find horizon centre and radius...
397  G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
398  fVP.GetCurrentTargetPoint();
399  G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
400  if(sceneRadius <= 0.) sceneRadius = 1.;
401  G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
402  G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
403  G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
404  G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
405  /*
406  G4cout << "cameraPosition: " << cameraPosition
407  << ", lightFrontCentre: " << lightFrontCentre
408  << ", lightFrontRadius: " << lightFrontRadius
409  << ", lightFrontCentreDistance: " << lightFrontCentreDistance
410  << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
411  << G4endl;
412  */
413  if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
414  // Light front in front of camera...
415  G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
416  circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
417  circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
418  /*
419  G4cout << "sineHorizonAngle: " << sineHorizonAngle
420  << ", circleCentre: " << circleCentre
421  << ", circleRadius: " << circleRadius
422  << G4endl;
423  */
424  } else {
425  circleRadius = -1.;
426  }
427  }
428  if (circleRadius > 0.) {
429  G4Circle lightFront(circleCentre);
430  lightFront.SetWorldRadius(circleRadius);
431  glColor3d(fDisplayLightFrontRed,
432  fDisplayLightFrontGreen,
433  fDisplayLightFrontBlue);
434  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(lightFront);
435  }
436  }
437  }
438 }
439 
440 #endif
Definition: G4Text.hh:73
const G4Colour & GetBackgroundColour() const
G4double GetAlpha() const
Definition: G4Colour.hh:142
G4double GetVisibleDensity() const
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
G4bool IsCullingInvisible() const
G4double GetExplodeFactor() const
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition: G4Colour.cc:126
const G4Colour & GetColour() const
G4bool IsDensityCulling() const
HepGeom::Point3D< G4double > G4Point3D
Definition: G4Point3D.hh:35
HepGeom::Vector3D< G4double > G4Vector3D
Definition: G4Vector3D.hh:35
G4double GetBlue() const
Definition: G4Colour.hh:141
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
int G4int
Definition: G4Types.hh:78
const std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() const
G4bool IsAuxEdgeVisible() const
G4double GetRed() const
Definition: G4Colour.hh:139
bool G4bool
Definition: G4Types.hh:79
G4double GetGreen() const
Definition: G4Colour.hh:140
std::vector< G4Plane3D > G4Planes
HepGeom::Transform3D G4Transform3D
G4bool IsCullingCovered() const
const G4VisAttributes * GetDefaultTextVisAttributes() const
G4bool IsExplode() const
G4bool IsSection() const
G4bool IsCutaway() const
G4int GetNoOfSides() const
DrawingStyle GetDrawingStyle() const
G4bool IsPicking() const
const G4VisAttributes * GetDefaultVisAttributes() const
double G4double
Definition: G4Types.hh:76
#define DBL_MAX
Definition: templates.hh:83
G4bool IsCulling() const