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.6)


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