Geant4-11
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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//
28//
29// Andrew Walkden 7th February 1997
30// Class G4OpenGLStoredViewer : Encapsulates the `storedness' of
31// an OpenGL view, for inheritance by
32// derived (X, Xm...) classes.
33
35
38#include "G4Text.hh"
39#include "G4Circle.hh"
40#include "G4UnitsTable.hh"
41#include "G4Scene.hh"
43
45(G4OpenGLStoredSceneHandler& sceneHandler):
46G4VViewer (sceneHandler, -1),
47G4OpenGLViewer (sceneHandler),
48fG4OpenGLStoredSceneHandler (sceneHandler),
49fDepthTestEnable(true)
50{
51 fLastVP = fDefaultVP; // Update in sub-class after KernelVisitDecision
52}
53
55
57
58 // If there's a significant difference with the last view parameters
59 // of either the scene handler or this viewer, trigger a rebuild.
60
64 }
65}
66
68
69 if (
70 (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) ||
72 (lastVP.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) ||
73 (lastVP.IsCulling () != fVP.IsCulling ()) ||
74 (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
75 (lastVP.IsDensityCulling () != fVP.IsDensityCulling ()) ||
76 (lastVP.IsCullingCovered () != fVP.IsCullingCovered ()) ||
77 (lastVP.GetCBDAlgorithmNumber() !=
79 // Note: Section and Cutaway can reveal back-facing faces. If
80 // backface culling is implemented, the image can look strange because
81 // the back-facing faces are not there. For the moment, we have disabled
82 // (commented out) backface culling (it seems not to affect performance -
83 // in fact, performance seems to improve), so there is no problem.
84 (lastVP.IsSection () != fVP.IsSection ()) ||
85 // Section (DCUT) is NOT implemented locally so we need to visit the kernel.
86 // (lastVP.IsCutaway () != fVP.IsCutaway ()) ||
87 // Cutaways are implemented locally so we do not need to visit the kernel.
88 (lastVP.IsExplode () != fVP.IsExplode ()) ||
89 (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) ||
92 (lastVP.IsMarkerNotHidden () != fVP.IsMarkerNotHidden ()) ||
98 (lastVP.IsPicking () != fVP.IsPicking ()) ||
99 (lastVP.GetVisAttributesModifiers() !=
101 (lastVP.IsSpecialMeshRendering() !=
103 )
104 return true;
105
106 if (lastVP.IsDensityCulling () &&
107 (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
108 return true;
109
110// /**************************************************************
111// If section (DCUT) is implemented locally, comment this out.
112 if (lastVP.IsSection () &&
113 (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
114 return true;
115// ***************************************************************/
116
117 /**************************************************************
118 If cutaways are implemented locally, comment this out.
119 if (lastVP.IsCutaway ()) {
120 if (lastVP.GetCutawayPlanes ().size () !=
121 fVP.GetCutawayPlanes ().size ()) return true;
122 for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
123 if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
124 return true;
125 }
126 ***************************************************************/
127
128 if (lastVP.GetCBDAlgorithmNumber() > 0) {
129 if (lastVP.GetCBDParameters().size() != fVP.GetCBDParameters().size()) return true;
130 else if (lastVP.GetCBDParameters() != fVP.GetCBDParameters()) return true;
131 }
132
133 if (lastVP.IsExplode () &&
134 (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
135 return true;
136
137 if (lastVP.IsSpecialMeshRendering() &&
139 return true;
140
141 // Time window parameters operate on the existing database so no need
142 // to rebuild even if they change.
143
144 return false;
145}
146
148
149 // We moved these from G4OpenGLViewer to G4ViewParamaters. To avoid
150 // editing many lines below we introduce these convenient aliases.
151#define CONVENIENT_DOUBLE_ALIAS(q) const G4double& f##q = fVP.Get##q();
152#define CONVENIENT_BOOL_ALIAS(q) const G4bool& f##q = fVP.Is##q();
153 CONVENIENT_DOUBLE_ALIAS(StartTime)
155 CONVENIENT_DOUBLE_ALIAS(FadeFactor)
156 CONVENIENT_BOOL_ALIAS(DisplayHeadTime)
157 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeX)
158 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeY)
159 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeSize)
160 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeRed)
161 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeGreen)
162 CONVENIENT_DOUBLE_ALIAS(DisplayHeadTimeBlue)
163 CONVENIENT_BOOL_ALIAS(DisplayLightFront)
164 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontX)
165 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontY)
166 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontZ)
167 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontT)
168 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontRed)
169 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontGreen)
170 CONVENIENT_DOUBLE_ALIAS(DisplayLightFrontBlue)
171
172 const G4Planes& cutaways = fVP.GetCutawayPlanes();
173 G4bool cutawayUnion = fVP.IsCutaway() &&
175 const size_t nCutaways = cutawayUnion? cutaways.size(): 1;
176 G4int iPass = 1;
177 G4bool secondPassForTransparencyRequested = false;
178 G4bool thirdPassForNonHiddenMarkersRequested = false;
179 fDepthTestEnable = true;
180 glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
181 do {
182 for (size_t iCutaway = 0; iCutaway < nCutaways; ++iCutaway) {
183
184 if (cutawayUnion) {
185 double a[4];
186 a[0] = cutaways[iCutaway].a();
187 a[1] = cutaways[iCutaway].b();
188 a[2] = cutaways[iCutaway].c();
189 a[3] = cutaways[iCutaway].d();
190 glClipPlane (GL_CLIP_PLANE2, a);
191 glEnable (GL_CLIP_PLANE2);
192 }
193
194 G4bool isPicking = fVP.IsPicking();
195
196 for (size_t iPO = 0;
197 iPO < fG4OpenGLStoredSceneHandler.fPOList.size(); ++iPO) {
198 if (POSelected(iPO)) {
201 G4Colour c = po.fColour;
203 const G4bool isTransparent = c.GetAlpha() < 1.;
204 if ( iPass == 1) {
205 if (isTransparent && transparency_enabled) {
206 secondPassForTransparencyRequested = true;
207 continue;
208 }
210 thirdPassForNonHiddenMarkersRequested = true;
211 continue;
212 }
213 } else if (iPass == 2) { // Second pass for transparency.
214 if (!isTransparent) {
215 continue;
216 }
217 } else { // Third pass for non-hidden markers
218 if (!po.fMarkerOrPolyline) {
219 continue;
220 }
221 }
222 if (isPicking) glLoadName(po.fPickName);
224 glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
225 } else {
226 glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
227 }
229 if (fDepthTestEnable !=false) {
230 glDisable (GL_DEPTH_TEST);
231 fDepthTestEnable = false;
232 }
233 } else {
234 if (fDepthTestEnable !=true) {
235 glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
236 fDepthTestEnable = true;
237 }
238 }
239 if (po.fpG4TextPlus) {
240 if (po.fpG4TextPlus->fProcessing2D) {
241 glMatrixMode (GL_PROJECTION);
242 glPushMatrix();
243 glLoadIdentity();
244 g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
245 glMatrixMode (GL_MODELVIEW);
246 glPushMatrix();
247 glLoadIdentity();
249 glMultMatrixd (oglt.GetGLMatrix ());
250 // This text is from a PODL. We don't want to create a new PODL.
252 } else {
253 glPushMatrix();
255 glMultMatrixd (oglt.GetGLMatrix ());
256 // This text is from a PODL. We don't want to create a new PODL.
258 glPopMatrix();
259 }
260
261 if (po.fpG4TextPlus->fProcessing2D) {
262 glMatrixMode (GL_PROJECTION);
263 glPopMatrix();
264 glMatrixMode (GL_MODELVIEW);
265 glPopMatrix();
266 }
267 } else {
268 glPushMatrix();
270 glMultMatrixd (oglt.GetGLMatrix ());
271 glCallList (po.fDisplayListId);
272 glPopMatrix();
273 }
274 }
275 }
276
277 G4Transform3D lastMatrixTransform;
278 G4bool first = true;
279
280 for (size_t iTO = 0;
281 iTO < fG4OpenGLStoredSceneHandler.fTOList.size(); ++iTO) {
282 if (TOSelected(iTO)) {
285 const G4Colour& c = to.fColour;
286 const G4bool isTransparent = c.GetAlpha() < 1.;
287 if ( iPass == 1) {
288 if (isTransparent && transparency_enabled) {
289 secondPassForTransparencyRequested = true;
290 continue;
291 }
293 thirdPassForNonHiddenMarkersRequested = true;
294 continue;
295 }
296 } else if (iPass == 2) { // Second pass for transparency.
297 if (!isTransparent) {
298 continue;
299 }
300 } else { // Third pass for non-hidden markers
301 if (!to.fMarkerOrPolyline) {
302 continue;
303 }
304 }
306 if (fDepthTestEnable !=false) {
307 glDisable (GL_DEPTH_TEST);
308 fDepthTestEnable = false;
309 }
310 } else {
311 if (fDepthTestEnable !=true) {
312 glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);
313 fDepthTestEnable = true;
314 }
315 }
316 if (to.fEndTime >= fStartTime && to.fStartTime <= fEndTime) {
317 if (fVP.IsPicking()) glLoadName(to.fPickName);
318 if (to.fpG4TextPlus) {
319 if (to.fpG4TextPlus->fProcessing2D) {
320 glMatrixMode (GL_PROJECTION);
321 glPushMatrix();
322 glLoadIdentity();
323 g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
324 glMatrixMode (GL_MODELVIEW);
325 glPushMatrix();
326 glLoadIdentity();
327 }
329 glMultMatrixd (oglt.GetGLMatrix ());
330 // This text is from a TODL. We don't want to create a new TODL.
332 if (to.fpG4TextPlus->fProcessing2D) {
333 glMatrixMode (GL_PROJECTION);
334 glPopMatrix();
335 glMatrixMode (GL_MODELVIEW);
336 glPopMatrix();
337 }
338 } else {
339 if (to.fTransform != lastMatrixTransform) {
340 if (! first) {
341 glPopMatrix();
342 }
343 first = false;
344 glPushMatrix();
346 glMultMatrixd (oglt.GetGLMatrix ());
347 }
348 const G4Colour& cc = to.fColour;
349 if (fFadeFactor > 0. && to.fEndTime < fEndTime) {
350 // Brightness scaling factor
351 G4double bsf = 1. - fFadeFactor *
352 ((fEndTime - to.fEndTime) / (fEndTime - fStartTime));
353 const G4Colour& bg = fVP.GetBackgroundColour();
355 glColor4d
356 (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
357 bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
358 bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue(),
359 bsf * cc.GetAlpha() + (1. - bsf) * bg.GetAlpha());
360 } else {
361 glColor3d
362 (bsf * cc.GetRed() + (1. - bsf) * bg.GetRed(),
363 bsf * cc.GetGreen() + (1. - bsf) * bg.GetGreen(),
364 bsf * cc.GetBlue() + (1. - bsf) * bg.GetBlue());
365 }
366 } else {
368 glColor4d(cc.GetRed(),cc.GetGreen(),cc.GetBlue(),cc.GetAlpha());
369 } else {
370 glColor3d(cc.GetRed(),cc.GetGreen(),cc.GetBlue());
371 }
372 }
373 glCallList (to.fDisplayListId);
374 }
375 if (to.fTransform != lastMatrixTransform) {
376 lastMatrixTransform = to.fTransform;
377 }
378 }
379 }
380 }
381 if (! first) {
382 glPopMatrix();
383 }
384
385 if (cutawayUnion) glDisable (GL_CLIP_PLANE2);
386 } // iCutaway
387
388 if (iPass == 2) secondPassForTransparencyRequested = false; // Done.
389 if (iPass == 3) thirdPassForNonHiddenMarkersRequested = false; // Done.
390
391 if (secondPassForTransparencyRequested) iPass = 2;
392 else if (thirdPassForNonHiddenMarkersRequested) iPass = 3;
393 else break;
394
395 } while (true);
396
397 // Display time at "head" of time range, which is fEndTime...
398 if (fDisplayHeadTime && fEndTime < G4VisAttributes::fVeryLongTime) {
399 glMatrixMode (GL_PROJECTION);
400 glPushMatrix();
401 glLoadIdentity();
402 g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
403 glMatrixMode (GL_MODELVIEW);
404 glPushMatrix();
405 glLoadIdentity();
406 G4Text headTimeText(G4BestUnit(fEndTime,"Time"),
407 G4Point3D(fDisplayHeadTimeX, fDisplayHeadTimeY, 0.));
408 headTimeText.SetScreenSize(fDisplayHeadTimeSize);
410 (fDisplayHeadTimeRed,
411 fDisplayHeadTimeGreen,
412 fDisplayHeadTimeBlue));
413 headTimeText.SetVisAttributes(&visAtts);
414 AddPrimitiveForASingleFrame(headTimeText);
415 glMatrixMode (GL_PROJECTION);
416 glPopMatrix();
417 glMatrixMode (GL_MODELVIEW);
418 glPopMatrix();
419 }
420
421 // Display light front...
422 if (fDisplayLightFront && fEndTime < G4VisAttributes::fVeryLongTime) {
423 G4double lightFrontRadius = (fEndTime - fDisplayLightFrontT) * c_light;
424 if (lightFrontRadius > 0.) {
425 G4Point3D lightFrontCentre(fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ);
426 G4Point3D circleCentre = lightFrontCentre;
427 G4double circleRadius = lightFrontRadius;
428 if (fVP.GetFieldHalfAngle() > 0.) {
429 // Perspective view. Find horizon centre and radius...
433 if(sceneRadius <= 0.) sceneRadius = 1.;
434 G4double cameraDistance = fVP.GetCameraDistance(sceneRadius);
435 G4Point3D cameraPosition = targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
436 G4Vector3D lightFrontToCameraDirection = cameraPosition - lightFrontCentre;
437 G4double lightFrontCentreDistance = lightFrontToCameraDirection.mag();
438 /*
439 G4cout << "cameraPosition: " << cameraPosition
440 << ", lightFrontCentre: " << lightFrontCentre
441 << ", lightFrontRadius: " << lightFrontRadius
442 << ", lightFrontCentreDistance: " << lightFrontCentreDistance
443 << ", dot: " << lightFrontToCameraDirection * fVP.GetViewpointDirection()
444 << G4endl;
445 */
446 if (lightFrontToCameraDirection * fVP.GetViewpointDirection() > 0. && lightFrontRadius < lightFrontCentreDistance) {
447 // Light front in front of camera...
448 G4double sineHorizonAngle = lightFrontRadius / lightFrontCentreDistance;
449 circleCentre = lightFrontCentre + (lightFrontRadius * sineHorizonAngle) * lightFrontToCameraDirection.unit();
450 circleRadius = lightFrontRadius * std::sqrt(1. - std::pow(sineHorizonAngle, 2));
451 /*
452 G4cout << "sineHorizonAngle: " << sineHorizonAngle
453 << ", circleCentre: " << circleCentre
454 << ", circleRadius: " << circleRadius
455 << G4endl;
456 */
457 } else {
458 circleRadius = -1.;
459 }
460 }
461 if (circleRadius > 0.) {
462 G4Circle lightFront(circleCentre);
463 lightFront.SetWorldRadius(circleRadius);
465 (fDisplayLightFrontRed,
466 fDisplayLightFrontGreen,
467 fDisplayLightFrontBlue));
468 lightFront.SetVisAttributes(visAtts);
469 AddPrimitiveForASingleFrame(lightFront);
470 }
471 }
472 }
473}
474
476{
477 // We don't want this to get into a display list or a TODL or a PODL so
478 // use the fMemoryForDisplayLists flag.
481 fG4OpenGLStoredSceneHandler.G4OpenGLStoredSceneHandler::AddPrimitive(text);
482 fG4OpenGLStoredSceneHandler.fMemoryForDisplayLists = memoryForDisplayListsKeep;
483}
484
486{
487 // We don't want this to get into a display list or a TODL or a PODL so
488 // use the fMemoryForDisplayLists flag.
491 fG4OpenGLStoredSceneHandler.G4OpenGLStoredSceneHandler::AddPrimitive(circle);
492 fG4OpenGLStoredSceneHandler.fMemoryForDisplayLists = memoryForDisplayListsKeep;
493}
#define CONVENIENT_BOOL_ALIAS(q)
#define CONVENIENT_DOUBLE_ALIAS(q)
#define G4OPENGL_FLT_BIG
Definition: G4OpenGL.hh:89
HepGeom::Point3D< G4double > G4Point3D
Definition: G4Point3D.hh:34
#define G4BestUnit(a, b)
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
std::vector< G4Plane3D > G4Planes
G4double GetBlue() const
Definition: G4Colour.hh:154
G4double GetAlpha() const
Definition: G4Colour.hh:155
G4double GetRed() const
Definition: G4Colour.hh:152
G4double GetGreen() const
Definition: G4Colour.hh:153
virtual G4bool CompareForKernelVisit(G4ViewParameters &)
virtual G4bool TOSelected(size_t)
G4OpenGLStoredViewer(G4OpenGLStoredSceneHandler &scene)
virtual G4bool POSelected(size_t)
virtual void DisplayTimePOColourModification(G4Colour &, size_t)
void AddPrimitiveForASingleFrame(const G4Text &text)
G4OpenGLStoredSceneHandler & fG4OpenGLStoredSceneHandler
const GLdouble * GetGLMatrix()
G4bool transparency_enabled
void g4GlOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
const G4VisExtent & GetExtent() const
const G4Point3D & GetStandardTargetPoint() const
Definition: G4Text.hh:72
void SetWorldRadius(G4double)
void SetScreenSize(G4double)
G4Scene * GetScene() const
G4VSceneHandler & fSceneHandler
Definition: G4VViewer.hh:215
void NeedKernelVisit()
Definition: G4VViewer.cc:78
G4ViewParameters fDefaultVP
Definition: G4VViewer.hh:220
G4ViewParameters fVP
Definition: G4VViewer.hh:219
const std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() const
G4int GetNoOfSides() const
G4bool IsSpecialMeshRendering() const
CutawayMode GetCutawayMode() const
G4double GetCameraDistance(G4double radius) const
G4double GetExplodeFactor() const
G4int GetNumberOfCloudPoints() const
G4bool IsMarkerNotHidden() const
G4double GetGlobalLineWidthScale() const
G4bool IsCutaway() const
const G4Colour & GetBackgroundColour() const
const G4Vector3D & GetViewpointDirection() const
G4bool IsSection() const
const G4Point3D & GetCurrentTargetPoint() const
G4bool IsPicking() const
G4double GetFieldHalfAngle() const
G4bool IsCulling() const
const G4VisAttributes * GetDefaultTextVisAttributes() const
G4bool IsExplode() const
const std::vector< G4double > & GetCBDParameters() const
G4int GetCBDAlgorithmNumber() const
const std::vector< G4ModelingParameters::PVNameCopyNo > & GetSpecialMeshVolumes() const
G4double GetGlobalMarkerScale() const
G4bool IsCullingInvisible() const
const G4VisAttributes * GetDefaultVisAttributes() const
const G4Planes & GetCutawayPlanes() const
G4bool IsDensityCulling() const
G4double GetVisibleDensity() const
G4bool IsCullingCovered() const
const G4Plane3D & GetSectionPlane() const
DrawingStyle GetDrawingStyle() const
G4bool IsAuxEdgeVisible() const
const G4Colour & GetColour() const
static constexpr G4double fVeryLongTime
G4double GetExtentRadius() const
Definition: G4VisExtent.cc:75
void SetVisAttributes(const G4VisAttributes *)
Definition: G4Visible.cc:96
BasicVector3D< T > unit() const
float c_light
Definition: hepunit.py:256