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