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

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

OpenGL Qt improvments. See History file

  • Property svn:mime-type set to text/cpp
File size: 15.1 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.39 2008/11/06 13:43:44 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
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 if (glGetError() == GL_OUT_OF_MEMORY) { // Could close?
176 G4cout <<
177 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
178 " display List for fTopPODL - try OpenGL Immediated mode."
179 << G4endl;
180 }
181 if (fMemoryForDisplayLists) {
182 glEndList();
183 if (glGetError() == GL_OUT_OF_MEMORY) { // Could close?
184 G4cout <<
185 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
186 " display List for fTopPODL - try OpenGL Immediated mode."
187 << G4endl;
188 }
189 }
190 if (fReadyForTransients || !fMemoryForDisplayLists) {
191 glPopMatrix();
192 glFlush ();
193 glDrawBuffer (GL_BACK);
194 }
195 fAddPrimitivePreambleNestingDepth--;
196}
197
198void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyline& polyline)
199{
200 AddPrimitivePreamble(polyline);
201 G4OpenGLSceneHandler::AddPrimitive(polyline);
202 AddPrimitivePostamble();
203}
204
205void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
206{
207 AddPrimitivePreamble(polymarker);
208 G4OpenGLSceneHandler::AddPrimitive(polymarker);
209 AddPrimitivePostamble();
210}
211
212void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Text& text)
213{
214 // Note: colour is still handled in
215 // G4OpenGLSceneHandler::AddPrimitive(const G4Text&), so it still
216 // gets into the display list
217 AddPrimitivePreamble(text);
218 G4OpenGLSceneHandler::AddPrimitive(text);
219 AddPrimitivePostamble();
220}
221
222void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Circle& circle)
223{
224 AddPrimitivePreamble(circle);
225 G4OpenGLSceneHandler::AddPrimitive(circle);
226 AddPrimitivePostamble();
227}
228
229void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Square& square)
230{
231 AddPrimitivePreamble(square);
232 G4OpenGLSceneHandler::AddPrimitive(square);
233 AddPrimitivePostamble();
234}
235
236void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Scale& scale)
237{
238 // Let base class split into primitives.
239 G4OpenGLSceneHandler::AddPrimitive(scale);
240}
241
242void G4OpenGLStoredSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
243{
244 // Note: colour is still handled in
245 // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&), so it still
246 // gets into the display list
247 AddPrimitivePreamble(polyhedron);
248 G4OpenGLSceneHandler::AddPrimitive(polyhedron);
249 AddPrimitivePostamble();
250}
251
252void G4OpenGLStoredSceneHandler::AddPrimitive (const G4NURBS& nurbs)
253{
254 // Note: colour is still handled in
255 // G4OpenGLSceneHandler::AddPrimitive(const G4NURBS&), so it still
256 // gets into the display list
257 AddPrimitivePreamble(nurbs);
258 G4OpenGLSceneHandler::AddPrimitive(nurbs);
259 AddPrimitivePostamble();
260}
261
262void G4OpenGLStoredSceneHandler::BeginPrimitives
263(const G4Transform3D& objectTransformation)
264{
265 G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
266
267 // Display list setup moved to AddPrimitivePreamble. See notes there.
268}
269
270void G4OpenGLStoredSceneHandler::EndPrimitives ()
271{
272 G4OpenGLSceneHandler::EndPrimitives ();
273}
274
275void G4OpenGLStoredSceneHandler::BeginPrimitives2D
276(const G4Transform3D& objectTransformation)
277{
278 G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
279}
280
281void G4OpenGLStoredSceneHandler::EndPrimitives2D ()
282{
283 G4OpenGLSceneHandler::EndPrimitives2D ();
284}
285
286void G4OpenGLStoredSceneHandler::BeginModeling () {
287 G4VSceneHandler::BeginModeling();
288 ClearStore(); // ...and all that goes with it.
289 /* Debug...
290 fDisplayListId = glGenLists (1);
291 G4cout << "OGL::fDisplayListId (start): " << fDisplayListId << G4endl;
292 */
293}
294
295void G4OpenGLStoredSceneHandler::EndModeling () {
296 // Make a List which calls the other lists.
297 fTopPODL = glGenLists (1);
298 if (glGetError() == GL_OUT_OF_MEMORY) { // Could pre-allocate?
299 G4cout <<
300 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
301 " display List for fTopPODL - try OpenGL Immediated mode."
302 << G4endl;
303 } else {
304 glNewList (fTopPODL, GL_COMPILE_AND_EXECUTE); {
305 for (size_t i = 0; i < fPOList.size (); i++) {
306 glPushMatrix();
307 G4OpenGLTransform3D oglt (fPOList[i].fTransform);
308 glMultMatrixd (oglt.GetGLMatrix ());
309 if (fpViewer->GetViewParameters().IsPicking())
310 glLoadName(fPOList[i].fPickName);
311 glCallList (fPOList[i].fDisplayListId);
312 glPopMatrix();
313 }
314 }
315 glEndList ();
316 if (glGetError() == GL_OUT_OF_MEMORY) { // Could close?
317 G4cout <<
318 "ERROR: G4OpenGLStoredSceneHandler::EndModeling: Failure to allocate"
319 " display List for fTopPODL - try OpenGL Immediated mode."
320 << G4endl;
321 }
322 }
323
324 G4VSceneHandler::EndModeling ();
325
326 /* Debug...
327 fDisplayListId = glGenLists (1);
328 G4cout << "OGL::fDisplayListId (end): " << fDisplayListId << G4endl;
329 */
330}
331
332void G4OpenGLStoredSceneHandler::ClearStore () {
333
334 G4VSceneHandler::ClearStore (); // Sets need kernel visit, etc.
335
336 // Delete OpenGL permanent display lists.
337 for (size_t i = 0; i < fPOList.size (); i++)
338 glDeleteLists (fPOList[i].fDisplayListId, 1);
339 if (fTopPODL) glDeleteLists (fTopPODL, 1);
340 fTopPODL = 0;
341
342 // Clear other lists, dictionary, etc.
343 fPOList.clear ();
344 fSolidMap.clear ();
345 ClearAndDestroyAtts();
346
347 // ...and clear transient store...
348 for (size_t i = 0; i < fTOList.size (); i++)
349 glDeleteLists(fTOList[i].fDisplayListId, 1);
350 fTOList.clear ();
351}
352
353void G4OpenGLStoredSceneHandler::ClearTransientStore () {
354
355 G4VSceneHandler::ClearTransientStore ();
356
357 // Delete OpenGL transient display lists and Transient Objects themselves.
358 for (size_t i = 0; i < fTOList.size (); i++)
359 glDeleteLists(fTOList[i].fDisplayListId, 1);
360 fTOList.clear ();
361
362 // Make sure screen corresponds to graphical database...
363 if (fpViewer) {
364 fpViewer -> SetView ();
365 fpViewer -> ClearView ();
366 fpViewer -> DrawView ();
367 }
368}
369
370void G4OpenGLStoredSceneHandler::RequestPrimitives (const G4VSolid& solid)
371{
372 if (fReadyForTransients) {
373 // Always draw transient solids, e.g., hits represented as solids.
374 // (As we have no control over the order of drawing of transient
375 // objects, we cannot do anything about transparent ones, as
376 // below, so always draw them.)
377 G4VSceneHandler::RequestPrimitives (solid);
378 return;
379 }
380
381 // For non-transient (run-duration) objects, ensure transparent
382 // objects are drawn last. The problem of
383 // blending/transparency/alpha is quite a tricky one - see History
384 // of opengl-V07-01-01/2/3.
385 // Get vis attributes - pick up defaults if none.
386 const G4VisAttributes* pVA =
387 fpViewer -> GetApplicableVisAttributes(fpVisAttribs);
388 const G4Colour& c = pVA -> GetColour ();
389 G4double opacity = c.GetAlpha ();
390
391 if (!fSecondPass) {
392 G4bool transparency_enabled = true;
393 G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
394 if (pViewer) transparency_enabled = pViewer->transparency_enabled;
395 if (transparency_enabled && opacity < 1.) {
396 // On first pass, transparent objects are not drawn, but flag is set...
397 fSecondPassRequested = true;
398 return;
399 }
400 }
401
402 // On second pass, opaque objects are not drwan...
403 if (fSecondPass && opacity >= 1.) return;
404
405 G4PhysicalVolumeModel* pPVModel =
406 dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
407
408 if (pPVModel) {
409 // If part of the geometry hierarchy, i.e., from a
410 // G4PhysicalVolumeModel, check if a display list already exists for
411 // this solid, re-use it if possible. We could be smarter, and
412 // recognise repeated branches of the geometry hierarchy, for
413 // example. But this algorithm should be secure, I think...
414 const G4VSolid* pSolid = &solid;
415 EAxis axis = kRho;
416 G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
417 if (pCurrentPV -> IsReplicated ()) {
418 G4int nReplicas;
419 G4double width;
420 G4double offset;
421 G4bool consuming;
422 pCurrentPV->GetReplicationData(axis,nReplicas,width,offset,consuming);
423 }
424 // Provided it is not parametrised (because if so, the
425 // solid's parameters might have been changed)...
426 if (!(pCurrentPV -> IsParameterised ()) &&
427 // Provided it is not replicated radially (because if so, the
428 // solid's parameters will have been changed)...
429 !(pCurrentPV -> IsReplicated () && axis == kRho) &&
430 // ...and if the solid has already been rendered...
431 (fSolidMap.find (pSolid) != fSolidMap.end ())) {
432 fDisplayListId = fSolidMap [pSolid];
433 PO po(fDisplayListId,*fpObjectTransformation);
434 if (fpViewer->GetViewParameters().IsPicking()) {
435 G4AttHolder* holder = new G4AttHolder;
436 // Load G4Atts from G4VisAttributes, if any...
437 const G4VisAttributes* va = pPVModel->GetCurrentLV()->GetVisAttributes();
438 if (va) {
439 const std::map<G4String,G4AttDef>* vaDefs = va->GetAttDefs();
440 if (vaDefs) holder->AddAtts(va->CreateAttValues(), vaDefs);
441 }
442 // Load G4Atts from G4PhysicalVolumeModel...
443 const std::map<G4String,G4AttDef>* defs = pPVModel->GetAttDefs();
444 if (defs) holder->AddAtts(pPVModel->CreateCurrentAttValues(), defs);
445 fPickMap[++fPickName] = holder;
446 po.fPickName = fPickName;
447 }
448 fPOList.push_back(po);
449 }
450 else {
451 G4VSceneHandler::RequestPrimitives (solid);
452 fSolidMap [pSolid] = fDisplayListId;
453 }
454 return;
455 }
456
457 // Otherwise invoke base class method...
458 G4VSceneHandler::RequestPrimitives (solid);
459}
460
461G4int G4OpenGLStoredSceneHandler::fSceneIdCount = 0;
462
463#endif
Note: See TracBrowser for help on using the repository browser.