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

Last change on this file since 937 was 936, checked in by garnier, 17 years ago

ajout de comments de debug

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