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

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

see history

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