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

Last change on this file since 934 was 934, checked in by garnier, 16 years ago

before CVS commit

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