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

Last change on this file since 1242 was 1242, checked in by garnier, 14 years ago

bugged version

  • Property svn:mime-type set to text/cpp
File size: 30.5 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4OpenGLSceneHandler.cc,v 1.56 2009/10/21 15:18:43 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#ifdef G4DEBUG_VIS_OGL
201// printf("G4OpenGLSceneHandler::AddPrimitive polymarker");
202#endif
203  G4int nPoints = polymarker.size ();
204  if (nPoints <= 0) return;
205
206  fProcessingPolymarker = true;
207
208  // Loads G4Atts for picking...
209  if (fpViewer->GetViewParameters().IsPicking()) {
210    G4AttHolder* holder = new G4AttHolder;
211    LoadAtts(polymarker, holder);
212    fPickMap[fPickName] = holder;
213  }
214
215  switch (polymarker.GetMarkerType()) {
216  default:
217  case G4Polymarker::dots:
218    {
219#ifdef G4DEBUG_VIS_OGL
220// printf(" dots ");
221#endif
222      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
223        G4Circle dot (polymarker);
224        dot.SetPosition (polymarker[iPoint]);
225        dot.SetWorldSize  (0.);
226        dot.SetScreenSize (0.1);  // Very small circle.
227        G4OpenGLSceneHandler::AddPrimitive (dot);
228      }
229    }
230    break;
231  case G4Polymarker::circles:
232    {
233#ifdef G4DEBUG_VIS_OGL
234// printf(" circle ");
235#endif
236#ifdef G4VIS_BUILD_OPTIMISE_3
237      std::vector <G4VMarker> circleV;
238      // FIXME, must check attribute before doing a list
239#endif
240      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
241        G4Circle circle (polymarker);
242        // If not already drawn
243        circle.SetPosition (polymarker[iPoint]);
244#ifdef G4VIS_BUILD_OPTIMISE_3
245        circleV.push_back(circle);
246#endif
247      }
248#ifdef G4VIS_BUILD_OPTIMISE_3
249      G4OpenGLSceneHandler::AddPrimitives (circleV);
250#endif
251    }
252    break;
253  case G4Polymarker::squares:
254    {
255#ifdef G4DEBUG_VIS_OGL
256// printf(" square ");
257#endif
258#ifdef G4VIS_BUILD_OPTIMISE_3
259      std::vector <G4VMarker> squareV;
260      // FIXME, must check attribute before doing a list
261#endif
262      for (size_t iPoint = 0; iPoint < polymarker.size (); iPoint++) {
263        G4Square square (polymarker);
264        square.SetPosition (polymarker[iPoint]);
265#ifdef G4VIS_BUILD_OPTIMISE_3
266        squareV.push_back(square);
267#endif
268      }
269#ifdef G4VIS_BUILD_OPTIMISE_3
270      G4OpenGLSceneHandler::AddPrimitives (squareV);
271#endif
272    }
273    break;
274  }
275
276#ifdef G4DEBUG_VIS_OGL
277// printf(" \n");
278#endif
279  fProcessingPolymarker = false;
280}
281
282void G4OpenGLSceneHandler::AddPrimitive (const G4Text& text) {
283
284  // Loads G4Atts for picking...
285  if (fpViewer->GetViewParameters().IsPicking()) {
286    G4AttHolder* holder = new G4AttHolder;
287    LoadAtts(text, holder);
288    fPickMap[fPickName] = holder;
289  }
290
291  const G4Colour& c = GetTextColour (text);  // Picks up default if none.
292  MarkerSizeType sizeType;
293  G4double size = GetMarkerSize (text, sizeType);
294  G4ThreeVector position (text.GetPosition ());
295  G4String textString = text.GetText();
296
297  G4int font_base = G4OpenGLFontBaseStore::GetFontBase(fpViewer,size);
298  if (font_base < 0) {
299    static G4int callCount = 0;
300    ++callCount;
301    if (callCount <= 10 || callCount%100 == 0) {
302      G4cout <<
303        "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
304             << callCount <<
305        "\n  No fonts available."
306        "\n  Called with text \""
307             << text.GetText ()
308             << "\"\n  at " << position
309             << ", size " << size
310             << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
311             << ", type " << G4int(sizeType)
312             << ", colour " << c
313             << G4endl;
314    }
315    return;
316  }
317  const char* textCString = textString.c_str();
318  glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
319  glDisable (GL_DEPTH_TEST);
320  glDisable (GL_LIGHTING);
321 
322  glRasterPos3d(position.x(),position.y(),position.z());
323  // No action on offset or layout at present.
324   glPushAttrib(GL_LIST_BIT);
325   glListBase(font_base);
326   glCallLists(strlen(textCString), GL_UNSIGNED_BYTE, (GLubyte *)textCString);
327   glPopAttrib();
328}
329
330void G4OpenGLSceneHandler::AddPrimitive (const G4Circle& circle) {
331  glEnable (GL_POINT_SMOOTH);
332  AddCircleSquare (circle, G4OpenGLBitMapStore::circle);
333}
334
335void G4OpenGLSceneHandler::AddPrimitive (const G4Square& square) {
336  glDisable (GL_POINT_SMOOTH);
337  AddCircleSquare (square, G4OpenGLBitMapStore::square);
338}
339
340void G4OpenGLSceneHandler::AddPrimitives (std::vector <G4VMarker> square) {
341  glDisable (GL_POINT_SMOOTH);
342  AddCircleSquares (square, G4OpenGLBitMapStore::square);
343}
344
345void G4OpenGLSceneHandler::AddCircleSquare
346(const G4VMarker& marker,
347 G4OpenGLBitMapStore::Shape shape) {
348
349  if (!fProcessingPolymarker) {  // Polymarker has already loaded atts.
350    // Loads G4Atts for picking...
351    if (fpViewer->GetViewParameters().IsPicking()) {
352      G4AttHolder* holder = new G4AttHolder;
353      LoadAtts(marker, holder);
354      fPickMap[fPickName] = holder;
355    }
356  }
357
358  // Note: colour treated in sub-class.
359
360  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ()) {
361#ifdef G4DEBUG_VIS_OGL
362    //    printf("+");
363#endif
364    glDisable (GL_DEPTH_TEST);
365  } else {
366#ifdef G4DEBUG_VIS_OGL
367    //    printf("-");
368#endif
369    glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
370 
371  glDisable (GL_LIGHTING);
372 
373  // Get vis attributes - pick up defaults if none.
374  const G4VisAttributes* pVA =
375    fpViewer -> GetApplicableVisAttributes (marker.GetVisAttributes ());
376
377  G4double lineWidth = GetLineWidth(pVA);
378  glLineWidth(lineWidth);
379
380  G4VMarker::FillStyle style = marker.GetFillStyle();
381
382  G4bool filled = false;
383  static G4bool hashedWarned = false;
384 
385  switch (style) {
386  case G4VMarker::noFill:
387    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
388    filled = false;
389    break;
390   
391  case G4VMarker::hashed:
392    if (!hashedWarned) {
393      G4cout << "Hashed fill style in G4OpenGLSceneHandler."
394             << "\n  Not implemented.  Using G4VMarker::filled."
395             << G4endl;
396      hashedWarned = true;
397    }
398    // Maybe use
399    //glPolygonStipple (fStippleMaskHashed);
400    // Drop through to filled...
401   
402  case G4VMarker::filled:
403    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
404    filled = true;
405    break;
406   
407  }
408
409  // A few useful quantities...
410  G4Point3D centre = marker.GetPosition();
411  MarkerSizeType sizeType;
412  G4double size = GetMarkerSize(marker, sizeType);
413
414
415  // Draw...
416   if (sizeType == world) {  // Size specified in world coordinates.
417
418     DrawXYPolygon (shape, size, centre, pVA);
419
420   } else { // Size specified in screen (window) coordinates.
421     glPointSize (size);       
422     glBegin (GL_POINTS);
423     glVertex3f(centre.x(),centre.y(),centre.z());
424     glEnd();
425     //Antialiasing
426     glEnable (GL_POINT_SMOOTH);
427     //Transparency
428     glEnable(GL_BLEND);
429     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
430
431     // L. GARNIER 1 March 2009
432     // Old method, we draw a bitmap instead of a GL_POINT.
433     // I remove it because it cost in term of computing performances
434     // and gl2ps can't draw bitmaps
435
436     //      glRasterPos3d(centre.x(),centre.y(),centre.z());
437     //      const GLubyte* marker =
438     //        G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
439     //      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
440     //      glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
441   }
442}
443
444void G4OpenGLSceneHandler::AddCircleSquares
445(std::vector <G4VMarker> marker,
446 G4OpenGLBitMapStore::Shape shape) {
447
448  if (marker.size() == 0) {
449    return;
450  }
451
452  if (!fProcessingPolymarker) {  // Polymarker has already loaded atts.
453    // Loads G4Atts for picking...
454    if (fpViewer->GetViewParameters().IsPicking()) {
455      G4AttHolder* holder = new G4AttHolder;
456      LoadAtts(marker[0], holder);
457      fPickMap[fPickName] = holder;
458    }
459  }
460
461  // Note: colour treated in sub-class.
462
463  if (fpViewer -> GetViewParameters ().IsMarkerNotHidden ()) {
464#ifdef G4DEBUG_VIS_OGL
465    //    printf("+");
466#endif
467    glDisable (GL_DEPTH_TEST);
468  } else {
469#ifdef G4DEBUG_VIS_OGL
470    //    printf("-");
471#endif
472    glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);}
473 
474  glDisable (GL_LIGHTING);
475 
476  // Get vis attributes - pick up defaults if none.
477  const G4VisAttributes* pVA =
478    fpViewer -> GetApplicableVisAttributes (marker[0].GetVisAttributes ());
479
480  G4double lineWidth = GetLineWidth(pVA);
481  glLineWidth(lineWidth);
482
483  G4VMarker::FillStyle style = marker[0].GetFillStyle();
484
485  G4bool filled = false;
486  static G4bool hashedWarned = false;
487 
488  switch (style) {
489  case G4VMarker::noFill:
490    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
491    filled = false;
492    break;
493   
494  case G4VMarker::hashed:
495    if (!hashedWarned) {
496      G4cout << "Hashed fill style in G4OpenGLSceneHandler."
497             << "\n  Not implemented.  Using G4VMarker::filled."
498             << G4endl;
499      hashedWarned = true;
500    }
501    // Maybe use
502    //glPolygonStipple (fStippleMaskHashed);
503    // Drop through to filled...
504   
505  case G4VMarker::filled:
506    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
507    filled = true;
508    break;
509   
510  }
511
512
513
514  MarkerSizeType sizeType;
515  G4double size = GetMarkerSize(marker[0], sizeType);
516
517  // Draw...
518   if (sizeType == world) {  // Size specified in world coordinates.
519
520     for (unsigned int a=0;a<marker.size();a++) {
521       G4Point3D centre = marker[a].GetPosition();
522       // A few useful quantities...
523       DrawXYPolygon (shape, size, centre, pVA);
524     }
525   } else { // Size specified in screen (window) coordinates.
526     // A few useful quantities...
527     glPointSize (size);
528     glBegin (GL_POINTS);
529     for (unsigned int a=0;a<marker.size();a++) {
530       G4Point3D centre = marker[a].GetPosition();
531       glVertex3f(centre.x(),centre.y(),centre.z());
532     }
533     glEnd();
534     //Antialiasing
535     glEnable (GL_POINT_SMOOTH);
536     //Transparency
537     glEnable(GL_BLEND);
538     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
539
540     // L. GARNIER 1 March 2009
541     // Old method, we draw a bitmap instead of a GL_POINT.
542     // I remove it because it cost in term of computing performances
543     // and gl2ps can't draw bitmaps
544
545     //      glRasterPos3d(centre.x(),centre.y(),centre.z());
546     //      const GLubyte* marker =
547     //        G4OpenGLBitMapStore::GetBitMap(shape, size, filled);
548     //      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
549     //      glBitmap(GLsizei(size), GLsizei(size), size/2., size/2., 0., 0., marker);
550   }
551}
552
553void G4OpenGLSceneHandler::DrawXYPolygon
554(G4OpenGLBitMapStore::Shape shape,
555 G4double size,
556 const G4Point3D& centre,
557 const G4VisAttributes* pApplicableVisAtts)
558{
559  G4int nSides;
560  G4double startPhi;
561  if (shape == G4OpenGLBitMapStore::circle) {
562    nSides = GetNoOfSides(pApplicableVisAtts);
563    startPhi = 0.;
564  } else {
565    nSides = 4;
566    startPhi = -pi / 4.;
567  }
568
569  const G4Vector3D& viewpointDirection =
570    fpViewer -> GetViewParameters().GetViewpointDirection();
571  const G4Vector3D& up = fpViewer->GetViewParameters().GetUpVector();
572  const G4double dPhi = twopi / nSides;
573  const G4double radius = size / 2.;
574  G4Vector3D start = radius * (up.cross(viewpointDirection)).unit();
575  G4double phi;
576  G4int i;
577
578  glBegin (GL_POLYGON);
579  for (i = 0, phi = startPhi; i < nSides; i++, phi += dPhi) {
580    G4Vector3D r = start; r.rotate(phi, viewpointDirection);
581    G4Vector3D p = centre + r;
582    glVertex3d (p.x(), p.y(), p.z());
583  }
584  glEnd ();
585}
586
587void G4OpenGLSceneHandler::AddPrimitive (const G4Scale& scale)
588{
589  G4VSceneHandler::AddPrimitive(scale);
590}
591
592//Method for handling G4Polyhedron objects for drawing solids.
593void G4OpenGLSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) {
594
595  // Assume all facets are planar convex quadrilaterals.
596  // Draw each facet individually
597 
598  if (polyhedron.GetNoFacets() == 0) return;
599
600  // Loads G4Atts for picking...
601  if (fpViewer->GetViewParameters().IsPicking()) {
602    G4AttHolder* holder = new G4AttHolder;
603    LoadAtts(polyhedron, holder);
604    fPickMap[fPickName] = holder;
605  }
606
607  // Get vis attributes - pick up defaults if none.
608  const G4VisAttributes* pVA =
609    fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
610
611  // Get view parameters that the user can force through the vis
612  // attributes, thereby over-riding the current view parameter.
613  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
614
615  //Get colour, etc...
616  G4bool transparency_enabled = true;
617  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
618  if (pViewer) transparency_enabled = pViewer->transparency_enabled;
619  const G4Colour& c = pVA->GetColour();
620  GLfloat materialColour [4];
621  materialColour [0] = c.GetRed ();
622  materialColour [1] = c.GetGreen ();
623  materialColour [2] = c.GetBlue ();
624  if (transparency_enabled) {
625    materialColour [3] = c.GetAlpha ();
626  } else {
627    materialColour [3] = 1.;
628  }
629
630  G4double lineWidth = GetLineWidth(pVA);
631  glLineWidth(lineWidth);
632
633  GLfloat clear_colour[4];
634  glGetFloatv (GL_COLOR_CLEAR_VALUE, clear_colour);
635
636  G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
637
638  G4bool clipping = pViewer->fVP.IsSection() || pViewer->fVP.IsCutaway();
639
640  // Lighting disabled unless otherwise requested
641  glDisable (GL_LIGHTING);
642
643  switch (drawing_style) {
644  case (G4ViewParameters::hlhsr):
645    // Set up as for hidden line removal but paint polygon faces later...
646  case (G4ViewParameters::hlr):
647    glEnable (GL_STENCIL_TEST);
648    // The stencil buffer is cleared in G4OpenGLViewer::ClearView.
649    // The procedure below leaves it clear.
650    glStencilFunc (GL_ALWAYS, 0, 1);
651    glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
652    glEnable (GL_DEPTH_TEST);
653    glDepthFunc (GL_LEQUAL);
654    if (materialColour[3] < 1.) {
655      // Transparent...
656      glDisable (GL_CULL_FACE);
657      glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
658    } else {
659      // Opaque...
660      if (clipping) {
661        glDisable (GL_CULL_FACE);
662        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
663      } else {
664        glEnable (GL_CULL_FACE);
665        glCullFace (GL_BACK);
666        glPolygonMode (GL_FRONT, GL_LINE);
667      }
668    }
669    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
670    break;
671  case (G4ViewParameters::hsr):
672    glEnable (GL_DEPTH_TEST);
673    glDepthFunc (GL_LEQUAL);   
674    if (materialColour[3] < 1.) {
675      // Transparent...
676      glDepthMask (0);  // Make depth buffer read-only.
677      glDisable (GL_CULL_FACE);
678      glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
679      glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, materialColour);
680    } else {
681      // Opaque...
682      glDepthMask (1);  // Make depth buffer writable (default).
683      if (clipping) {
684        glDisable (GL_CULL_FACE);
685        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
686      } else {
687        glEnable (GL_CULL_FACE);
688        glCullFace (GL_BACK);
689        glPolygonMode (GL_FRONT, GL_FILL);
690      }
691      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
692    }
693    if (!fProcessing2D) glEnable (GL_LIGHTING);
694    break;
695  case (G4ViewParameters::wireframe):
696  default:
697    glEnable (GL_DEPTH_TEST);
698    glDepthFunc (GL_LEQUAL);    //??? was GL_ALWAYS
699    glDisable (GL_CULL_FACE);
700    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
701    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
702    break;
703  }
704
705  //Loop through all the facets...
706  glBegin (GL_QUADS);
707  G4bool notLastFace;
708  do {
709
710    //First, find vertices, edgeflags and normals and note "not last facet"...
711    G4Point3D vertex[4];
712    G4int edgeFlag[4];
713    G4Normal3D normals[4];
714    G4int n;
715    notLastFace = polyhedron.GetNextFacet(n, vertex, edgeFlag, normals);
716
717    //Loop through the four edges of each G4Facet...
718    G4int edgeCount = 0;
719    for(edgeCount = 0; edgeCount < n; ++edgeCount) {
720      // Check to see if edge is visible or not...
721      if (isAuxEdgeVisible) {
722        edgeFlag[edgeCount] = 1;
723      }
724      if (edgeFlag[edgeCount] > 0) {
725        glEdgeFlag (GL_TRUE);
726      } else {
727        glEdgeFlag (GL_FALSE);
728      }
729      glNormal3d (normals[edgeCount].x(),
730                  normals[edgeCount].y(),
731                  normals[edgeCount].z());
732      glVertex3d (vertex[edgeCount].x(),
733                  vertex[edgeCount].y(),
734                  vertex[edgeCount].z());
735    }
736    // HepPolyhedron produces triangles too; in that case add an extra
737    // vertex identical to first...
738    if (n == 3) {
739      edgeCount = 3;
740      normals[edgeCount] = normals[0];
741      vertex[edgeCount] = vertex[0];
742      edgeFlag[edgeCount] = -1;
743      glEdgeFlag (GL_FALSE);
744      glNormal3d (normals[edgeCount].x(),
745                  normals[edgeCount].y(),
746                  normals[edgeCount].z());
747      glVertex3d (vertex[edgeCount].x(),
748                  vertex[edgeCount].y(),
749                  vertex[edgeCount].z());
750    }
751    // Trap situation where number of edges is > 4...
752    if (n > 4) {
753      G4cerr <<
754        "G4OpenGLSceneHandler::AddPrimitive(G4Polyhedron): WARNING";
755      G4PhysicalVolumeModel* pPVModel =
756        dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
757      if (pPVModel) {
758        G4VPhysicalVolume* pCurrentPV = pPVModel->GetCurrentPV();
759        G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
760        G4cerr <<
761        "\n  Volume " << pCurrentPV->GetName() <<
762        ", Solid " << pCurrentLV->GetSolid()->GetName() <<
763          " (" << pCurrentLV->GetSolid()->GetEntityType();
764      }
765      G4cerr<<
766        "\n   G4Polyhedron facet with " << n << " edges" << G4endl;
767    }
768
769    // Do it all over again (twice) for hlr...
770    if  (drawing_style == G4ViewParameters::hlr ||
771         drawing_style == G4ViewParameters::hlhsr) {
772
773      glEnd ();  // Placed here to balance glBegin above, allowing GL
774                 // state changes below, then glBegin again.  Avoids
775                 // having glBegin/End pairs *inside* loop in the more
776                 // usual case of no hidden line removal.
777
778      // Lighting disabled unless otherwise requested
779      glDisable (GL_LIGHTING);
780
781      // Draw through stencil...
782      glStencilFunc (GL_EQUAL, 0, 1);
783      glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
784      if (drawing_style == G4ViewParameters::hlhsr) {
785        if (!fProcessing2D) glEnable (GL_LIGHTING);
786      }
787      glEnable (GL_DEPTH_TEST);
788      glDepthFunc (GL_LEQUAL);   
789      if (materialColour[3] < 1.) {
790        // Transparent...
791        glDepthMask (0);  // Make depth buffer read-only.
792        glDisable (GL_CULL_FACE);
793        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
794      } else {
795        // Opaque...
796        glDepthMask (1);  // Make depth buffer writable (default).
797        if (clipping) {
798          glDisable (GL_CULL_FACE);
799          glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
800        } else {
801          glEnable (GL_CULL_FACE);
802          glCullFace (GL_BACK);
803          glPolygonMode (GL_FRONT, GL_FILL);
804        }
805      }
806      GLfloat* painting_colour;
807      if  (drawing_style == G4ViewParameters::hlr) {
808        if (materialColour[3] < 1.) {
809          // Transparent - don't paint...
810          goto end_of_drawing_through_stencil;
811        }
812        painting_colour = clear_colour;
813      } else {  // drawing_style == G4ViewParameters::hlhsr
814        painting_colour = materialColour;
815      }
816      if (materialColour[3] < 1.) {
817        // Transparent...
818        glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, painting_colour);
819      } else {
820        // Opaque...
821        glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, painting_colour);
822      }
823      glColor4fv (painting_colour);
824      glBegin (GL_QUADS);
825      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
826        if (edgeFlag[edgeCount] > 0) {
827          glEdgeFlag (GL_TRUE);
828        } else {
829          glEdgeFlag (GL_FALSE);
830        }
831        glNormal3d (normals[edgeCount].x(),
832                    normals[edgeCount].y(),
833                    normals[edgeCount].z());
834        glVertex3d (vertex[edgeCount].x(),
835                    vertex[edgeCount].y(),
836                    vertex[edgeCount].z());
837      }
838      glEnd ();
839    end_of_drawing_through_stencil:
840
841      // and once more to reset the stencil bits...
842      glStencilFunc (GL_ALWAYS, 0, 1);
843      glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
844      glDepthFunc (GL_LEQUAL);  // to make sure line gets drawn. 
845      if (materialColour[3] < 1.) {
846        // Transparent...
847        glDisable (GL_CULL_FACE);
848        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
849      } else {
850        // Opaque...
851        if (clipping) {
852          glDisable (GL_CULL_FACE);
853          glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
854        } else {
855          glEnable (GL_CULL_FACE);
856          glCullFace (GL_BACK);
857          glPolygonMode (GL_FRONT, GL_LINE);
858        }
859      }
860      glDisable (GL_LIGHTING);
861      glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
862      glBegin (GL_QUADS);
863      for (int edgeCount = 0; edgeCount < 4; ++edgeCount) {
864        if (edgeFlag[edgeCount] > 0) {
865          glEdgeFlag (GL_TRUE);
866        } else {
867          glEdgeFlag (GL_FALSE);
868        }
869        glNormal3d (normals[edgeCount].x(),
870                    normals[edgeCount].y(),
871                    normals[edgeCount].z());
872        glVertex3d (vertex[edgeCount].x(),
873                    vertex[edgeCount].y(),
874                    vertex[edgeCount].z());
875      }
876      glEnd ();
877      glDepthFunc (GL_LEQUAL);   // Revert for next facet.
878      glBegin (GL_QUADS);      // Ready for next facet.  GL
879                               // says it ignores incomplete
880                               // quadrilaterals, so final empty
881                               // glBegin/End sequence should be OK.
882    }
883  } while (notLastFace); 
884 
885  glEnd ();
886  glDisable (GL_STENCIL_TEST);  // Revert to default for next primitive.
887  glDepthMask (1);              // Revert to default for next primitive.
888  glDisable (GL_LIGHTING);      // Revert to default for next primitive.
889}
890
891//Method for handling G4NURBS objects for drawing solids.
892//Knots and Ctrl Pnts MUST be arrays of GLfloats.
893void G4OpenGLSceneHandler::AddPrimitive (const G4NURBS& nurb) {
894
895  // Loads G4Atts for picking...
896  if (fpViewer->GetViewParameters().IsPicking()) {
897    G4AttHolder* holder = new G4AttHolder;
898    LoadAtts(nurb, holder);
899    fPickMap[fPickName] = holder;
900  }
901
902  GLUnurbsObj *gl_nurb;
903  gl_nurb = gluNewNurbsRenderer ();
904
905  GLfloat *u_knot_array, *u_knot_array_ptr;
906  u_knot_array = u_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::U)];
907  G4NURBS::KnotsIterator u_iterator (nurb, G4NURBS::U);
908  while (u_iterator.pick (u_knot_array_ptr++)){}
909
910  GLfloat *v_knot_array, *v_knot_array_ptr;
911  v_knot_array = v_knot_array_ptr = new GLfloat [nurb.GetnbrKnots(G4NURBS::V)];
912  G4NURBS::KnotsIterator v_iterator (nurb, G4NURBS::V);
913  while (v_iterator.pick (v_knot_array_ptr++)){}
914
915  GLfloat *ctrl_pnt_array, *ctrl_pnt_array_ptr;
916  ctrl_pnt_array = ctrl_pnt_array_ptr =
917    new GLfloat [nurb.GettotalnbrCtrlPts () * G4NURBS::NofC];
918  G4NURBS::CtrlPtsCoordsIterator c_p_iterator (nurb);
919  while (c_p_iterator.pick (ctrl_pnt_array_ptr++)){}
920
921  // Get vis attributes - pick up defaults if none.
922  const G4VisAttributes* pVA =
923    fpViewer -> GetApplicableVisAttributes (nurb.GetVisAttributes ());
924
925  // Get view parameters that the user can force through the vis
926  // attributes, thereby over-riding the current view parameter.
927  G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
928  //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
929 
930  //Get colour, etc..
931  const G4Colour& c = pVA -> GetColour ();
932
933  switch (drawing_style) {
934
935  case (G4ViewParameters::hlhsr):
936    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
937    // << "Using hidden surface removal." << G4endl;
938  case (G4ViewParameters::hsr):
939    {
940      if (!fProcessing2D) glEnable (GL_LIGHTING);
941      glEnable (GL_DEPTH_TEST);
942      glEnable (GL_AUTO_NORMAL);
943      glEnable (GL_NORMALIZE);
944      gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_FILL);
945      gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
946      GLfloat materialColour [4];
947      materialColour [0] = c.GetRed ();
948      materialColour [1] = c.GetGreen ();
949      materialColour [2] = c.GetBlue ();
950      materialColour [3] = 1.0;  // = c.GetAlpha () for transparency -
951                                 // but see complication in
952                                 // AddPrimitive(const G4Polyhedron&).
953      glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColour);
954      break;
955    }
956  case (G4ViewParameters::hlr):
957    //    G4cout << "Hidden line removal not implememented in G4OpenGL.\n"
958    // << "Using wireframe." << G4endl;
959  case (G4ViewParameters::wireframe):
960  default:
961    glDisable (GL_LIGHTING);
962//    glDisable (GL_DEPTH_TEST);
963    glEnable (GL_DEPTH_TEST);
964    glDisable (GL_AUTO_NORMAL);
965    glDisable (GL_NORMALIZE);
966    gluNurbsProperty (gl_nurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
967    gluNurbsProperty (gl_nurb, GLU_SAMPLING_TOLERANCE, 50.0);
968    glColor3d (c.GetRed (), c.GetGreen (), c.GetBlue ());
969    break;
970  }     
971
972  gluBeginSurface (gl_nurb);
973  G4int u_stride = 4;
974  G4int v_stride = nurb.GetnbrCtrlPts(G4NURBS::U) * 4;
975
976  gluNurbsSurface (gl_nurb,
977                   nurb.GetnbrKnots (G4NURBS::U), (GLfloat*)u_knot_array,
978                   nurb.GetnbrKnots (G4NURBS::V), (GLfloat*)v_knot_array,
979                   u_stride,
980                   v_stride, 
981                   ctrl_pnt_array,
982                   nurb.GetUorder (),
983                   nurb.GetVorder (),
984                   GL_MAP2_VERTEX_4);
985 
986  gluEndSurface (gl_nurb);
987
988  delete [] u_knot_array;  // These should be allocated with smart allocators
989  delete [] v_knot_array;  // to avoid memory explosion.
990  delete [] ctrl_pnt_array;
991
992  gluDeleteNurbsRenderer (gl_nurb);
993}
994
995void G4OpenGLSceneHandler::AddCompound(const G4VTrajectory& traj) {
996  G4VSceneHandler::AddCompound(traj);  // For now.
997}
998
999void G4OpenGLSceneHandler::AddCompound(const G4VHit& hit) {
1000  G4VSceneHandler::AddCompound(hit);  // For now.
1001}
1002
1003void G4OpenGLSceneHandler::AddCompound(const G4THitsMap<G4double>& hits) {
1004  G4VSceneHandler::AddCompound(hits);  // For now.
1005}
1006
1007#endif
Note: See TracBrowser for help on using the repository browser.