source: trunk/geant4/visualization/OpenGL/src/G4OpenGLSceneHandler.cc @ 658

Last change on this file since 658 was 631, checked in by garnier, 17 years ago

maj a jour par rapport au repository de geant4

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