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

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

HEAD

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