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

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

before tag

  • Property svn:mime-type set to text/cpp
File size: 17.3 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//
[1346]27// $Id: G4OpenGLStoredSceneHandler.cc,v 1.46 2010/11/10 17:11:20 allison Exp $
[1340]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),
81fAddPrimitivePreambleNestingDepth (0),
[1160]82fTopPODL (0)
[529]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
[593]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.
[529]98
[593]99 if (fpViewer->GetViewParameters().IsPicking()) {
100 fPickMap[++fPickName] = 0;
101 }
[529]102
[593]103 const G4Colour& c = GetColour (visible);
[529]104
[593]105 if (fMemoryForDisplayLists) {
106 fDisplayListId = glGenLists (1);
[1346]107#ifdef G4DEBUG_VIS_OGL
108 printf("G4OpenGLStoredSceneHandler::AddPrimitivePreamble ->glGenList(1) : %d readyForTranscient:%d\n",fDisplayListId,fReadyForTransients);
109#endif
[1340]110 if (glGetError() == GL_OUT_OF_MEMORY ||
[1346]111 fDisplayListId > fDisplayListLimit) {
[593]112 G4cout <<
[1346]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***************************************************"
[593]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();
[1346]132#ifdef G4DEBUG_VIS_OGL
133 printf("G4OpenGLStoredSceneHandler::AddPrimitivePreamble ->fTOList puch_back\n");
134#endif
[593]135 fTOList.push_back(to);
136 glDrawBuffer (GL_FRONT);
[1346]137#ifdef G4DEBUG_VIS_OGL
138 printf("+1\n");
139#endif
[593]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;
[1346]149#ifdef G4DEBUG_VIS_OGL
150 printf("G4OpenGLStoredSceneHandler::AddPrimitivePreamble ->fPOList puch_back\n");
151#endif
[593]152 fPOList.push_back(po);
153 glNewList (fDisplayListId, GL_COMPILE);
154 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
155 }
[529]156 } else {
[593]157 glDrawBuffer (GL_FRONT);
[1346]158#ifdef G4DEBUG_VIS_OGL
159 printf("+2\n");
160#endif
[593]161 glPushMatrix();
162 G4OpenGLTransform3D oglt (*fpObjectTransformation);
163 glMultMatrixd (oglt.GetGLMatrix ());
[529]164 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
165 }
[593]166
167 if (fProcessing2D) {
168 // Push current 3D world matrices and load identity to define screen
169 // coordinates...
170 glMatrixMode (GL_PROJECTION);
[1346]171#ifdef G4DEBUG_VIS_OGL
172 printf("+3\n");
173#endif
[593]174 glPushMatrix();
175 glLoadIdentity();
[789]176 glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
[593]177 glMatrixMode (GL_MODELVIEW);
[1346]178#ifdef G4DEBUG_VIS_OGL
179 printf("+4\n");
180#endif
[593]181 glPushMatrix();
182 glLoadIdentity();
[688]183 G4OpenGLTransform3D oglt (*fpObjectTransformation);
184 glMultMatrixd (oglt.GetGLMatrix ());
185 glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
[593]186 }
[529]187}
188
189void G4OpenGLStoredSceneHandler::AddPrimitivePostamble()
190{
[593]191 if (fProcessing2D) {
192 // Pop current 3D world matrices back again...
193 glMatrixMode (GL_PROJECTION);
[1346]194#ifdef G4DEBUG_VIS_OGL
195 printf("-1\n");
196#endif
[593]197 glPopMatrix();
198 glMatrixMode (GL_MODELVIEW);
[1346]199#ifdef G4DEBUG_VIS_OGL
200 printf("-2\n");
201#endif
[593]202 glPopMatrix();
203 }
204
[877]205 // if ((glGetError() == GL_TABLE_TOO_LARGE) || (glGetError() == GL_OUT_OF_MEMORY)) { // Could close?
206 if (glGetError() == GL_OUT_OF_MEMORY) { // Could close?
[866]207 G4cout <<
208 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
209 " display List for fTopPODL - try OpenGL Immediated mode."
210 << G4endl;
211 }
[593]212 if (fMemoryForDisplayLists) {
213 glEndList();
[866]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 }
[593]220 }
221 if (fReadyForTransients || !fMemoryForDisplayLists) {
[1346]222#ifdef G4DEBUG_VIS_OGL
223 printf("-3\n");
224#endif
[593]225 glPopMatrix();
226 glFlush ();
227 glDrawBuffer (GL_BACK);
228 }
[529]229 fAddPrimitivePreambleNestingDepth--;
230}
231
232void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyline& polyline)
233{
[1346]234#ifdef G4DEBUG_VIS_OGL
235 printf("G4OpenGLStoredSceneHandler::AddPrimitive polyline\n");
236 // if (glIsList(7)) return;
237#endif
[529]238 AddPrimitivePreamble(polyline);
239 G4OpenGLSceneHandler::AddPrimitive(polyline);
240 AddPrimitivePostamble();
241}
242
[593]243void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
244{
[1346]245#ifdef G4DEBUG_VIS_OGL
246 printf("G4OpenGLStoredSceneHandler::AddPrimitive polymarker\n");
247 if (glIsList(7)) return;
248#endif
[593]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
[1346]259#ifdef G4DEBUG_VIS_OGL
260 printf("G4OpenGLStoredSceneHandler::AddPrimitive Text\n");
261#endif
[593]262 AddPrimitivePreamble(text);
263 G4OpenGLSceneHandler::AddPrimitive(text);
264 AddPrimitivePostamble();
265}
266
[529]267void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Circle& circle)
268{
[1346]269#ifdef G4DEBUG_VIS_OGL
270 printf("G4OpenGLStoredSceneHandler::AddPrimitive circle\n");
271#endif
[529]272 AddPrimitivePreamble(circle);
273 G4OpenGLSceneHandler::AddPrimitive(circle);
274 AddPrimitivePostamble();
275}
276
277void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Square& square)
278{
[1346]279#ifdef G4DEBUG_VIS_OGL
280 printf("G4OpenGLStoredSceneHandler::AddPrimitive square\n");
281#endif
[529]282 AddPrimitivePreamble(square);
283 G4OpenGLSceneHandler::AddPrimitive(square);
284 AddPrimitivePostamble();
285}
286
[593]287void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Scale& scale)
[529]288{
[593]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
[1346]298#ifdef G4DEBUG_VIS_OGL
299 printf("G4OpenGLStoredSceneHandler::AddPrimitive polyhedron\n");
300#endif
[593]301 AddPrimitivePreamble(polyhedron);
302 G4OpenGLSceneHandler::AddPrimitive(polyhedron);
[529]303 AddPrimitivePostamble();
304}
305
[593]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
[1346]311#ifdef G4DEBUG_VIS_OGL
312 printf("G4OpenGLStoredSceneHandler::AddPrimitive nurbs\n");
313#endif
[593]314 AddPrimitivePreamble(nurbs);
315 G4OpenGLSceneHandler::AddPrimitive(nurbs);
316 AddPrimitivePostamble();
317}
318
[529]319void G4OpenGLStoredSceneHandler::BeginPrimitives
[593]320(const G4Transform3D& objectTransformation)
321{
322 G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
[529]323
[593]324 // Display list setup moved to AddPrimitivePreamble. See notes there.
[529]325}
326
[593]327void G4OpenGLStoredSceneHandler::EndPrimitives ()
328{
329 G4OpenGLSceneHandler::EndPrimitives ();
[529]330}
331
[688]332void G4OpenGLStoredSceneHandler::BeginPrimitives2D
333(const G4Transform3D& objectTransformation)
[529]334{
[688]335 G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
[529]336}
337
338void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
339{
[593]340 G4OpenGLSceneHandler::EndPrimitives2D ();
[529]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);
[1346]355#ifdef G4DEBUG_VIS_OGL
356 printf("G4OpenGLStoredSceneHandler::EndModeling ->glGenList(1) fTopPODL:%d\n",fTopPODL);
357#endif
[866]358 if (glGetError() == GL_OUT_OF_MEMORY) { // Could pre-allocate?
[529]359 G4cout <<
360 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
361 " display List for fTopPODL - try OpenGL Immediated mode."
362 << G4endl;
[593]363 } else {
[529]364 glNewList (fTopPODL, GL_COMPILE_AND_EXECUTE); {
365 for (size_t i = 0; i < fPOList.size (); i++) {
[1346]366#ifdef G4DEBUG_VIS_OGL
367 printf("+5\n");
368#endif
[529]369 glPushMatrix();
370 G4OpenGLTransform3D oglt (fPOList[i].fTransform);
371 glMultMatrixd (oglt.GetGLMatrix ());
[593]372 if (fpViewer->GetViewParameters().IsPicking())
373 glLoadName(fPOList[i].fPickName);
[529]374 glCallList (fPOList[i].fDisplayListId);
[1346]375#ifdef G4DEBUG_VIS_OGL
376 printf("-4\n");
377#endif
[529]378 glPopMatrix();
379 }
380 }
381 glEndList ();
[866]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 }
[529]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
[1346]400 //G4cout << "G4OpenGLStoredSceneHandler::ClearStore" << G4endl;
401
[529]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 ();
[1340]419
[1343]420 fMemoryForDisplayLists = true;
[529]421}
422
423void G4OpenGLStoredSceneHandler::ClearTransientStore () {
424
[1346]425 //G4cout << "G4OpenGLStoredSceneHandler::ClearTransientStore" << G4endl;
426
[529]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 ();
[1307]433
[1343]434 fMemoryForDisplayLists = true;
[1340]435
[1307]436 // Make sure screen corresponds to graphical database...
437 if (fpViewer) {
438 fpViewer -> SetView ();
439 fpViewer -> ClearView ();
440 fpViewer -> DrawView ();
441 }
[529]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];
[593]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);
[529]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
[1343]537G4int G4OpenGLStoredSceneHandler::fDisplayListId = 0;
538G4bool G4OpenGLStoredSceneHandler::fMemoryForDisplayLists = true;
539G4int G4OpenGLStoredSceneHandler::fDisplayListLimit = 50000;
[1340]540
[529]541#endif
Note: See TracBrowser for help on using the repository browser.