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

Last change on this file since 1346 was 1346, checked in by garnier, 13 years ago

before tag

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