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

Last change on this file since 746 was 688, checked in by garnier, 16 years ago

mise a jour par rapport au commit sur cvs de geant4

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