source: trunk/source/visualization/OpenGL/src/G4OpenGLSceneHandler.cc~ @ 1350

Last change on this file since 1350 was 1350, checked in by garnier, 13 years ago

update to last version 4.9.4

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