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

Last change on this file since 746 was 688, checked in by garnier, 16 years ago

mise a jour par rapport au commit sur cvs de geant4

  • Property svn:mime-type set to text/cpp
File size: 25.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.52 2008/01/04 22:07:01 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
371    DrawXYPolygon (shape, size, centre, pVA);
372
373  } else { // Size specified in screen (window) coordinates.
374
375    glRasterPos3d(centre.x(),centre.y(),centre.z());
376    const GLubyte* marker =
377      G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
378    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
379    glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
380  }
381}
382
383void G4OpenGLSceneHandler::DrawXYPolygon
384(G4OpenGLBitMapStore::Shape shape,
385 G4double size,
386 const G4Point3D& centre,
387 const G4VisAttributes* pApplicableVisAtts)
388{
389  G4int nSides;
390  G4double startPhi;
391  if (shape == G4OpenGLBitMapStore::circle) {
392    nSides = GetNoOfSides(pApplicableVisAtts);
393    startPhi = 0.;
394  } else {
395    nSides = 4;
396    startPhi = -pi / 4.;
397  }
398
399  const G4Vector3D& viewpointDirection =
400    fpViewer -> GetViewParameters().GetViewpointDirection();
401  const G4Vector3D& up = fpViewer->GetViewParameters().GetUpVector();
402  const G4double dPhi = twopi / nSides;
403  const G4double radius = size / 2.;
404  G4Vector3D start = radius * (up.cross(viewpointDirection)).unit();
405  G4double phi;
406  G4int i;
407
408  glBegin (GL_POLYGON);
409  for (i = 0, phi = startPhi; i < nSides; i++, phi += dPhi) {
410    G4Vector3D r = start; r.rotate(phi, viewpointDirection);
411    G4Vector3D p = centre + r;
412    glVertex3d (p.x(), p.y(), p.z());
413  }
414  glEnd ();
415}
416
417void G4OpenGLSceneHandler::AddPrimitive (const G4Scale& scale)
418{
419  G4VSceneHandler::AddPrimitive(scale);
420}
421
422//Method for handling G4Polyhedron objects for drawing solids.
423void G4OpenGLSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) {
424
425  // Assume all facets are planar convex quadrilaterals.
426  // Draw each facet individually
427 
428  if (polyhedron.GetNoFacets() == 0) return;
429
430  // Loads G4Atts for picking...
431  if (fpViewer->GetViewParameters().IsPicking()) {
432    G4AttHolder* holder = new G4AttHolder;
433    LoadAtts(polyhedron, holder);
434    fPickMap[fPickName] = holder;
435  }
436
437  // Get vis attributes - pick up defaults if none.
438  const G4VisAttributes* pVA =
439    fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
440
441  // Get view parameters that the user can force through the vis
442  // attributes, thereby over-riding the current view parameter.
443  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
444
445  //Get colour, etc...
446  G4bool transparency_enabled = true;
447  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
448  if (pViewer) transparency_enabled = pViewer->transparency_enabled;
449  const G4Colour& c = pVA->GetColour();
450  GLfloat materialColour [4];
451  materialColour [0] = c.GetRed ();
452  materialColour [1] = c.GetGreen ();
453  materialColour [2] = c.GetBlue ();
454  if (transparency_enabled) {
455    materialColour [3] = c.GetAlpha ();
456  } else {
457    materialColour [3] = 1.;
458  }
459
460  G4double lineWidth = GetLineWidth(pVA);
461  glLineWidth(lineWidth);
462
463  GLfloat clear_colour[4];
464  glGetFloatv (GL_COLOR_CLEAR_VALUE, clear_colour);
465
466  G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
467
468  G4bool clipping = pViewer->fVP.IsSection() || pViewer->fVP.IsCutaway();
469
470  switch (drawing_style) {
471  case (G4ViewParameters::hlhsr):
472    // Set up as for hidden line removal but paint polygon faces later...
473  case (G4ViewParameters::hlr):
474    glEnable (GL_STENCIL_TEST);
475    // The stencil buffer is cleared in G4OpenGLViewer::ClearView.
476    // The procedure below leaves it clear.
477    glStencilFunc (GL_ALWAYS, 0, 1);
478    glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
479    glEnable (GL_DEPTH_TEST);
480    glDepthFunc (GL_LEQUAL);
481    if (materialColour[3] < 1.) {
482      // Transparent...
483      glDisable (GL_CULL_FACE);
484      glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
485    } else {
486      // Opaque...
487      if (clipping) {
488        glDisable (GL_CULL_FACE);
489        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
490      } else {
491        glEnable (GL_CULL_FACE);
492        glCullFace (GL_BACK);
493        glPolygonMode (GL_FRONT, GL_LINE);
494      }
495    }
496    glDisable (GL_LIGHTING);
497    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
498    break;
499  case (G4ViewParameters::hsr):
500    glEnable (GL_DEPTH_TEST);
501    glDepthFunc (GL_LEQUAL);   
502    if (materialColour[3] < 1.) {
503      // Transparent...
504      glDepthMask (0);  // Make depth buffer read-only.
505      glDisable (GL_CULL_FACE);
506      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
507      glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialColour);
508    } else {
509      // Opaque...
510      glDepthMask (1);  // Make depth buffer writable (default).
511      if (clipping) {
512        glDisable (GL_CULL_FACE);
513        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
514      } else {
515        glEnable (GL_CULL_FACE);
516        glCullFace (GL_BACK);
517        glPolygonMode (GL_FRONT, GL_FILL);
518      }
519      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
520    }
521    if (!fProcessing2D) glEnable (GL_LIGHTING);
522    break;
523  case (G4ViewParameters::wireframe):
524  default:
525    glEnable (GL_DEPTH_TEST);
526    glDepthFunc (GL_LEQUAL);    //??? was GL_ALWAYS
527    glDisable (GL_CULL_FACE);
528    glDisable (GL_LIGHTING);
529    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
530    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
531    break;
532  }
533
534  //Loop through all the facets...
535  glBegin (GL_QUADS);
536  G4bool notLastFace;
537  do {
538
539    //First, find vertices, edgeflags and normals and note "not last facet"...
540    G4Point3D vertex[4];
541    G4int edgeFlag[4];
542    G4Normal3D normals[4];
543    G4int n;
544    notLastFace = polyhedron.GetNextFacet(n, vertex, edgeFlag, normals);
545
546    //Loop through the four edges of each G4Facet...
547    G4int edgeCount = 0;
548    for(edgeCount = 0; edgeCount < n; ++edgeCount) {
549      // Check to see if edge is visible or not...
550      if (isAuxEdgeVisible) {
551        edgeFlag[edgeCount] = 1;
552      }
553      if (edgeFlag[edgeCount] > 0) {
554        glEdgeFlag (GL_TRUE);
555      } else {
556        glEdgeFlag (GL_FALSE);
557      }
558      glNormal3d (normals[edgeCount].x(),
559                  normals[edgeCount].y(),
560                  normals[edgeCount].z());
561      glVertex3d (vertex[edgeCount].x(),
562                  vertex[edgeCount].y(),
563                  vertex[edgeCount].z());
564    }
565    // HepPolyhedron produces triangles too; in that case add an extra
566    // vertex identical to first...
567    if (n == 3) {
568      edgeCount = 3;
569      normals[edgeCount] = normals[0];
570      vertex[edgeCount] = vertex[0];
571      edgeFlag[edgeCount] = -1;
572      glEdgeFlag (GL_FALSE);
573      glNormal3d (normals[edgeCount].x(),
574                  normals[edgeCount].y(),
575                  normals[edgeCount].z());
576      glVertex3d (vertex[edgeCount].x(),
577                  vertex[edgeCount].y(),
578                  vertex[edgeCount].z());
579    }
580    // Trap situation where number of edges is > 4...
581    if (n > 4) {
582      G4cerr <<
583        "G4OpenGLSceneHandler::AddPrimitive(G4Polyhedron): WARNING";
584      G4PhysicalVolumeModel* pPVModel =
585        dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
586      if (pPVModel) {
587        G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
588        G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
589        G4cerr <<
590        "\n  Volume " << pCurrentPV->GetName() <<
591        ", Solid " << pCurrentLV->GetSolid()->GetName() <<
592          " (" << pCurrentLV->GetSolid()->GetEntityType();
593      }
594      G4cerr<<
595        "\n   G4Polyhedron facet with " << n << " edges" << G4endl;
596    }
597
598    // Do it all over again (twice) for hlr...
599    if  (drawing_style == G4ViewParameters::hlr ||
600         drawing_style == G4ViewParameters::hlhsr) {
601
602      glEnd ();  // Placed here to balance glBegin above, allowing GL
603                 // state changes below, then glBegin again.  Avoids
604                 // having glBegin/End pairs *inside* loop in the more
605                 // usual case of no hidden line removal.
606
607      // Draw through stencil...
608      glStencilFunc (GL_EQUAL, 0, 1);
609      glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
610      if (drawing_style == G4ViewParameters::hlhsr) {
611        if (!fProcessing2D) glEnable (GL_LIGHTING);
612      }
613      glEnable (GL_DEPTH_TEST);
614      glDepthFunc (GL_LEQUAL);   
615      if (materialColour[3] < 1.) {
616        // Transparent...
617        glDepthMask (0);  // Make depth buffer read-only.
618        glDisable (GL_CULL_FACE);
619        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
620      } else {
621        // Opaque...
622        glDepthMask (1);  // Make depth buffer writable (default).
623        if (clipping) {
624          glDisable (GL_CULL_FACE);
625          glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
626        } else {
627          glEnable (GL_CULL_FACE);
628          glCullFace (GL_BACK);
629          glPolygonMode (GL_FRONT, GL_FILL);
630        }
631      }
632      GLfloat* painting_colour;
633      if  (drawing_style == G4ViewParameters::hlr) {
634        if (materialColour[3] < 1.) {
635          // Transparent - don't paint...
636          goto end_of_drawing_through_stencil;
637        }
638        painting_colour = clear_colour;
639      } else {  // drawing_style == G4ViewParameters::hlhsr
640        painting_colour = materialColour;
641      }
642      if (materialColour[3] < 1.) {
643        // Transparent...
644        glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, painting_colour);
645      } else {
646        // Opaque...
647        glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, painting_colour);
648      }
649      glColor4fv (painting_colour);
650      glBegin (GL_QUADS);
651      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
652        if (edgeFlag[edgeCount] > 0) {
653          glEdgeFlag (GL_TRUE);
654        } else {
655          glEdgeFlag (GL_FALSE);
656        }
657        glNormal3d (normals[edgeCount].x(),
658                    normals[edgeCount].y(),
659                    normals[edgeCount].z());
660        glVertex3d (vertex[edgeCount].x(),
661                    vertex[edgeCount].y(),
662                    vertex[edgeCount].z());
663      }
664      glEnd ();
665    end_of_drawing_through_stencil:
666
667      // and once more to reset the stencil bits...
668      glStencilFunc (GL_ALWAYS, 0, 1);
669      glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
670      glDepthFunc (GL_LEQUAL);  // to make sure line gets drawn. 
671      if (materialColour[3] < 1.) {
672        // Transparent...
673        glDisable (GL_CULL_FACE);
674        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
675      } else {
676        // Opaque...
677        if (clipping) {
678          glDisable (GL_CULL_FACE);
679          glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
680        } else {
681          glEnable (GL_CULL_FACE);
682          glCullFace (GL_BACK);
683          glPolygonMode (GL_FRONT, GL_LINE);
684        }
685      }
686      glDisable (GL_LIGHTING);
687      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
688      glBegin (GL_QUADS);
689      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
690        if (edgeFlag[edgeCount] > 0) {
691          glEdgeFlag (GL_TRUE);
692        } else {
693          glEdgeFlag (GL_FALSE);
694        }
695        glNormal3d (normals[edgeCount].x(),
696                    normals[edgeCount].y(),
697                    normals[edgeCount].z());
698        glVertex3d (vertex[edgeCount].x(),
699                    vertex[edgeCount].y(),
700                    vertex[edgeCount].z());
701      }
702      glEnd ();
703      glDepthFunc (GL_LEQUAL);   // Revert for next facet.
704      glBegin (GL_QUADS);      // Ready for next facet.  GL
705                               // says it ignores incomplete
706                               // quadrilaterals, so final empty
707                               // glBegin/End sequence should be OK.
708    }
709  } while (notLastFace); 
710 
711  glEnd ();
712  glDisable (GL_STENCIL_TEST);  // Revert to default for next primitive.
713  glDepthMask (1);              // Revert to default for next primitive.
714}
715
716//Method for handling G4NURBS objects for drawing solids.
717//Knots and Ctrl Pnts MUST be arrays of GLfloats.
718void G4OpenGLSceneHandler::AddPrimitive (const G4NURBS& nurb) {
719
720  // Loads G4Atts for picking...
721  if (fpViewer->GetViewParameters().IsPicking()) {
722    G4AttHolder* holder = new G4AttHolder;
723    LoadAtts(nurb, holder);
724    fPickMap[fPickName] = holder;
725  }
726
727  GLUnurbsObj *gl_nurb;
728  gl_nurb = gluNewNurbsRenderer ();
729
730  GLfloat *u_knot_array, *u_knot_array_ptr;
731  u_knot_array = u_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::U)];
732  G4NURBS::KnotsIterator u_iterator (nurb, G4NURBS::U);
733  while (u_iterator.pick (u_knot_array_ptr++));
734
735  GLfloat *v_knot_array, *v_knot_array_ptr;
736  v_knot_array = v_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::V)];
737  G4NURBS::KnotsIterator v_iterator (nurb, G4NURBS::V);
738  while (v_iterator.pick (v_knot_array_ptr++));
739
740  GLfloat *ctrl_pnt_array, *ctrl_pnt_array_ptr;
741  ctrl_pnt_array = ctrl_pnt_array_ptr =
742    new GLfloat [nurb.GettotalnbrCtrlPts () * G4NURBS::NofC];
743  G4NURBS::CtrlPtsCoordsIterator c_p_iterator (nurb);
744  while (c_p_iterator.pick (ctrl_pnt_array_ptr++));
745
746  // Get vis attributes - pick up defaults if none.
747  const G4VisAttributes* pVA =
748    fpViewer -> GetApplicableVisAttributes (nurb.GetVisAttributes ());
749
750  // Get view parameters that the user can force through the vis
751  // attributes, thereby over-riding the current view parameter.
752  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
753  //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
754 
755  //Get colour, etc..
756  const G4Colour& c = pVA -> GetColour ();
757
758  switch (drawing_style) {
759
760  case (G4ViewParameters::hlhsr):
761    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
762    // << "Using hidden surface removal." << G4endl;
763  case (G4ViewParameters::hsr):
764    {
765      if (!fProcessing2D) glEnable (GL_LIGHTING);
766      glEnable (GL_DEPTH_TEST);
767      glEnable (GL_AUTO_NORMAL);
768      glEnable (GL_NORMALIZE);
769      gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_FILL);
770      gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
771      GLfloat materialColour [4];
772      materialColour [0] = c.GetRed ();
773      materialColour [1] = c.GetGreen ();
774      materialColour [2] = c.GetBlue ();
775      materialColour [3] = 1.0;  // = c.GetAlpha () for transparency -
776                                 // but see complication in
777                                 // AddPrimitive(const G4Polyhedron&).
778      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
779      break;
780    }
781  case (G4ViewParameters::hlr):
782    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
783    // << "Using wireframe." << G4endl;
784  case (G4ViewParameters::wireframe):
785  default:
786    glDisable (GL_LIGHTING);
787//    glDisable (GL_DEPTH_TEST);
788    glEnable (GL_DEPTH_TEST);
789    glDisable (GL_AUTO_NORMAL);
790    glDisable (GL_NORMALIZE);
791    gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
792    gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
793    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
794    break;
795  }     
796
797  gluBeginSurface (gl_nurb);
798  G4int u_stride = 4;
799  G4int v_stride = nurb.GetnbrCtrlPts(G4NURBS::U) * 4;
800
801  gluNurbsSurface (gl_nurb,
802                   nurb.GetnbrKnots (G4NURBS::U), (GLfloat*)u_knot_array,
803                   nurb.GetnbrKnots (G4NURBS::V), (GLfloat*)v_knot_array,
804                   u_stride,
805                   v_stride, 
806                   ctrl_pnt_array,
807                   nurb.GetUorder (),
808                   nurb.GetVorder (),
809                   GL_MAP2_VERTEX_4);
810 
811  gluEndSurface (gl_nurb);
812
813  delete [] u_knot_array;  // These should be allocated with smart allocators
814  delete [] v_knot_array;  // to avoid memory explosion.
815  delete [] ctrl_pnt_array;
816
817  gluDeleteNurbsRenderer (gl_nurb);
818}
819
820void G4OpenGLSceneHandler::AddCompound(const G4VTrajectory& traj) {
821  G4VSceneHandler::AddCompound(traj);  // For now.
822}
823
824void G4OpenGLSceneHandler::AddCompound(const G4VHit& hit) {
825  G4VSceneHandler::AddCompound(hit);  // For now.
826}
827
828#endif
Note: See TracBrowser for help on using the repository browser.