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

Last change on this file since 1147 was 1140, checked in by garnier, 15 years ago

update to CVS

  • Property svn:mime-type set to text/cpp
File size: 27.6 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
47#include "G4OpenGLQtViewer.hh"
48#include "G4OpenGLSceneHandler.hh"
49#include "G4OpenGLViewer.hh"
50#include "G4OpenGLFontBaseStore.hh"
51#include "G4OpenGLTransform3D.hh"
52#include "G4Point3D.hh"
53#include "G4Normal3D.hh"
54#include "G4Transform3D.hh"
55#include "G4Polyline.hh"
56#include "G4Polymarker.hh"
57#include "G4Text.hh"
58#include "G4Circle.hh"
59#include "G4Square.hh"
60#include "G4VMarker.hh"
61#include "G4Polyhedron.hh"
62#include "G4VisAttributes.hh"
63#include "G4PhysicalVolumeModel.hh"
64#include "G4VPhysicalVolume.hh"
65#include "G4LogicalVolume.hh"
66#include "G4VSolid.hh"
67#include "G4Scene.hh"
68#include "G4VisExtent.hh"
69#include "G4AttHolder.hh"
70
71G4OpenGLSceneHandler::G4OpenGLSceneHandler (G4VGraphicsSystem& system,
72                              G4int id,
73                              const G4String& name):
74  G4VSceneHandler (system, id, name),
75  fPickName(0),
76  fProcessing2D (false),
77  fProcessingPolymarker(false)
78{}
79
80G4OpenGLSceneHandler::~G4OpenGLSceneHandler ()
81{
82  ClearStore ();
83}
84
85const GLubyte G4OpenGLSceneHandler::fStippleMaskHashed [128] = {
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  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
101  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
102};
103
104void G4OpenGLSceneHandler::ClearAndDestroyAtts()
105{
106  std::map<GLuint, G4AttHolder*>::iterator i;
107  for (i = fPickMap.begin(); i != fPickMap.end(); ++i) delete i->second;
108  fPickMap.clear();
109}
110
111void G4OpenGLSceneHandler::PreAddSolid
112(const G4Transform3D& objectTransformation,
113 const G4VisAttributes& visAttribs)
114{
115  G4VSceneHandler::PreAddSolid (objectTransformation, visAttribs);
116}
117
118void G4OpenGLSceneHandler::BeginPrimitives
119(const G4Transform3D& objectTransformation)
120{
121  G4VSceneHandler::BeginPrimitives (objectTransformation);
122}
123
124void G4OpenGLSceneHandler::EndPrimitives ()
125{
126  G4VSceneHandler::EndPrimitives ();
127}
128
129void G4OpenGLSceneHandler::BeginPrimitives2D
130(const G4Transform3D& objectTransformation)
131{
132  G4VSceneHandler::BeginPrimitives2D (objectTransformation);
133  fProcessing2D = true;
134}
135
136void G4OpenGLSceneHandler::EndPrimitives2D ()
137{
138  fProcessing2D = false;
139  G4VSceneHandler::EndPrimitives2D ();
140}
141
142const G4Polyhedron* G4OpenGLSceneHandler::CreateSectionPolyhedron ()
143{
144  // Clipping done in G4OpenGLViewer::SetView.
145  return 0;
146
147  // But...OpenGL no longer seems to reconstruct clipped edges, so,
148  // when the BooleanProcessor is up to it, abandon this and use
149  // generic clipping in G4VSceneHandler::CreateSectionPolyhedron...
150  // return G4VSceneHandler::CreateSectionPolyhedron();
151}
152
153const G4Polyhedron* G4OpenGLSceneHandler::CreateCutawayPolyhedron ()
154{
155  // Cutaway done in G4OpenGLViewer::SetView.
156  return 0;
157
158  // But...if not, when the BooleanProcessor is up to it...
159  // return G4VSceneHandler::CreateCutawayPolyhedron();
160}
161
162void G4OpenGLSceneHandler::AddPrimitive (const G4Polyline& line)
163{
164  G4int nPoints = line.size ();
165  if (nPoints <= 0) return;
166
167  // Loads G4Atts for picking...
168  if (fpViewer->GetViewParameters().IsPicking()) {
169    G4AttHolder* holder = new G4AttHolder;
170    LoadAtts(line, holder);
171    fPickMap[fPickName] = holder;
172  }
173
174  // Note: colour treated in sub-class.
175
176  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
177    glDisable (GL_DEPTH_TEST);
178  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
179
180  glDisable (GL_LIGHTING);
181
182  // Get vis attributes - pick up defaults if none.
183  const G4VisAttributes* pVA =
184    fpViewer -> GetApplicableVisAttributes (line.GetVisAttributes ());
185
186  G4double lineWidth = GetLineWidth(pVA);
187  glLineWidth(lineWidth);
188
189  glBegin (GL_LINE_STRIP);
190  for (G4int iPoint = 0; iPoint < nPoints; iPoint++) {
191  G4double x, y, z;
192    x = line[iPoint].x();
193    y = line[iPoint].y();
194    z = line[iPoint].z();
195    glVertex3d (x, y, z);
196  }
197  glEnd ();
198}
199
200void G4OpenGLSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
201{
202  G4int nPoints = polymarker.size ();
203  if (nPoints <= 0) return;
204
205  fProcessingPolymarker = true;
206
207  // Loads G4Atts for picking...
208  if (fpViewer->GetViewParameters().IsPicking()) {
209    G4AttHolder* holder = new G4AttHolder;
210    LoadAtts(polymarker, holder);
211    fPickMap[fPickName] = holder;
212  }
213
214  switch (polymarker.GetMarkerType()) {
215  default:
216  case G4Polymarker::dots:
217    {
218      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
219        G4Circle dot (polymarker);
220        dot.SetPosition (polymarker[iPoint]);
221        dot.SetWorldSize  (0.);
222        dot.SetScreenSize (0.1);  // Very small circle.
223        G4OpenGLSceneHandler::AddPrimitive (dot);
224      }
225    }
226    break;
227  case G4Polymarker::circles:
228    {
229      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
230        G4Circle circle (polymarker);
231        circle.SetPosition (polymarker[iPoint]);
232        G4OpenGLSceneHandler::AddPrimitive (circle);
233      }
234    }
235    break;
236  case G4Polymarker::squares:
237    {
238      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
239        G4Square square (polymarker);
240        square.SetPosition (polymarker[iPoint]);
241        G4OpenGLSceneHandler::AddPrimitive (square);
242      }
243    }
244    break;
245  }
246
247  fProcessingPolymarker = false;
248}
249
250void G4OpenGLSceneHandler::AddPrimitive (const G4Text& text) {
251
252  // Loads G4Atts for picking...
253  if (fpViewer->GetViewParameters().IsPicking()) {
254    G4AttHolder* holder = new G4AttHolder;
255    LoadAtts(text, holder);
256    fPickMap[fPickName] = holder;
257  }
258
259  const G4Colour& c = GetTextColour (text);  // Picks up default if none.
260  MarkerSizeType sizeType;
261  G4double size = GetMarkerSize (text, sizeType);
262  G4ThreeVector position (text.GetPosition ());
263  G4String textString = text.GetText();
264
265  G4int font_base = G4OpenGLFontBaseStore::GetFontBase(fpViewer,size);
266  if (font_base < 0) {
267   
268#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
269    G4OpenGLQtViewer* oGLSQtViewer = dynamic_cast<G4OpenGLQtViewer*>(fpViewer);
270    if (oGLSQtViewer) {
271      // FIXME : No font for the moment
272      const char* textCString = textString.c_str();
273      oGLSQtViewer->drawText(textCString,position.x(),position.y(),position.z(),size);
274    }
275#endif
276    static G4int callCount = 0;
277    ++callCount;
278    if (callCount <= 10 || callCount%100 == 0) {
279      G4cout <<
280        "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
281             << callCount <<
282        "\n  No fonts available."
283        "\n  Called with text \""
284             << text.GetText ()
285             << "\"\n  at " << position
286             << ", size " << size
287             << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
288             << ", type " << G4int(sizeType)
289             << ", colour " << c
290             << G4endl;
291    }
292    return;
293  }
294  const char* textCString = textString.c_str();
295  glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
296#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
297  G4OpenGLQtViewer* oGLSQtViewer = dynamic_cast<G4OpenGLQtViewer*>(fpViewer);
298
299  if (oGLSQtViewer) {
300    // FIXME : No font for the moment
301    oGLSQtViewer->drawText(textCString,position.x(),position.y(),position.z(),size);
302  }
303#else
304    glDisable (GL_DEPTH_TEST);
305    glDisable (GL_LIGHTING);
306   
307    glRasterPos3d(position.x(),position.y(),position.z());
308    // No action on offset or layout at present.
309    glPushAttrib(GL_LIST_BIT);
310    glListBase(font_base);
311    glCallLists(strlen(textCString), GL_UNSIGNED_BYTE, (GLubyte *)textCString);
312    glPopAttrib();
313#endif
314  //     //////////////
315  //     makeCurrent();
316  //     glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT);
317  //     glRasterPos3d(x, y, z);
318  //     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
319  //     glEnable(GL_BLEND);
320  //     glListBase(fontDisplayListBase(fnt, listBase));
321  //     glCallLists(str.length(), GL_UNSIGNED_BYTE, str.local8Bit());
322  //     glPopAttrib();
323  //     //////////////
324 
325#ifdef G4DEBUG_VIS_OGL
326  printf ("G4OpenGLSceneHandler::AddPrimitives TEXT\n");
327#endif 
328}
329
330void G4OpenGLSceneHandler::AddPrimitive (const G4Circle& circle) {
331  glEnable (GL_POINT_SMOOTH);
332  AddCircleSquare (circle, G4OpenGLBitMapStore::circle);
333}
334
335void G4OpenGLSceneHandler::AddPrimitive (const G4Square& square) {
336  glDisable (GL_POINT_SMOOTH);
337  AddCircleSquare (square, G4OpenGLBitMapStore::square);
338}
339
340void G4OpenGLSceneHandler::AddCircleSquare
341(const G4VMarker& marker,
342 G4OpenGLBitMapStore::Shape shape) {
343
344  if (!fProcessingPolymarker) {  // Polymarker has already loaded atts.
345    // Loads G4Atts for picking...
346    if (fpViewer->GetViewParameters().IsPicking()) {
347      G4AttHolder* holder = new G4AttHolder;
348      LoadAtts(marker, holder);
349      fPickMap[fPickName] = holder;
350    }
351  }
352
353  // Note: colour treated in sub-class.
354
355  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
356    glDisable (GL_DEPTH_TEST);
357  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
358 
359  glDisable (GL_LIGHTING);
360 
361  // Get vis attributes - pick up defaults if none.
362  const G4VisAttributes* pVA =
363    fpViewer -> GetApplicableVisAttributes (marker.GetVisAttributes ());
364
365  G4double lineWidth = GetLineWidth(pVA);
366  glLineWidth(lineWidth);
367
368  G4VMarker::FillStyle style = marker.GetFillStyle();
369
370  G4bool filled = false;
371  static G4bool hashedWarned = false;
372 
373  switch (style) {
374  case G4VMarker::noFill:
375    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
376    filled = false;
377    break;
378   
379  case G4VMarker::hashed:
380    if (!hashedWarned) {
381      G4cout << "Hashed fill style in G4OpenGLSceneHandler."
382             << "\n  Not implemented.  Using G4VMarker::filled."
383             << G4endl;
384      hashedWarned = true;
385    }
386    // Maybe use
387    //glPolygonStipple (fStippleMaskHashed);
388    // Drop through to filled...
389   
390  case G4VMarker::filled:
391    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
392    filled = true;
393    break;
394   
395  }
396
397  // A few useful quantities...
398  G4Point3D centre = marker.GetPosition();
399  MarkerSizeType sizeType;
400  G4double size = GetMarkerSize(marker, sizeType);
401
402  // Draw...
403   if (sizeType == world) {  // Size specified in world coordinates.
404
405     DrawXYPolygon (shape, size, centre, pVA);
406
407   } else { // Size specified in screen (window) coordinates.
408     glPointSize (size);       
409     glBegin (GL_POINTS);
410     glVertex3f(centre.x(),centre.y(),centre.z());
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 G4THitsMap<G4double>& hits) {
882  G4VSceneHandler::AddCompound(hits);  // For now.
883}
884
885#endif
Note: See TracBrowser for help on using the repository browser.