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

Last change on this file since 873 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

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