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