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

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

pas de modif, juste du test

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