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

Last change on this file since 949 was 948, checked in by garnier, 15 years ago

Works for OGLSXm et non vector

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