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 #ifdef G4VIS_BUILD_OI_DRIVER 26 27 27 /*----------------------------HEPVis---------- 28 /*----------------------------HEPVis----------------------------------------*/ 28 /* 29 /* */ 29 /* Node: SoGL2PSAction 30 /* Node: SoGL2PSAction */ 30 /* Author: Guy Barrand 31 /* Author: Guy Barrand */ 31 /* 32 /* */ 32 /*-------------------------------------------- 33 /*--------------------------------------------------------------------------*/ 33 34 34 // this : 35 // this : 35 #include <HEPVis/actions/SoGL2PSAction.h> 36 #include <HEPVis/actions/SoGL2PSAction.h> 36 37 37 // Inventor : 38 // Inventor : 38 #include <Inventor/elements/SoViewportRegionEl 39 #include <Inventor/elements/SoViewportRegionElement.h> 39 #include <Inventor/errors/SoDebugError.h> 40 #include <Inventor/errors/SoDebugError.h> 40 41 41 #include <Inventor/system/gl.h> << 42 #include "Geant4_gl2ps.h" 42 43 43 #include <stdio.h> 44 #include <stdio.h> 44 45 45 SO_ACTION_SOURCE(SoGL2PSAction) 46 SO_ACTION_SOURCE(SoGL2PSAction) 46 ////////////////////////////////////////////// 47 ////////////////////////////////////////////////////////////////////////////// 47 void SoGL2PSAction::initClass( 48 void SoGL2PSAction::initClass( 48 ) 49 ) 49 ////////////////////////////////////////////// 50 ////////////////////////////////////////////////////////////////////////////// 50 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 51 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 51 { 52 { 52 static bool first = true; 53 static bool first = true; 53 if (first) { 54 if (first) { 54 first = false; 55 first = false; 55 SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRen 56 SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRenderAction); 56 } 57 } 57 } 58 } 58 ////////////////////////////////////////////// 59 ////////////////////////////////////////////////////////////////////////////// 59 SoGL2PSAction::SoGL2PSAction( 60 SoGL2PSAction::SoGL2PSAction( 60 const SbViewportRegion& aViewPortRegion 61 const SbViewportRegion& aViewPortRegion 61 ) 62 ) 62 :SoGLRenderAction(aViewPortRegion) 63 :SoGLRenderAction(aViewPortRegion) 63 ,fContext(0) << 64 ,G4OpenGL2PSAction() 64 ,fFile(0) << 65 ,fFileName("out.pdf") << 66 ,fTitle("title") << 67 ,fProducer("HEPVis::SoGL2PSAction") << 68 ,fFormat(TOOLS_GL2PS_PDF) << 69 ////////////////////////////////////////////// 65 ////////////////////////////////////////////////////////////////////////////// 70 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 66 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 71 { 67 { >> 68 setFileName("out.ps"); 72 SO_ACTION_CONSTRUCTOR(SoGL2PSAction); 69 SO_ACTION_CONSTRUCTOR(SoGL2PSAction); 73 } 70 } 74 71 75 ////////////////////////////////////////////// << 72 bool SoGL2PSAction::enableFileWriting( 76 SoGL2PSAction::~SoGL2PSAction() << 73 ) 77 ////////////////////////////////////////////// << 78 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 79 { << 80 closeFile(); << 81 } << 82 << 83 ////////////////////////////////////////////// << 84 void SoGL2PSAction::setFileName(const std::str << 85 ////////////////////////////////////////////// << 86 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 87 { << 88 fFileName = aFileName; << 89 } << 90 << 91 ////////////////////////////////////////////// << 92 void SoGL2PSAction::setTitleAndProducer(const << 93 ////////////////////////////////////////////// << 94 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 95 { << 96 fTitle = aTitle; << 97 fProducer = aProducer; << 98 } << 99 << 100 void SoGL2PSAction::setExportImageFormat_PS() << 101 void SoGL2PSAction::setExportImageFormat_EPS() << 102 void SoGL2PSAction::setExportImageFormat_TEX() << 103 void SoGL2PSAction::setExportImageFormat_PDF() << 104 void SoGL2PSAction::setExportImageFormat_SVG() << 105 void SoGL2PSAction::setExportImageFormat_PGF() << 106 << 107 ////////////////////////////////////////////// << 108 bool SoGL2PSAction::enableFileWriting() << 109 ////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////// 110 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 75 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 111 { 76 { 112 if(!openFile()) { << 77 fFile = ::fopen(fFileName,"w"); >> 78 if(!fFile) { 113 SoDebugError::post("SoGL2PSAction::enableF 79 SoDebugError::post("SoGL2PSAction::enableFileWriting", 114 "openFile() failed for << 80 "Cannot open file %s",fFileName.c_str()); 115 fFileName.c_str()); << 116 return false; 81 return false; 117 } 82 } 118 #ifdef __COIN__ 83 #ifdef __COIN__ 119 #else //SGI 84 #else //SGI 120 const SbViewportRegion& vpr = getViewportReg 85 const SbViewportRegion& vpr = getViewportRegion(); 121 SoViewportRegionElement::set(getState(),vpr) 86 SoViewportRegionElement::set(getState(),vpr); 122 SbVec2s origin = vpr.getViewportOriginPixels << 87 G4gl2psBegin(); 123 SbVec2s size = vpr.getViewportSizePixels(); << 124 if(!beginPage(origin[0],origin[1],size[0],si << 125 SoDebugError::post("SoGL2PSAction::enableF << 126 return false; << 127 } << 128 #endif 88 #endif 129 return true; 89 return true; 130 } 90 } 131 ////////////////////////////////////////////// 91 ////////////////////////////////////////////////////////////////////////////// 132 void SoGL2PSAction::disableFileWriting( 92 void SoGL2PSAction::disableFileWriting( 133 ) 93 ) 134 ////////////////////////////////////////////// 94 ////////////////////////////////////////////////////////////////////////////// 135 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 95 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 136 { 96 { 137 #ifdef __COIN__ 97 #ifdef __COIN__ 138 #else //SGI 98 #else //SGI 139 endPage(); << 99 gl2psEndPage(); 140 #endif 100 #endif 141 closeFile(); << 101 ::fclose(fFile); 142 } << 102 fFile = 0; 143 << 144 ////////////////////////////////////////////// << 145 void SoGL2PSAction::beginTraversal( << 146 SoNode* aNode << 147 ) << 148 ////////////////////////////////////////////// << 149 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 150 { << 151 if(fContext && fFile) { << 152 #ifdef __COIN__ << 153 const SbViewportRegion& vpr = getViewportR << 154 SoViewportRegionElement::set(getState(),vp << 155 SbVec2s origin = vpr.getViewportOriginPixe << 156 SbVec2s size = vpr.getViewportSizePixels() << 157 if(!beginPage(origin[0],origin[1],size[0], << 158 SoDebugError::post("SoGL2PSAction::begin << 159 return; << 160 } << 161 traverse(aNode); << 162 if(!endPage()) { << 163 SoDebugError::post("SoGL2PSAction::begin << 164 return; << 165 } << 166 #else //SGI << 167 SoGLRenderAction::beginTraversal(aNode); << 168 #endif << 169 } else { << 170 SoGLRenderAction::beginTraversal(aNode); << 171 } << 172 } << 173 << 174 #include <tools/gl2ps> << 175 << 176 ////////////////////////////////////////////// << 177 bool SoGL2PSAction::openFile() << 178 ////////////////////////////////////////////// << 179 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 180 { << 181 if(fFile) { << 182 ::fclose(fFile); << 183 fFile = 0; << 184 } << 185 if(fContext) { << 186 ::tools_gl2psDeleteContext(fContext); << 187 fContext = 0; << 188 } << 189 << 190 fContext = ::tools_gl2psCreateContext(); << 191 if(!fContext) return false; << 192 << 193 tools_gl2ps_gl_funcs_t _funcs = { << 194 (tools_glIsEnabled_func)glIsEnabled, << 195 (tools_glBegin_func)glBegin, << 196 (tools_glEnd_func)glEnd, << 197 (tools_glGetFloatv_func)glGetFloatv, << 198 (tools_glVertex3f_func)glVertex3f, << 199 (tools_glGetBooleanv_func)glGetBooleanv, << 200 (tools_glGetIntegerv_func)glGetIntegerv, << 201 (tools_glRenderMode_func)glRenderMode, << 202 (tools_glFeedbackBuffer_func)glFeedbackBuf << 203 (tools_glPassThrough_func)glPassThrough << 204 }; << 205 ::tools_gl2ps_set_gl_funcs(fContext,&_funcs) << 206 << 207 fFile = ::fopen(fFileName.c_str(),"wb"); << 208 if(!fFile) { << 209 ::tools_gl2psDeleteContext(fContext); << 210 fContext = 0; << 211 return false; << 212 } << 213 << 214 return true; << 215 } << 216 << 217 ////////////////////////////////////////////// << 218 void SoGL2PSAction::closeFile() << 219 ////////////////////////////////////////////// << 220 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 221 { << 222 if(fFile) { << 223 ::fclose(fFile); << 224 fFile = 0; << 225 } << 226 if(fContext) { << 227 ::tools_gl2psDeleteContext(fContext); << 228 fContext = 0; << 229 } << 230 } << 231 << 232 ////////////////////////////////////////////// << 233 bool SoGL2PSAction::beginPage(int a_x,int a_y, << 234 ////////////////////////////////////////////// << 235 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 236 { << 237 if(!fContext) return false; << 238 if(!fFile) return false; << 239 << 240 if( (a_w<=0) || (a_h<=0) ) return false; << 241 << 242 int options = << 243 TOOLS_GL2PS_BEST_ROOT | << 244 TOOLS_GL2PS_DRAW_BACKGROUND | << 245 TOOLS_GL2PS_USE_CURRENT_VIEWPORT; << 246 int sort = TOOLS_GL2PS_BSP_SORT; << 247 << 248 int vp[4]; << 249 vp[0] = a_x; << 250 vp[1] = a_y; << 251 vp[2] = a_w; << 252 vp[3] = a_h; << 253 << 254 int bufferSize = 0; << 255 << 256 tools_GLint res = ::tools_gl2psBeginPage << 257 (fContext,fTitle.c_str(),fProducer.c_str() << 258 vp,fFormat,sort,options,TOOLS_GL_RGBA,0, << 259 bufferSize,fFile,fFileName.c_str()); << 260 if (res == TOOLS_GL2PS_ERROR) return false; << 261 << 262 // enable blending for all << 263 ::tools_gl2psEnable(fContext,TOOLS_GL2PS_BLE << 264 << 265 return true; << 266 } << 267 << 268 ////////////////////////////////////////////// << 269 bool SoGL2PSAction::endPage() << 270 ////////////////////////////////////////////// << 271 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 272 { << 273 int _status = 0; << 274 if(fContext) { << 275 _status = ::tools_gl2psEndPage(fContext); << 276 } << 277 if (_status == TOOLS_GL2PS_OVERFLOW) return << 278 return true; << 279 } 103 } 280 104 281 ////////////////////////////////////////////// 105 ////////////////////////////////////////////////////////////////////////////// 282 bool SoGL2PSAction::addBitmap( 106 bool SoGL2PSAction::addBitmap( 283 int aWidth 107 int aWidth 284 ,int aHeight 108 ,int aHeight 285 ,float aXorig 109 ,float aXorig 286 ,float aYorig 110 ,float aYorig 287 ,float aXmove 111 ,float aXmove 288 ,float aYmove 112 ,float aYmove 289 ) 113 ) 290 ////////////////////////////////////////////// 114 ///////////////////////////////////////////////////////////////////////////// 291 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 115 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 292 { 116 { 293 if(!fContext) return false; << 117 if(!fFile) return false; 294 GLboolean valid; 118 GLboolean valid; 295 ::glGetBooleanv(GL_CURRENT_RASTER_POSITION_V << 119 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID,&valid); 296 if(!valid) return false; 120 if(!valid) return false; 297 float pos[4]; 121 float pos[4]; 298 ::glGetFloatv(GL_CURRENT_RASTER_POSITION,pos << 122 glGetFloatv(GL_CURRENT_RASTER_POSITION,pos); 299 int xoff = -(int)(aXmove + aXorig); 123 int xoff = -(int)(aXmove + aXorig); 300 int yoff = -(int)(aYmove + aYorig); 124 int yoff = -(int)(aYmove + aYorig); 301 int x = (int)(pos[0] + xoff); 125 int x = (int)(pos[0] + xoff); 302 int y = (int)(pos[1] + yoff); 126 int y = (int)(pos[1] + yoff); 303 // Should clip against viewport area : 127 // Should clip against viewport area : 304 GLint vp[4]; 128 GLint vp[4]; 305 ::glGetIntegerv(GL_VIEWPORT,vp); << 129 glGetIntegerv(GL_VIEWPORT,vp); 306 GLsizei w = aWidth; 130 GLsizei w = aWidth; 307 GLsizei h = aHeight; 131 GLsizei h = aHeight; 308 if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x; 132 if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x; 309 if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y; 133 if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y; 310 int s = 3 * w * h; 134 int s = 3 * w * h; 311 if(s<=0) return false; 135 if(s<=0) return false; 312 float* image = (float*)::malloc(s * sizeof(f 136 float* image = (float*)::malloc(s * sizeof(float)); 313 if(!image) return false; 137 if(!image) return false; 314 ::glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image << 138 glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image); 315 GLint status = ::tools_gl2psDrawPixels(fCont << 139 GLint status = gl2psDrawPixels(w,h,xoff,yoff,GL_RGB,GL_FLOAT,image); 316 ::free(image); 140 ::free(image); 317 return (status!=TOOLS_GL2PS_SUCCESS ? false << 141 return (status!=GL2PS_SUCCESS ? false : true); >> 142 } >> 143 ////////////////////////////////////////////////////////////////////////////// >> 144 void SoGL2PSAction::beginTraversal( >> 145 SoNode* aNode >> 146 ) >> 147 ////////////////////////////////////////////////////////////////////////////// >> 148 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// >> 149 { >> 150 if(fFile) { >> 151 #ifdef __COIN__ >> 152 const SbViewportRegion& vpr = getViewportRegion(); >> 153 SoViewportRegionElement::set(getState(),vpr); >> 154 G4gl2psBegin(); >> 155 traverse(aNode); >> 156 gl2psEndPage(); >> 157 #else //SGI >> 158 // Should have already do G4gl2psBegin() before >> 159 SoGLRenderAction::beginTraversal(aNode); >> 160 // Should do gl2psEndPage() after >> 161 #endif >> 162 } else { >> 163 SoGLRenderAction::beginTraversal(aNode); >> 164 } 318 } 165 } 319 166 >> 167 #endif 320 168