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

Last change on this file since 1305 was 1286, checked in by garnier, 14 years ago

update from CVS

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