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

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

juste pour sauver le boulot ....ne compile pas....

  • Property svn:mime-type set to text/cpp
File size: 26.0 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4OpenGLSceneHandler.cc,v 1.54 2008/04/04 13:32:22 allison Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Andrew Walkden  27th March 1996
32// OpenGL stored scene - creates OpenGL display lists.
33// OpenGL immediate scene - draws immediately to buffer
34//                           (saving space on server).
35
36#ifdef G4VIS_BUILD_OPENGL_DRIVER
37
38// Included here - problems with HP compiler if not before other includes?
39#include "G4NURBS.hh"
40
41// Here follows a special for Mesa, the OpenGL emulator.  Does not affect
42// other OpenGL's, as far as I'm aware.   John Allison 18/9/96.
43#define CENTERLINE_CLPP  /* CenterLine C++ workaround: */
44// Also seems to be required for HP's CC and AIX xlC, at least.
45
46#include "G4OpenGLSceneHandler.hh"
47#include "G4OpenGLViewer.hh"
48#include "G4OpenGLFontBaseStore.hh"
49#include "G4OpenGLTransform3D.hh"
50#include "G4Point3D.hh"
51#include "G4Normal3D.hh"
52#include "G4Transform3D.hh"
53#include "G4Polyline.hh"
54#include "G4Polymarker.hh"
55#include "G4Text.hh"
56#include "G4Circle.hh"
57#include "G4Square.hh"
58#include "G4VMarker.hh"
59#include "G4Polyhedron.hh"
60#include "G4VisAttributes.hh"
61#include "G4PhysicalVolumeModel.hh"
62#include "G4VPhysicalVolume.hh"
63#include "G4LogicalVolume.hh"
64#include "G4VSolid.hh"
65#include "G4Scene.hh"
66#include "G4VisExtent.hh"
67#include "G4AttHolder.hh"
68
69G4OpenGLSceneHandler::G4OpenGLSceneHandler (G4VGraphicsSystem& system,
70                              G4int id,
71                              const G4String& name):
72  G4VSceneHandler (system, id, name),
73  fPickName(0),
74  fProcessing2D (false),
75  fProcessingPolymarker(false)
76{}
77
78G4OpenGLSceneHandler::~G4OpenGLSceneHandler ()
79{
80  ClearStore ();
81}
82
83const GLubyte G4OpenGLSceneHandler::fStippleMaskHashed [128] = {
84  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
85  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
86  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
87  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
88  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
89  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
90  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
91  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
92  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
93  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
94  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
95  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
96  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
97  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
98  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
99  0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
100};
101
102void G4OpenGLSceneHandler::ClearAndDestroyAtts()
103{
104  std::map<GLuint, G4AttHolder*>::iterator i;
105  for (i = fPickMap.begin(); i != fPickMap.end(); ++i) delete i->second;
106  fPickMap.clear();
107}
108
109void G4OpenGLSceneHandler::PreAddSolid
110(const G4Transform3D& objectTransformation,
111 const G4VisAttributes& visAttribs)
112{
113  G4VSceneHandler::PreAddSolid (objectTransformation, visAttribs);
114}
115
116void G4OpenGLSceneHandler::BeginPrimitives
117(const G4Transform3D& objectTransformation)
118{
119  G4VSceneHandler::BeginPrimitives (objectTransformation);
120}
121
122void G4OpenGLSceneHandler::EndPrimitives ()
123{
124  G4VSceneHandler::EndPrimitives ();
125}
126
127void G4OpenGLSceneHandler::BeginPrimitives2D
128(const G4Transform3D& objectTransformation)
129{
130  G4VSceneHandler::BeginPrimitives2D (objectTransformation);
131  fProcessing2D = true;
132}
133
134void G4OpenGLSceneHandler::EndPrimitives2D ()
135{
136  fProcessing2D = false;
137  G4VSceneHandler::EndPrimitives2D ();
138}
139
140const G4Polyhedron* G4OpenGLSceneHandler::CreateSectionPolyhedron ()
141{
142  // Clipping done in G4OpenGLViewer::SetView.
143  return 0;
144
145  // But...OpenGL no longer seems to reconstruct clipped edges, so,
146  // when the BooleanProcessor is up to it, abandon this and use
147  // generic clipping in G4VSceneHandler::CreateSectionPolyhedron...
148  // return G4VSceneHandler::CreateSectionPolyhedron();
149}
150
151const G4Polyhedron* G4OpenGLSceneHandler::CreateCutawayPolyhedron ()
152{
153  // Cutaway done in G4OpenGLViewer::SetView.
154  return 0;
155
156  // But...if not, when the BooleanProcessor is up to it...
157  // return G4VSceneHandler::CreateCutawayPolyhedron();
158}
159
160void G4OpenGLSceneHandler::AddPrimitive (const G4Polyline& line)
161{
162  G4int nPoints = line.size ();
163  if (nPoints <= 0) return;
164
165  // Loads G4Atts for picking...
166  if (fpViewer->GetViewParameters().IsPicking()) {
167    G4AttHolder* holder = new G4AttHolder;
168    LoadAtts(line, holder);
169    fPickMap[fPickName] = holder;
170  }
171
172  // Note: colour treated in sub-class.
173
174  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ())
175    glDisable (GL_DEPTH_TEST);
176  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
177
178  glDisable (GL_LIGHTING);
179
180  // Get vis attributes - pick up defaults if none.
181  const G4VisAttributes* pVA =
182    fpViewer -> GetApplicableVisAttributes (line.GetVisAttributes ());
183
184  G4double lineWidth = GetLineWidth(pVA);
185  glLineWidth(lineWidth);
186
187  glBegin (GL_LINE_STRIP);
188  for (G4int iPoint = 0; iPoint < nPoints; iPoint++) {
189  G4double x, y, z;
190    x = line[iPoint].x();
191    y = line[iPoint].y();
192    z = line[iPoint].z();
193    glVertex3d (x, y, z);
194  }
195  glEnd ();
196}
197
198void G4OpenGLSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
199{
200  G4int nPoints = polymarker.size ();
201  if (nPoints <= 0) return;
202
203  fProcessingPolymarker = true;
204
205  // Loads G4Atts for picking...
206  if (fpViewer->GetViewParameters().IsPicking()) {
207    G4AttHolder* holder = new G4AttHolder;
208    LoadAtts(polymarker, holder);
209    fPickMap[fPickName] = holder;
210  }
211
212  switch (polymarker.GetMarkerType()) {
213  default:
214  case G4Polymarker::dots:
215    {
216      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
217        G4Circle dot (polymarker);
218        dot.SetPosition (polymarker[iPoint]);
219        dot.SetWorldSize  (0.);
220        dot.SetScreenSize (0.1);  // Very small circle.
221        G4OpenGLSceneHandler::AddPrimitive (dot);
222      }
223    }
224    break;
225  case G4Polymarker::circles:
226    {
227      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
228        G4Circle circle (polymarker);
229        circle.SetPosition (polymarker[iPoint]);
230        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    printf("G4OpenGLSceneHandler::AddCircleSquare  world\n");
371    DrawXYPolygon (shape, size, centre, pVA);
372
373//   } else { // Size specified in screen (window) coordinates.
374//     printf("G4OpenGLSceneHandler::AddCircleSquare  bitmap\n");
375
376//     glRasterPos3d(centre.x(),centre.y(),centre.z());
377//     const GLubyte* marker =
378//       G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
379//     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
380//     glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
381//   }
382}
383
384void G4OpenGLSceneHandler::DrawXYPolygon
385(G4OpenGLBitMapStore::Shape shape,
386 G4double size,
387 const G4Point3D& centre,
388 const G4VisAttributes* pApplicableVisAtts)
389{
390  G4int nSides;
391  G4double startPhi;
392  if (shape == G4OpenGLBitMapStore::circle) {
393    nSides = GetNoOfSides(pApplicableVisAtts);
394    startPhi = 0.;
395  } else {
396    nSides = 4;
397    startPhi = -pi / 4.;
398  }
399
400  const G4Vector3D& viewpointDirection =
401    fpViewer -> GetViewParameters().GetViewpointDirection();
402  const G4Vector3D& up = fpViewer->GetViewParameters().GetUpVector();
403  const G4double dPhi = twopi / nSides;
404  const G4double radius = size / 2.;
405  G4Vector3D start = radius * (up.cross(viewpointDirection)).unit();
406  G4double phi;
407  G4int i;
408
409  glBegin (GL_POLYGON);
410  for (i = 0, phi = startPhi; i < nSides; i++, phi += dPhi) {
411    G4Vector3D r = start; r.rotate(phi, viewpointDirection);
412    G4Vector3D p = centre + r;
413    glVertex3d (p.x(), p.y(), p.z());
414  }
415  glEnd ();
416}
417
418void G4OpenGLSceneHandler::AddPrimitive (const G4Scale& scale)
419{
420  G4VSceneHandler::AddPrimitive(scale);
421}
422
423//Method for handling G4Polyhedron objects for drawing solids.
424void G4OpenGLSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) {
425
426  // Assume all facets are planar convex quadrilaterals.
427  // Draw each facet individually
428 
429  if (polyhedron.GetNoFacets() == 0) return;
430
431  // Loads G4Atts for picking...
432  if (fpViewer->GetViewParameters().IsPicking()) {
433    G4AttHolder* holder = new G4AttHolder;
434    LoadAtts(polyhedron, holder);
435    fPickMap[fPickName] = holder;
436  }
437
438  // Get vis attributes - pick up defaults if none.
439  const G4VisAttributes* pVA =
440    fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
441
442  // Get view parameters that the user can force through the vis
443  // attributes, thereby over-riding the current view parameter.
444  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
445
446  //Get colour, etc...
447  G4bool transparency_enabled = true;
448  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
449  if (pViewer) transparency_enabled = pViewer->transparency_enabled;
450  const G4Colour& c = pVA->GetColour();
451  GLfloat materialColour [4];
452  materialColour [0] = c.GetRed ();
453  materialColour [1] = c.GetGreen ();
454  materialColour [2] = c.GetBlue ();
455  if (transparency_enabled) {
456    materialColour [3] = c.GetAlpha ();
457  } else {
458    materialColour [3] = 1.;
459  }
460
461  G4double lineWidth = GetLineWidth(pVA);
462  glLineWidth(lineWidth);
463
464  GLfloat clear_colour[4];
465  glGetFloatv (GL_COLOR_CLEAR_VALUE, clear_colour);
466
467  G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
468
469  G4bool clipping = pViewer->fVP.IsSection() || pViewer->fVP.IsCutaway();
470
471  // Lighting disabled unless otherwise requested
472  glDisable (GL_LIGHTING);
473
474  switch (drawing_style) {
475  case (G4ViewParameters::hlhsr):
476    // Set up as for hidden line removal but paint polygon faces later...
477  case (G4ViewParameters::hlr):
478    glEnable (GL_STENCIL_TEST);
479    // The stencil buffer is cleared in G4OpenGLViewer::ClearView.
480    // The procedure below leaves it clear.
481    glStencilFunc (GL_ALWAYS, 0, 1);
482    glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
483    glEnable (GL_DEPTH_TEST);
484    glDepthFunc (GL_LEQUAL);
485    if (materialColour[3] < 1.) {
486      // Transparent...
487      glDisable (GL_CULL_FACE);
488      glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
489    } else {
490      // Opaque...
491      if (clipping) {
492        glDisable (GL_CULL_FACE);
493        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
494      } else {
495        glEnable (GL_CULL_FACE);
496        glCullFace (GL_BACK);
497        glPolygonMode (GL_FRONT, GL_LINE);
498      }
499    }
500    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
501    break;
502  case (G4ViewParameters::hsr):
503    glEnable (GL_DEPTH_TEST);
504    glDepthFunc (GL_LEQUAL);   
505    if (materialColour[3] < 1.) {
506      // Transparent...
507      glDepthMask (0);  // Make depth buffer read-only.
508      glDisable (GL_CULL_FACE);
509      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
510      glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialColour);
511    } else {
512      // Opaque...
513      glDepthMask (1);  // Make depth buffer writable (default).
514      if (clipping) {
515        glDisable (GL_CULL_FACE);
516        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
517      } else {
518        glEnable (GL_CULL_FACE);
519        glCullFace (GL_BACK);
520        glPolygonMode (GL_FRONT, GL_FILL);
521      }
522      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
523    }
524    if (!fProcessing2D) glEnable (GL_LIGHTING);
525    break;
526  case (G4ViewParameters::wireframe):
527  default:
528    glEnable (GL_DEPTH_TEST);
529    glDepthFunc (GL_LEQUAL);    //??? was GL_ALWAYS
530    glDisable (GL_CULL_FACE);
531    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
532    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
533    break;
534  }
535
536  //Loop through all the facets...
537  glBegin (GL_QUADS);
538  G4bool notLastFace;
539  do {
540
541    //First, find vertices, edgeflags and normals and note "not last facet"...
542    G4Point3D vertex[4];
543    G4int edgeFlag[4];
544    G4Normal3D normals[4];
545    G4int n;
546    notLastFace = polyhedron.GetNextFacet(n, vertex, edgeFlag, normals);
547
548    //Loop through the four edges of each G4Facet...
549    G4int edgeCount = 0;
550    for(edgeCount = 0; edgeCount < n; ++edgeCount) {
551      // Check to see if edge is visible or not...
552      if (isAuxEdgeVisible) {
553        edgeFlag[edgeCount] = 1;
554      }
555      if (edgeFlag[edgeCount] > 0) {
556        glEdgeFlag (GL_TRUE);
557      } else {
558        glEdgeFlag (GL_FALSE);
559      }
560      glNormal3d (normals[edgeCount].x(),
561                  normals[edgeCount].y(),
562                  normals[edgeCount].z());
563      glVertex3d (vertex[edgeCount].x(),
564                  vertex[edgeCount].y(),
565                  vertex[edgeCount].z());
566    }
567    // HepPolyhedron produces triangles too; in that case add an extra
568    // vertex identical to first...
569    if (n == 3) {
570      edgeCount = 3;
571      normals[edgeCount] = normals[0];
572      vertex[edgeCount] = vertex[0];
573      edgeFlag[edgeCount] = -1;
574      glEdgeFlag (GL_FALSE);
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    // Trap situation where number of edges is > 4...
583    if (n > 4) {
584      G4cerr <<
585        "G4OpenGLSceneHandler::AddPrimitive(G4Polyhedron): WARNING";
586      G4PhysicalVolumeModel* pPVModel =
587        dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
588      if (pPVModel) {
589        G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
590        G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
591        G4cerr <<
592        "\n  Volume " << pCurrentPV->GetName() <<
593        ", Solid " << pCurrentLV->GetSolid()->GetName() <<
594          " (" << pCurrentLV->GetSolid()->GetEntityType();
595      }
596      G4cerr<<
597        "\n   G4Polyhedron facet with " << n << " edges" << G4endl;
598    }
599
600    // Do it all over again (twice) for hlr...
601    if  (drawing_style == G4ViewParameters::hlr ||
602         drawing_style == G4ViewParameters::hlhsr) {
603
604      glEnd ();  // Placed here to balance glBegin above, allowing GL
605                 // state changes below, then glBegin again.  Avoids
606                 // having glBegin/End pairs *inside* loop in the more
607                 // usual case of no hidden line removal.
608
609      // Lighting disabled unless otherwise requested
610      glDisable (GL_LIGHTING);
611
612      // Draw through stencil...
613      glStencilFunc (GL_EQUAL, 0, 1);
614      glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
615      if (drawing_style == G4ViewParameters::hlhsr) {
616        if (!fProcessing2D) glEnable (GL_LIGHTING);
617      }
618      glEnable (GL_DEPTH_TEST);
619      glDepthFunc (GL_LEQUAL);   
620      if (materialColour[3] < 1.) {
621        // Transparent...
622        glDepthMask (0);  // Make depth buffer read-only.
623        glDisable (GL_CULL_FACE);
624        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
625      } else {
626        // Opaque...
627        glDepthMask (1);  // Make depth buffer writable (default).
628        if (clipping) {
629          glDisable (GL_CULL_FACE);
630          glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
631        } else {
632          glEnable (GL_CULL_FACE);
633          glCullFace (GL_BACK);
634          glPolygonMode (GL_FRONT, GL_FILL);
635        }
636      }
637      GLfloat* painting_colour;
638      if  (drawing_style == G4ViewParameters::hlr) {
639        if (materialColour[3] < 1.) {
640          // Transparent - don't paint...
641          goto end_of_drawing_through_stencil;
642        }
643        painting_colour = clear_colour;
644      } else {  // drawing_style == G4ViewParameters::hlhsr
645        painting_colour = materialColour;
646      }
647      if (materialColour[3] < 1.) {
648        // Transparent...
649        glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, painting_colour);
650      } else {
651        // Opaque...
652        glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, painting_colour);
653      }
654      glColor4fv (painting_colour);
655      glBegin (GL_QUADS);
656      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
657        if (edgeFlag[edgeCount] > 0) {
658          glEdgeFlag (GL_TRUE);
659        } else {
660          glEdgeFlag (GL_FALSE);
661        }
662        glNormal3d (normals[edgeCount].x(),
663                    normals[edgeCount].y(),
664                    normals[edgeCount].z());
665        glVertex3d (vertex[edgeCount].x(),
666                    vertex[edgeCount].y(),
667                    vertex[edgeCount].z());
668      }
669      glEnd ();
670    end_of_drawing_through_stencil:
671
672      // and once more to reset the stencil bits...
673      glStencilFunc (GL_ALWAYS, 0, 1);
674      glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
675      glDepthFunc (GL_LEQUAL);  // to make sure line gets drawn. 
676      if (materialColour[3] < 1.) {
677        // Transparent...
678        glDisable (GL_CULL_FACE);
679        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
680      } else {
681        // Opaque...
682        if (clipping) {
683          glDisable (GL_CULL_FACE);
684          glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
685        } else {
686          glEnable (GL_CULL_FACE);
687          glCullFace (GL_BACK);
688          glPolygonMode (GL_FRONT, GL_LINE);
689        }
690      }
691      glDisable (GL_LIGHTING);
692      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
693      glBegin (GL_QUADS);
694      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
695        if (edgeFlag[edgeCount] > 0) {
696          glEdgeFlag (GL_TRUE);
697        } else {
698          glEdgeFlag (GL_FALSE);
699        }
700        glNormal3d (normals[edgeCount].x(),
701                    normals[edgeCount].y(),
702                    normals[edgeCount].z());
703        glVertex3d (vertex[edgeCount].x(),
704                    vertex[edgeCount].y(),
705                    vertex[edgeCount].z());
706      }
707      glEnd ();
708      glDepthFunc (GL_LEQUAL);   // Revert for next facet.
709      glBegin (GL_QUADS);      // Ready for next facet.  GL
710                               // says it ignores incomplete
711                               // quadrilaterals, so final empty
712                               // glBegin/End sequence should be OK.
713    }
714  } while (notLastFace); 
715 
716  glEnd ();
717  glDisable (GL_STENCIL_TEST);  // Revert to default for next primitive.
718  glDepthMask (1);              // Revert to default for next primitive.
719  glDisable (GL_LIGHTING);      // Revert to default for next primitive.
720}
721
722//Method for handling G4NURBS objects for drawing solids.
723//Knots and Ctrl Pnts MUST be arrays of GLfloats.
724void G4OpenGLSceneHandler::AddPrimitive (const G4NURBS& nurb) {
725
726  // Loads G4Atts for picking...
727  if (fpViewer->GetViewParameters().IsPicking()) {
728    G4AttHolder* holder = new G4AttHolder;
729    LoadAtts(nurb, holder);
730    fPickMap[fPickName] = holder;
731  }
732
733  GLUnurbsObj *gl_nurb;
734  gl_nurb = gluNewNurbsRenderer ();
735
736  GLfloat *u_knot_array, *u_knot_array_ptr;
737  u_knot_array = u_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::U)];
738  G4NURBS::KnotsIterator u_iterator (nurb, G4NURBS::U);
739  while (u_iterator.pick (u_knot_array_ptr++)){}
740
741  GLfloat *v_knot_array, *v_knot_array_ptr;
742  v_knot_array = v_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::V)];
743  G4NURBS::KnotsIterator v_iterator (nurb, G4NURBS::V);
744  while (v_iterator.pick (v_knot_array_ptr++)){}
745
746  GLfloat *ctrl_pnt_array, *ctrl_pnt_array_ptr;
747  ctrl_pnt_array = ctrl_pnt_array_ptr =
748    new GLfloat [nurb.GettotalnbrCtrlPts () * G4NURBS::NofC];
749  G4NURBS::CtrlPtsCoordsIterator c_p_iterator (nurb);
750  while (c_p_iterator.pick (ctrl_pnt_array_ptr++)){}
751
752  // Get vis attributes - pick up defaults if none.
753  const G4VisAttributes* pVA =
754    fpViewer -> GetApplicableVisAttributes (nurb.GetVisAttributes ());
755
756  // Get view parameters that the user can force through the vis
757  // attributes, thereby over-riding the current view parameter.
758  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
759  //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
760 
761  //Get colour, etc..
762  const G4Colour& c = pVA -> GetColour ();
763
764  switch (drawing_style) {
765
766  case (G4ViewParameters::hlhsr):
767    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
768    // << "Using hidden surface removal." << G4endl;
769  case (G4ViewParameters::hsr):
770    {
771      if (!fProcessing2D) glEnable (GL_LIGHTING);
772      glEnable (GL_DEPTH_TEST);
773      glEnable (GL_AUTO_NORMAL);
774      glEnable (GL_NORMALIZE);
775      gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_FILL);
776      gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
777      GLfloat materialColour [4];
778      materialColour [0] = c.GetRed ();
779      materialColour [1] = c.GetGreen ();
780      materialColour [2] = c.GetBlue ();
781      materialColour [3] = 1.0;  // = c.GetAlpha () for transparency -
782                                 // but see complication in
783                                 // AddPrimitive(const G4Polyhedron&).
784      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
785      break;
786    }
787  case (G4ViewParameters::hlr):
788    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
789    // << "Using wireframe." << G4endl;
790  case (G4ViewParameters::wireframe):
791  default:
792    glDisable (GL_LIGHTING);
793//    glDisable (GL_DEPTH_TEST);
794    glEnable (GL_DEPTH_TEST);
795    glDisable (GL_AUTO_NORMAL);
796    glDisable (GL_NORMALIZE);
797    gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
798    gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
799    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
800    break;
801  }     
802
803  gluBeginSurface (gl_nurb);
804  G4int u_stride = 4;
805  G4int v_stride = nurb.GetnbrCtrlPts(G4NURBS::U) * 4;
806
807  gluNurbsSurface (gl_nurb,
808                   nurb.GetnbrKnots (G4NURBS::U), (GLfloat*)u_knot_array,
809                   nurb.GetnbrKnots (G4NURBS::V), (GLfloat*)v_knot_array,
810                   u_stride,
811                   v_stride, 
812                   ctrl_pnt_array,
813                   nurb.GetUorder (),
814                   nurb.GetVorder (),
815                   GL_MAP2_VERTEX_4);
816 
817  gluEndSurface (gl_nurb);
818
819  delete [] u_knot_array;  // These should be allocated with smart allocators
820  delete [] v_knot_array;  // to avoid memory explosion.
821  delete [] ctrl_pnt_array;
822
823  gluDeleteNurbsRenderer (gl_nurb);
824}
825
826void G4OpenGLSceneHandler::AddCompound(const G4VTrajectory& traj) {
827  G4VSceneHandler::AddCompound(traj);  // For now.
828}
829
830void G4OpenGLSceneHandler::AddCompound(const G4VHit& hit) {
831  G4VSceneHandler::AddCompound(hit);  // For now.
832}
833
834#endif
Note: See TracBrowser for help on using the repository browser.