source: tags/visualization-10.5-before-10.6_test/OpenGL/src/G4OpenGLStoredSceneHandler.cc @ 1102

Last change on this file since 1102 was 1041, checked in by garnier, 15 years ago

en test

  • Property svn:mime-type set to text/cpp
File size: 18.8 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: G4OpenGLStoredSceneHandler.cc,v 1.40 2009/02/04 16:48:41 lgarnier Exp $
28// GEANT4 tag $Name:  $
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"
48#include "G4LogicalVolume.hh"
49#include "G4Polyline.hh"
50#include "G4Polymarker.hh"
51#include "G4Text.hh"
52#include "G4Circle.hh"
53#include "G4Square.hh"
54#include "G4Polyhedron.hh"
55#include "G4AttHolder.hh"
56#include "G4OpenGLTransform3D.hh"
57#include "G4OpenGLViewer.hh"
58
59G4OpenGLStoredSceneHandler::PO::PO
60(G4int id,
61 const G4Transform3D& tr):
62  fDisplayListId(id),
63  fTransform(tr),
64  fPickName(0)
65{}
66
67G4OpenGLStoredSceneHandler::TO::TO
68(G4int id,
69 const G4Transform3D& tr):
70  fDisplayListId(id),
71  fTransform(tr),
72  fPickName(0),
73  fStartTime(-DBL_MAX),
74  fEndTime(DBL_MAX)
75{}
76
77G4OpenGLStoredSceneHandler::G4OpenGLStoredSceneHandler
78(G4VGraphicsSystem& system,
79 const G4String& name):
80G4OpenGLSceneHandler (system, fSceneIdCount++, name),
81fMemoryForDisplayLists (true),
82fAddPrimitivePreambleNestingDepth (0),
83fTopPODL (0),
84fLastPolymarker(),
85fLastPolymarkers(),
86nbDoublons(0)
87{}
88
89G4OpenGLStoredSceneHandler::~G4OpenGLStoredSceneHandler ()
90{}
91
92void G4OpenGLStoredSceneHandler::AddPrimitivePreamble(const G4Visible& visible)
93{
94  // Track nesting depth to avoid recursive calls, for example, from a
95  // G4Polymarker that invokes a G4Circle...
96  fAddPrimitivePreambleNestingDepth++;
97  if (fAddPrimitivePreambleNestingDepth > 1) return;
98
99  // Because of our need to control colour of transients (display by
100  // time fading), display lists may only cover a single primitive.
101  // So display list setup is here.
102
103  if (fpViewer->GetViewParameters().IsPicking()) {
104    fPickMap[++fPickName] = 0;
105  }
106
107  const G4Colour& c = GetColour (visible);
108
109  if (fMemoryForDisplayLists) {
110    fDisplayListId = glGenLists (1);
111    if (glGetError() == GL_OUT_OF_MEMORY) {  // Could pre-allocate?
112      G4cout <<
113        "********************* WARNING! ********************"
114        "\nUnable to allocate any more display lists in OpenGL."
115        "\n     Continuing drawing in IMMEDIATE MODE."
116        "\n***************************************************"
117             << G4endl;
118      fMemoryForDisplayLists = false;
119    }
120  }
121  if (fMemoryForDisplayLists) {
122    if (fReadyForTransients) {
123      TO to(fDisplayListId, *fpObjectTransformation);
124      to.fPickName = fPickName;
125      to.fColour = c;
126      const G4VisAttributes* pVA =
127        fpViewer->GetApplicableVisAttributes(visible.GetVisAttributes());
128      to.fStartTime = pVA->GetStartTime();
129      to.fEndTime = pVA->GetEndTime();
130      fTOList.push_back(to);
131      glDrawBuffer (GL_FRONT);
132      glPushMatrix();
133      G4OpenGLTransform3D oglt (*fpObjectTransformation);
134      glMultMatrixd (oglt.GetGLMatrix ());
135      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
136      glNewList (fDisplayListId, GL_COMPILE_AND_EXECUTE);
137    }
138    else {
139      PO po(fDisplayListId, *fpObjectTransformation);
140      po.fPickName = fPickName;
141      fPOList.push_back(po);
142      glNewList (fDisplayListId, GL_COMPILE);
143      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
144    }
145  } else {
146    glDrawBuffer (GL_FRONT);
147    glPushMatrix();
148    G4OpenGLTransform3D oglt (*fpObjectTransformation);
149    glMultMatrixd (oglt.GetGLMatrix ());
150    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
151  }
152
153  if (fProcessing2D) {
154    // Push current 3D world matrices and load identity to define screen
155    // coordinates...
156    glMatrixMode (GL_PROJECTION);
157    glPushMatrix();
158    glLoadIdentity();
159    glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
160    glMatrixMode (GL_MODELVIEW);
161    glPushMatrix();
162    glLoadIdentity();
163    G4OpenGLTransform3D oglt (*fpObjectTransformation);
164    glMultMatrixd (oglt.GetGLMatrix ());
165    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
166  }
167}
168
169void G4OpenGLStoredSceneHandler::AddPrimitivePostamble()
170{
171  if (fProcessing2D) {
172    // Pop current 3D world matrices back again...
173    glMatrixMode (GL_PROJECTION);
174    glPopMatrix();
175    glMatrixMode (GL_MODELVIEW);
176    glPopMatrix();
177  }
178
179  //  if ((glGetError() == GL_TABLE_TOO_LARGE) || (glGetError() == GL_OUT_OF_MEMORY)) {  // Could close?
180  if (glGetError() == GL_OUT_OF_MEMORY) {  // Could close?
181    G4cout <<
182      "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
183      "  display List for fTopPODL - try OpenGL Immediated mode."
184           << G4endl;
185  }
186  if (fMemoryForDisplayLists) {
187    glEndList();
188    if (glGetError() == GL_OUT_OF_MEMORY) {  // Could close?
189      G4cout <<
190        "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
191        "  display List for fTopPODL - try OpenGL Immediated mode."
192             << G4endl;
193    }
194  }
195  if (fReadyForTransients || !fMemoryForDisplayLists) {
196    glPopMatrix();
197#ifdef G4DEBUG_VIS_OGL
198    //    printf("G4OpenGLStoredSceneHandler::AddPrimitivePostamble glFlush\n");
199#endif
200    glFlush ();
201    glDrawBuffer (GL_BACK);
202  }
203  fAddPrimitivePreambleNestingDepth--;
204}
205
206void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyline& polyline)
207{
208  AddPrimitivePreamble(polyline);
209  G4OpenGLSceneHandler::AddPrimitive(polyline);
210  AddPrimitivePostamble();
211}
212
213void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
214{
215
216#ifdef G4DEBUG_VIS_OGL
217  //  printf("G4OpenGLStoredSceneHandler::AddPrimitive\n");
218#endif
219  std::vector< G4Polymarker > poly;
220    // 40,83 N03 lancement
221    // 289,69 10Gev supp doublons   + bitmap 21 frame en 10 sec
222    // 343,45 10Gev full polymarker + bitmap 15 frame en 10 sec
223    // 351,67 10Gev full polymarker - bitmap 31 frame en 10 sec
224    // 257,90 10Gev supp doublons MAX  - bitmap 42 frame en 10 sec 45757 markers et 16243 enleves
225  // Check
226  int reste = polymarker.size();
227  bool res = false;
228  if (fLastPolymarkers.size() > 0 ) {
229
230    // Loop new point to check
231    for (unsigned int b=0; b< polymarker.size();b++) {
232      //      printf("check %d/%d\n",b,polymarker.size());
233      res= false;
234
235      // Look for stored point list
236      for (unsigned int a=0; a< fLastPolymarkers.size() ;a++) {
237        //        printf("against vect %d/%d\n",a,fLastPolymarker.size());
238        std::vector < G4Polymarker > storedPoly = fLastPolymarkers[a];
239        int size= storedPoly.size()-1;
240
241        // Look for point
242        for (int aPoly=0; aPoly< size ;aPoly++) {
243          //          printf("against poly %d/%d\n",aPoly,storedPoly.size());
244          if (( storedPoly[size-aPoly].GetPosition().x() == polymarker[b].x()) &&
245              ( storedPoly[size-aPoly].GetPosition().y() == polymarker[b].y()) &&
246              ( storedPoly[size-aPoly].GetPosition().z() == polymarker[b].z())) {
247            reste --;
248//            res= true;
249            nbDoublons++;
250            //            printf("G4OpenGLStoredSceneHandler::AddPrimitive  PT %d/%d ALREADY FOUND TOT:%d. OLD pos %d/%d in %d PolyMarkerList STILL %d Marker in list. Point is %f %f %f\n",b,polymarker.size(),  nbDoublons,  aPoly,size,a   ,reste,polymarker[b].x(),polymarker[b].y(),polymarker[b].z());
251          }
252        }
253      }
254      // Add
255      if (!res) {
256        G4Polymarker tmp;
257        tmp.SetPosition(polymarker[b]);
258        poly.push_back(tmp);
259      }
260    }
261  } else {
262    for (unsigned int b=0; b< polymarker.size();b++) {
263      G4Polymarker tmp;
264      tmp.SetPosition(polymarker[b]);
265      poly.push_back(tmp);
266    }
267  }
268  fLastPolymarkers.push_back(poly);
269#define TEST_MARKER 1
270#ifdef TEST_MARKER
271  // if it is already done
272  if (poly.size() == 1 ) {
273    //   if (polymarker.size() >0) {
274    //     if (( fLastPolymarker.GetPosition().x() == polymarker[0].x()) &&
275    //         ( fLastPolymarker.GetPosition().y() == polymarker[0].y()) &&
276    //         ( fLastPolymarker.GetPosition().z() == polymarker[0].z())) {
277    //       if (fpViewer->GetViewParameters().IsPicking()) {
278    //         glLoadName(++fPickName);
279    //         fPickMap[fPickName] = 0;
280    //       }
281    //       printf("G4OpenGLStoredSceneHandler::AddPrimitive  SUPPR ----------- point %f %f %f\n",polymarker[0].x(),polymarker[0].y(),polymarker[0].z());
282    const G4Colour& c = GetColour (polymarker);
283    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
284    G4OpenGLSceneHandler::AddPrimitive(poly[0]);
285    //    fLastPolymarker.SetPosition(polymarker[poly]);
286    return;
287  }
288#endif 
289#ifdef G4DEBUG_VIS_OGL
290  //  printf("G4OpenGLStoredSceneHandler::AddPrimitive new marker list\n");
291#endif
292  AddPrimitivePreamble(polymarker);
293  G4OpenGLSceneHandler::AddPrimitive(polymarker);
294  AddPrimitivePostamble();
295//  fLastPolymarker.SetPosition(polymarker[polymarker.size()-1]);
296}
297
298void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Text& text)
299{
300  // Note: colour is still handled in
301  // G4OpenGLSceneHandler::AddPrimitive(const G4Text&), so it still
302  // gets into the display list
303  AddPrimitivePreamble(text);
304  G4OpenGLSceneHandler::AddPrimitive(text);
305  AddPrimitivePostamble();
306}
307
308void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Circle& circle)
309{
310  AddPrimitivePreamble(circle);
311  G4OpenGLSceneHandler::AddPrimitive(circle);
312  AddPrimitivePostamble();
313}
314
315void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Square& square)
316{
317  AddPrimitivePreamble(square);
318  G4OpenGLSceneHandler::AddPrimitive(square);
319  AddPrimitivePostamble();
320}
321
322void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Scale& scale)
323{
324  // Let base class split into primitives.
325  G4OpenGLSceneHandler::AddPrimitive(scale);
326}
327
328void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
329{
330  // Note: colour is still handled in
331  // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&), so it still
332  // gets into the display list
333  AddPrimitivePreamble(polyhedron);
334  printf("G4OpenGLStoredSceneHandler::AddPrimitive :\n");
335  G4OpenGLSceneHandler::AddPrimitive(polyhedron);
336  AddPrimitivePostamble();
337}
338
339void G4OpenGLStoredSceneHandler::AddPrimitive (const G4NURBS& nurbs)
340{
341  // Note: colour is still handled in
342  // G4OpenGLSceneHandler::AddPrimitive(const G4NURBS&), so it still
343  // gets into the display list
344  AddPrimitivePreamble(nurbs);
345  G4OpenGLSceneHandler::AddPrimitive(nurbs);
346  AddPrimitivePostamble();
347}
348
349void G4OpenGLStoredSceneHandler::BeginPrimitives
350(const G4Transform3D& objectTransformation)
351
352  G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
353
354  // Display list setup moved to AddPrimitivePreamble.  See notes there.
355}
356
357void G4OpenGLStoredSceneHandler::EndPrimitives ()
358{
359  G4OpenGLSceneHandler::EndPrimitives ();
360}
361
362void G4OpenGLStoredSceneHandler::BeginPrimitives2D
363(const G4Transform3D& objectTransformation)
364{
365  G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
366}
367
368void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
369{
370  G4OpenGLSceneHandler::EndPrimitives2D ();
371}
372
373void G4OpenGLStoredSceneHandler::BeginModeling () {
374  G4VSceneHandler::BeginModeling();
375  ClearStore();  // ...and all that goes with it.
376  /* Debug...
377  fDisplayListId = glGenLists (1);
378  G4cout << "OGL::fDisplayListId (start): " << fDisplayListId << G4endl;
379  */
380}
381
382void G4OpenGLStoredSceneHandler::EndModeling () {
383  // Make a List which calls the other lists.
384#ifdef G4DEBUG_VIS_OGL
385  printf("G4OpenGLStoredSceneHandler::EndModeling Cree une nouvelle liste+++++++++++++++++++++++++++\n");
386#endif
387  fTopPODL = glGenLists (1);
388  if (glGetError() == GL_OUT_OF_MEMORY) {  // Could pre-allocate?
389    G4cout <<
390      "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
391      "  display List for fTopPODL - try OpenGL Immediated mode."
392           << G4endl;
393  } else {
394    glNewList (fTopPODL, GL_COMPILE_AND_EXECUTE); {
395      for (size_t i = 0; i < fPOList.size (); i++) {
396        glPushMatrix();
397        G4OpenGLTransform3D oglt (fPOList[i].fTransform);
398        glMultMatrixd (oglt.GetGLMatrix ());
399        if (fpViewer->GetViewParameters().IsPicking())
400          glLoadName(fPOList[i].fPickName);
401        glCallList (fPOList[i].fDisplayListId);
402        glPopMatrix();
403      }
404    }
405    glEndList ();
406    if (glGetError() == GL_OUT_OF_MEMORY) {  // Could close?
407      G4cout <<
408        "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
409        "  display List for fTopPODL - try OpenGL Immediated mode."
410             << G4endl;
411    }
412  }
413
414  G4VSceneHandler::EndModeling ();
415
416  /* Debug...
417  fDisplayListId = glGenLists (1);
418  G4cout << "OGL::fDisplayListId (end): " << fDisplayListId << G4endl;
419  */
420}
421
422void G4OpenGLStoredSceneHandler::ClearStore () {
423
424  G4VSceneHandler::ClearStore ();  // Sets need kernel visit, etc.
425
426  // Delete OpenGL permanent display lists.
427  for (size_t i = 0; i < fPOList.size (); i++)
428    glDeleteLists (fPOList[i].fDisplayListId, 1);
429  if (fTopPODL) glDeleteLists (fTopPODL, 1);
430  fTopPODL = 0;
431
432  // Clear other lists, dictionary, etc.
433  fPOList.clear ();
434  fSolidMap.clear ();
435  ClearAndDestroyAtts();
436
437  // ...and clear transient store...
438  for (size_t i = 0; i < fTOList.size (); i++)
439    glDeleteLists(fTOList[i].fDisplayListId, 1);
440  fTOList.clear ();
441}
442
443void G4OpenGLStoredSceneHandler::ClearTransientStore () {
444
445  G4VSceneHandler::ClearTransientStore ();
446
447  // Delete OpenGL transient display lists and Transient Objects themselves.
448  for (size_t i = 0; i < fTOList.size (); i++)
449    glDeleteLists(fTOList[i].fDisplayListId, 1);
450  fTOList.clear ();
451
452  // Make sure screen corresponds to graphical database...
453  if (fpViewer) {
454#ifdef G4DEBUG_VIS_OGL
455    printf("G4OpenGLStoredSceneHandler::ClearTransientStore () BEGIN\n");
456#endif
457    fpViewer -> SetView ();
458    fpViewer -> ClearView ();
459    fpViewer -> DrawView ();
460#ifdef G4DEBUG_VIS_OGL
461    printf("G4OpenGLStoredSceneHandler::ClearTransientStore () END\n");
462#endif
463  }
464}
465
466void G4OpenGLStoredSceneHandler::RequestPrimitives (const G4VSolid& solid)
467{
468  if (fReadyForTransients) {
469    // Always draw transient solids, e.g., hits represented as solids.
470    // (As we have no control over the order of drawing of transient
471    // objects, we cannot do anything about transparent ones, as
472    // below, so always draw them.)
473    G4VSceneHandler::RequestPrimitives (solid);
474    return;
475  }
476
477  // For non-transient (run-duration) objects, ensure transparent
478  // objects are drawn last.  The problem of
479  // blending/transparency/alpha is quite a tricky one - see History
480  // of opengl-V07-01-01/2/3.
481  // Get vis attributes - pick up defaults if none.
482  const G4VisAttributes* pVA =
483    fpViewer -> GetApplicableVisAttributes(fpVisAttribs);
484  const G4Colour& c = pVA -> GetColour ();
485  G4double opacity = c.GetAlpha ();
486
487  if (!fSecondPass) {
488    G4bool transparency_enabled = true;
489    G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
490    if (pViewer) transparency_enabled = pViewer->transparency_enabled;
491    if (transparency_enabled && opacity < 1.) {
492      // On first pass, transparent objects are not drawn, but flag is set...
493      fSecondPassRequested = true;
494      return;
495    }
496  }
497
498  // On second pass, opaque objects are not drwan...
499  if (fSecondPass && opacity >= 1.) return;
500
501  G4PhysicalVolumeModel* pPVModel =
502    dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
503 
504  if (pPVModel) {
505    // If part of the geometry hierarchy, i.e., from a
506    // G4PhysicalVolumeModel, check if a display list already exists for
507    // this solid, re-use it if possible.  We could be smarter, and
508    // recognise repeated branches of the geometry hierarchy, for
509    // example.  But this algorithm should be secure, I think...
510    const G4VSolid* pSolid = &solid;
511    EAxis axis = kRho;
512    G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
513    if (pCurrentPV -> IsReplicated ()) {
514      G4int nReplicas;
515      G4double width;
516      G4double offset;
517      G4bool consuming;
518      pCurrentPV->GetReplicationData(axis,nReplicas,width,offset,consuming);
519    }
520    // Provided it is not parametrised (because if so, the
521    // solid's parameters might have been changed)...
522    if (!(pCurrentPV -> IsParameterised ()) &&
523        // Provided it is not replicated radially (because if so, the
524        // solid's parameters will have been changed)...
525        !(pCurrentPV -> IsReplicated () && axis == kRho) &&
526        // ...and if the solid has already been rendered...
527        (fSolidMap.find (pSolid) != fSolidMap.end ())) {
528      fDisplayListId = fSolidMap [pSolid];
529      PO po(fDisplayListId,*fpObjectTransformation);
530      if (fpViewer->GetViewParameters().IsPicking()) {
531        G4AttHolder* holder = new G4AttHolder;
532        // Load G4Atts from G4VisAttributes, if any...
533        const G4VisAttributes* va = pPVModel->GetCurrentLV()->GetVisAttributes();
534        if (va) {
535          const std::map<G4String,G4AttDef>* vaDefs = va->GetAttDefs();
536          if (vaDefs) holder->AddAtts(va->CreateAttValues(), vaDefs);
537        }
538        // Load G4Atts from G4PhysicalVolumeModel...
539        const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs();
540        if (defs) holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs);
541        fPickMap[++fPickName] = holder;
542        po.fPickName = fPickName;
543      }
544      fPOList.push_back(po);
545    }
546    else {
547      G4VSceneHandler::RequestPrimitives (solid);
548      fSolidMap [pSolid] = fDisplayListId;
549    }
550    return;
551  }
552
553  // Otherwise invoke base class method...
554  G4VSceneHandler::RequestPrimitives (solid);
555}
556
557G4int G4OpenGLStoredSceneHandler::fSceneIdCount = 0;
558
559#endif
Note: See TracBrowser for help on using the repository browser.