source: trunk/geant4/visualization/OpenGL/src/G4OpenGLStoredSceneHandler.cc@ 668

Last change on this file since 668 was 631, checked in by garnier, 18 years ago

maj a jour par rapport au repository de geant4

  • Property svn:mime-type set to text/cpp
File size: 13.7 KB
RevLine 
[529]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//
[593]27// $Id: G4OpenGLStoredSceneHandler.cc,v 1.34 2007/04/04 16:50:27 allison Exp $
[631]28// GEANT4 tag $Name: $
[529]29//
30//
31// Andrew Walkden 10th February 1997
32// OpenGL stored scene - creates OpenGL display lists.
33
34#ifdef G4VIS_BUILD_OPENGL_DRIVER
35
36// Included here - problems with HP compiler if not before other includes?
37#include "G4NURBS.hh"
38
39// Here follows a special for Mesa, the OpenGL emulator. Does not affect
40// other OpenGL's, as far as I'm aware. John Allison 18/9/96.
41#define CENTERLINE_CLPP /* CenterLine C++ workaround: */
42// Also seems to be required for HP's CC and AIX xlC, at least.
43
44#include "G4OpenGLStoredSceneHandler.hh"
45
46#include "G4PhysicalVolumeModel.hh"
47#include "G4VPhysicalVolume.hh"
[593]48#include "G4LogicalVolume.hh"
[529]49#include "G4Polyline.hh"
50#include "G4Polymarker.hh"
[593]51#include "G4Text.hh"
[529]52#include "G4Circle.hh"
53#include "G4Square.hh"
[593]54#include "G4Polyhedron.hh"
55#include "G4AttHolder.hh"
[529]56
57G4OpenGLStoredSceneHandler::G4OpenGLStoredSceneHandler (G4VGraphicsSystem& system,
58 const G4String& name):
59G4OpenGLSceneHandler (system, fSceneIdCount++, name),
60fMemoryForDisplayLists (true),
61fAddPrimitivePreambleNestingDepth (0),
[593]62fTopPODL (0),
63fProcessing2D (false)
[529]64{}
65
66G4OpenGLStoredSceneHandler::~G4OpenGLStoredSceneHandler ()
67{}
68
69void G4OpenGLStoredSceneHandler::AddPrimitivePreamble(const G4Visible& visible)
70{
71 // Track nesting depth to avoid recursive calls, for example, from a
72 // G4Polymarker that invokes a G4Circle...
73 fAddPrimitivePreambleNestingDepth++;
74 if (fAddPrimitivePreambleNestingDepth > 1) return;
75
[593]76 // Because of our need to control colour of transients (display by
77 // time fading), display lists may only cover a single primitive.
78 // So display list setup is here.
[529]79
[593]80 if (fpViewer->GetViewParameters().IsPicking()) {
81 fPickMap[++fPickName] = 0;
82 }
[529]83
[593]84 const G4Colour& c = GetColour (visible);
[529]85
[593]86 if (fMemoryForDisplayLists) {
87 fDisplayListId = glGenLists (1);
88 if (!fDisplayListId) { // Could pre-allocate?
89 G4cout <<
90 "********************* WARNING! ********************"
91 "\nUnable to allocate any more display lists in OpenGL."
92 "\n Continuing drawing in IMMEDIATE MODE."
93 "\n***************************************************"
94 << G4endl;
95 fMemoryForDisplayLists = false;
96 }
97 }
98 if (fMemoryForDisplayLists) {
99 if (fReadyForTransients) {
100 TO to(fDisplayListId, *fpObjectTransformation);
101 to.fPickName = fPickName;
102 to.fColour = c;
103 const G4VisAttributes* pVA =
104 fpViewer->GetApplicableVisAttributes(visible.GetVisAttributes());
105 to.fStartTime = pVA->GetStartTime();
106 to.fEndTime = pVA->GetEndTime();
107 fTOList.push_back(to);
108 glDrawBuffer (GL_FRONT);
109 glPushMatrix();
110 G4OpenGLTransform3D oglt (*fpObjectTransformation);
111 glMultMatrixd (oglt.GetGLMatrix ());
112 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
113 glNewList (fDisplayListId, GL_COMPILE_AND_EXECUTE);
114 }
115 else {
116 PO po(fDisplayListId, *fpObjectTransformation);
117 po.fPickName = fPickName;
118 fPOList.push_back(po);
119 glNewList (fDisplayListId, GL_COMPILE);
120 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
121 }
[529]122 } else {
[593]123 glDrawBuffer (GL_FRONT);
124 glPushMatrix();
125 G4OpenGLTransform3D oglt (*fpObjectTransformation);
126 glMultMatrixd (oglt.GetGLMatrix ());
[529]127 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
128 }
[593]129
130 if (fProcessing2D) {
131 // Push current 3D world matrices and load identity to define screen
132 // coordinates...
133 glMatrixMode (GL_PROJECTION);
134 glPushMatrix();
135 glLoadIdentity();
136 glOrtho (-1., 1., -1., 1., -G4OPENGL_DBL_MAX, G4OPENGL_DBL_MAX);
137 glMatrixMode (GL_MODELVIEW);
138 glPushMatrix();
139 glLoadIdentity();
140 }
[529]141}
142
143void G4OpenGLStoredSceneHandler::AddPrimitivePostamble()
144{
[593]145 if (fProcessing2D) {
146 // Pop current 3D world matrices back again...
147 glMatrixMode (GL_PROJECTION);
148 glPopMatrix();
149 glMatrixMode (GL_MODELVIEW);
150 glPopMatrix();
151 }
152
153 if (fMemoryForDisplayLists) {
154 glEndList();
155 }
156 if (fReadyForTransients || !fMemoryForDisplayLists) {
157 glPopMatrix();
158 glFlush ();
159 glDrawBuffer (GL_BACK);
160 }
[529]161 fAddPrimitivePreambleNestingDepth--;
162}
163
164void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyline& polyline)
165{
166 AddPrimitivePreamble(polyline);
167 G4OpenGLSceneHandler::AddPrimitive(polyline);
168 AddPrimitivePostamble();
169}
170
[593]171void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
172{
173 AddPrimitivePreamble(polymarker);
174 G4OpenGLSceneHandler::AddPrimitive(polymarker);
175 AddPrimitivePostamble();
176}
177
178void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Text& text)
179{
180 // Note: colour is still handled in
181 // G4OpenGLSceneHandler::AddPrimitive(const G4Text&), so it still
182 // gets into the display list
183 AddPrimitivePreamble(text);
184 G4OpenGLSceneHandler::AddPrimitive(text);
185 AddPrimitivePostamble();
186}
187
[529]188void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Circle& circle)
189{
190 AddPrimitivePreamble(circle);
191 G4OpenGLSceneHandler::AddPrimitive(circle);
192 AddPrimitivePostamble();
193}
194
195void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Square& square)
196{
197 AddPrimitivePreamble(square);
198 G4OpenGLSceneHandler::AddPrimitive(square);
199 AddPrimitivePostamble();
200}
201
[593]202void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Scale& scale)
[529]203{
[593]204 // Let base class split into primitives.
205 G4OpenGLSceneHandler::AddPrimitive(scale);
206}
207
208void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
209{
210 // Note: colour is still handled in
211 // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&), so it still
212 // gets into the display list
213 AddPrimitivePreamble(polyhedron);
214 G4OpenGLSceneHandler::AddPrimitive(polyhedron);
[529]215 AddPrimitivePostamble();
216}
217
[593]218void G4OpenGLStoredSceneHandler::AddPrimitive (const G4NURBS& nurbs)
219{
220 // Note: colour is still handled in
221 // G4OpenGLSceneHandler::AddPrimitive(const G4NURBS&), so it still
222 // gets into the display list
223 AddPrimitivePreamble(nurbs);
224 G4OpenGLSceneHandler::AddPrimitive(nurbs);
225 AddPrimitivePostamble();
226}
227
[529]228void G4OpenGLStoredSceneHandler::BeginPrimitives
[593]229(const G4Transform3D& objectTransformation)
230{
231 G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
[529]232
[593]233 // Display list setup moved to AddPrimitivePreamble. See notes there.
[529]234}
235
[593]236void G4OpenGLStoredSceneHandler::EndPrimitives ()
237{
238 G4OpenGLSceneHandler::EndPrimitives ();
[529]239}
240
241void G4OpenGLStoredSceneHandler::BeginPrimitives2D()
242{
[593]243 G4OpenGLSceneHandler::BeginPrimitives2D();
244 fProcessing2D = true;
[529]245}
246
247void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
248{
[593]249 fProcessing2D = false;
250 G4OpenGLSceneHandler::EndPrimitives2D ();
[529]251}
252
253void G4OpenGLStoredSceneHandler::BeginModeling () {
254 G4VSceneHandler::BeginModeling();
255 ClearStore(); // ...and all that goes with it.
256 /* Debug...
257 fDisplayListId = glGenLists (1);
258 G4cout << "OGL::fDisplayListId (start): " << fDisplayListId << G4endl;
259 */
260}
261
262void G4OpenGLStoredSceneHandler::EndModeling () {
263 // Make a List which calls the other lists.
264 fTopPODL = glGenLists (1);
265 if (!fTopPODL) {
266 G4cout <<
267 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
268 " display List for fTopPODL - try OpenGL Immediated mode."
269 << G4endl;
[593]270 } else {
[529]271 glNewList (fTopPODL, GL_COMPILE_AND_EXECUTE); {
272 for (size_t i = 0; i < fPOList.size (); i++) {
273 glPushMatrix();
274 G4OpenGLTransform3D oglt (fPOList[i].fTransform);
275 glMultMatrixd (oglt.GetGLMatrix ());
[593]276 if (fpViewer->GetViewParameters().IsPicking())
277 glLoadName(fPOList[i].fPickName);
[529]278 glCallList (fPOList[i].fDisplayListId);
279 glPopMatrix();
280 }
281 }
282 glEndList ();
283 }
284
285 G4VSceneHandler::EndModeling ();
286
287 /* Debug...
288 fDisplayListId = glGenLists (1);
289 G4cout << "OGL::fDisplayListId (end): " << fDisplayListId << G4endl;
290 */
291}
292
293void G4OpenGLStoredSceneHandler::ClearStore () {
294
295 G4VSceneHandler::ClearStore (); // Sets need kernel visit, etc.
296
297 // Delete OpenGL permanent display lists.
298 for (size_t i = 0; i < fPOList.size (); i++)
299 glDeleteLists (fPOList[i].fDisplayListId, 1);
300 if (fTopPODL) glDeleteLists (fTopPODL, 1);
301 fTopPODL = 0;
302
303 // Clear other lists, dictionary, etc.
304 fPOList.clear ();
305 fSolidMap.clear ();
[593]306 ClearAndDestroyAtts();
[529]307
308 // ...and clear transient store...
309 for (size_t i = 0; i < fTOList.size (); i++)
310 glDeleteLists(fTOList[i].fDisplayListId, 1);
311 fTOList.clear ();
312}
313
314void G4OpenGLStoredSceneHandler::ClearTransientStore () {
315
316 G4VSceneHandler::ClearTransientStore ();
317
318 // Delete OpenGL transient display lists and Transient Objects themselves.
319 for (size_t i = 0; i < fTOList.size (); i++)
320 glDeleteLists(fTOList[i].fDisplayListId, 1);
321 fTOList.clear ();
322
323 // Make sure screen corresponds to graphical database...
324 if (fpViewer) {
325 fpViewer -> SetView ();
326 fpViewer -> ClearView ();
327 fpViewer -> DrawView ();
328 }
329}
330
331void G4OpenGLStoredSceneHandler::RequestPrimitives (const G4VSolid& solid)
332{
333 if (fReadyForTransients) {
334 // Always draw transient solids, e.g., hits represented as solids.
335 // (As we have no control over the order of drawing of transient
336 // objects, we cannot do anything about transparent ones, as
337 // below, so always draw them.)
338 G4VSceneHandler::RequestPrimitives (solid);
339 return;
340 }
341
342 // For non-transient (run-duration) objects, ensure transparent
343 // objects are drawn last. The problem of
344 // blending/transparency/alpha is quite a tricky one - see History
345 // of opengl-V07-01-01/2/3.
346 // Get vis attributes - pick up defaults if none.
347 const G4VisAttributes* pVA =
348 fpViewer -> GetApplicableVisAttributes(fpVisAttribs);
349 const G4Colour& c = pVA -> GetColour ();
350 G4double opacity = c.GetAlpha ();
351
352 if (!fSecondPass) {
353 G4bool transparency_enabled = true;
354 G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
355 if (pViewer) transparency_enabled = pViewer->transparency_enabled;
356 if (transparency_enabled && opacity < 1.) {
357 // On first pass, transparent objects are not drawn, but flag is set...
358 fSecondPassRequested = true;
359 return;
360 }
361 }
362
363 // On second pass, opaque objects are not drwan...
364 if (fSecondPass && opacity >= 1.) return;
365
366 G4PhysicalVolumeModel* pPVModel =
367 dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
368
369 if (pPVModel) {
370 // If part of the geometry hierarchy, i.e., from a
371 // G4PhysicalVolumeModel, check if a display list already exists for
372 // this solid, re-use it if possible. We could be smarter, and
373 // recognise repeated branches of the geometry hierarchy, for
374 // example. But this algorithm should be secure, I think...
375 const G4VSolid* pSolid = &solid;
376 EAxis axis = kRho;
377 G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
378 if (pCurrentPV -> IsReplicated ()) {
379 G4int nReplicas;
380 G4double width;
381 G4double offset;
382 G4bool consuming;
383 pCurrentPV->GetReplicationData(axis,nReplicas,width,offset,consuming);
384 }
385 // Provided it is not parametrised (because if so, the
386 // solid's parameters might have been changed)...
387 if (!(pCurrentPV -> IsParameterised ()) &&
388 // Provided it is not replicated radially (because if so, the
389 // solid's parameters will have been changed)...
390 !(pCurrentPV -> IsReplicated () && axis == kRho) &&
391 // ...and if the solid has already been rendered...
392 (fSolidMap.find (pSolid) != fSolidMap.end ())) {
393 fDisplayListId = fSolidMap [pSolid];
[593]394 PO po(fDisplayListId,*fpObjectTransformation);
395 if (fpViewer->GetViewParameters().IsPicking()) {
396 G4AttHolder* holder = new G4AttHolder;
397 // Load G4Atts from G4VisAttributes, if any...
398 const G4VisAttributes* va = pPVModel->GetCurrentLV()->GetVisAttributes();
399 if (va) {
400 const std::map<G4String,G4AttDef>* vaDefs = va->GetAttDefs();
401 if (vaDefs) holder->AddAtts(va->CreateAttValues(), vaDefs);
402 }
403 // Load G4Atts from G4PhysicalVolumeModel...
404 const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs();
405 if (defs) holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs);
406 fPickMap[++fPickName] = holder;
407 po.fPickName = fPickName;
408 }
409 fPOList.push_back(po);
[529]410 }
411 else {
412 G4VSceneHandler::RequestPrimitives (solid);
413 fSolidMap [pSolid] = fDisplayListId;
414 }
415 return;
416 }
417
418 // Otherwise invoke base class method...
419 G4VSceneHandler::RequestPrimitives (solid);
420}
421
422G4int G4OpenGLStoredSceneHandler::fSceneIdCount = 0;
423
424#endif
Note: See TracBrowser for help on using the repository browser.