source: trunk/source/visualization/OpenGL/src/G4OpenGLSceneHandler.cc @ 1238

Last change on this file since 1238 was 1238, checked in by garnier, 14 years ago

otpimisations

  • Property svn:mime-type set to text/cpp
File size: 26.9 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: G4OpenGLSceneHandler.cc,v 1.56 2009/10/21 15:18:43 allison Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Andrew Walkden  27th March 1996
32// OpenGL stored scene - creates OpenGL display lists.
33// OpenGL immediate scene - draws immediately to buffer
34//                           (saving space on server).
35
36#ifdef G4VIS_BUILD_OPENGL_DRIVER
37
38// Included here - problems with HP compiler if not before other includes?
39#include "G4NURBS.hh"
40
41// Here follows a special for Mesa, the OpenGL emulator.  Does not affect
42// other OpenGL's, as far as I'm aware.   John Allison 18/9/96.
43#define CENTERLINE_CLPP  /* CenterLine C++ workaround: */
44// Also seems to be required for HP's CC and AIX xlC, at least.
45
46#include "G4OpenGLSceneHandler.hh"
47#include "G4OpenGLViewer.hh"
48#include "G4OpenGLFontBaseStore.hh"
49#include "G4OpenGLTransform3D.hh"
50#include "G4Point3D.hh"
51#include "G4Normal3D.hh"
52#include "G4Transform3D.hh"
53#include "G4Polyline.hh"
54#include "G4Polymarker.hh"
55#include "G4Text.hh"
56#include "G4Circle.hh"
57#include "G4Square.hh"
58#include "G4VMarker.hh"
59#include "G4Polyhedron.hh"
60#include "G4VisAttributes.hh"
61#include "G4PhysicalVolumeModel.hh"
62#include "G4VPhysicalVolume.hh"
63#include "G4LogicalVolume.hh"
64#include "G4VSolid.hh"
65#include "G4Scene.hh"
66#include "G4VisExtent.hh"
67#include "G4AttHolder.hh"
68
69G4OpenGLSceneHandler::G4OpenGLSceneHandler (G4VGraphicsSystem& system,
70                              G4int id,
71                              const G4String& name):
72  G4VSceneHandler (system, id, name),
73  fPickName(0),
74  fProcessing2D (false),
75  fProcessingPolymarker(false)
76{}
77
78G4OpenGLSceneHandler::~G4OpenGLSceneHandler ()
79{
80  ClearStore ();
81}
82
83const GLubyte G4OpenGLSceneHandler::fStippleMaskHashed [128] = {
84  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
85  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
86  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
87  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
88  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
89  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
90  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
91  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
92  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
93  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
94  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
95  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
96  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
97  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
98  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
99  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
100};
101
102void G4OpenGLSceneHandler::ClearAndDestroyAtts()
103{
104  std::map<GLuint, G4AttHolder*>::iterator i;
105  for (i = fPickMap.begin(); i != fPickMap.end(); ++i) delete i->second;
106  fPickMap.clear();
107}
108
109void G4OpenGLSceneHandler::PreAddSolid
110(const G4Transform3D& objectTransformation,
111 const G4VisAttributes& visAttribs)
112{
113  G4VSceneHandler::PreAddSolid (objectTransformation, visAttribs);
114}
115
116void G4OpenGLSceneHandler::BeginPrimitives
117(const G4Transform3D& objectTransformation)
118{
119  G4VSceneHandler::BeginPrimitives (objectTransformation);
120}
121
122void G4OpenGLSceneHandler::EndPrimitives ()
123{
124  G4VSceneHandler::EndPrimitives ();
125}
126
127void G4OpenGLSceneHandler::BeginPrimitives2D
128(const G4Transform3D& objectTransformation)
129{
130  G4VSceneHandler::BeginPrimitives2D (objectTransformation);
131  fProcessing2D = true;
132}
133
134void G4OpenGLSceneHandler::EndPrimitives2D ()
135{
136  fProcessing2D = false;
137  G4VSceneHandler::EndPrimitives2D ();
138}
139
140const G4Polyhedron* G4OpenGLSceneHandler::CreateSectionPolyhedron ()
141{
142  // Clipping done in G4OpenGLViewer::SetView.
143  return 0;
144
145  // But...OpenGL no longer seems to reconstruct clipped edges, so,
146  // when the BooleanProcessor is up to it, abandon this and use
147  // generic clipping in G4VSceneHandler::CreateSectionPolyhedron...
148  // return G4VSceneHandler::CreateSectionPolyhedron();
149}
150
151const G4Polyhedron* G4OpenGLSceneHandler::CreateCutawayPolyhedron ()
152{
153  // Cutaway done in G4OpenGLViewer::SetView.
154  return 0;
155
156  // But...if not, when the BooleanProcessor is up to it...
157  // return G4VSceneHandler::CreateCutawayPolyhedron();
158}
159
160void G4OpenGLSceneHandler::AddPrimitive (const G4Polyline& line)
161{
162  G4int nPoints = line.size ();
163  if (nPoints <= 0) return;
164
165  // Loads G4Atts for picking...
166  if (fpViewer->GetViewParameters().IsPicking()) {
167    G4AttHolder* holder = new G4AttHolder;
168    LoadAtts(line, holder);
169    fPickMap[fPickName] = holder;
170  }
171
172  // Note: colour treated in sub-class.
173
174  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
175    glDisable (GL_DEPTH_TEST);
176  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
177
178  glDisable (GL_LIGHTING);
179
180  // Get vis attributes - pick up defaults if none.
181  const G4VisAttributes* pVA =
182    fpViewer -> GetApplicableVisAttributes (line.GetVisAttributes ());
183
184  G4double lineWidth = GetLineWidth(pVA);
185  glLineWidth(lineWidth);
186
187  glBegin (GL_LINE_STRIP);
188  for (G4int iPoint = 0; iPoint < nPoints; iPoint++) {
189  G4double x, y, z;
190    x = line[iPoint].x();
191    y = line[iPoint].y();
192    z = line[iPoint].z();
193    glVertex3d (x, y, z);
194  }
195  glEnd ();
196}
197
198void G4OpenGLSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
199{
200#ifdef G4DEBUG_VIS_OGL
201// printf("G4OpenGLSceneHandler::AddPrimitive polymarker");
202#endif
203  G4int nPoints = polymarker.size ();
204  if (nPoints <= 0) return;
205
206  fProcessingPolymarker = true;
207
208  // Loads G4Atts for picking...
209  if (fpViewer->GetViewParameters().IsPicking()) {
210    G4AttHolder* holder = new G4AttHolder;
211    LoadAtts(polymarker, holder);
212    fPickMap[fPickName] = holder;
213  }
214
215  switch (polymarker.GetMarkerType()) {
216  default:
217  case G4Polymarker::dots:
218    {
219#ifdef G4DEBUG_VIS_OGL
220// printf(" dots ");
221#endif
222      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
223        G4Circle dot (polymarker);
224        dot.SetPosition (polymarker[iPoint]);
225        dot.SetWorldSize  (0.);
226        dot.SetScreenSize (0.1);  // Very small circle.
227        G4OpenGLSceneHandler::AddPrimitive (dot);
228      }
229    }
230    break;
231  case G4Polymarker::circles:
232    {
233#ifdef G4DEBUG_VIS_OGL
234// printf(" circle ");
235#endif
236      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
237        G4Circle circle (polymarker);
238        circle.SetPosition (polymarker[iPoint]);
239        G4OpenGLSceneHandler::AddPrimitive (circle);
240      }
241    }
242    break;
243  case G4Polymarker::squares:
244    {
245#ifdef G4DEBUG_VIS_OGL
246// printf(" square ");
247#endif
248      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
249        G4Square square (polymarker);
250        square.SetPosition (polymarker[iPoint]);
251        G4OpenGLSceneHandler::AddPrimitive (square);
252      }
253    }
254    break;
255  }
256
257#ifdef G4DEBUG_VIS_OGL
258// printf(" \n");
259#endif
260  fProcessingPolymarker = false;
261}
262
263void G4OpenGLSceneHandler::AddPrimitive (const G4Text& text) {
264
265  // Loads G4Atts for picking...
266  if (fpViewer->GetViewParameters().IsPicking()) {
267    G4AttHolder* holder = new G4AttHolder;
268    LoadAtts(text, holder);
269    fPickMap[fPickName] = holder;
270  }
271
272  const G4Colour& c = GetTextColour (text);  // Picks up default if none.
273  MarkerSizeType sizeType;
274  G4double size = GetMarkerSize (text, sizeType);
275  G4ThreeVector position (text.GetPosition ());
276  G4String textString = text.GetText();
277
278  G4int font_base = G4OpenGLFontBaseStore::GetFontBase(fpViewer,size);
279  if (font_base < 0) {
280    static G4int callCount = 0;
281    ++callCount;
282    if (callCount <= 10 || callCount%100 == 0) {
283      G4cout <<
284        "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
285             << callCount <<
286        "\n  No fonts available."
287        "\n  Called with text \""
288             << text.GetText ()
289             << "\"\n  at " << position
290             << ", size " << size
291             << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
292             << ", type " << G4int(sizeType)
293             << ", colour " << c
294             << G4endl;
295    }
296    return;
297  }
298  const char* textCString = textString.c_str();
299  glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
300  glDisable (GL_DEPTH_TEST);
301  glDisable (GL_LIGHTING);
302 
303  glRasterPos3d(position.x(),position.y(),position.z());
304  // No action on offset or layout at present.
305   glPushAttrib(GL_LIST_BIT);
306   glListBase(font_base);
307   glCallLists(strlen(textCString), GL_UNSIGNED_BYTE, (GLubyte *)textCString);
308   glPopAttrib();
309}
310
311void G4OpenGLSceneHandler::AddPrimitive (const G4Circle& circle) {
312  glEnable (GL_POINT_SMOOTH);
313  AddCircleSquare (circle, G4OpenGLBitMapStore::circle);
314}
315
316void G4OpenGLSceneHandler::AddPrimitive (const G4Square& square) {
317  glDisable (GL_POINT_SMOOTH);
318  AddCircleSquare (square, G4OpenGLBitMapStore::square);
319}
320
321void G4OpenGLSceneHandler::AddCircleSquare
322(const G4VMarker& marker,
323 G4OpenGLBitMapStore::Shape shape) {
324
325  if (!fProcessingPolymarker) {  // Polymarker has already loaded atts.
326    // Loads G4Atts for picking...
327    if (fpViewer->GetViewParameters().IsPicking()) {
328      G4AttHolder* holder = new G4AttHolder;
329      LoadAtts(marker, holder);
330      fPickMap[fPickName] = holder;
331    }
332  }
333
334  // Note: colour treated in sub-class.
335
336  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ()) {
337#ifdef G4DEBUG_VIS_OGL
338    //    printf("+");
339#endif
340    glDisable (GL_DEPTH_TEST);
341  } else {
342#ifdef G4DEBUG_VIS_OGL
343    //    printf("-");
344#endif
345    glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
346 
347  glDisable (GL_LIGHTING);
348 
349  // Get vis attributes - pick up defaults if none.
350  const G4VisAttributes* pVA =
351    fpViewer -> GetApplicableVisAttributes (marker.GetVisAttributes ());
352
353  G4double lineWidth = GetLineWidth(pVA);
354  glLineWidth(lineWidth);
355
356  G4VMarker::FillStyle style = marker.GetFillStyle();
357
358  G4bool filled = false;
359  static G4bool hashedWarned = false;
360 
361  switch (style) {
362  case G4VMarker::noFill:
363    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
364    filled = false;
365    break;
366   
367  case G4VMarker::hashed:
368    if (!hashedWarned) {
369      G4cout << "Hashed fill style in G4OpenGLSceneHandler."
370             << "\n  Not implemented.  Using G4VMarker::filled."
371             << G4endl;
372      hashedWarned = true;
373    }
374    // Maybe use
375    //glPolygonStipple (fStippleMaskHashed);
376    // Drop through to filled...
377   
378  case G4VMarker::filled:
379    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
380    filled = true;
381    break;
382   
383  }
384
385  // A few useful quantities...
386  G4Point3D centre = marker.GetPosition();
387  MarkerSizeType sizeType;
388  G4double size = GetMarkerSize(marker, sizeType);
389
390
391  // Draw...
392   if (sizeType == world) {  // Size specified in world coordinates.
393
394     DrawXYPolygon (shape, size, centre, pVA);
395
396   } else { // Size specified in screen (window) coordinates.
397     glPointSize (size);       
398     glBegin (GL_POINTS);
399     glVertex3f(centre.x(),centre.y(),centre.z());
400     glEnd();
401     //Antialiasing
402     glEnable (GL_POINT_SMOOTH);
403     //Transparency
404     glEnable(GL_BLEND);
405     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
406
407     // L. GARNIER 1 March 2009
408     // Old method, we draw a bitmap instead of a GL_POINT.
409     // I remove it because it cost in term of computing performances
410     // and gl2ps can't draw bitmaps
411
412     //      glRasterPos3d(centre.x(),centre.y(),centre.z());
413     //      const GLubyte* marker =
414     //        G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
415     //      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
416     //      glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
417   }
418}
419
420void G4OpenGLSceneHandler::DrawXYPolygon
421(G4OpenGLBitMapStore::Shape shape,
422 G4double size,
423 const G4Point3D& centre,
424 const G4VisAttributes* pApplicableVisAtts)
425{
426  G4int nSides;
427  G4double startPhi;
428  if (shape == G4OpenGLBitMapStore::circle) {
429    nSides = GetNoOfSides(pApplicableVisAtts);
430    startPhi = 0.;
431  } else {
432    nSides = 4;
433    startPhi = -pi / 4.;
434  }
435
436  const G4Vector3D& viewpointDirection =
437    fpViewer -> GetViewParameters().GetViewpointDirection();
438  const G4Vector3D& up = fpViewer->GetViewParameters().GetUpVector();
439  const G4double dPhi = twopi / nSides;
440  const G4double radius = size / 2.;
441  G4Vector3D start = radius * (up.cross(viewpointDirection)).unit();
442  G4double phi;
443  G4int i;
444
445  glBegin (GL_POLYGON);
446  for (i = 0, phi = startPhi; i < nSides; i++, phi += dPhi) {
447    G4Vector3D r = start; r.rotate(phi, viewpointDirection);
448    G4Vector3D p = centre + r;
449    glVertex3d (p.x(), p.y(), p.z());
450  }
451  glEnd ();
452}
453
454void G4OpenGLSceneHandler::AddPrimitive (const G4Scale& scale)
455{
456  G4VSceneHandler::AddPrimitive(scale);
457}
458
459//Method for handling G4Polyhedron objects for drawing solids.
460void G4OpenGLSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) {
461
462  // Assume all facets are planar convex quadrilaterals.
463  // Draw each facet individually
464 
465  if (polyhedron.GetNoFacets() == 0) return;
466
467  // Loads G4Atts for picking...
468  if (fpViewer->GetViewParameters().IsPicking()) {
469    G4AttHolder* holder = new G4AttHolder;
470    LoadAtts(polyhedron, holder);
471    fPickMap[fPickName] = holder;
472  }
473
474  // Get vis attributes - pick up defaults if none.
475  const G4VisAttributes* pVA =
476    fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
477
478  // Get view parameters that the user can force through the vis
479  // attributes, thereby over-riding the current view parameter.
480  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
481
482  //Get colour, etc...
483  G4bool transparency_enabled = true;
484  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
485  if (pViewer) transparency_enabled = pViewer->transparency_enabled;
486  const G4Colour& c = pVA->GetColour();
487  GLfloat materialColour [4];
488  materialColour [0] = c.GetRed ();
489  materialColour [1] = c.GetGreen ();
490  materialColour [2] = c.GetBlue ();
491  if (transparency_enabled) {
492    materialColour [3] = c.GetAlpha ();
493  } else {
494    materialColour [3] = 1.;
495  }
496
497  G4double lineWidth = GetLineWidth(pVA);
498  glLineWidth(lineWidth);
499
500  GLfloat clear_colour[4];
501  glGetFloatv (GL_COLOR_CLEAR_VALUE, clear_colour);
502
503  G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
504
505  G4bool clipping = pViewer->fVP.IsSection() || pViewer->fVP.IsCutaway();
506
507  // Lighting disabled unless otherwise requested
508  glDisable (GL_LIGHTING);
509
510  switch (drawing_style) {
511  case (G4ViewParameters::hlhsr):
512    // Set up as for hidden line removal but paint polygon faces later...
513  case (G4ViewParameters::hlr):
514    glEnable (GL_STENCIL_TEST);
515    // The stencil buffer is cleared in G4OpenGLViewer::ClearView.
516    // The procedure below leaves it clear.
517    glStencilFunc (GL_ALWAYS, 0, 1);
518    glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
519    glEnable (GL_DEPTH_TEST);
520    glDepthFunc (GL_LEQUAL);
521    if (materialColour[3] < 1.) {
522      // Transparent...
523      glDisable (GL_CULL_FACE);
524      glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
525    } else {
526      // Opaque...
527      if (clipping) {
528        glDisable (GL_CULL_FACE);
529        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
530      } else {
531        glEnable (GL_CULL_FACE);
532        glCullFace (GL_BACK);
533        glPolygonMode (GL_FRONT, GL_LINE);
534      }
535    }
536    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
537    break;
538  case (G4ViewParameters::hsr):
539    glEnable (GL_DEPTH_TEST);
540    glDepthFunc (GL_LEQUAL);   
541    if (materialColour[3] < 1.) {
542      // Transparent...
543      glDepthMask (0);  // Make depth buffer read-only.
544      glDisable (GL_CULL_FACE);
545      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
546      glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialColour);
547    } else {
548      // Opaque...
549      glDepthMask (1);  // Make depth buffer writable (default).
550      if (clipping) {
551        glDisable (GL_CULL_FACE);
552        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
553      } else {
554        glEnable (GL_CULL_FACE);
555        glCullFace (GL_BACK);
556        glPolygonMode (GL_FRONT, GL_FILL);
557      }
558      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
559    }
560    if (!fProcessing2D) glEnable (GL_LIGHTING);
561    break;
562  case (G4ViewParameters::wireframe):
563  default:
564    glEnable (GL_DEPTH_TEST);
565    glDepthFunc (GL_LEQUAL);    //??? was GL_ALWAYS
566    glDisable (GL_CULL_FACE);
567    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
568    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
569    break;
570  }
571
572  //Loop through all the facets...
573  glBegin (GL_QUADS);
574  G4bool notLastFace;
575  do {
576
577    //First, find vertices, edgeflags and normals and note "not last facet"...
578    G4Point3D vertex[4];
579    G4int edgeFlag[4];
580    G4Normal3D normals[4];
581    G4int n;
582    notLastFace = polyhedron.GetNextFacet(n, vertex, edgeFlag, normals);
583
584    //Loop through the four edges of each G4Facet...
585    G4int edgeCount = 0;
586    for(edgeCount = 0; edgeCount < n; ++edgeCount) {
587      // Check to see if edge is visible or not...
588      if (isAuxEdgeVisible) {
589        edgeFlag[edgeCount] = 1;
590      }
591      if (edgeFlag[edgeCount] > 0) {
592        glEdgeFlag (GL_TRUE);
593      } else {
594        glEdgeFlag (GL_FALSE);
595      }
596      glNormal3d (normals[edgeCount].x(),
597                  normals[edgeCount].y(),
598                  normals[edgeCount].z());
599      glVertex3d (vertex[edgeCount].x(),
600                  vertex[edgeCount].y(),
601                  vertex[edgeCount].z());
602    }
603    // HepPolyhedron produces triangles too; in that case add an extra
604    // vertex identical to first...
605    if (n == 3) {
606      edgeCount = 3;
607      normals[edgeCount] = normals[0];
608      vertex[edgeCount] = vertex[0];
609      edgeFlag[edgeCount] = -1;
610      glEdgeFlag (GL_FALSE);
611      glNormal3d (normals[edgeCount].x(),
612                  normals[edgeCount].y(),
613                  normals[edgeCount].z());
614      glVertex3d (vertex[edgeCount].x(),
615                  vertex[edgeCount].y(),
616                  vertex[edgeCount].z());
617    }
618    // Trap situation where number of edges is > 4...
619    if (n > 4) {
620      G4cerr <<
621        "G4OpenGLSceneHandler::AddPrimitive(G4Polyhedron): WARNING";
622      G4PhysicalVolumeModel* pPVModel =
623        dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
624      if (pPVModel) {
625        G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
626        G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
627        G4cerr <<
628        "\n  Volume " << pCurrentPV->GetName() <<
629        ", Solid " << pCurrentLV->GetSolid()->GetName() <<
630          " (" << pCurrentLV->GetSolid()->GetEntityType();
631      }
632      G4cerr<<
633        "\n   G4Polyhedron facet with " << n << " edges" << G4endl;
634    }
635
636    // Do it all over again (twice) for hlr...
637    if  (drawing_style == G4ViewParameters::hlr ||
638         drawing_style == G4ViewParameters::hlhsr) {
639
640      glEnd ();  // Placed here to balance glBegin above, allowing GL
641                 // state changes below, then glBegin again.  Avoids
642                 // having glBegin/End pairs *inside* loop in the more
643                 // usual case of no hidden line removal.
644
645      // Lighting disabled unless otherwise requested
646      glDisable (GL_LIGHTING);
647
648      // Draw through stencil...
649      glStencilFunc (GL_EQUAL, 0, 1);
650      glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
651      if (drawing_style == G4ViewParameters::hlhsr) {
652        if (!fProcessing2D) glEnable (GL_LIGHTING);
653      }
654      glEnable (GL_DEPTH_TEST);
655      glDepthFunc (GL_LEQUAL);   
656      if (materialColour[3] < 1.) {
657        // Transparent...
658        glDepthMask (0);  // Make depth buffer read-only.
659        glDisable (GL_CULL_FACE);
660        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
661      } else {
662        // Opaque...
663        glDepthMask (1);  // Make depth buffer writable (default).
664        if (clipping) {
665          glDisable (GL_CULL_FACE);
666          glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
667        } else {
668          glEnable (GL_CULL_FACE);
669          glCullFace (GL_BACK);
670          glPolygonMode (GL_FRONT, GL_FILL);
671        }
672      }
673      GLfloat* painting_colour;
674      if  (drawing_style == G4ViewParameters::hlr) {
675        if (materialColour[3] < 1.) {
676          // Transparent - don't paint...
677          goto end_of_drawing_through_stencil;
678        }
679        painting_colour = clear_colour;
680      } else {  // drawing_style == G4ViewParameters::hlhsr
681        painting_colour = materialColour;
682      }
683      if (materialColour[3] < 1.) {
684        // Transparent...
685        glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, painting_colour);
686      } else {
687        // Opaque...
688        glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, painting_colour);
689      }
690      glColor4fv (painting_colour);
691      glBegin (GL_QUADS);
692      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
693        if (edgeFlag[edgeCount] > 0) {
694          glEdgeFlag (GL_TRUE);
695        } else {
696          glEdgeFlag (GL_FALSE);
697        }
698        glNormal3d (normals[edgeCount].x(),
699                    normals[edgeCount].y(),
700                    normals[edgeCount].z());
701        glVertex3d (vertex[edgeCount].x(),
702                    vertex[edgeCount].y(),
703                    vertex[edgeCount].z());
704      }
705      glEnd ();
706    end_of_drawing_through_stencil:
707
708      // and once more to reset the stencil bits...
709      glStencilFunc (GL_ALWAYS, 0, 1);
710      glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
711      glDepthFunc (GL_LEQUAL);  // to make sure line gets drawn. 
712      if (materialColour[3] < 1.) {
713        // Transparent...
714        glDisable (GL_CULL_FACE);
715        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
716      } else {
717        // Opaque...
718        if (clipping) {
719          glDisable (GL_CULL_FACE);
720          glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
721        } else {
722          glEnable (GL_CULL_FACE);
723          glCullFace (GL_BACK);
724          glPolygonMode (GL_FRONT, GL_LINE);
725        }
726      }
727      glDisable (GL_LIGHTING);
728      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
729      glBegin (GL_QUADS);
730      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
731        if (edgeFlag[edgeCount] > 0) {
732          glEdgeFlag (GL_TRUE);
733        } else {
734          glEdgeFlag (GL_FALSE);
735        }
736        glNormal3d (normals[edgeCount].x(),
737                    normals[edgeCount].y(),
738                    normals[edgeCount].z());
739        glVertex3d (vertex[edgeCount].x(),
740                    vertex[edgeCount].y(),
741                    vertex[edgeCount].z());
742      }
743      glEnd ();
744      glDepthFunc (GL_LEQUAL);   // Revert for next facet.
745      glBegin (GL_QUADS);      // Ready for next facet.  GL
746                               // says it ignores incomplete
747                               // quadrilaterals, so final empty
748                               // glBegin/End sequence should be OK.
749    }
750  } while (notLastFace); 
751 
752  glEnd ();
753  glDisable (GL_STENCIL_TEST);  // Revert to default for next primitive.
754  glDepthMask (1);              // Revert to default for next primitive.
755  glDisable (GL_LIGHTING);      // Revert to default for next primitive.
756}
757
758//Method for handling G4NURBS objects for drawing solids.
759//Knots and Ctrl Pnts MUST be arrays of GLfloats.
760void G4OpenGLSceneHandler::AddPrimitive (const G4NURBS& nurb) {
761
762  // Loads G4Atts for picking...
763  if (fpViewer->GetViewParameters().IsPicking()) {
764    G4AttHolder* holder = new G4AttHolder;
765    LoadAtts(nurb, holder);
766    fPickMap[fPickName] = holder;
767  }
768
769  GLUnurbsObj *gl_nurb;
770  gl_nurb = gluNewNurbsRenderer ();
771
772  GLfloat *u_knot_array, *u_knot_array_ptr;
773  u_knot_array = u_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::U)];
774  G4NURBS::KnotsIterator u_iterator (nurb, G4NURBS::U);
775  while (u_iterator.pick (u_knot_array_ptr++)){}
776
777  GLfloat *v_knot_array, *v_knot_array_ptr;
778  v_knot_array = v_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::V)];
779  G4NURBS::KnotsIterator v_iterator (nurb, G4NURBS::V);
780  while (v_iterator.pick (v_knot_array_ptr++)){}
781
782  GLfloat *ctrl_pnt_array, *ctrl_pnt_array_ptr;
783  ctrl_pnt_array = ctrl_pnt_array_ptr =
784    new GLfloat [nurb.GettotalnbrCtrlPts () * G4NURBS::NofC];
785  G4NURBS::CtrlPtsCoordsIterator c_p_iterator (nurb);
786  while (c_p_iterator.pick (ctrl_pnt_array_ptr++)){}
787
788  // Get vis attributes - pick up defaults if none.
789  const G4VisAttributes* pVA =
790    fpViewer -> GetApplicableVisAttributes (nurb.GetVisAttributes ());
791
792  // Get view parameters that the user can force through the vis
793  // attributes, thereby over-riding the current view parameter.
794  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
795  //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
796 
797  //Get colour, etc..
798  const G4Colour& c = pVA -> GetColour ();
799
800  switch (drawing_style) {
801
802  case (G4ViewParameters::hlhsr):
803    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
804    // << "Using hidden surface removal." << G4endl;
805  case (G4ViewParameters::hsr):
806    {
807      if (!fProcessing2D) glEnable (GL_LIGHTING);
808      glEnable (GL_DEPTH_TEST);
809      glEnable (GL_AUTO_NORMAL);
810      glEnable (GL_NORMALIZE);
811      gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_FILL);
812      gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
813      GLfloat materialColour [4];
814      materialColour [0] = c.GetRed ();
815      materialColour [1] = c.GetGreen ();
816      materialColour [2] = c.GetBlue ();
817      materialColour [3] = 1.0;  // = c.GetAlpha () for transparency -
818                                 // but see complication in
819                                 // AddPrimitive(const G4Polyhedron&).
820      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
821      break;
822    }
823  case (G4ViewParameters::hlr):
824    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
825    // << "Using wireframe." << G4endl;
826  case (G4ViewParameters::wireframe):
827  default:
828    glDisable (GL_LIGHTING);
829//    glDisable (GL_DEPTH_TEST);
830    glEnable (GL_DEPTH_TEST);
831    glDisable (GL_AUTO_NORMAL);
832    glDisable (GL_NORMALIZE);
833    gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
834    gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
835    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
836    break;
837  }     
838
839  gluBeginSurface (gl_nurb);
840  G4int u_stride = 4;
841  G4int v_stride = nurb.GetnbrCtrlPts(G4NURBS::U) * 4;
842
843  gluNurbsSurface (gl_nurb,
844                   nurb.GetnbrKnots (G4NURBS::U), (GLfloat*)u_knot_array,
845                   nurb.GetnbrKnots (G4NURBS::V), (GLfloat*)v_knot_array,
846                   u_stride,
847                   v_stride, 
848                   ctrl_pnt_array,
849                   nurb.GetUorder (),
850                   nurb.GetVorder (),
851                   GL_MAP2_VERTEX_4);
852 
853  gluEndSurface (gl_nurb);
854
855  delete [] u_knot_array;  // These should be allocated with smart allocators
856  delete [] v_knot_array;  // to avoid memory explosion.
857  delete [] ctrl_pnt_array;
858
859  gluDeleteNurbsRenderer (gl_nurb);
860}
861
862void G4OpenGLSceneHandler::AddCompound(const G4VTrajectory& traj) {
863  G4VSceneHandler::AddCompound(traj);  // For now.
864}
865
866void G4OpenGLSceneHandler::AddCompound(const G4VHit& hit) {
867  G4VSceneHandler::AddCompound(hit);  // For now.
868}
869
870void G4OpenGLSceneHandler::AddCompound(const G4THitsMap<G4double>& hits) {
871  G4VSceneHandler::AddCompound(hits);  // For now.
872}
873
874#endif
Note: See TracBrowser for help on using the repository browser.