Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/visualization/OpenGL/src/G4OpenGLViewer.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /visualization/OpenGL/src/G4OpenGLViewer.cc (Version 11.3.0) and /visualization/OpenGL/src/G4OpenGLViewer.cc (Version 10.2.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 //                                                 26 //
                                                   >>  27 // $Id: G4OpenGLViewer.cc 94428 2015-11-16 12:40:02Z gcosmo $
 27 //                                                 28 //
 28 //                                                 29 // 
 29 // Andrew Walkden  27th March 1996                 30 // Andrew Walkden  27th March 1996
 30 // OpenGL view - opens window, hard copy, etc.     31 // OpenGL view - opens window, hard copy, etc.
 31                                                    32 
                                                   >>  33 #ifdef G4VIS_BUILD_OPENGL_DRIVER
                                                   >>  34 
 32 #include "G4ios.hh"                                35 #include "G4ios.hh"
 33 #include <CLHEP/Units/SystemOfUnits.h>         <<  36 #include "G4SystemOfUnits.hh"
 34 #include "G4OpenGLViewer.hh"                       37 #include "G4OpenGLViewer.hh"
 35 #include "G4OpenGLSceneHandler.hh"                 38 #include "G4OpenGLSceneHandler.hh"
 36 #include "G4OpenGLTransform3D.hh"                  39 #include "G4OpenGLTransform3D.hh"
 37                                                <<  40 #include "G4OpenGL2PSAction.hh"
 38 #include "G4gl2ps.hh"                          << 
 39 #define GL2PS_TEXT_B  TOOLS_GL2PS_TEXT_B       << 
 40 #define GL2PS_TEXT_BL TOOLS_GL2PS_TEXT_BL      << 
 41 #define GL2PS_TEXT_BR TOOLS_GL2PS_TEXT_BR      << 
 42                                                    41 
 43 #include "G4Scene.hh"                              42 #include "G4Scene.hh"
 44 #include "G4VisExtent.hh"                          43 #include "G4VisExtent.hh"
 45 #include "G4LogicalVolume.hh"                      44 #include "G4LogicalVolume.hh"
 46 #include "G4VSolid.hh"                             45 #include "G4VSolid.hh"
 47 #include "G4Point3D.hh"                            46 #include "G4Point3D.hh"
 48 #include "G4Normal3D.hh"                           47 #include "G4Normal3D.hh"
 49 #include "G4Plane3D.hh"                            48 #include "G4Plane3D.hh"
 50 #include "G4AttHolder.hh"                          49 #include "G4AttHolder.hh"
 51 #include "G4AttCheck.hh"                           50 #include "G4AttCheck.hh"
 52 #include "G4Text.hh"                               51 #include "G4Text.hh"
 53                                                    52 
                                                   >>  53 #ifdef G4OPENGL_VERSION_2
                                                   >>  54 // We need to have a Wt gl drawer because we will draw inside the WtGL component (ImmediateWtViewer)
                                                   >>  55 #include "G4OpenGLVboDrawer.hh"
                                                   >>  56 #endif
                                                   >>  57 
                                                   >>  58 // GL2PS
                                                   >>  59 #include "Geant4_gl2ps.h"
                                                   >>  60 
 54 #include <sstream>                                 61 #include <sstream>
 55 #include <string>                                  62 #include <string>
 56 #include <iomanip>                             << 
 57                                                    63 
 58 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneH     64 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
 59 G4VViewer (scene, -1),                             65 G4VViewer (scene, -1),
                                                   >>  66 #ifdef G4OPENGL_VERSION_2
                                                   >>  67 fVboDrawer(NULL),
                                                   >>  68 #endif
 60 fPrintColour (true),                               69 fPrintColour (true),
 61 fVectoredPs (true),                                70 fVectoredPs (true),
 62 fOpenGLSceneHandler(scene),                        71 fOpenGLSceneHandler(scene),
 63 background (G4Colour(0.,0.,0.)),                   72 background (G4Colour(0.,0.,0.)),
 64 transparency_enabled (true),                       73 transparency_enabled (true),
 65 antialiasing_enabled (false),                      74 antialiasing_enabled (false),
 66 haloing_enabled (false),                           75 haloing_enabled (false),
                                                   >>  76 fStartTime(-DBL_MAX),
                                                   >>  77 fEndTime(DBL_MAX),
                                                   >>  78 fFadeFactor(0.),
                                                   >>  79 fDisplayHeadTime(false),
                                                   >>  80 fDisplayHeadTimeX(-0.9),
                                                   >>  81 fDisplayHeadTimeY(-0.9),
                                                   >>  82 fDisplayHeadTimeSize(24.),
                                                   >>  83 fDisplayHeadTimeRed(0.),
                                                   >>  84 fDisplayHeadTimeGreen(1.),
                                                   >>  85 fDisplayHeadTimeBlue(1.),
                                                   >>  86 fDisplayLightFront(false),
                                                   >>  87 fDisplayLightFrontX(0.),
                                                   >>  88 fDisplayLightFrontY(0.),
                                                   >>  89 fDisplayLightFrontZ(0.),
                                                   >>  90 fDisplayLightFrontT(0.),
                                                   >>  91 fDisplayLightFrontRed(0.),
                                                   >>  92 fDisplayLightFrontGreen(1.),
                                                   >>  93 fDisplayLightFrontBlue(0.),
 67 fRot_sens(1.),                                     94 fRot_sens(1.),
 68 fPan_sens(0.01),                                   95 fPan_sens(0.01),
 69 fWinSize_x(0),                                     96 fWinSize_x(0),
 70 fWinSize_y(0),                                     97 fWinSize_y(0),
 71 fDefaultExportImageFormat("pdf"),                  98 fDefaultExportImageFormat("pdf"),
 72 fExportImageFormat("pdf"),                         99 fExportImageFormat("pdf"),
 73 fExportFilenameIndex(0),                          100 fExportFilenameIndex(0),
 74 fPrintSizeX(-1),                                  101 fPrintSizeX(-1),
 75 fPrintSizeY(-1),                                  102 fPrintSizeY(-1),
 76 fPointSize (0),                                   103 fPointSize (0),
 77 fDefaultExportFilename("G4OpenGL"),               104 fDefaultExportFilename("G4OpenGL"),
 78 fSizeHasChanged(0),                               105 fSizeHasChanged(0),
 79 fGl2psDefaultLineWith(1),                         106 fGl2psDefaultLineWith(1),
 80 fGl2psDefaultPointSize(2),                        107 fGl2psDefaultPointSize(2),
 81 fGlViewInitialized(false),                        108 fGlViewInitialized(false),
 82 fIsGettingPickInfos(false)                        109 fIsGettingPickInfos(false)
                                                   >> 110 #ifdef G4OPENGL_VERSION_2
                                                   >> 111 ,fShaderProgram(0)
                                                   >> 112 ,fVertexPositionAttribute(0)
                                                   >> 113 ,fVertexNormalAttribute(0)
                                                   >> 114 ,fpMatrixUniform(0)
                                                   >> 115 ,fcMatrixUniform(0)
                                                   >> 116 ,fmvMatrixUniform(0)
                                                   >> 117 ,fnMatrixUniform(0)
                                                   >> 118 #endif
 83 {                                                 119 {
 84   // Make changes to view parameters for OpenG    120   // Make changes to view parameters for OpenGL...
 85   fVP.SetAutoRefresh(true);                       121   fVP.SetAutoRefresh(true);
 86   fDefaultVP.SetAutoRefresh(true);                122   fDefaultVP.SetAutoRefresh(true);
 87   fGL2PSAction = new G4gl2ps();                << 123 
 88   tools_gl2ps_gl_funcs_t _funcs = {            << 124   fGL2PSAction = new G4OpenGL2PSAction();
 89     (tools_glIsEnabled_func)glIsEnabled,       << 125 
 90     (tools_glBegin_func)glBegin,               << 
 91     (tools_glEnd_func)glEnd,                   << 
 92     (tools_glGetFloatv_func)glGetFloatv,       << 
 93     (tools_glVertex3f_func)glVertex3f,         << 
 94     (tools_glGetBooleanv_func)glGetBooleanv,   << 
 95     (tools_glGetIntegerv_func)glGetIntegerv,   << 
 96     (tools_glRenderMode_func)glRenderMode,     << 
 97     (tools_glFeedbackBuffer_func)glFeedbackBuf << 
 98     (tools_glPassThrough_func)glPassThrough    << 
 99   };                                           << 
100   fGL2PSAction->setOpenGLFunctions(&_funcs);   << 
101                                                << 
102   // add supported export image format            126   // add supported export image format
103   addExportImageFormat("eps");                    127   addExportImageFormat("eps");
104   addExportImageFormat("ps");                     128   addExportImageFormat("ps");
105   addExportImageFormat("pdf");                    129   addExportImageFormat("pdf");
106   addExportImageFormat("svg");                    130   addExportImageFormat("svg");
107                                                   131 
108   // Change the default name                      132   // Change the default name
109   fExportFilename += fDefaultExportFilename +     133   fExportFilename += fDefaultExportFilename + "_" + GetShortName().data();
110                                                   134   
111   //  glClearColor (0.0, 0.0, 0.0, 0.0);          135   //  glClearColor (0.0, 0.0, 0.0, 0.0);
112   //  glClearDepth (1.0);                         136   //  glClearDepth (1.0);
113   //  glDisable (GL_BLEND);                       137   //  glDisable (GL_BLEND);
114   //  glDisable (GL_LINE_SMOOTH);                 138   //  glDisable (GL_LINE_SMOOTH);
115   //  glDisable (GL_POLYGON_SMOOTH);              139   //  glDisable (GL_POLYGON_SMOOTH);
116                                                   140 
117 }                                                 141 }
118                                                   142 
119 G4OpenGLViewer::~G4OpenGLViewer ()                143 G4OpenGLViewer::~G4OpenGLViewer ()
120 {                                                 144 {
121   delete fGL2PSAction;                            145   delete fGL2PSAction;
122 }                                                 146 }
123                                                   147 
124 void G4OpenGLViewer::InitializeGLView ()          148 void G4OpenGLViewer::InitializeGLView () 
125 {                                                 149 {
126   if (fWinSize_x == 0) {                       << 150 #ifdef G4OPENGL_VERSION_2
127     fWinSize_x = fVP.GetWindowSizeHintX();     << 151   if (fVboDrawer) {
128   }                                            << 152     
129   if (fWinSize_y == 0) {                       << 153     // First, load a simple shader
130     fWinSize_y = fVP.GetWindowSizeHintY();     << 154     fShaderProgram = glCreateProgram();
                                                   >> 155     Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
                                                   >> 156     const char * vSrc = fVboDrawer->getVertexShaderSrc();
                                                   >> 157     glShaderSource(vertexShader, 1, &vSrc, NULL);
                                                   >> 158     glCompileShader(vertexShader);
                                                   >> 159     glAttachShader(fShaderProgram, vertexShader);
                                                   >> 160     
                                                   >> 161     Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
                                                   >> 162     const char * fSrc = fVboDrawer->getFragmentShaderSrc();
                                                   >> 163     glShaderSource(fragmentShader, 1, &fSrc, NULL);
                                                   >> 164     glCompileShader(fragmentShader);
                                                   >> 165     
                                                   >> 166     glAttachShader(fShaderProgram, fragmentShader);
                                                   >> 167     glLinkProgram(fShaderProgram);
                                                   >> 168     glUseProgram(fShaderProgram);
                                                   >> 169     
                                                   >> 170     //   UniformLocation uColor = getUniformLocation(fShaderProgram, "uColor");
                                                   >> 171     //   uniform4fv(uColor, [0.0, 0.3, 0.0, 1.0]);
                                                   >> 172     
                                                   >> 173     // Extract the references to the attributes from the shader.
                                                   >> 174     
                                                   >> 175     fVertexPositionAttribute =
                                                   >> 176     glGetAttribLocation(fShaderProgram, "aVertexPosition");
                                                   >> 177     
                                                   >> 178     
                                                   >> 179     glEnableVertexAttribArray(fVertexPositionAttribute);
                                                   >> 180     
                                                   >> 181     // Extract the references the uniforms from the shader
                                                   >> 182     fpMatrixUniform  = glGetUniformLocation(fShaderProgram, "uPMatrix");
                                                   >> 183     fcMatrixUniform  = glGetUniformLocation(fShaderProgram, "uCMatrix");
                                                   >> 184     fmvMatrixUniform = glGetUniformLocation(fShaderProgram, "uMVMatrix");
                                                   >> 185     fnMatrixUniform  = glGetUniformLocation(fShaderProgram, "uNMatrix");
                                                   >> 186     ftMatrixUniform  = glGetUniformLocation(fShaderProgram, "uTMatrix");
                                                   >> 187     
                                                   >> 188     /*    glUniformMatrix4fv(fcMatrixUniform, 1, 0, identity);
                                                   >> 189      glUniformMatrix4fv(fpMatrixUniform, 1, 0, identity);
                                                   >> 190      glUniformMatrix4fv(ftMatrixUniform, 1, 0, identity);
                                                   >> 191      glUniformMatrix4fv(fmvMatrixUniform, 1, 0, identity);
                                                   >> 192      */
                                                   >> 193     // We have to set that in order to avoid calls on opengl commands before all is ready
                                                   >> 194     fGlViewInitialized = true;
131   }                                               195   }
                                                   >> 196 #endif
                                                   >> 197   
                                                   >> 198 
                                                   >> 199   fWinSize_x = fVP.GetWindowSizeHintX();
                                                   >> 200   fWinSize_y = fVP.GetWindowSizeHintY();
132                                                   201 
133   glClearColor (0.0, 0.0, 0.0, 0.0);              202   glClearColor (0.0, 0.0, 0.0, 0.0);
134   glClearDepth (1.0);                             203   glClearDepth (1.0);
                                                   >> 204 #ifndef G4OPENGL_VERSION_2
135   glDisable (GL_LINE_SMOOTH);                     205   glDisable (GL_LINE_SMOOTH);
136   glDisable (GL_POLYGON_SMOOTH);                  206   glDisable (GL_POLYGON_SMOOTH);
                                                   >> 207 #endif
137                                                   208 
138 // clear the buffers and window?                  209 // clear the buffers and window?
139   ClearView ();                                   210   ClearView ();
140   FinishView ();                                  211   FinishView ();
141                                                   212   
142   glDepthFunc (GL_LEQUAL);                        213   glDepthFunc (GL_LEQUAL);
143   glDepthMask (GL_TRUE);                          214   glDepthMask (GL_TRUE);
144                                                   215   
145   glEnable (GL_BLEND);                            216   glEnable (GL_BLEND);
146   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_    217   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
147                                                   218   
148 }                                                 219 }
149                                                   220 
150 void G4OpenGLViewer::ClearView () {               221 void G4OpenGLViewer::ClearView () {
151   ClearViewWithoutFlush();                        222   ClearViewWithoutFlush();
152                                                << 
153   if(!isFramebufferReady()) {                  << 
154     return;                                    << 
155   }                                            << 
156                                                << 
157   glFlush();                                      223   glFlush();
158 }                                                 224 }
159                                                   225 
160                                                   226 
161 void G4OpenGLViewer::ClearViewWithoutFlush ()     227 void G4OpenGLViewer::ClearViewWithoutFlush () {
162   // Ready for clear ?                            228   // Ready for clear ?
163   // See : http://lists.apple.com/archives/mac    229   // See : http://lists.apple.com/archives/mac-opengl/2012/Jul/msg00038.html
164   if(!isFramebufferReady()) {                  << 230 #if GL_EXT_framebuffer_object
165     return;                                    << 231 //  if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_UNDEFINED) {
166   }                                            << 232 //    return;
                                                   >> 233 //  }
                                                   >> 234 #endif
167                                                   235   
168   glClearColor (background.GetRed(),              236   glClearColor (background.GetRed(),
169                 background.GetGreen(),            237                 background.GetGreen(),
170                 background.GetBlue(),             238                 background.GetBlue(),
171                 1.);                              239                 1.);
172   glClearDepth (1.0);                             240   glClearDepth (1.0);
173   //Below line does not compile with Mesa incl    241   //Below line does not compile with Mesa includes.
174   //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BU    242   //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
175   glClear (GL_COLOR_BUFFER_BIT);                  243   glClear (GL_COLOR_BUFFER_BIT);
176   glClear (GL_DEPTH_BUFFER_BIT);                  244   glClear (GL_DEPTH_BUFFER_BIT);
177   glClear (GL_STENCIL_BUFFER_BIT);                245   glClear (GL_STENCIL_BUFFER_BIT);
178 }                                                 246 }
179                                                   247 
180                                                   248 
181 void G4OpenGLViewer::ResizeWindow(unsigned int    249 void G4OpenGLViewer::ResizeWindow(unsigned int aWidth, unsigned int aHeight) {
182   if ((fWinSize_x != aWidth) || (fWinSize_y !=    250   if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
183     fWinSize_x = aWidth;                          251     fWinSize_x = aWidth;
184     fWinSize_y = aHeight;                         252     fWinSize_y = aHeight;
185     fSizeHasChanged = true;                       253     fSizeHasChanged = true;
186   } else {                                        254   } else {
187     fSizeHasChanged = false;                      255     fSizeHasChanged = false;
188   }                                               256   }
189 }                                                 257 }
190                                                   258 
191 /**                                               259 /**
192  * Set the viewport of the scene                  260  * Set the viewport of the scene
193  * MAXIMUM SIZE is :                              261  * MAXIMUM SIZE is :
194  * GLint dims[2];                                 262  * GLint dims[2];
195  * glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);     263  * glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
196  */                                               264  */
197 void G4OpenGLViewer::ResizeGLView()               265 void G4OpenGLViewer::ResizeGLView()
198 {                                                 266 {
199   // Check size                                   267   // Check size
200   GLint dims[2];                                  268   GLint dims[2];
201   dims[0] = 0;                                    269   dims[0] = 0;
202   dims[1] = 0;                                    270   dims[1] = 0;
203                                                   271 
204   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);      272   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
205                                                   273 
206   if ((dims[0] !=0 ) && (dims[1] !=0)) {          274   if ((dims[0] !=0 ) && (dims[1] !=0)) {
207                                                   275 
208     if (fWinSize_x > (unsigned)dims[0]) {         276     if (fWinSize_x > (unsigned)dims[0]) {
209       G4cerr << "Try to resize view greater th    277       G4cerr << "Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<" is resize to "<<  dims[0] << G4endl;
210       fWinSize_x = dims[0];                       278       fWinSize_x = dims[0];
211     }                                             279     }
212     if (fWinSize_y > (unsigned)dims[1]) {         280     if (fWinSize_y > (unsigned)dims[1]) {
213       G4cerr << "Try to resize view greater th    281       G4cerr << "Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<" is resize to "<<  dims[1] << G4endl;
214       fWinSize_y = dims[1];                       282       fWinSize_y = dims[1];
215     }                                             283     }
216   }                                               284   }
217                                                   285     
218   glViewport(0, 0, fWinSize_x,fWinSize_y);        286   glViewport(0, 0, fWinSize_x,fWinSize_y);   
219                                                   287 
220                                                   288 
221 }                                                 289 }
222                                                   290 
223                                                   291 
224 void G4OpenGLViewer::SetView () {                 292 void G4OpenGLViewer::SetView () {
225   // if getting pick infos, should not resize     293   // if getting pick infos, should not resize the view.
226   if (fIsGettingPickInfos) return;                294   if (fIsGettingPickInfos) return;
227                                                   295   
228   if (!fSceneHandler.GetScene()) {                296   if (!fSceneHandler.GetScene()) {
229     return;                                       297     return;
230   }                                               298   }
231   // Calculates view representation based on e    299   // Calculates view representation based on extent of object being
232   // viewed and (initial) viewpoint.  (Note: i    300   // viewed and (initial) viewpoint.  (Note: it can change later due
233   // to user interaction via visualization sys    301   // to user interaction via visualization system's GUI.)
234                                                   302   
235   // Lighting.                                    303   // Lighting.
236   GLfloat lightPosition [4];                      304   GLfloat lightPosition [4];
237   lightPosition [0] = fVP.GetActualLightpointD    305   lightPosition [0] = fVP.GetActualLightpointDirection().x();
238   lightPosition [1] = fVP.GetActualLightpointD    306   lightPosition [1] = fVP.GetActualLightpointDirection().y();
239   lightPosition [2] = fVP.GetActualLightpointD    307   lightPosition [2] = fVP.GetActualLightpointDirection().z();
240   lightPosition [3] = 0.;                         308   lightPosition [3] = 0.;
241   // Light position is "true" light direction,    309   // Light position is "true" light direction, so must come after gluLookAt.
242   GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f    310   GLfloat ambient [] = { 0.2f, 0.2f, 0.2f, 1.f};
243   GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f    311   GLfloat diffuse [] = { 0.8f, 0.8f, 0.8f, 1.f};
244   glEnable (GL_LIGHT0);                           312   glEnable (GL_LIGHT0);
245   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);     313   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
246   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);     314   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
247                                                   315   
248   G4double ratioX = 1;                            316   G4double ratioX = 1;
249   G4double ratioY = 1;                            317   G4double ratioY = 1;
250   if (fWinSize_y > fWinSize_x) {                  318   if (fWinSize_y > fWinSize_x) {
251     ratioX = ((G4double)fWinSize_y) / ((G4doub    319     ratioX = ((G4double)fWinSize_y) / ((G4double)fWinSize_x);
252   }                                               320   }
253   if (fWinSize_x > fWinSize_y) {                  321   if (fWinSize_x > fWinSize_y) {
254     ratioY = ((G4double)fWinSize_x) / ((G4doub    322     ratioY = ((G4double)fWinSize_x) / ((G4double)fWinSize_y);
255   }                                               323   }
256                                                   324   
257   // Get radius of scene, etc.                    325   // Get radius of scene, etc.
258   // Note that this procedure properly takes i    326   // Note that this procedure properly takes into account zoom, dolly and pan.
259   const G4Point3D targetPoint                     327   const G4Point3D targetPoint
260     = fSceneHandler.GetScene()->GetStandardTar    328     = fSceneHandler.GetScene()->GetStandardTargetPoint()
261     + fVP.GetCurrentTargetPoint ();               329     + fVP.GetCurrentTargetPoint ();
262   G4double radius = fSceneHandler.GetScene()->    330   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
263   if(radius<=0.) radius = 1.;                     331   if(radius<=0.) radius = 1.;
264   const G4double cameraDistance = fVP.GetCamer    332   const G4double cameraDistance = fVP.GetCameraDistance (radius);
265   const G4Point3D cameraPosition =                333   const G4Point3D cameraPosition =
266     targetPoint + cameraDistance * fVP.GetView    334     targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
267   const GLdouble pnear  = fVP.GetNearDistance     335   const GLdouble pnear  = fVP.GetNearDistance (cameraDistance, radius);
268   const GLdouble pfar   = fVP.GetFarDistance      336   const GLdouble pfar   = fVP.GetFarDistance  (cameraDistance, pnear, radius);
269   const GLdouble right  = fVP.GetFrontHalfHeig    337   const GLdouble right  = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
270   const GLdouble left   = -right;                 338   const GLdouble left   = -right;
271   const GLdouble top    = fVP.GetFrontHalfHeig    339   const GLdouble top    = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
272   const GLdouble bottom = -top;                   340   const GLdouble bottom = -top;
273                                                   341   
274   // FIXME                                        342   // FIXME
275   ResizeGLView();                                 343   ResizeGLView();
276   //SHOULD SetWindowsSizeHint()...                344   //SHOULD SetWindowsSizeHint()...
277                                                   345 
278   glMatrixMode (GL_PROJECTION); // set up Frus    346   glMatrixMode (GL_PROJECTION); // set up Frustum.
279   glLoadIdentity();                               347   glLoadIdentity();
280                                                   348 
281   const G4Vector3D scaleFactor = fVP.GetScaleF    349   const G4Vector3D scaleFactor = fVP.GetScaleFactor();
282   glScaled(scaleFactor.x(),scaleFactor.y(),sca    350   glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z());
283                                                   351   
284   if (fVP.GetFieldHalfAngle() == 0.) {            352   if (fVP.GetFieldHalfAngle() == 0.) {
285     g4GlOrtho (left, right, bottom, top, pnear    353     g4GlOrtho (left, right, bottom, top, pnear, pfar);
286   }                                               354   }
287   else {                                          355   else {
288     g4GlFrustum (left, right, bottom, top, pne    356     g4GlFrustum (left, right, bottom, top, pnear, pfar);
289   }                                               357   }  
290                                                   358 
291   glMatrixMode (GL_MODELVIEW); // apply furthe    359   glMatrixMode (GL_MODELVIEW); // apply further transformations to scene.
292   glLoadIdentity();                               360   glLoadIdentity();
293                                                   361   
294   const G4Normal3D& upVector = fVP.GetUpVector    362   const G4Normal3D& upVector = fVP.GetUpVector ();  
295   G4Point3D gltarget;                             363   G4Point3D gltarget;
296   if (cameraDistance > 1.e-6 * radius) {          364   if (cameraDistance > 1.e-6 * radius) {
297     gltarget = targetPoint;                       365     gltarget = targetPoint;
298   }                                               366   }
299   else {                                          367   else {
300     gltarget = targetPoint - radius * fVP.GetV    368     gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit();
301   }                                               369   }
302                                                   370 
303   const G4Point3D& pCamera = cameraPosition;      371   const G4Point3D& pCamera = cameraPosition;  // An alias for brevity.
304                                                   372 
305   g4GluLookAt (pCamera.x(),  pCamera.y(),  pCa    373   g4GluLookAt (pCamera.x(),  pCamera.y(),  pCamera.z(),       // Viewpoint.
306              gltarget.x(), gltarget.y(), gltar    374              gltarget.x(), gltarget.y(), gltarget.z(),      // Target point.
307              upVector.x(), upVector.y(), upVec    375              upVector.x(), upVector.y(), upVector.z());     // Up vector.
308   // Light position is "true" light direction,    376   // Light position is "true" light direction, so must come after gluLookAt.
309   glLightfv (GL_LIGHT0, GL_POSITION, lightPosi    377   glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
310                                                   378 
311   // The idea is to use back-to-back clipping  << 379   // OpenGL no longer seems to reconstruct clipped edges, so, when the
312   // down to just a few pixels, which can make << 380   // BooleanProcessor is up to it, abandon this and use generic
313   // now, comment this out and use the generic << 381   // clipping in G4OpenGLSceneHandler::CreateSectionPolyhedron.  Also,
314   // G4VSolid* G4OpenGLSceneHandler::CreateSec << 382   // force kernel visit on change of clipping plane in
315   // { return G4VSceneHandler::CreateSectionSo << 383   // G4OpenGLStoredViewer::CompareForKernelVisit.
316 //  if (fVP.IsSection () ) {  // pair of back  << 384   //if (fVP.IsSection () ) {  // pair of back to back clip planes.
317 //    const G4Plane3D& sp = fVP.GetSectionPlan << 385   if (false) {  // pair of back to back clip planes.
318 //    double sArray[4];                        << 386     const G4Plane3D& sp = fVP.GetSectionPlane ();
319 //    sArray[0] = sp.a();                      << 387     double sArray[4];
320 //    sArray[1] = sp.b();                      << 388     sArray[0] = sp.a();
321 //    sArray[2] = sp.c();                      << 389     sArray[1] = sp.b();
322 //    sArray[3] = sp.d() + radius * 1.e-05;    << 390     sArray[2] = sp.c();
323 //    glClipPlane (GL_CLIP_PLANE0, sArray);    << 391     sArray[3] = sp.d() + radius * 1.e-05;
324 //    glEnable (GL_CLIP_PLANE0);               << 392     glClipPlane (GL_CLIP_PLANE0, sArray);
325 //    sArray[0] = -sp.a();                     << 393     glEnable (GL_CLIP_PLANE0);
326 //    sArray[1] = -sp.b();                     << 394     sArray[0] = -sp.a();
327 //    sArray[2] = -sp.c();                     << 395     sArray[1] = -sp.b();
328 //    sArray[3] = -sp.d() + radius * 1.e-05;   << 396     sArray[2] = -sp.c();
329 //    glClipPlane (GL_CLIP_PLANE1, sArray);    << 397     sArray[3] = -sp.d() + radius * 1.e-05;
330 //    glEnable (GL_CLIP_PLANE1);               << 398     glClipPlane (GL_CLIP_PLANE1, sArray);
331 //  } else {                                   << 399     glEnable (GL_CLIP_PLANE1);
332 //    glDisable (GL_CLIP_PLANE0);              << 400   } else {
333 //    glDisable (GL_CLIP_PLANE1);              << 401     glDisable (GL_CLIP_PLANE0);
334 //  }                                          << 402     glDisable (GL_CLIP_PLANE1);
                                                   >> 403   }
335                                                   404 
336   // What we call intersection of cutaways is     405   // What we call intersection of cutaways is easy in OpenGL.  You
337   // just keep cutting.  Unions are more trick    406   // just keep cutting.  Unions are more tricky - you have to have
338   // multiple passes and this is handled in       407   // multiple passes and this is handled in
339   // G4OpenGLImmediate/StoredViewer::ProcessVi    408   // G4OpenGLImmediate/StoredViewer::ProcessView.
340   const G4Planes& cutaways = fVP.GetCutawayPla    409   const G4Planes& cutaways = fVP.GetCutawayPlanes();
341   size_t nPlanes = cutaways.size();               410   size_t nPlanes = cutaways.size();
342   if (fVP.IsCutaway() &&                          411   if (fVP.IsCutaway() &&
343       fVP.GetCutawayMode() == G4ViewParameters << 412       fVP.GetCutawayMode() == G4ViewParameters::cutawayIntersection &&
                                                   >> 413       nPlanes > 0) {
344     double a[4];                                  414     double a[4];
345     a[0] = cutaways[0].a();                       415     a[0] = cutaways[0].a();
346     a[1] = cutaways[0].b();                       416     a[1] = cutaways[0].b();
347     a[2] = cutaways[0].c();                       417     a[2] = cutaways[0].c();
348     a[3] = cutaways[0].d();                       418     a[3] = cutaways[0].d();
349     glClipPlane (GL_CLIP_PLANE2, a);              419     glClipPlane (GL_CLIP_PLANE2, a);
350     glEnable (GL_CLIP_PLANE2);                    420     glEnable (GL_CLIP_PLANE2);
351     if (nPlanes > 1) {                            421     if (nPlanes > 1) {
352       a[0] = cutaways[1].a();                     422       a[0] = cutaways[1].a();
353       a[1] = cutaways[1].b();                     423       a[1] = cutaways[1].b();
354       a[2] = cutaways[1].c();                     424       a[2] = cutaways[1].c();
355       a[3] = cutaways[1].d();                     425       a[3] = cutaways[1].d();
356       glClipPlane (GL_CLIP_PLANE3, a);            426       glClipPlane (GL_CLIP_PLANE3, a);
357       glEnable (GL_CLIP_PLANE3);                  427       glEnable (GL_CLIP_PLANE3);
358     }                                             428     }
359     if (nPlanes > 2) {                            429     if (nPlanes > 2) {
360       a[0] = cutaways[2].a();                     430       a[0] = cutaways[2].a();
361       a[1] = cutaways[2].b();                     431       a[1] = cutaways[2].b();
362       a[2] = cutaways[2].c();                     432       a[2] = cutaways[2].c();
363       a[3] = cutaways[2].d();                     433       a[3] = cutaways[2].d();
364       glClipPlane (GL_CLIP_PLANE4, a);            434       glClipPlane (GL_CLIP_PLANE4, a);
365       glEnable (GL_CLIP_PLANE4);                  435       glEnable (GL_CLIP_PLANE4);
366     }                                             436     }
367   } else {                                        437   } else {
368     glDisable (GL_CLIP_PLANE2);                   438     glDisable (GL_CLIP_PLANE2);
369     glDisable (GL_CLIP_PLANE3);                   439     glDisable (GL_CLIP_PLANE3);
370     glDisable (GL_CLIP_PLANE4);                   440     glDisable (GL_CLIP_PLANE4);
371   }                                               441   }
372                                                   442 
373   // Background.                                  443   // Background.
374   background = fVP.GetBackgroundColour ();        444   background = fVP.GetBackgroundColour ();
375                                                   445 
376 }                                                 446 }
377                                                   447 
378                                                   448 
379                                                   449 
380 void G4OpenGLViewer::ResetView () {               450 void G4OpenGLViewer::ResetView () {
381   G4VViewer::ResetView();                         451   G4VViewer::ResetView();
382   fRot_sens = 1;                                  452   fRot_sens = 1;
383   fPan_sens = 0.01;                               453   fPan_sens = 0.01;
384 }                                                 454 }
385                                                   455 
386                                                   456 
387 void G4OpenGLViewer::HaloingFirstPass () {        457 void G4OpenGLViewer::HaloingFirstPass () {
388                                                   458   
389   //To perform haloing, first Draw all informa    459   //To perform haloing, first Draw all information to the depth buffer
390   //alone, using a chunky line width, and then    460   //alone, using a chunky line width, and then Draw all info again, to
391   //the colour buffer, setting a thinner line     461   //the colour buffer, setting a thinner line width an the depth testing 
392   //function to less than or equal, so if two     462   //function to less than or equal, so if two lines cross, the one 
393   //passing behind the other will not pass the    463   //passing behind the other will not pass the depth test, and so not
394   //get rendered either side of the infront li    464   //get rendered either side of the infront line for a short distance.
395                                                   465 
396   //First, disable writing to the colo(u)r buf    466   //First, disable writing to the colo(u)r buffer...
397   glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, G    467   glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
398                                                   468 
399   //Now enable writing to the depth buffer...     469   //Now enable writing to the depth buffer...
400   glDepthMask (GL_TRUE);                          470   glDepthMask (GL_TRUE);
401   glDepthFunc (GL_LESS);                          471   glDepthFunc (GL_LESS);
402   glClearDepth (1.0);                             472   glClearDepth (1.0);
403                                                   473 
404   //Finally, set the line width to something w    474   //Finally, set the line width to something wide...
405   ChangeLineWidth(3.0);                           475   ChangeLineWidth(3.0);
406                                                   476 
407 }                                                 477 }
408                                                   478 
409 void G4OpenGLViewer::HaloingSecondPass () {       479 void G4OpenGLViewer::HaloingSecondPass () {
410                                                   480 
411   //And finally, turn the colour buffer back o    481   //And finally, turn the colour buffer back on with a sesible line width...
412   glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_T    482   glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
413   glDepthFunc (GL_LEQUAL);                        483   glDepthFunc (GL_LEQUAL);
414   ChangeLineWidth(1.0);                           484   ChangeLineWidth(1.0);
415                                                   485 
416 }                                                 486 }
417                                                   487 
418 G4String G4OpenGLViewer::Pick(GLdouble x, GLdo    488 G4String G4OpenGLViewer::Pick(GLdouble x, GLdouble y)
419 {                                                 489 {
420   const std::vector < G4OpenGLViewerPickMap* > << 490   std::vector < G4OpenGLViewerPickMap* >  pickMap = GetPickDetails(x,y);
421   G4String txt = "";                              491   G4String txt = "";
422   if (pickMap.size() == 0) {                      492   if (pickMap.size() == 0) {
423 //        txt += "No hits recorded.";;         << 493     txt += "Too many hits.  Zoom in to reduce overlaps.";;
424   } else {                                        494   } else {
425     for (unsigned int a=0; a < pickMap.size(); << 495     for (unsigned int a=0; a< pickMap.size(); a++) {
426       if (pickMap[a]->getAttributes().size() > << 496       txt += pickMap[a]->print();
427         txt += pickMap[a]->print();            << 
428       }                                        << 
429     }                                             497     }
430   }                                               498   }
431   return txt;                                     499   return txt;
432 }                                                 500 }
433                                                   501 
434 const std::vector < G4OpenGLViewerPickMap* > & << 502 std::vector < G4OpenGLViewerPickMap* > G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
435 {                                                 503 {
436   static std::vector < G4OpenGLViewerPickMap*  << 504   std::vector < G4OpenGLViewerPickMap* > pickMapVector;
437   for (auto pickMap: pickMapVector) {          << 
438     delete pickMap;                            << 
439   }                                            << 
440   pickMapVector.clear();                       << 
441                                                   505   
                                                   >> 506   std::ostringstream oss;
442   const G4int BUFSIZE = 512;                      507   const G4int BUFSIZE = 512;
443   GLuint selectBuffer[BUFSIZE];                   508   GLuint selectBuffer[BUFSIZE];
444   glSelectBuffer(BUFSIZE, selectBuffer);          509   glSelectBuffer(BUFSIZE, selectBuffer);
445   glRenderMode(GL_SELECT);                        510   glRenderMode(GL_SELECT);
446   glInitNames();                                  511   glInitNames();
447   glPushName(0);                                  512   glPushName(0);
448   glMatrixMode(GL_PROJECTION);                    513   glMatrixMode(GL_PROJECTION);
449   G4double currentProjectionMatrix[16];           514   G4double currentProjectionMatrix[16];
450   glGetDoublev(GL_PROJECTION_MATRIX, currentPr    515   glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
451   glPushMatrix();                                 516   glPushMatrix();
452   glLoadIdentity();                               517   glLoadIdentity();
453   GLint viewport[4];                              518   GLint viewport[4];
454   glGetIntegerv(GL_VIEWPORT, viewport);           519   glGetIntegerv(GL_VIEWPORT, viewport);
455 /*  G4cout                                     << 
456   << "viewport, x,y: "                         << 
457   << viewport[0] << ',' << viewport[1] << ','  << 
458   << ", " << x << ',' << y                     << 
459   << G4endl;                                   << 
460 */                                             << 
461   fIsGettingPickInfos = true;                     520   fIsGettingPickInfos = true;
462   // Define 5x5 pixel pick area                   521   // Define 5x5 pixel pick area
463   g4GluPickMatrix(x, viewport[3] - y, 5., 5.,     522   g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
464   glMultMatrixd(currentProjectionMatrix);         523   glMultMatrixd(currentProjectionMatrix);
465   glMatrixMode(GL_MODELVIEW);                     524   glMatrixMode(GL_MODELVIEW);
466   DrawView();                                     525   DrawView();
467   GLint hits = glRenderMode(GL_RENDER);           526   GLint hits = glRenderMode(GL_RENDER);
468   fIsGettingPickInfos = false;                    527   fIsGettingPickInfos = false;
469   if (hits < 0) {                              << 
470     G4cout << "Too many hits.  Zoom in to redu << 
471     goto restoreMatrices;                      << 
472   }                                            << 
473   if (hits > 0) {                                 528   if (hits > 0) {
                                                   >> 529     
474     GLuint* p = selectBuffer;                     530     GLuint* p = selectBuffer;
475     for (GLint i = 0; i < hits; ++i) {            531     for (GLint i = 0; i < hits; ++i) {
                                                   >> 532       G4OpenGLViewerPickMap* pickMap = new G4OpenGLViewerPickMap();
476       GLuint nnames = *p++;                       533       GLuint nnames = *p++;
477       // This bit of debug code or...             534       // This bit of debug code or...
478       //GLuint zmin = *p++;                       535       //GLuint zmin = *p++;
479       //GLuint zmax = *p++;                       536       //GLuint zmax = *p++;
480       //G4cout << "Hit " << i << ": " << nname    537       //G4cout << "Hit " << i << ": " << nnames << " names"
481       //       << "\nzmin: " << zmin << ", zma    538       //       << "\nzmin: " << zmin << ", zmax: " << zmax << G4endl;
482       // ...just increment the pointer            539       // ...just increment the pointer
483       p++;                                        540       p++;
484       p++;                                        541       p++;
485       for (GLuint j = 0; j < nnames; ++j) {       542       for (GLuint j = 0; j < nnames; ++j) {
                                                   >> 543         oss.clear();
486         GLuint name = *p++;                       544         GLuint name = *p++;
                                                   >> 545         pickMap->setHitNumber(i);
                                                   >> 546         pickMap->setSubHitNumber(j);
                                                   >> 547         pickMap->setPickName(name);
487   std::map<GLuint, G4AttHolder*>::iterator ite    548   std::map<GLuint, G4AttHolder*>::iterator iter =
488     fOpenGLSceneHandler.fPickMap.find(name);      549     fOpenGLSceneHandler.fPickMap.find(name);
489   if (iter != fOpenGLSceneHandler.fPickMap.end    550   if (iter != fOpenGLSceneHandler.fPickMap.end()) {
490     G4AttHolder* attHolder = iter->second;        551     G4AttHolder* attHolder = iter->second;
491     if(attHolder && attHolder->GetAttDefs().si    552     if(attHolder && attHolder->GetAttDefs().size()) {
492       for (size_t iAtt = 0;                       553       for (size_t iAtt = 0;
493      iAtt < attHolder->GetAttDefs().size(); ++    554      iAtt < attHolder->GetAttDefs().size(); ++iAtt) {
494               std::ostringstream oss;          << 
495         oss << G4AttCheck(attHolder->GetAttVal    555         oss << G4AttCheck(attHolder->GetAttValues()[iAtt],
496                                 attHolder->Get    556                                 attHolder->GetAttDefs()[iAtt]);
497               G4OpenGLViewerPickMap* pickMap = << 
498 //              G4cout                         << 
499 //              << "i,j, attHolder->GetAttDefs << 
500 //              << i << ',' << j               << 
501 //              << ", " << attHolder->GetAttDe << 
502 //              << G4endl;                     << 
503 //              G4cout << "G4OpenGLViewer::Get << 
504               pickMap->addAttributes(oss.str()    557               pickMap->addAttributes(oss.str());
505               pickMap->setHitNumber(i);        << 
506               pickMap->setSubHitNumber(j);     << 
507               pickMap->setPickName(name);      << 
508               pickMapVector.push_back(pickMap) << 
509             }                                     558             }
510           }                                       559           }
511         }                                         560         }
512       }                                           561       }
                                                   >> 562       pickMapVector.push_back(pickMap);
513     }                                             563     }
514   }                                               564   }
515                                                << 
516 restoreMatrices:                               << 
517   glMatrixMode(GL_PROJECTION);                    565   glMatrixMode(GL_PROJECTION);
518   glPopMatrix();                                  566   glPopMatrix();
519   glMatrixMode(GL_MODELVIEW);                     567   glMatrixMode(GL_MODELVIEW);
520                                                << 
521   return pickMapVector;                           568   return pickMapVector;
522 }                                                 569 }
523                                                   570 
524 GLubyte* G4OpenGLViewer::grabPixels               571 GLubyte* G4OpenGLViewer::grabPixels
525 (int inColor, unsigned int width, unsigned int    572 (int inColor, unsigned int width, unsigned int height) {
526                                                   573   
527   GLubyte* buffer;                                574   GLubyte* buffer;
528   GLint swapbytes, lsbfirst, rowlength;           575   GLint swapbytes, lsbfirst, rowlength;
529   GLint skiprows, skippixels, alignment;          576   GLint skiprows, skippixels, alignment;
530   GLenum format;                                  577   GLenum format;
531   int size;                                       578   int size;
532                                                   579 
533   if (inColor) {                                  580   if (inColor) {
534     format = GL_RGB;                              581     format = GL_RGB;
535     size = width*height*3;                        582     size = width*height*3;
536   } else {                                        583   } else {
537     format = GL_LUMINANCE;                        584     format = GL_LUMINANCE;
538     size = width*height*1;                        585     size = width*height*1;
539   }                                               586   }
540                                                   587 
541   buffer = new GLubyte[size];                     588   buffer = new GLubyte[size];
542   if (buffer == NULL)                             589   if (buffer == NULL)
543     return NULL;                                  590     return NULL;
544                                                   591 
545   glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapby    592   glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
546   glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirs    593   glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
547   glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlen    594   glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
548                                                   595 
549   glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprow    596   glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
550   glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skipp    597   glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
551   glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignme    598   glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
552                                                   599 
553   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALS    600   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
554   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE    601   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
555   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);        602   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
556                                                   603 
557   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);         604   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
558   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);       605   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
559   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);         606   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
560                                                   607 
561   glReadBuffer(GL_FRONT);                         608   glReadBuffer(GL_FRONT);
562   glReadPixels (0, 0, (GLsizei)width, (GLsizei    609   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
563                                                   610 
564   glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbyt    611   glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
565   glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst    612   glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
566   glPixelStorei (GL_UNPACK_ROW_LENGTH, rowleng    613   glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
567                                                   614   
568   glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows    615   glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
569   glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippi    616   glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
570   glPixelStorei (GL_UNPACK_ALIGNMENT, alignmen    617   glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
571                                                   618   
572   return buffer;                                  619   return buffer;
573 }                                                 620 }
574                                                   621 
                                                   >> 622 bool G4OpenGLViewer::printEPS() {
                                                   >> 623   bool res;
                                                   >> 624 
                                                   >> 625   // Change the LC_NUMERIC value in order to have "." separtor and not ","
                                                   >> 626   // This case is only useful for French, Canadien...
                                                   >> 627   size_t len = strlen(setlocale(LC_NUMERIC,NULL));
                                                   >> 628   char* oldLocale = (char*)(malloc(len+1));
                                                   >> 629   if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
                                                   >> 630   setlocale(LC_NUMERIC,"C");
                                                   >> 631 
                                                   >> 632   if (((fExportImageFormat == "eps") || (fExportImageFormat == "ps")) && (!fVectoredPs)) {
                                                   >> 633     res = printNonVectoredEPS();
                                                   >> 634   } else {
                                                   >> 635     res = printVectoredEPS();
                                                   >> 636   }
                                                   >> 637 
                                                   >> 638   // restore the local
                                                   >> 639   if (oldLocale) {
                                                   >> 640     setlocale(LC_NUMERIC,oldLocale);
                                                   >> 641     free(oldLocale);
                                                   >> 642   }
                                                   >> 643 
                                                   >> 644   if (res == false) {
                                                   >> 645     G4cerr << "Error saving file... " << getRealPrintFilename().c_str() << G4endl;
                                                   >> 646   } else {
                                                   >> 647     G4cout << "File " << getRealPrintFilename().c_str() << " size: " << getRealExportWidth() << "x" << getRealExportHeight() << " has been saved " << G4endl;
                                                   >> 648 
                                                   >> 649     // increment index if necessary
                                                   >> 650     if ( fExportFilenameIndex != -1) {
                                                   >> 651       fExportFilenameIndex++;
                                                   >> 652     }
                                                   >> 653   }
                                                   >> 654 
                                                   >> 655   return res;
                                                   >> 656 }
                                                   >> 657 
575 bool G4OpenGLViewer::printVectoredEPS() {         658 bool G4OpenGLViewer::printVectoredEPS() {
576   return printGl2PS();                            659   return printGl2PS();
577 }                                                 660 }
578                                                   661 
579 bool G4OpenGLViewer::printNonVectoredEPS () {     662 bool G4OpenGLViewer::printNonVectoredEPS () {
580                                                   663 
581   int width = getRealExportWidth();               664   int width = getRealExportWidth();
582   int height = getRealExportHeight();             665   int height = getRealExportHeight();
583                                                   666 
584   FILE* fp;                                       667   FILE* fp;
585   GLubyte* pixels;                                668   GLubyte* pixels;
586   GLubyte* curpix;                                669   GLubyte* curpix;
587   int components, pos, i;                         670   int components, pos, i;
588                                                   671 
589   pixels = grabPixels (fPrintColour, width, he    672   pixels = grabPixels (fPrintColour, width, height);
590                                                   673 
591   if (pixels == NULL) {                           674   if (pixels == NULL) {
592       G4cerr << "Failed to get pixels from Ope    675       G4cerr << "Failed to get pixels from OpenGl viewport" << G4endl;
593     return false;                                 676     return false;
594   }                                               677   }
595   if (fPrintColour) {                             678   if (fPrintColour) {
596     components = 3;                               679     components = 3;
597   } else {                                        680   } else {
598     components = 1;                               681     components = 1;
599   }                                               682   }
600   std::string name = getRealPrintFilename();      683   std::string name = getRealPrintFilename();
601   fp = fopen (name.c_str(), "w");                 684   fp = fopen (name.c_str(), "w");
602   if (fp == NULL) {                               685   if (fp == NULL) {
603     G4cerr << "Can't open filename " << name.c    686     G4cerr << "Can't open filename " << name.c_str() << G4endl;
604     return false;                                 687     return false;
605   }                                               688   }
606                                                   689   
607   fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");     690   fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
608   fprintf (fp, "%%%%Title: %s\n", name.c_str()    691   fprintf (fp, "%%%%Title: %s\n", name.c_str());
609   fprintf (fp, "%%%%Creator: OpenGL pixmap ren    692   fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
610   fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n",    693   fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
611   fprintf (fp, "%%%%EndComments\n");              694   fprintf (fp, "%%%%EndComments\n");
612   fprintf (fp, "gsave\n");                        695   fprintf (fp, "gsave\n");
613   fprintf (fp, "/bwproc {\n");                    696   fprintf (fp, "/bwproc {\n");
614   fprintf (fp, "    rgbproc\n");                  697   fprintf (fp, "    rgbproc\n");
615   fprintf (fp, "    dup length 3 idiv string 0    698   fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
616   fprintf (fp, "    5 -1 roll {\n");              699   fprintf (fp, "    5 -1 roll {\n");
617   fprintf (fp, "    add 2 1 roll 1 sub dup 0 e    700   fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
618   fprintf (fp, "    { pop 3 idiv 3 -1 roll dup    701   fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
619   fprintf (fp, "       3 1 roll 5 -1 roll } pu    702   fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
620   fprintf (fp, "    { 2 1 roll } ifelse\n");      703   fprintf (fp, "    { 2 1 roll } ifelse\n");
621   fprintf (fp, "    }forall\n");                  704   fprintf (fp, "    }forall\n");
622   fprintf (fp, "    pop pop pop\n");              705   fprintf (fp, "    pop pop pop\n");
623   fprintf (fp, "} def\n");                        706   fprintf (fp, "} def\n");
624   fprintf (fp, "systemdict /colorimage known n    707   fprintf (fp, "systemdict /colorimage known not {\n");
625   fprintf (fp, "   /colorimage {\n");             708   fprintf (fp, "   /colorimage {\n");
626   fprintf (fp, "       pop\n");                   709   fprintf (fp, "       pop\n");
627   fprintf (fp, "       pop\n");                   710   fprintf (fp, "       pop\n");
628   fprintf (fp, "       /rgbproc exch def\n");     711   fprintf (fp, "       /rgbproc exch def\n");
629   fprintf (fp, "       { bwproc } image\n");      712   fprintf (fp, "       { bwproc } image\n");
630   fprintf (fp, "   }  def\n");                    713   fprintf (fp, "   }  def\n");
631   fprintf (fp, "} if\n");                         714   fprintf (fp, "} if\n");
632   fprintf (fp, "/picstr %d string def\n", widt    715   fprintf (fp, "/picstr %d string def\n", width * components);
633   fprintf (fp, "%d %d scale\n", width, height)    716   fprintf (fp, "%d %d scale\n", width, height);
634   fprintf (fp, "%d %d %d\n", width, height, 8)    717   fprintf (fp, "%d %d %d\n", width, height, 8);
635   fprintf (fp, "[%d 0 0 %d 0 0]\n", width, hei    718   fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
636   fprintf (fp, "{currentfile picstr readhexstr    719   fprintf (fp, "{currentfile picstr readhexstring pop}\n");
637   fprintf (fp, "false %d\n", components);         720   fprintf (fp, "false %d\n", components);
638   fprintf (fp, "colorimage\n");                   721   fprintf (fp, "colorimage\n");
639                                                   722   
640   curpix = (GLubyte*) pixels;                     723   curpix = (GLubyte*) pixels;
641   pos = 0;                                        724   pos = 0;
642   for (i = width*height*components; i>0; i--)     725   for (i = width*height*components; i>0; i--) {
643     fprintf (fp, "%02hx ", (unsigned short)(*(    726     fprintf (fp, "%02hx ", (unsigned short)(*(curpix++)));
644     if (++pos >= 32) {                            727     if (++pos >= 32) {
645       fprintf (fp, "\n");                         728       fprintf (fp, "\n");
646       pos = 0;                                    729       pos = 0; 
647     }                                             730     }
648   }                                               731   }
649   if (pos)                                        732   if (pos)
650     fprintf (fp, "\n");                           733     fprintf (fp, "\n");
651                                                   734 
652   fprintf (fp, "grestore\n");                     735   fprintf (fp, "grestore\n");
653   fprintf (fp, "showpage\n");                     736   fprintf (fp, "showpage\n");
654   delete [] pixels;                               737   delete [] pixels;
655   fclose (fp);                                    738   fclose (fp);
656                                                   739 
657   // Reset for next time (useful if size chang << 740   // Reset for next time (useful is size change)
658   //  fPrintSizeX = -1;                           741   //  fPrintSizeX = -1;
659   //  fPrintSizeY = -1;                           742   //  fPrintSizeY = -1;
660                                                   743 
661   return true;                                    744   return true;
662 }                                                 745 }
663                                                   746 
664 /** Return if gl2ps is currently writing          747 /** Return if gl2ps is currently writing
665  */                                               748  */
666 bool G4OpenGLViewer::isGl2psWriting() {           749 bool G4OpenGLViewer::isGl2psWriting() {
667                                                   750 
668   if (!fGL2PSAction) return false;                751   if (!fGL2PSAction) return false;
669   if (fGL2PSAction->fileWritingEnabled()) {       752   if (fGL2PSAction->fileWritingEnabled()) {
670     return true;                                  753     return true;
671   }                                               754   }
672   return false;                                   755   return false;
673 }                                                 756 }
674                                                   757 
675                                                   758 
676 G4bool G4OpenGLViewer::isFramebufferReady() {  << 
677   bool check = false;                          << 
678 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER             << 
679   check = true;                                << 
680 #endif                                         << 
681 #ifdef G4VIS_BUILD_OPENGLX_DRIVER              << 
682   check = false;                               << 
683 #endif                                         << 
684 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER             << 
685   check = false;                               << 
686 #endif                                         << 
687 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER          << 
688   check = false;                               << 
689 #endif                                         << 
690                                                << 
691 #if GL_ARB_framebuffer_object                  << 
692   if (check) {                                 << 
693 //    if ( glCheckFramebufferStatus(GL_FRAMEBU << 
694 //      return false;                          << 
695 //    }                                        << 
696   }                                            << 
697 #endif                                         << 
698     return true;                               << 
699 }                                              << 
700                                                << 
701                                                << 
702 /* Draw Gl2Ps text if needed                      759 /* Draw Gl2Ps text if needed
703  */                                               760  */
704 void G4OpenGLViewer::DrawText(const G4Text& g4    761 void G4OpenGLViewer::DrawText(const G4Text& g4text)
705 {                                                 762 {
706   // gl2ps or GL window ?                         763   // gl2ps or GL window ?
707   if (isGl2psWriting()) {                         764   if (isGl2psWriting()) {
708                                                   765 
709     G4VSceneHandler::MarkerSizeType sizeType;     766     G4VSceneHandler::MarkerSizeType sizeType;
710     G4double size = fSceneHandler.GetMarkerSiz    767     G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
711     G4Point3D position = g4text.GetPosition();    768     G4Point3D position = g4text.GetPosition();
712                                                   769 
713     G4String textString = g4text.GetText();       770     G4String textString = g4text.GetText();
714                                                   771     
715     glRasterPos3d(position.x(),position.y(),po    772     glRasterPos3d(position.x(),position.y(),position.z());
716     GLint align = GL2PS_TEXT_B;                   773     GLint align = GL2PS_TEXT_B;
717                                                   774 
718     switch (g4text.GetLayout()) {                 775     switch (g4text.GetLayout()) {
719     case G4Text::left: align = GL2PS_TEXT_BL;     776     case G4Text::left: align = GL2PS_TEXT_BL; break;
720     case G4Text::centre: align = GL2PS_TEXT_B;    777     case G4Text::centre: align = GL2PS_TEXT_B; break;
721     case G4Text::right: align = GL2PS_TEXT_BR;    778     case G4Text::right: align = GL2PS_TEXT_BR;
722     }                                             779     }
723                                                   780     
724     fGL2PSAction->addTextOpt(textString.c_str( << 781     gl2psTextOpt(textString.c_str(),"Times-Roman",GLshort(size),align,0);
725                                                   782 
726   } else {                                        783   } else {
727                                                   784 
728     static G4int callCount = 0;                   785     static G4int callCount = 0;
729     ++callCount;                                  786     ++callCount;
730     //if (callCount <= 10 || callCount%100 ==     787     //if (callCount <= 10 || callCount%100 == 0) {
731     if (callCount <= 1) {                         788     if (callCount <= 1) {
732       G4cout <<                                   789       G4cout <<
733   "G4OpenGLViewer::DrawText: Not implemented f    790   "G4OpenGLViewer::DrawText: Not implemented for \""
734        << fName <<                                791        << fName <<
735   "\"\n  Called with "                            792   "\"\n  Called with "
736        << g4text                                  793        << g4text
737        << G4endl;                                 794        << G4endl;
738     }                                             795     }
739   }                                               796   }
740 }                                                 797 }
741                                                   798 
742 /** Change PointSize on gl2ps if needed           799 /** Change PointSize on gl2ps if needed
743  */                                               800  */
744 void G4OpenGLViewer::ChangePointSize(G4double     801 void G4OpenGLViewer::ChangePointSize(G4double size) {
745                                                   802 
746   if (isGl2psWriting()) {                         803   if (isGl2psWriting()) {
747     fGL2PSAction->setPointSize(int(size));        804     fGL2PSAction->setPointSize(int(size));
748   } else {                                        805   } else {
749     glPointSize (size);                           806     glPointSize (size);
750   }                                               807   }
751 }                                                 808 }
752                                                   809 
753                                                   810 
754 /** Change LineSize on gl2ps if needed            811 /** Change LineSize on gl2ps if needed
755  */                                               812  */
756 void G4OpenGLViewer::ChangeLineWidth(G4double     813 void G4OpenGLViewer::ChangeLineWidth(G4double width) {
757                                                   814 
758   if (isGl2psWriting()) {                         815   if (isGl2psWriting()) {
759     fGL2PSAction->setLineWidth(int(width));       816     fGL2PSAction->setLineWidth(int(width));
760   } else {                                        817   } else {
761     glLineWidth (width);                          818     glLineWidth (width);
762   }                                               819   }
763 }                                                 820 }
764                                                   821 
765 /**                                               822 /**
766  Export image with the given name with width a    823  Export image with the given name with width and height
767  Several cases :                                  824  Several cases :
768  If name is "", filename will have the default    825  If name is "", filename will have the default value
769  If name is "toto.png", set the name to "toto"    826  If name is "toto.png", set the name to "toto" and the format to "png". No incremented suffix is added.
770  If name is "toto", set the name to "toto" and    827  If name is "toto", set the name to "toto" and the format to default (or current format if specify).
771    Will also add an incremented suffix at the     828    Will also add an incremented suffix at the end of the file
772 */                                                829 */
773 bool G4OpenGLViewer::exportImage(std::string n    830 bool G4OpenGLViewer::exportImage(std::string name, int width, int height) {
774                                                   831 
775   if (! setExportFilename(name)) {                832   if (! setExportFilename(name)) {
776     return false;                                 833     return false;
777   }                                               834   }
778                                                   835 
779   if ((width != -1) && (height != -1)) {          836   if ((width != -1) && (height != -1)) {
780     setExportSize(width, height);                 837     setExportSize(width, height);
781   }                                               838   }
782                                                   839 
783   if (fExportImageFormat == "eps") {              840   if (fExportImageFormat == "eps") {
784     fGL2PSAction->setExportImageFormat_EPS();  << 841     fGL2PSAction->setExportImageFormat(GL2PS_EPS);
785   } else if (fExportImageFormat == "ps") {        842   } else if (fExportImageFormat == "ps") {
786     fGL2PSAction->setExportImageFormat_PS();   << 843     fGL2PSAction->setExportImageFormat(GL2PS_PS);
787   } else if (fExportImageFormat == "svg") {       844   } else if (fExportImageFormat == "svg") {
788     fGL2PSAction->setExportImageFormat_SVG();  << 845     fGL2PSAction->setExportImageFormat(GL2PS_SVG);
789   } else if (fExportImageFormat == "pdf") {       846   } else if (fExportImageFormat == "pdf") {
790     fGL2PSAction->setExportImageFormat_PDF();  << 847     fGL2PSAction->setExportImageFormat(GL2PS_PDF);
791   } else {                                        848   } else {
792     setExportImageFormat(fExportImageFormat,tr    849     setExportImageFormat(fExportImageFormat,true); // will display a message if this format is not correct for the current viewer
793     return false;                                 850     return false;
794   }                                               851   }
795                                                << 852   return printEPS();
796   bool res;                                    << 
797                                                << 
798   // Change the LC_NUMERIC value in order to h << 
799   // This case is only useful for French, Cana << 
800   size_t len = strlen(setlocale(LC_NUMERIC,NUL << 
801   char* oldLocale = (char*)(malloc(len+1));    << 
802   if(oldLocale!=NULL) strncpy(oldLocale,setloc << 
803   setlocale(LC_NUMERIC,"C");                   << 
804                                                << 
805   if (((fExportImageFormat == "eps") || (fExpo << 
806     res = printNonVectoredEPS();               << 
807   } else {                                     << 
808     res = printVectoredEPS();                  << 
809   }                                            << 
810                                                << 
811   // restore the local                         << 
812   if (oldLocale) {                             << 
813     setlocale(LC_NUMERIC,oldLocale);           << 
814     free(oldLocale);                           << 
815   }                                            << 
816                                                << 
817   if (res == false) {                          << 
818     G4cerr << "Error saving file... " << getRe << 
819   } else {                                     << 
820     G4cout << "File " << getRealPrintFilename( << 
821                                                << 
822     // increment index if necessary            << 
823     if ( fExportFilenameIndex != -1) {         << 
824       fExportFilenameIndex++;                  << 
825     }                                          << 
826   }                                            << 
827                                                << 
828   return res;                                  << 
829 }                                                 853 }
830                                                   854 
831                                                   855 
832 bool G4OpenGLViewer::printGl2PS() {               856 bool G4OpenGLViewer::printGl2PS() {
833                                                   857 
834   int width = getRealExportWidth();               858   int width = getRealExportWidth();
835   int height = getRealExportHeight();             859   int height = getRealExportHeight();
836   bool res = true;                                860   bool res = true;
837                                                   861 
838   // no need to redraw at each new primitive f    862   // no need to redraw at each new primitive for printgl2PS
839   G4OpenGLSceneHandler& oglSceneHandler = dyna    863   G4OpenGLSceneHandler& oglSceneHandler = dynamic_cast<G4OpenGLSceneHandler&>(fSceneHandler);
840   G4OpenGLSceneHandler::FlushAction originalFl << 864   G4int drawInterval = oglSceneHandler.GetEventsDrawInterval();
841   oglSceneHandler.SetFlushAction(G4OpenGLScene << 865   oglSceneHandler.SetEventsDrawInterval(1000); // some big value but not too big to not crash memory
842                                                   866 
843   if (!fGL2PSAction) return false;                867   if (!fGL2PSAction) return false;
844                                                   868 
845   fGL2PSAction->setFileName(getRealPrintFilena    869   fGL2PSAction->setFileName(getRealPrintFilename().c_str());
846   // try to resize                                870   // try to resize
847   int X = fWinSize_x;                             871   int X = fWinSize_x;
848   int Y = fWinSize_y;                             872   int Y = fWinSize_y;
849                                                   873 
850   fWinSize_x = width;                             874   fWinSize_x = width;
851   fWinSize_y = height;                            875   fWinSize_y = height;
852   // Laurent G. 16/03/10 : Not the good way to    876   // Laurent G. 16/03/10 : Not the good way to do. 
853   // We should draw in a new offscreen context    877   // We should draw in a new offscreen context instead of
854   // resizing and drawing in current window...    878   // resizing and drawing in current window...
855   // This should be solve when we will do an o    879   // This should be solve when we will do an offscreen method
856   // to render OpenGL                             880   // to render OpenGL
857   // See :                                        881   // See : 
858   // http://developer.apple.com/Mac/library/do    882   // http://developer.apple.com/Mac/library/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_offscreen/opengl_offscreen.html
859   // http://www.songho.ca/opengl/gl_fbo.html      883   // http://www.songho.ca/opengl/gl_fbo.html
860                                                   884 
861    ResizeGLView();                                885    ResizeGLView();
862    bool extendBuffer = true;                      886    bool extendBuffer = true;
863    bool endWriteAction = false;                   887    bool endWriteAction = false;
864    bool beginWriteAction = true;                  888    bool beginWriteAction = true;
865    bool filePointerOk = true;                     889    bool filePointerOk = true;
866    while ((extendBuffer) && (! endWriteAction)    890    while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
867                                                << 891      
868      beginWriteAction = fGL2PSAction->enableFi    892      beginWriteAction = fGL2PSAction->enableFileWriting();
869      if(beginWriteAction) {                    << 
870        GLint vp[4];                            << 
871        ::glGetIntegerv(GL_VIEWPORT,vp);        << 
872        fGL2PSAction->setViewport(vp[0],vp[1],v << 
873        beginWriteAction = fGL2PSAction->beginP << 
874      }                                         << 
875                                                << 
876      // 3 cases :                                 893      // 3 cases :
877      // - true                                    894      // - true
878      // - false && ! fGL2PSAction->fileWriting    895      // - false && ! fGL2PSAction->fileWritingEnabled() => bad file name
879      // - false && fGL2PSAction->fileWritingEn    896      // - false && fGL2PSAction->fileWritingEnabled() => buffer size problem ?
880                                                   897 
881      filePointerOk = fGL2PSAction->fileWriting    898      filePointerOk = fGL2PSAction->fileWritingEnabled();
882                                                   899        
883      if (beginWriteAction) {                      900      if (beginWriteAction) {
884                                                << 901        
885        // Set the viewport                        902        // Set the viewport
886        // By default, we choose the line width    903        // By default, we choose the line width (trajectories...)
887        fGL2PSAction->setLineWidth(fGl2psDefaul    904        fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
888        // By default, we choose the point size    905        // By default, we choose the point size (markers...)
889        fGL2PSAction->setPointSize(fGl2psDefaul    906        fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
890                                                   907        
891        DrawView ();                               908        DrawView ();
892                                                << 909        endWriteAction = fGL2PSAction->disableFileWriting();
893        endWriteAction = fGL2PSAction->endPage( << 
894        fGL2PSAction->disableFileWriting();     << 
895      }                                            910      }
896      if (filePointerOk) {                         911      if (filePointerOk) {
897        if ((! endWriteAction) || (! beginWrite    912        if ((! endWriteAction) || (! beginWriteAction)) {
898          extendBuffer = fGL2PSAction->extendBu    913          extendBuffer = fGL2PSAction->extendBufferSize();
899        }                                          914        }
900      }                                            915      }
901    }                                              916    }
902    fGL2PSAction->resetBufferSizeParameters();     917    fGL2PSAction->resetBufferSizeParameters();
903                                                   918 
904    if (!extendBuffer ) {                          919    if (!extendBuffer ) {
905      G4cerr << "ERROR: gl2ps buffer size is no    920      G4cerr << "ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<< G4endl;
906      res = false;                                 921      res = false;
907    }                                              922    }
908    if (!beginWriteAction ) {                      923    if (!beginWriteAction ) {
909      G4cerr << "ERROR: saving file "<<getRealP    924      G4cerr << "ERROR: saving file "<<getRealPrintFilename().c_str()<<". Check read/write access. No output produced" << G4endl;
910      res = false;                                 925      res = false;
911    }                                              926    }
912    if (!endWriteAction ) {                        927    if (!endWriteAction ) {
913      G4cerr << "gl2ps error. No output produce    928      G4cerr << "gl2ps error. No output produced" << G4endl;
914      res = false;                                 929      res = false;
915    }                                              930    }
916   fWinSize_x = X;                                 931   fWinSize_x = X;
917   fWinSize_y = Y;                                 932   fWinSize_y = Y;
918                                                   933 
919   oglSceneHandler.SetFlushAction(originalFlush << 934   oglSceneHandler.SetEventsDrawInterval(drawInterval);
920                                                   935 
921   // Reset for next time (useful is size chang    936   // Reset for next time (useful is size change)
922   //  fPrintSizeX = 0;                            937   //  fPrintSizeX = 0;
923   //  fPrintSizeY = 0;                            938   //  fPrintSizeY = 0;
924                                                   939 
925   return res;                                     940   return res;
926 }                                                 941 }
927                                                   942 
928 unsigned int G4OpenGLViewer::getWinWidth() con    943 unsigned int G4OpenGLViewer::getWinWidth() const{
929   return fWinSize_x;                              944   return fWinSize_x;
930 }                                                 945 }
931                                                   946 
932 unsigned int G4OpenGLViewer::getWinHeight() co    947 unsigned int G4OpenGLViewer::getWinHeight() const{
933   return fWinSize_y;                              948   return fWinSize_y;
934 }                                                 949 }
935                                                   950 
936 G4bool G4OpenGLViewer::sizeHasChanged() {         951 G4bool G4OpenGLViewer::sizeHasChanged() {
937   return fSizeHasChanged;                         952   return fSizeHasChanged;
938 }                                                 953 }
939                                                   954 
940 G4int G4OpenGLViewer::getRealExportWidth() {      955 G4int G4OpenGLViewer::getRealExportWidth() {
941   if (fPrintSizeX == -1) {                        956   if (fPrintSizeX == -1) {
942     return fWinSize_x;                            957     return fWinSize_x;
943   }                                               958   }
944   GLint dims[2];                                  959   GLint dims[2];
945   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);      960   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
946                                                   961 
947   // L.Garnier 01-2010: Some problems with mac    962   // L.Garnier 01-2010: Some problems with mac 10.6
948   if ((dims[0] !=0 ) && (dims[1] !=0)) {          963   if ((dims[0] !=0 ) && (dims[1] !=0)) {
949     if (fPrintSizeX > dims[0]){                   964     if (fPrintSizeX > dims[0]){
950       return dims[0];                             965       return dims[0];
951     }                                             966     }
952   }                                               967   }
953   if (fPrintSizeX < -1){                          968   if (fPrintSizeX < -1){
954     return 0;                                     969     return 0;
955   }                                               970   }
956   return fPrintSizeX;                             971   return fPrintSizeX;
957 }                                                 972 }
958                                                   973 
959 G4int G4OpenGLViewer::getRealExportHeight() {     974 G4int G4OpenGLViewer::getRealExportHeight() {
960   if (fPrintSizeY == -1) {                        975   if (fPrintSizeY == -1) {
961     return fWinSize_y;                            976     return fWinSize_y;
962   }                                               977   }
963   GLint dims[2];                                  978   GLint dims[2];
964   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);      979   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
965                                                   980 
966   // L.Garnier 01-2010: Some problems with mac    981   // L.Garnier 01-2010: Some problems with mac 10.6
967   if ((dims[0] !=0 ) && (dims[1] !=0)) {          982   if ((dims[0] !=0 ) && (dims[1] !=0)) {
968     if (fPrintSizeY > dims[1]){                   983     if (fPrintSizeY > dims[1]){
969       return dims[1];                             984       return dims[1];
970     }                                             985     }
971   }                                               986   }
972   if (fPrintSizeY < -1){                          987   if (fPrintSizeY < -1){
973     return 0;                                     988     return 0;
974   }                                               989   }
975   return fPrintSizeY;                             990   return fPrintSizeY;
976 }                                                 991 }
977                                                   992 
978 void G4OpenGLViewer::setExportSize(G4int X, G4    993 void G4OpenGLViewer::setExportSize(G4int X, G4int Y) {
979   fPrintSizeX = X;                                994   fPrintSizeX = X;
980   fPrintSizeY = Y;                                995   fPrintSizeY = Y;
981 }                                                 996 }
982                                                   997 
983 /**                                               998 /**
984  If name is "" or "!", filename and extension     999  If name is "" or "!", filename and extension will have the default value.
985  If name is "toto.png", set the name to "toto"    1000  If name is "toto.png", set the name to "toto" and the format to "png". No incremented suffix is added.
986  If name is "toto", set the name to "toto" and    1001  If name is "toto", set the name to "toto" and the format to default (or current format if specify).
987  If name is the same as previous, do not reset    1002  If name is the same as previous, do not reset incremented suffix.
988 */                                                1003 */
989 bool G4OpenGLViewer::setExportFilename(G4Strin    1004 bool G4OpenGLViewer::setExportFilename(G4String name,G4bool inc) {
990   if (name == "!") {                              1005   if (name == "!") {
991     name = "";                                    1006     name = "";
992   }                                               1007   }
993                                                   1008 
994   if (inc) {                                      1009   if (inc) {
995     if ((name != "") && (fExportFilename != na    1010     if ((name != "") && (fExportFilename != name)) {
996       fExportFilenameIndex=0;                     1011       fExportFilenameIndex=0;
997     }                                             1012     }
998   } else {                                        1013   } else {
999     fExportFilenameIndex=-1;                      1014     fExportFilenameIndex=-1;
1000   }                                              1015   }
1001                                                  1016 
1002   if (name.size() == 0) {                        1017   if (name.size() == 0) {
1003     name = getRealPrintFilename().c_str();       1018     name = getRealPrintFilename().c_str();
1004   } else {                                       1019   } else {
1005     // guess format by extention                 1020     // guess format by extention
1006     std::string extension = name.substr(name.    1021     std::string extension = name.substr(name.find_last_of(".") + 1);
1007     // If there is a dot in the name the abov << 1022     // no format
1008     if (extension.size() >= 3 && extension.si << 1023     if (name.size() != extension.size()) {
1009       if (setExportImageFormat(extension, fal << 1024       if (! setExportImageFormat(extension, false)) {
1010         fExportFilename = name.substr(0,name. << 
1011       } else {  // No viable extension found  << 
1012         return false;                            1025         return false;
1013       }                                          1026       }
1014     } else {  // Assume name is already the r << 
1015         fExportFilename = name;               << 
1016     }                                            1027     }
                                                   >> 1028     // get the name
                                                   >> 1029     fExportFilename = name.substr(0,name.find_last_of("."));
1017   }                                              1030   }
1018   return true;                                   1031   return true;
1019 }                                                1032 }
1020                                                  1033 
1021 std::string G4OpenGLViewer::getRealPrintFilen    1034 std::string G4OpenGLViewer::getRealPrintFilename() {
1022   std::string temp = fExportFilename;            1035   std::string temp = fExportFilename;
1023   if (fExportFilenameIndex != -1) {              1036   if (fExportFilenameIndex != -1) {
1024     temp += std::string("_");                    1037     temp += std::string("_");
1025     std::ostringstream os;                       1038     std::ostringstream os;
1026     os << std::setw(4) << std::setfill('0') < << 1039     os << fExportFilenameIndex;
1027     std::string nb_str = os.str();               1040     std::string nb_str = os.str();
1028     temp += nb_str;                              1041     temp += nb_str;
1029   }                                              1042   }
1030   temp += "."+fExportImageFormat;                1043   temp += "."+fExportImageFormat;
1031   return temp;                                   1044   return temp;
1032 }                                                1045 }
1033                                                  1046 
1034 GLdouble G4OpenGLViewer::getSceneNearWidth()     1047 GLdouble G4OpenGLViewer::getSceneNearWidth()
1035 {                                                1048 {
1036   if (!fSceneHandler.GetScene()) {               1049   if (!fSceneHandler.GetScene()) {
1037     return 0;                                    1050     return 0;
1038   }                                              1051   }
1039   const G4Point3D targetPoint                    1052   const G4Point3D targetPoint
1040     = fSceneHandler.GetScene()->GetStandardTa    1053     = fSceneHandler.GetScene()->GetStandardTargetPoint()
1041     + fVP.GetCurrentTargetPoint ();              1054     + fVP.GetCurrentTargetPoint ();
1042   G4double radius = fSceneHandler.GetScene()-    1055   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1043   if(radius<=0.) radius = 1.;                    1056   if(radius<=0.) radius = 1.;
1044   const G4double cameraDistance = fVP.GetCame    1057   const G4double cameraDistance = fVP.GetCameraDistance (radius);
1045   const GLdouble pnear   = fVP.GetNearDistanc    1058   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
1046   return 2 * fVP.GetFrontHalfHeight (pnear, r    1059   return 2 * fVP.GetFrontHalfHeight (pnear, radius);
1047 }                                                1060 }
1048                                                  1061 
1049 GLdouble G4OpenGLViewer::getSceneFarWidth()      1062 GLdouble G4OpenGLViewer::getSceneFarWidth()
1050 {                                                1063 {
1051   if (!fSceneHandler.GetScene()) {               1064   if (!fSceneHandler.GetScene()) {
1052     return 0;                                    1065     return 0;
1053   }                                              1066   }
1054   const G4Point3D targetPoint                    1067   const G4Point3D targetPoint
1055     = fSceneHandler.GetScene()->GetStandardTa    1068     = fSceneHandler.GetScene()->GetStandardTargetPoint()
1056     + fVP.GetCurrentTargetPoint ();              1069     + fVP.GetCurrentTargetPoint ();
1057   G4double radius = fSceneHandler.GetScene()-    1070   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1058   if(radius<=0.) radius = 1.;                    1071   if(radius<=0.) radius = 1.;
1059   const G4double cameraDistance = fVP.GetCame    1072   const G4double cameraDistance = fVP.GetCameraDistance (radius);
1060   const GLdouble pnear   = fVP.GetNearDistanc    1073   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
1061   const GLdouble pfar    = fVP.GetFarDistance    1074   const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
1062   return 2 * fVP.GetFrontHalfHeight (pfar, ra    1075   return 2 * fVP.GetFrontHalfHeight (pfar, radius);
1063 }                                                1076 }
1064                                                  1077 
1065                                                  1078 
1066 GLdouble G4OpenGLViewer::getSceneDepth()         1079 GLdouble G4OpenGLViewer::getSceneDepth()
1067 {                                                1080 {
1068   if (!fSceneHandler.GetScene()) {               1081   if (!fSceneHandler.GetScene()) {
1069     return 0;                                    1082     return 0;
1070   }                                              1083   }
1071   const G4Point3D targetPoint                    1084   const G4Point3D targetPoint
1072     = fSceneHandler.GetScene()->GetStandardTa    1085     = fSceneHandler.GetScene()->GetStandardTargetPoint()
1073     + fVP.GetCurrentTargetPoint ();              1086     + fVP.GetCurrentTargetPoint ();
1074   G4double radius = fSceneHandler.GetScene()-    1087   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
1075   if(radius<=0.) radius = 1.;                    1088   if(radius<=0.) radius = 1.;
1076   const G4double cameraDistance = fVP.GetCame    1089   const G4double cameraDistance = fVP.GetCameraDistance (radius);
1077   const GLdouble pnear   = fVP.GetNearDistanc    1090   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
1078   return fVP.GetFarDistance  (cameraDistance,    1091   return fVP.GetFarDistance  (cameraDistance, pnear, radius)- pnear;
1079 }                                                1092 }
1080                                                  1093 
1081                                                  1094 
1082                                                  1095 
1083 void G4OpenGLViewer::rotateScene(G4double dx,    1096 void G4OpenGLViewer::rotateScene(G4double dx, G4double dy)
1084 {                                                1097 {
1085   if (fVP.GetRotationStyle() == G4ViewParamet    1098   if (fVP.GetRotationStyle() == G4ViewParameters::freeRotation) {
1086     rotateSceneInViewDirection(dx,dy);           1099     rotateSceneInViewDirection(dx,dy);
1087   } else {                                       1100   } else {
1088     if( dx != 0) {                               1101     if( dx != 0) {
1089       rotateSceneThetaPhi(dx,0);                 1102       rotateSceneThetaPhi(dx,0);
1090     }                                            1103     }
1091     if( dy != 0) {                               1104     if( dy != 0) {
1092       rotateSceneThetaPhi(0,dy);                 1105       rotateSceneThetaPhi(0,dy);
1093     }                                            1106     }
1094   }                                              1107   }
1095 }                                                1108 }
1096                                                  1109 
1097                                                  1110 
1098 void G4OpenGLViewer::rotateSceneToggle(G4doub    1111 void G4OpenGLViewer::rotateSceneToggle(G4double dx, G4double dy)
1099 {                                                1112 {
1100   if (fVP.GetRotationStyle() != G4ViewParamet    1113   if (fVP.GetRotationStyle() != G4ViewParameters::freeRotation) {
1101     rotateSceneInViewDirection(dx,dy);           1114     rotateSceneInViewDirection(dx,dy);
1102   } else {                                       1115   } else {
1103     if( dx != 0) {                               1116     if( dx != 0) {
1104       rotateSceneThetaPhi(dx,0);                 1117       rotateSceneThetaPhi(dx,0);
1105     }                                            1118     }
1106     if( dy != 0) {                               1119     if( dy != 0) {
1107       rotateSceneThetaPhi(0,dy);                 1120       rotateSceneThetaPhi(0,dy);
1108     }                                            1121     }
1109   }                                              1122   }
1110 }                                                1123 }
1111                                                  1124 
1112 void G4OpenGLViewer::rotateSceneThetaPhi(G4do    1125 void G4OpenGLViewer::rotateSceneThetaPhi(G4double dx, G4double dy)
1113 {                                                1126 {
1114   if (!fSceneHandler.GetScene()) {               1127   if (!fSceneHandler.GetScene()) {
1115     return;                                      1128     return;
1116   }                                              1129   }
1117                                                  1130 
1118   G4Vector3D vp;                                 1131   G4Vector3D vp;
1119   G4Vector3D up;                                 1132   G4Vector3D up;
1120                                                  1133   
1121   G4Vector3D xprime;                             1134   G4Vector3D xprime;
1122   G4Vector3D yprime;                             1135   G4Vector3D yprime;
1123   G4Vector3D zprime;                             1136   G4Vector3D zprime;
1124                                                  1137   
1125   G4double delta_alpha;                          1138   G4double delta_alpha;
1126   G4double delta_theta;                          1139   G4double delta_theta;
1127                                                  1140   
1128   G4Vector3D new_vp;                             1141   G4Vector3D new_vp;
1129   G4Vector3D new_up;                             1142   G4Vector3D new_up;
1130                                                  1143   
1131   G4double cosalpha;                             1144   G4double cosalpha;
1132   G4double sinalpha;                             1145   G4double sinalpha;
1133                                                  1146   
1134   G4Vector3D a1;                                 1147   G4Vector3D a1;
1135   G4Vector3D a2;                                 1148   G4Vector3D a2;
1136   G4Vector3D delta;                              1149   G4Vector3D delta;
1137   G4Vector3D viewPoint;                          1150   G4Vector3D viewPoint;
1138                                                  1151 
1139                                                  1152     
1140   //phi spin stuff here                          1153   //phi spin stuff here
1141                                                  1154   
1142   vp = fVP.GetViewpointDirection ().unit ();     1155   vp = fVP.GetViewpointDirection ().unit ();
1143   up = fVP.GetUpVector ().unit ();               1156   up = fVP.GetUpVector ().unit ();
1144                                                  1157   
1145   yprime = (up.cross(vp)).unit();                1158   yprime = (up.cross(vp)).unit();
1146   zprime = (vp.cross(yprime)).unit();            1159   zprime = (vp.cross(yprime)).unit();
1147                                                  1160   
1148   if (fVP.GetLightsMoveWithCamera()) {           1161   if (fVP.GetLightsMoveWithCamera()) {
1149     delta_alpha = dy * fRot_sens;                1162     delta_alpha = dy * fRot_sens;
1150     delta_theta = -dx * fRot_sens;               1163     delta_theta = -dx * fRot_sens;
1151   } else {                                       1164   } else {
1152     delta_alpha = -dy * fRot_sens;               1165     delta_alpha = -dy * fRot_sens;
1153     delta_theta = dx * fRot_sens;                1166     delta_theta = dx * fRot_sens;
1154   }                                              1167   }    
1155                                                  1168   
1156   delta_alpha *= CLHEP::deg;                  << 1169   delta_alpha *= deg;
1157   delta_theta *= CLHEP::deg;                  << 1170   delta_theta *= deg;
1158                                                  1171   
1159   new_vp = std::cos(delta_alpha) * vp + std::    1172   new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1160                                                  1173   
1161   // to avoid z rotation flipping                1174   // to avoid z rotation flipping
1162   // to allow more than 360° rotation        << 1175   // to allow more than 360∞ rotation
1163                                                  1176 
1164   if (fVP.GetLightsMoveWithCamera()) {           1177   if (fVP.GetLightsMoveWithCamera()) {
1165     new_up = (new_vp.cross(yprime)).unit();      1178     new_up = (new_vp.cross(yprime)).unit();
1166     if (new_vp.z()*vp.z() <0) {                  1179     if (new_vp.z()*vp.z() <0) {
1167       new_up.set(new_up.x(),-new_up.y(),new_u    1180       new_up.set(new_up.x(),-new_up.y(),new_up.z());
1168     }                                            1181     }
1169   } else {                                       1182   } else {
1170     new_up = up;                                 1183     new_up = up;
1171     if (new_vp.z()*vp.z() <0) {                  1184     if (new_vp.z()*vp.z() <0) {
1172       new_up.set(new_up.x(),-new_up.y(),new_u    1185       new_up.set(new_up.x(),-new_up.y(),new_up.z());
1173     }                                            1186     }
1174   }                                              1187   }
1175   fVP.SetUpVector(new_up);                       1188   fVP.SetUpVector(new_up);
1176   ////////////////                               1189   ////////////////
1177   // Rotates by fixed azimuthal angle delta_t    1190   // Rotates by fixed azimuthal angle delta_theta.
1178                                                  1191   
1179   cosalpha = new_up.dot (new_vp.unit());         1192   cosalpha = new_up.dot (new_vp.unit());
1180   sinalpha = std::sqrt (1. - std::pow (cosalp    1193   sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1181   yprime = (new_up.cross (new_vp.unit())).uni    1194   yprime = (new_up.cross (new_vp.unit())).unit ();
1182   xprime = yprime.cross (new_up);                1195   xprime = yprime.cross (new_up);
1183   // Projection of vp on plane perpendicular     1196   // Projection of vp on plane perpendicular to up...
1184   a1 = sinalpha * xprime;                        1197   a1 = sinalpha * xprime;
1185   // Required new projection...                  1198   // Required new projection...
1186   a2 = sinalpha * (std::cos (delta_theta) * x    1199   a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1187   // Required Increment vector...                1200   // Required Increment vector...
1188   delta = a2 - a1;                               1201   delta = a2 - a1;
1189   // So new viewpoint is...                      1202   // So new viewpoint is...
1190   viewPoint = new_vp.unit() + delta;             1203   viewPoint = new_vp.unit() + delta;
1191                                                  1204   
1192   fVP.SetViewAndLights (viewPoint);              1205   fVP.SetViewAndLights (viewPoint);
1193 }                                                1206 }
1194                                                  1207 
1195                                                  1208 
1196 void G4OpenGLViewer::rotateSceneInViewDirecti    1209 void G4OpenGLViewer::rotateSceneInViewDirection(G4double dx, G4double dy)
1197 {                                                1210 {
1198   if (!fSceneHandler.GetScene()) {               1211   if (!fSceneHandler.GetScene()) {
1199     return;                                      1212     return;
1200   }                                              1213   }
1201                                                  1214 
1202   G4Vector3D vp;                                 1215   G4Vector3D vp;
1203   G4Vector3D up;                                 1216   G4Vector3D up;
1204                                                  1217   
1205   G4Vector3D xprime;                             1218   G4Vector3D xprime;
1206   G4Vector3D yprime;                             1219   G4Vector3D yprime;
1207   G4Vector3D zprime;                             1220   G4Vector3D zprime;
1208                                                  1221   
1209   G4Vector3D new_vp;                             1222   G4Vector3D new_vp;
1210   G4Vector3D new_up;                             1223   G4Vector3D new_up;
1211                                                  1224   
1212   G4Vector3D a1;                                 1225   G4Vector3D a1;
1213   G4Vector3D a2;                                 1226   G4Vector3D a2;
1214   G4Vector3D delta;                              1227   G4Vector3D delta;
1215   G4Vector3D viewPoint;                          1228   G4Vector3D viewPoint;
1216                                                  1229 
1217   dx = dx/100;                                   1230   dx = dx/100;
1218   dy = dy/100;                                   1231   dy = dy/100;
1219                                                  1232 
1220   //phi spin stuff here                          1233   //phi spin stuff here
1221                                                  1234   
1222   vp = fVP.GetViewpointDirection ().unit();      1235   vp = fVP.GetViewpointDirection ().unit();
1223   up = fVP.GetUpVector ().unit();                1236   up = fVP.GetUpVector ().unit();
1224                                                  1237 
1225   G4Vector3D zPrimeVector = G4Vector3D(up.y()    1238   G4Vector3D zPrimeVector = G4Vector3D(up.y()*vp.z()-up.z()*vp.y(),
1226                              up.z()*vp.x()-up    1239                              up.z()*vp.x()-up.x()*vp.z(),
1227                              up.x()*vp.y()-up    1240                              up.x()*vp.y()-up.y()*vp.x());
1228                                                  1241 
1229   viewPoint = vp/fRot_sens + (zPrimeVector*dx    1242   viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
1230   new_up = G4Vector3D(viewPoint.y()*zPrimeVec    1243   new_up = G4Vector3D(viewPoint.y()*zPrimeVector.z()-viewPoint.z()*zPrimeVector.y(),
1231                        viewPoint.z()*zPrimeVe    1244                        viewPoint.z()*zPrimeVector.x()-viewPoint.x()*zPrimeVector.z(),
1232                        viewPoint.x()*zPrimeVe    1245                        viewPoint.x()*zPrimeVector.y()-viewPoint.y()*zPrimeVector.x());
1233                                                  1246 
1234   G4Vector3D new_upUnit = new_up.unit();         1247   G4Vector3D new_upUnit = new_up.unit();
1235                                                  1248   
1236                                                  1249   
1237                                                  1250 
1238    fVP.SetUpVector(new_upUnit);                  1251    fVP.SetUpVector(new_upUnit);
1239    fVP.SetViewAndLights (viewPoint);             1252    fVP.SetViewAndLights (viewPoint);
1240 }                                                1253 }
1241                                                  1254 
1242                                                  1255 
1243 void G4OpenGLViewer::addExportImageFormat(std    1256 void G4OpenGLViewer::addExportImageFormat(std::string format) {
1244   fExportImageFormatVector.push_back(format);    1257   fExportImageFormatVector.push_back(format);
1245 }                                                1258 }
1246                                                  1259 
1247 bool G4OpenGLViewer::setExportImageFormat(std    1260 bool G4OpenGLViewer::setExportImageFormat(std::string format, bool quiet) {
1248   bool found = false;                            1261   bool found = false;
1249   std::string list;                              1262   std::string list;
1250   for (unsigned int a=0; a<fExportImageFormat    1263   for (unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
1251     list +=fExportImageFormatVector.at(a) + "    1264     list +=fExportImageFormatVector.at(a) + " ";
1252                                                  1265 
1253     if (fExportImageFormatVector.at(a) == for    1266     if (fExportImageFormatVector.at(a) == format) {
1254       if (! quiet) {                             1267       if (! quiet) {
1255         G4cout << " Changing export format to    1268         G4cout << " Changing export format to \"" << format << "\"" << G4endl;
1256       }                                          1269       }
1257       if (format != fExportImageFormat) {        1270       if (format != fExportImageFormat) {
1258         fExportFilenameIndex = 0;                1271         fExportFilenameIndex = 0;
1259         fExportImageFormat = format;             1272         fExportImageFormat = format;
1260       }                                          1273       }
1261       return true;                               1274       return true;
1262     }                                            1275     }
1263   }                                              1276   }
1264   if (! found) {                                 1277   if (! found) {
1265     if (format.size() == 0) {                    1278     if (format.size() == 0) {
1266       G4cout << " Current formats availables     1279       G4cout << " Current formats availables are : " << list << G4endl;
1267     } else {                                     1280     } else {
1268       G4cerr << " Format \"" << format << "\"    1281       G4cerr << " Format \"" << format << "\" is not available for the selected viewer. Current formats availables are : " << list << G4endl;
1269     }                                            1282     }
1270   }                                              1283   }
1271   return false;                                  1284   return false;
1272 }                                                1285 }
1273                                                  1286 
1274                                                  1287 
1275 // From MESA implementation :                    1288 // From MESA implementation :
1276 // http://www.techques.com/question/1-8660454    1289 // http://www.techques.com/question/1-8660454/gluPickMatrix-code-from-Mesa
1277                                                  1290 
1278 void G4OpenGLViewer::g4GluPickMatrix(GLdouble    1291 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
1279                      GLint viewport[4])          1292                      GLint viewport[4])
1280   {                                              1293   {
1281     GLdouble mat[16];                         << 1294     GLfloat mat[16];
1282     GLdouble sx, sy;                          << 1295     GLfloat sx, sy;
1283     GLdouble tx, ty;                          << 1296     GLfloat tx, ty;
1284                                                  1297     
1285     sx = viewport[2] / width;                    1298     sx = viewport[2] / width;
1286     sy = viewport[3] / height;                   1299     sy = viewport[3] / height;
1287     tx = (viewport[2] + 2.0 * (viewport[0] -     1300     tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
1288     ty = (viewport[3] + 2.0 * (viewport[1] -     1301     ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
1289                                                  1302     
1290 #define M(row, col) mat[col*4+row]               1303 #define M(row, col) mat[col*4+row]
1291     M(0, 0) = sx;                                1304     M(0, 0) = sx;
1292     M(0, 1) = 0.0;                               1305     M(0, 1) = 0.0;
1293     M(0, 2) = 0.0;                               1306     M(0, 2) = 0.0;
1294     M(0, 3) = tx;                                1307     M(0, 3) = tx;
1295     M(1, 0) = 0.0;                               1308     M(1, 0) = 0.0;
1296     M(1, 1) = sy;                                1309     M(1, 1) = sy;
1297     M(1, 2) = 0.0;                               1310     M(1, 2) = 0.0;
1298     M(1, 3) = ty;                                1311     M(1, 3) = ty;
1299     M(2, 0) = 0.0;                               1312     M(2, 0) = 0.0;
1300     M(2, 1) = 0.0;                               1313     M(2, 1) = 0.0;
1301     M(2, 2) = 1.0;                               1314     M(2, 2) = 1.0;
1302     M(2, 3) = 0.0;                               1315     M(2, 3) = 0.0;
1303     M(3, 0) = 0.0;                               1316     M(3, 0) = 0.0;
1304     M(3, 1) = 0.0;                               1317     M(3, 1) = 0.0;
1305     M(3, 2) = 0.0;                               1318     M(3, 2) = 0.0;
1306     M(3, 3) = 1.0;                               1319     M(3, 3) = 1.0;
1307 #undef M                                         1320 #undef M
1308                                                  1321     
1309     glMultMatrixd(mat);                       << 1322     glMultMatrixf(mat);
1310 }                                                1323 }
1311                                                  1324 
1312                                                  1325 
1313                                                  1326 
1314                                                  1327 
1315                                                  1328 
1316 // From MESA implementation :                    1329 // From MESA implementation :
1317 // https://github.com/jlamarche/iOS-OpenGLES-    1330 // https://github.com/jlamarche/iOS-OpenGLES-Stuff/blob/master/Wavefront%20OBJ%20Loader/Classes/gluLookAt.m
1318 // or http://www.daniweb.com/software-develop    1331 // or http://www.daniweb.com/software-development/game-development/threads/308901/lookat-matrix-source-code
1319                                                  1332 
1320 void G4OpenGLViewer::g4GluLookAt( GLdouble ey    1333 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
1321                         GLdouble centerx, GLd    1334                         GLdouble centerx, GLdouble centery, GLdouble
1322                         centerz,                 1335                         centerz,
1323                         GLdouble upx, GLdoubl    1336                         GLdouble upx, GLdouble upy, GLdouble upz )
1324 {                                                1337 {
1325   GLdouble mat[16];                           << 1338   GLfloat mat[16];
1326   GLdouble x[3], y[3], z[3];                  << 1339   GLfloat x[3], y[3], z[3];
1327   GLdouble mag;                               << 1340   GLfloat mag;
1328                                                  1341   
1329   /* Make rotation matrix */                     1342   /* Make rotation matrix */
1330                                                  1343   
1331   /* Z vector */                                 1344   /* Z vector */
1332   z[0] = eyex - centerx;                         1345   z[0] = eyex - centerx;
1333   z[1] = eyey - centery;                         1346   z[1] = eyey - centery;
1334   z[2] = eyez - centerz;                         1347   z[2] = eyez - centerz;
1335   mag = std::sqrt(z[0] * z[0] + z[1] * z[1] +    1348   mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
1336   if (mag) {      /* mpichler, 19950515 */       1349   if (mag) {      /* mpichler, 19950515 */
1337     z[0] /= mag;                                 1350     z[0] /= mag;
1338     z[1] /= mag;                                 1351     z[1] /= mag;
1339     z[2] /= mag;                                 1352     z[2] /= mag;
1340   }                                              1353   }
1341                                                  1354   
1342   /* Y vector */                                 1355   /* Y vector */
1343   y[0] = upx;                                    1356   y[0] = upx;
1344   y[1] = upy;                                    1357   y[1] = upy;
1345   y[2] = upz;                                    1358   y[2] = upz;
1346                                                  1359   
1347   /* X vector = Y cross Z */                     1360   /* X vector = Y cross Z */
1348   x[0] = y[1] * z[2] - y[2] * z[1];              1361   x[0] = y[1] * z[2] - y[2] * z[1];
1349   x[1] = -y[0] * z[2] + y[2] * z[0];             1362   x[1] = -y[0] * z[2] + y[2] * z[0];
1350   x[2] = y[0] * z[1] - y[1] * z[0];              1363   x[2] = y[0] * z[1] - y[1] * z[0];
1351                                                  1364   
1352   /* Recompute Y = Z cross X */                  1365   /* Recompute Y = Z cross X */
1353   y[0] = z[1] * x[2] - z[2] * x[1];              1366   y[0] = z[1] * x[2] - z[2] * x[1];
1354   y[1] = -z[0] * x[2] + z[2] * x[0];             1367   y[1] = -z[0] * x[2] + z[2] * x[0];
1355   y[2] = z[0] * x[1] - z[1] * x[0];              1368   y[2] = z[0] * x[1] - z[1] * x[0];
1356                                                  1369   
1357   /* mpichler, 19950515 */                       1370   /* mpichler, 19950515 */
1358   /* cross product gives area of parallelogra    1371   /* cross product gives area of parallelogram, which is < 1.0 for
1359    * non-perpendicular unit-length vectors; s    1372    * non-perpendicular unit-length vectors; so normalize x, y here
1360    */                                            1373    */
1361                                                  1374   
1362   mag = std::sqrt(x[0] * x[0] + x[1] * x[1] +    1375   mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
1363   if (mag) {                                     1376   if (mag) {
1364     x[0] /= mag;                                 1377     x[0] /= mag;
1365     x[1] /= mag;                                 1378     x[1] /= mag;
1366     x[2] /= mag;                                 1379     x[2] /= mag;
1367   }                                              1380   }
1368                                                  1381   
1369   mag = std::sqrt(y[0] * y[0] + y[1] * y[1] +    1382   mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
1370   if (mag) {                                     1383   if (mag) {
1371     y[0] /= mag;                                 1384     y[0] /= mag;
1372     y[1] /= mag;                                 1385     y[1] /= mag;
1373     y[2] /= mag;                                 1386     y[2] /= mag;
1374   }                                              1387   }
1375                                                  1388   
1376 #define M(row,col)  mat[col*4+row]               1389 #define M(row,col)  mat[col*4+row]
1377   M(0, 0) = x[0];                                1390   M(0, 0) = x[0];
1378   M(0, 1) = x[1];                                1391   M(0, 1) = x[1];
1379   M(0, 2) = x[2];                                1392   M(0, 2) = x[2];
1380   M(0, 3) = 0.0;                                 1393   M(0, 3) = 0.0;
1381   M(1, 0) = y[0];                                1394   M(1, 0) = y[0];
1382   M(1, 1) = y[1];                                1395   M(1, 1) = y[1];
1383   M(1, 2) = y[2];                                1396   M(1, 2) = y[2];
1384   M(1, 3) = 0.0;                                 1397   M(1, 3) = 0.0;
1385   M(2, 0) = z[0];                                1398   M(2, 0) = z[0];
1386   M(2, 1) = z[1];                                1399   M(2, 1) = z[1];
1387   M(2, 2) = z[2];                                1400   M(2, 2) = z[2];
1388   M(2, 3) = 0.0;                                 1401   M(2, 3) = 0.0;
1389   M(3, 0) = 0.0;                                 1402   M(3, 0) = 0.0;
1390   M(3, 1) = 0.0;                                 1403   M(3, 1) = 0.0;
1391   M(3, 2) = 0.0;                                 1404   M(3, 2) = 0.0;
1392   M(3, 3) = 1.0;                                 1405   M(3, 3) = 1.0;
1393 #undef M                                         1406 #undef M
1394   glMultMatrixd(mat);                         << 1407   glMultMatrixf(mat);
1395                                                  1408   
1396   /* Translate Eye to Origin */                  1409   /* Translate Eye to Origin */
1397   glTranslated(-eyex, -eyey, -eyez);          << 1410   glTranslatef(-eyex, -eyey, -eyez);
1398 }                                                1411 }
1399                                                  1412 
1400 void G4OpenGLViewer::g4GlOrtho (GLdouble left    1413 void G4OpenGLViewer::g4GlOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1401   //  glOrtho (left, right, bottom, top, near    1414   //  glOrtho (left, right, bottom, top, near, far);
1402                                                  1415   
1403   GLdouble a = 2.0 / (right - left);             1416   GLdouble a = 2.0 / (right - left);
1404   GLdouble b = 2.0 / (top - bottom);             1417   GLdouble b = 2.0 / (top - bottom);
1405   GLdouble c = -2.0 / (zFar - zNear);            1418   GLdouble c = -2.0 / (zFar - zNear);
1406                                                  1419   
1407   GLdouble tx = - (right + left)/(right - lef    1420   GLdouble tx = - (right + left)/(right - left);
1408   GLdouble ty = - (top + bottom)/(top - botto    1421   GLdouble ty = - (top + bottom)/(top - bottom);
1409   GLdouble tz = - (zFar + zNear)/(zFar - zNea    1422   GLdouble tz = - (zFar + zNear)/(zFar - zNear);
1410                                                  1423   
1411   GLdouble ortho[16] = {                         1424   GLdouble ortho[16] = {
1412     a, 0, 0, 0,                                  1425     a, 0, 0, 0,
1413     0, b, 0, 0,                                  1426     0, b, 0, 0,
1414     0, 0, c, 0,                                  1427     0, 0, c, 0,
1415     tx, ty, tz, 1                                1428     tx, ty, tz, 1
1416   };                                             1429   };
1417   glMultMatrixd(ortho);                          1430   glMultMatrixd(ortho);
1418                                                  1431   
1419 }                                                1432 }
1420                                                  1433 
1421                                                  1434 
1422 void G4OpenGLViewer::g4GlFrustum (GLdouble le    1435 void G4OpenGLViewer::g4GlFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) {
1423   //  glFrustum (left, right, bottom, top, ne    1436   //  glFrustum (left, right, bottom, top, near, far);
1424                                                  1437   
1425   GLdouble deltaX = right - left;                1438   GLdouble deltaX = right - left;
1426   GLdouble deltaY = top - bottom;                1439   GLdouble deltaY = top - bottom;
1427   GLdouble deltaZ = zFar - zNear;                1440   GLdouble deltaZ = zFar - zNear;
1428                                                  1441   
1429   GLdouble a = 2.0f * zNear / deltaX;            1442   GLdouble a = 2.0f * zNear / deltaX;
1430   GLdouble b = 2.0f * zNear / deltaY;            1443   GLdouble b = 2.0f * zNear / deltaY;
1431   GLdouble c = (right + left) / deltaX;          1444   GLdouble c = (right + left) / deltaX;
1432   GLdouble d = (top + bottom) / deltaY;          1445   GLdouble d = (top + bottom) / deltaY;
1433   GLdouble e = -(zFar + zNear) / (zFar - zNea    1446   GLdouble e = -(zFar + zNear) / (zFar - zNear);
1434   GLdouble f = -2.0f * zFar * zNear / deltaZ;    1447   GLdouble f = -2.0f * zFar * zNear / deltaZ;
1435                                                  1448   
1436   GLdouble proj[16] = {                          1449   GLdouble proj[16] = {
1437     a, 0, 0, 0,                                  1450     a, 0, 0, 0,
1438     0, b, 0, 0,                                  1451     0, b, 0, 0,
1439     c, d, e, -1.0f,                              1452     c, d, e, -1.0f,
1440     0, 0, f, 0                                   1453     0, 0, f, 0
1441   };                                             1454   };
1442                                                  1455   
1443   glMultMatrixd(proj);                           1456   glMultMatrixd(proj);
1444                                                  1457   
1445 }                                                1458 }
1446                                                  1459 
                                                   >> 1460 
                                                   >> 1461 #ifdef G4OPENGL_VERSION_2
                                                   >> 1462 
                                                   >> 1463 // Associate the VBO drawer to the OpenGLViewer and the OpenGLSceneHandler
                                                   >> 1464 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
                                                   >> 1465   fVboDrawer = drawer;
                                                   >> 1466   try {
                                                   >> 1467     G4OpenGLSceneHandler& sh = dynamic_cast<G4OpenGLSceneHandler&>(fSceneHandler);
                                                   >> 1468     sh.setVboDrawer(fVboDrawer);
                                                   >> 1469   } catch(std::bad_cast exp) { }
                                                   >> 1470 }
                                                   >> 1471 
                                                   >> 1472 #endif
                                                   >> 1473 
                                                   >> 1474 
1447 G4String G4OpenGLViewerPickMap::print() {        1475 G4String G4OpenGLViewerPickMap::print() {
1448   std::ostringstream txt;                        1476   std::ostringstream txt;
                                                   >> 1477 
                                                   >> 1478   txt << fName;
                                                   >> 1479 
                                                   >> 1480   txt << "Hit: " << fHitNumber << ", Sub-hit: " << fSubHitNumber << ", PickName: " << fPickName << "\n";
                                                   >> 1481   
1449   for (unsigned int a=0; a<fAttributes.size()    1482   for (unsigned int a=0; a<fAttributes.size(); a++) {
1450     txt << fAttributes[a];                    << 1483     txt << fAttributes[a] << "\n";
1451     if (a < fAttributes.size() - 1) txt << "\ << 
1452   }                                              1484   }
1453   return txt.str();                              1485   return txt.str();
1454 }                                                1486 }
                                                   >> 1487 
                                                   >> 1488 #endif
1455                                                  1489