source: trunk/source/visualization/OpenGL/src/G4OpenGLStoredSceneHandler.cc @ 940

Last change on this file since 940 was 938, checked in by garnier, 16 years ago

print PS marche, mais plein de msg de debug a supprimer

  • Property svn:mime-type set to text/cpp
File size: 17.9 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//
[915]27// $Id: G4OpenGLStoredSceneHandler.cc,v 1.40 2009/02/04 16:48:41 lgarnier Exp $
[877]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"
[914]56#include "G4OpenGLTransform3D.hh"
57#include "G4OpenGLViewer.hh"
[529]58
[790]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):
[529]80G4OpenGLSceneHandler (system, fSceneIdCount++, name),
81fMemoryForDisplayLists (true),
82fAddPrimitivePreambleNestingDepth (0),
[936]83fTopPODL (0),
84fLastPolymarker(),
[938]85fLastPolymarkers(),
86nbDoublons(0)
[529]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
[593]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.
[529]102
[593]103  if (fpViewer->GetViewParameters().IsPicking()) {
104    fPickMap[++fPickName] = 0;
105  }
[529]106
[593]107  const G4Colour& c = GetColour (visible);
[529]108
[593]109  if (fMemoryForDisplayLists) {
110    fDisplayListId = glGenLists (1);
[866]111    if (glGetError() == GL_OUT_OF_MEMORY) {  // Could pre-allocate?
[593]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    }
[529]145  } else {
[593]146    glDrawBuffer (GL_FRONT);
147    glPushMatrix();
148    G4OpenGLTransform3D oglt (*fpObjectTransformation);
149    glMultMatrixd (oglt.GetGLMatrix ());
[529]150    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
151  }
[593]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();
[789]159    glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
[593]160    glMatrixMode (GL_MODELVIEW);
161    glPushMatrix();
162    glLoadIdentity();
[688]163    G4OpenGLTransform3D oglt (*fpObjectTransformation);
164    glMultMatrixd (oglt.GetGLMatrix ());
165    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
[593]166  }
[529]167}
168
169void G4OpenGLStoredSceneHandler::AddPrimitivePostamble()
170{
[593]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
[877]179  //  if ((glGetError() == GL_TABLE_TOO_LARGE) || (glGetError() == GL_OUT_OF_MEMORY)) {  // Could close?
180  if (glGetError() == GL_OUT_OF_MEMORY) {  // Could close?
[866]181    G4cout <<
182      "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
183      "  display List for fTopPODL - try OpenGL Immediated mode."
184           << G4endl;
185  }
[593]186  if (fMemoryForDisplayLists) {
187    glEndList();
[866]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    }
[593]194  }
195  if (fReadyForTransients || !fMemoryForDisplayLists) {
196    glPopMatrix();
197    glFlush ();
198    glDrawBuffer (GL_BACK);
199  }
[529]200  fAddPrimitivePreambleNestingDepth--;
201}
202
203void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyline& polyline)
204{
205  AddPrimitivePreamble(polyline);
206  G4OpenGLSceneHandler::AddPrimitive(polyline);
207  AddPrimitivePostamble();
208}
209
[593]210void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
211{
[936]212
[938]213  std::vector< G4Polymarker > poly;
[936]214    // 40,83 N03 lancement
215    // 289,69 10Gev supp doublons   + bitmap 21 frame en 10 sec
216    // 343,45 10Gev full polymarker + bitmap 15 frame en 10 sec
217    // 351,67 10Gev full polymarker - bitmap 31 frame en 10 sec
[938]218    // 257,90 10Gev supp doublons MAX  - bitmap 42 frame en 10 sec 45757 markers et 16243 enleves
[936]219  // Check
220  int reste = polymarker.size();
[938]221  bool res = false;
222  if (fLastPolymarkers.size() > 0 ) {
[936]223    for (unsigned int b=0; b< polymarker.size();b++) {
[938]224      res= false;
225      int size= fLastPolymarkers.size()-1;
226      for (unsigned int a=0; a< fLastPolymarkers.size() ;a++) {
227        if (( fLastPolymarkers[size-a].GetPosition().x() == polymarker[b].x()) &&
228            ( fLastPolymarkers[size-a].GetPosition().y() == polymarker[b].y()) &&
229            ( fLastPolymarkers[size-a].GetPosition().z() == polymarker[b].z())) {
230          reste --;
231          res= true;
232          nbDoublons++;
233          printf("G4OpenGLStoredSceneHandler::AddPrimitive  DOUBLON %d total:%d et pos %d reste %d ----------- point %f %f %f\n",size-a,nbDoublons,b,reste,polymarker[b].x(),polymarker[b].y(),polymarker[b].z());
234        }
[936]235      }
[938]236      // Add
237      if (!res) {
238        G4Polymarker tmp;
239        tmp.SetPosition(polymarker[b]);
240        fLastPolymarkers.push_back(tmp);
241      poly.push_back(tmp);
242      }
[936]243    }
[938]244  } else {
245    for (unsigned int b=0; b< polymarker.size();b++) {
246      G4Polymarker tmp;
247      tmp.SetPosition(polymarker[b]);
248      fLastPolymarkers.push_back(tmp);
249      poly.push_back(tmp);
250    }
[936]251  }
252#define TEST_MARKER 1
253#ifdef TEST_MARKER
254  // if it is already done
[938]255  if (poly.size() == 1 ) {
256    //   if (polymarker.size() >0) {
257    //     if (( fLastPolymarker.GetPosition().x() == polymarker[0].x()) &&
258    //         ( fLastPolymarker.GetPosition().y() == polymarker[0].y()) &&
259    //         ( fLastPolymarker.GetPosition().z() == polymarker[0].z())) {
260    //       if (fpViewer->GetViewParameters().IsPicking()) {
261    //         glLoadName(++fPickName);
262    //         fPickMap[fPickName] = 0;
263    //       }
264    //       printf("G4OpenGLStoredSceneHandler::AddPrimitive  SUPPR ----------- point %f %f %f\n",polymarker[0].x(),polymarker[0].y(),polymarker[0].z());
265    const G4Colour& c = GetColour (polymarker);
266    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
267    G4OpenGLSceneHandler::AddPrimitive(poly[0]);
268    //    fLastPolymarker.SetPosition(polymarker[poly]);
269    return;
[936]270  }
271#endif 
272  printf("G4OpenGLStoredSceneHandler::AddPrimitive Cree une nouvelle liste de markers\n");
[593]273  AddPrimitivePreamble(polymarker);
274  G4OpenGLSceneHandler::AddPrimitive(polymarker);
275  AddPrimitivePostamble();
[938]276//  fLastPolymarker.SetPosition(polymarker[polymarker.size()-1]);
[593]277}
278
279void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Text& text)
280{
281  // Note: colour is still handled in
282  // G4OpenGLSceneHandler::AddPrimitive(const G4Text&), so it still
283  // gets into the display list
284  AddPrimitivePreamble(text);
285  G4OpenGLSceneHandler::AddPrimitive(text);
286  AddPrimitivePostamble();
287}
288
[529]289void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Circle& circle)
290{
291  AddPrimitivePreamble(circle);
292  G4OpenGLSceneHandler::AddPrimitive(circle);
293  AddPrimitivePostamble();
294}
295
296void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Square& square)
297{
298  AddPrimitivePreamble(square);
299  G4OpenGLSceneHandler::AddPrimitive(square);
300  AddPrimitivePostamble();
301}
302
[593]303void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Scale& scale)
[529]304{
[593]305  // Let base class split into primitives.
306  G4OpenGLSceneHandler::AddPrimitive(scale);
307}
308
309void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
310{
311  // Note: colour is still handled in
312  // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&), so it still
313  // gets into the display list
314  AddPrimitivePreamble(polyhedron);
315  G4OpenGLSceneHandler::AddPrimitive(polyhedron);
[529]316  AddPrimitivePostamble();
317}
318
[593]319void G4OpenGLStoredSceneHandler::AddPrimitive (const G4NURBS& nurbs)
320{
321  // Note: colour is still handled in
322  // G4OpenGLSceneHandler::AddPrimitive(const G4NURBS&), so it still
323  // gets into the display list
324  AddPrimitivePreamble(nurbs);
325  G4OpenGLSceneHandler::AddPrimitive(nurbs);
326  AddPrimitivePostamble();
327}
328
[529]329void G4OpenGLStoredSceneHandler::BeginPrimitives
[593]330(const G4Transform3D& objectTransformation)
331
332  G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
[529]333
[593]334  // Display list setup moved to AddPrimitivePreamble.  See notes there.
[529]335}
336
[593]337void G4OpenGLStoredSceneHandler::EndPrimitives ()
338{
339  G4OpenGLSceneHandler::EndPrimitives ();
[529]340}
341
[688]342void G4OpenGLStoredSceneHandler::BeginPrimitives2D
343(const G4Transform3D& objectTransformation)
[529]344{
[688]345  G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
[529]346}
347
348void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
349{
[593]350  G4OpenGLSceneHandler::EndPrimitives2D ();
[529]351}
352
353void G4OpenGLStoredSceneHandler::BeginModeling () {
354  G4VSceneHandler::BeginModeling();
355  ClearStore();  // ...and all that goes with it.
356  /* Debug...
357  fDisplayListId = glGenLists (1);
358  G4cout << "OGL::fDisplayListId (start): " << fDisplayListId << G4endl;
359  */
360}
361
362void G4OpenGLStoredSceneHandler::EndModeling () {
363  // Make a List which calls the other lists.
[936]364  printf("G4OpenGLStoredSceneHandler::EndModeling Cree une nouvelle liste+++++++++++++++++++++++++++\n");
[529]365  fTopPODL = glGenLists (1);
[866]366  if (glGetError() == GL_OUT_OF_MEMORY) {  // Could pre-allocate?
[529]367    G4cout <<
368      "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
369      "  display List for fTopPODL - try OpenGL Immediated mode."
370           << G4endl;
[593]371  } else {
[529]372    glNewList (fTopPODL, GL_COMPILE_AND_EXECUTE); {
373      for (size_t i = 0; i < fPOList.size (); i++) {
374        glPushMatrix();
375        G4OpenGLTransform3D oglt (fPOList[i].fTransform);
376        glMultMatrixd (oglt.GetGLMatrix ());
[593]377        if (fpViewer->GetViewParameters().IsPicking())
378          glLoadName(fPOList[i].fPickName);
[529]379        glCallList (fPOList[i].fDisplayListId);
380        glPopMatrix();
381      }
382    }
383    glEndList ();
[866]384    if (glGetError() == GL_OUT_OF_MEMORY) {  // Could close?
385      G4cout <<
386        "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
387        "  display List for fTopPODL - try OpenGL Immediated mode."
388             << G4endl;
389    }
[529]390  }
391
392  G4VSceneHandler::EndModeling ();
393
394  /* Debug...
395  fDisplayListId = glGenLists (1);
396  G4cout << "OGL::fDisplayListId (end): " << fDisplayListId << G4endl;
397  */
398}
399
400void G4OpenGLStoredSceneHandler::ClearStore () {
401
402  G4VSceneHandler::ClearStore ();  // Sets need kernel visit, etc.
403
404  // Delete OpenGL permanent display lists.
405  for (size_t i = 0; i < fPOList.size (); i++)
406    glDeleteLists (fPOList[i].fDisplayListId, 1);
407  if (fTopPODL) glDeleteLists (fTopPODL, 1);
408  fTopPODL = 0;
409
410  // Clear other lists, dictionary, etc.
411  fPOList.clear ();
412  fSolidMap.clear ();
[593]413  ClearAndDestroyAtts();
[529]414
415  // ...and clear transient store...
416  for (size_t i = 0; i < fTOList.size (); i++)
417    glDeleteLists(fTOList[i].fDisplayListId, 1);
418  fTOList.clear ();
419}
420
421void G4OpenGLStoredSceneHandler::ClearTransientStore () {
422
423  G4VSceneHandler::ClearTransientStore ();
424
425  // Delete OpenGL transient display lists and Transient Objects themselves.
426  for (size_t i = 0; i < fTOList.size (); i++)
427    glDeleteLists(fTOList[i].fDisplayListId, 1);
428  fTOList.clear ();
429
430  // Make sure screen corresponds to graphical database...
431  if (fpViewer) {
432    fpViewer -> SetView ();
433    fpViewer -> ClearView ();
434    fpViewer -> DrawView ();
435  }
436}
437
438void G4OpenGLStoredSceneHandler::RequestPrimitives (const G4VSolid& solid)
439{
440  if (fReadyForTransients) {
441    // Always draw transient solids, e.g., hits represented as solids.
442    // (As we have no control over the order of drawing of transient
443    // objects, we cannot do anything about transparent ones, as
444    // below, so always draw them.)
445    G4VSceneHandler::RequestPrimitives (solid);
446    return;
447  }
448
449  // For non-transient (run-duration) objects, ensure transparent
450  // objects are drawn last.  The problem of
451  // blending/transparency/alpha is quite a tricky one - see History
452  // of opengl-V07-01-01/2/3.
453  // Get vis attributes - pick up defaults if none.
454  const G4VisAttributes* pVA =
455    fpViewer -> GetApplicableVisAttributes(fpVisAttribs);
456  const G4Colour& c = pVA -> GetColour ();
457  G4double opacity = c.GetAlpha ();
458
459  if (!fSecondPass) {
460    G4bool transparency_enabled = true;
461    G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
462    if (pViewer) transparency_enabled = pViewer->transparency_enabled;
463    if (transparency_enabled && opacity < 1.) {
464      // On first pass, transparent objects are not drawn, but flag is set...
465      fSecondPassRequested = true;
466      return;
467    }
468  }
469
470  // On second pass, opaque objects are not drwan...
471  if (fSecondPass && opacity >= 1.) return;
472
473  G4PhysicalVolumeModel* pPVModel =
474    dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
475 
476  if (pPVModel) {
477    // If part of the geometry hierarchy, i.e., from a
478    // G4PhysicalVolumeModel, check if a display list already exists for
479    // this solid, re-use it if possible.  We could be smarter, and
480    // recognise repeated branches of the geometry hierarchy, for
481    // example.  But this algorithm should be secure, I think...
482    const G4VSolid* pSolid = &solid;
483    EAxis axis = kRho;
484    G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
485    if (pCurrentPV -> IsReplicated ()) {
486      G4int nReplicas;
487      G4double width;
488      G4double offset;
489      G4bool consuming;
490      pCurrentPV->GetReplicationData(axis,nReplicas,width,offset,consuming);
491    }
492    // Provided it is not parametrised (because if so, the
493    // solid's parameters might have been changed)...
494    if (!(pCurrentPV -> IsParameterised ()) &&
495        // Provided it is not replicated radially (because if so, the
496        // solid's parameters will have been changed)...
497        !(pCurrentPV -> IsReplicated () && axis == kRho) &&
498        // ...and if the solid has already been rendered...
499        (fSolidMap.find (pSolid) != fSolidMap.end ())) {
500      fDisplayListId = fSolidMap [pSolid];
[593]501      PO po(fDisplayListId,*fpObjectTransformation);
502      if (fpViewer->GetViewParameters().IsPicking()) {
503        G4AttHolder* holder = new G4AttHolder;
504        // Load G4Atts from G4VisAttributes, if any...
505        const G4VisAttributes* va = pPVModel->GetCurrentLV()->GetVisAttributes();
506        if (va) {
507          const std::map<G4String,G4AttDef>* vaDefs = va->GetAttDefs();
508          if (vaDefs) holder->AddAtts(va->CreateAttValues(), vaDefs);
509        }
510        // Load G4Atts from G4PhysicalVolumeModel...
511        const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs();
512        if (defs) holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs);
513        fPickMap[++fPickName] = holder;
514        po.fPickName = fPickName;
515      }
516      fPOList.push_back(po);
[529]517    }
518    else {
519      G4VSceneHandler::RequestPrimitives (solid);
520      fSolidMap [pSolid] = fDisplayListId;
521    }
522    return;
523  }
524
525  // Otherwise invoke base class method...
526  G4VSceneHandler::RequestPrimitives (solid);
527}
528
529G4int G4OpenGLStoredSceneHandler::fSceneIdCount = 0;
530
531#endif
Note: See TracBrowser for help on using the repository browser.