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

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

pb quand on ne met pas la vis QT corrige

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