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

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

before tag

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