Geant4  10.01
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 85582 2014-10-31 09:07:30Z 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  fDepthTestEnable = true;
147  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
148  fOldDisplayListColor = G4Colour();
149  do {
150  for (size_t iCutaway = 0; iCutaway < nCutaways; ++iCutaway) {
151 
152  if (cutawayUnion) {
153  double a[4];
154  a[0] = cutaways[iCutaway].a();
155  a[1] = cutaways[iCutaway].b();
156  a[2] = cutaways[iCutaway].c();
157  a[3] = cutaways[iCutaway].d();
158  glClipPlane (GL_CLIP_PLANE2, a);
159  glEnable (GL_CLIP_PLANE2);
160  }
161 
162  G4bool isPicking = fVP.IsPicking();
163 
164  for (size_t iPO = 0;
165  iPO < fG4OpenGLStoredSceneHandler.fPOList.size(); ++iPO) {
166  if (POSelected(iPO)) {
167  G4OpenGLStoredSceneHandler::PO& po =
168  fG4OpenGLStoredSceneHandler.fPOList[iPO];
169  G4Colour c = po.fColour;
170  DisplayTimePOColourModification(c,iPO);
171  const G4bool isTransparent = c.GetAlpha() < 1.;
172  if ( iPass == 1) {
173  if (isTransparent && transparency_enabled) {
174  secondPassForTransparencyRequested = true;
175  continue;
176  }
177  if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
178  thirdPassForNonHiddenMarkersRequested = true;
179  continue;
180  }
181  } else if (iPass == 2) { // Second pass for transparency.
182  if (!isTransparent) {
183  continue;
184  }
185  } else { // Third pass for non-hidden markers
186  if (!po.fMarkerOrPolyline) {
187  continue;
188  }
189  }
190  if (isPicking) glLoadName(po.fPickName);
191  if ((iPO == 0) || (c != fOldDisplayListColor)) {
192  fOldDisplayListColor = c;
193  if (transparency_enabled) {
194  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
195  } else {
196  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
197  }
198  }
199  if (po.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
200  if (fDepthTestEnable !=false) {
201  glDisable (GL_DEPTH_TEST);
202  fDepthTestEnable = false;
203  }
204  } else {
205  if (fDepthTestEnable !=true) {
206  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
207  fDepthTestEnable = true;
208  }
209  }
210  if (po.fpG4TextPlus) {
211  if (po.fpG4TextPlus->fProcessing2D) {
212  glMatrixMode (GL_PROJECTION);
213  glPushMatrix();
214  glLoadIdentity();
215  glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
216  glMatrixMode (GL_MODELVIEW);
217  glPushMatrix();
218  glLoadIdentity();
219  G4OpenGLTransform3D oglt (po.fTransform);
220  glMultMatrixd (oglt.GetGLMatrix ());
221  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
222  (po.fpG4TextPlus->fG4Text);
223  } else {
224  glPushMatrix();
225  G4OpenGLTransform3D oglt (po.fTransform);
226  glMultMatrixd (oglt.GetGLMatrix ());
227  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
228  (po.fpG4TextPlus->fG4Text);
229  glPopMatrix();
230  }
231 
232  if (po.fpG4TextPlus->fProcessing2D) {
233  glMatrixMode (GL_PROJECTION);
234  glPopMatrix();
235  glMatrixMode (GL_MODELVIEW);
236  glPopMatrix();
237  }
238  } else {
239  glPushMatrix();
240  G4OpenGLTransform3D oglt (po.fTransform);
241  glMultMatrixd (oglt.GetGLMatrix ());
242  glCallList (po.fDisplayListId);
243  glPopMatrix();
244  }
245  }
246  }
247 
248  G4Transform3D lastMatrixTransform;
249  G4bool first = true;
250 
251  for (size_t iTO = 0;
252  iTO < fG4OpenGLStoredSceneHandler.fTOList.size(); ++iTO) {
253  if (TOSelected(iTO)) {
254  G4OpenGLStoredSceneHandler::TO& to =
255  fG4OpenGLStoredSceneHandler.fTOList[iTO];
256  const G4Colour& c = to.fColour;
257  const G4bool isTransparent = c.GetAlpha() < 1.;
258  if ( iPass == 1) {
259  if (isTransparent && transparency_enabled) {
260  secondPassForTransparencyRequested = true;
261  continue;
262  }
263  if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
264  thirdPassForNonHiddenMarkersRequested = true;
265  continue;
266  }
267  } else if (iPass == 2) { // Second pass for transparency.
268  if (!isTransparent) {
269  continue;
270  }
271  } else { // Third pass for non-hidden markers
272  if (!to.fMarkerOrPolyline) {
273  continue;
274  }
275  }
276  if (to.fMarkerOrPolyline && fVP.IsMarkerNotHidden()) {
277  if (fDepthTestEnable !=false) {
278  glDisable (GL_DEPTH_TEST);
279  fDepthTestEnable = false;
280  }
281  } else {
282  if (fDepthTestEnable !=true) {
283  glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
284  fDepthTestEnable = true;
285  }
286  }
287  if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
288  if (fVP.IsPicking()) glLoadName(to.fPickName);
289  if (to.fpG4TextPlus) {
290  if (to.fpG4TextPlus->fProcessing2D) {
291  glMatrixMode (GL_PROJECTION);
292  glPushMatrix();
293  glLoadIdentity();
294  glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
295  glMatrixMode (GL_MODELVIEW);
296  glPushMatrix();
297  glLoadIdentity();
298  }
299  G4OpenGLTransform3D oglt (to.fTransform);
300  glMultMatrixd (oglt.GetGLMatrix ());
301  if (transparency_enabled) {
302  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
303  } else {
304  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
305  }
306  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive
307  (to.fpG4TextPlus->fG4Text);
308  if (to.fpG4TextPlus->fProcessing2D) {
309  glMatrixMode (GL_PROJECTION);
310  glPopMatrix();
311  glMatrixMode (GL_MODELVIEW);
312  glPopMatrix();
313  }
314  } else {
315  if (to.fTransform != lastMatrixTransform) {
316  if (! first) {
317  glPopMatrix();
318  }
319  first = false;
320  glPushMatrix();
321  G4OpenGLTransform3D oglt (to.fTransform);
322  glMultMatrixd (oglt.GetGLMatrix ());
323  }
324  const G4Colour& cc = to.fColour;
325  if (fFadeFactor > 0. && to.fEndTime < fEndTime) {
326  // Brightness scaling factor
327  G4double bsf = 1. - fFadeFactor *
328  ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
329  const G4Colour& bg = fVP.GetBackgroundColour();
330  if (transparency_enabled) {
331  glColor4d
332  (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
333  bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
334  bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue(),
335  bsf * cc.GetAlpha() + (1. - bsf) * bg.GetAlpha());
336  } else {
337  glColor3d
338  (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
339  bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
340  bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue());
341  }
342  } else {
343  if (transparency_enabled) {
344  glColor4d(cc.GetRed(),cc.GetGreen(),cc.GetBlue(),cc.GetAlpha());
345  } else {
346  glColor3d(cc.GetRed(),cc.GetGreen(),cc.GetBlue());
347  }
348  }
349  glCallList (to.fDisplayListId);
350  }
351  if (to.fTransform != lastMatrixTransform) {
352  lastMatrixTransform = to.fTransform;
353  }
354  }
355  }
356  }
357  if (! first) {
358  glPopMatrix();
359  }
360 
361  if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
362  } // iCutaway
363 
364  if (iPass == 2) secondPassForTransparencyRequested = false; // Done.
365  if (iPass == 3) thirdPassForNonHiddenMarkersRequested = false; // Done.
366 
367  if (secondPassForTransparencyRequested) iPass = 2;
368  else if (thirdPassForNonHiddenMarkersRequested) iPass = 3;
369  else break;
370 
371  } while (true);
372 
373  // Display time at "head" of time range, which is fEndTime...
374  if (fDisplayHeadTime && fEndTime < DBL_MAX) {
375  glMatrixMode (GL_PROJECTION);
376  glPushMatrix();
377  glLoadIdentity();
378  glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
379  glMatrixMode (GL_MODELVIEW);
380  glPushMatrix();
381  glLoadIdentity();
382  G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
383  G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
384  headTimeText.SetScreenSize(fDisplayHeadTimeSize);
385  G4VisAttributes visAtts (G4Colour
386  (fDisplayHeadTimeRed,
387  fDisplayHeadTimeGreen,
388  fDisplayHeadTimeBlue));
389  headTimeText.SetVisAttributes(&visAtts);
390  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(headTimeText);
391  glMatrixMode (GL_PROJECTION);
392  glPopMatrix();
393  glMatrixMode (GL_MODELVIEW);
394  glPopMatrix();
395  }
396 
397  // Display light front...
398  if (fDisplayLightFront && fEndTime < DBL_MAX) {
399  G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
400  if (lightFrontRadius > 0.) {
401  G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
402  G4Point3D circleCentre = lightFrontCentre;
403  G4double circleRadius = lightFrontRadius;
404  if (fVP.GetFieldHalfAngle() > 0.) {
405  // Perspective view. Find horizon centre and radius...
406  G4Point3D targetPoint = fSceneHandler.GetScene()->GetStandardTargetPoint() +
407  fVP.GetCurrentTargetPoint();
408  G4double sceneRadius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
409  if(sceneRadius <= 0.) sceneRadius = 1.;
410  G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
411  G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
412  G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
413  G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
414  /*
415  G4cout << "cameraPosition: " << cameraPosition
416  << ", lightFrontCentre: " << lightFrontCentre
417  << ", lightFrontRadius: " << lightFrontRadius
418  << ", lightFrontCentreDistance: " << lightFrontCentreDistance
419  << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
420  << G4endl;
421  */
422  if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
423  // Light front in front of camera...
424  G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
425  circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
426  circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
427  /*
428  G4cout << "sineHorizonAngle: " << sineHorizonAngle
429  << ", circleCentre: " << circleCentre
430  << ", circleRadius: " << circleRadius
431  << G4endl;
432  */
433  } else {
434  circleRadius = -1.;
435  }
436  }
437  if (circleRadius > 0.) {
438  G4Circle lightFront(circleCentre);
439  lightFront.SetWorldRadius(circleRadius);
440  glColor3d(fDisplayLightFrontRed,
441  fDisplayLightFrontGreen,
442  fDisplayLightFrontBlue);
443  fOpenGLSceneHandler.G4OpenGLSceneHandler::AddPrimitive(lightFront);
444  }
445  }
446  }
447 }
448 
449 #endif
Definition: G4Text.hh:73
const G4Colour & GetBackgroundColour() const
G4double GetAlpha() const
Definition: G4Colour.hh:142
G4double GetVisibleDensity() const
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 a
Definition: TRTMaterials.hh:39
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