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

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

update

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