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

Last change on this file since 816 was 790, checked in by garnier, 18 years ago

r818@wl-72126: garnier | 2008-04-30 12:42:00 +0200
modif mise a jour sur le cvs

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