Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // >> 23 #ifdef G4VIS_BUILD_OI_DRIVER 26 24 27 /*----------------------------HEPVis---------- 25 /*----------------------------HEPVis----------------------------------------*/ 28 /* 26 /* */ 29 /* Node: SoGL2PSAction 27 /* Node: SoGL2PSAction */ 30 /* Author: Guy Barrand 28 /* Author: Guy Barrand */ 31 /* 29 /* */ 32 /*-------------------------------------------- 30 /*--------------------------------------------------------------------------*/ 33 31 34 // this : 32 // this : 35 #include <HEPVis/actions/SoGL2PSAction.h> 33 #include <HEPVis/actions/SoGL2PSAction.h> 36 34 37 // Inventor : 35 // Inventor : 38 #include <Inventor/elements/SoViewportRegionEl 36 #include <Inventor/elements/SoViewportRegionElement.h> 39 #include <Inventor/errors/SoDebugError.h> 37 #include <Inventor/errors/SoDebugError.h> 40 38 41 #include <Inventor/system/gl.h> << 39 #include "Geant4_gl2ps.h" 42 40 43 #include <stdio.h> 41 #include <stdio.h> 44 42 45 SO_ACTION_SOURCE(SoGL2PSAction) 43 SO_ACTION_SOURCE(SoGL2PSAction) 46 ////////////////////////////////////////////// 44 ////////////////////////////////////////////////////////////////////////////// 47 void SoGL2PSAction::initClass( 45 void SoGL2PSAction::initClass( 48 ) 46 ) 49 ////////////////////////////////////////////// 47 ////////////////////////////////////////////////////////////////////////////// 50 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 48 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 51 { 49 { 52 static bool first = true; << 50 SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRenderAction); 53 if (first) { << 54 first = false; << 55 SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRen << 56 } << 57 } 51 } 58 ////////////////////////////////////////////// 52 ////////////////////////////////////////////////////////////////////////////// 59 SoGL2PSAction::SoGL2PSAction( 53 SoGL2PSAction::SoGL2PSAction( 60 const SbViewportRegion& aViewPortRegion 54 const SbViewportRegion& aViewPortRegion 61 ) 55 ) 62 :SoGLRenderAction(aViewPortRegion) 56 :SoGLRenderAction(aViewPortRegion) 63 ,fContext(0) << 57 ,fFileName("out.ps") 64 ,fFile(0) 58 ,fFile(0) 65 ,fFileName("out.pdf") << 66 ,fTitle("title") << 67 ,fProducer("HEPVis::SoGL2PSAction") << 68 ,fFormat(TOOLS_GL2PS_PDF) << 69 ////////////////////////////////////////////// 59 ////////////////////////////////////////////////////////////////////////////// 70 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 60 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 71 { 61 { 72 SO_ACTION_CONSTRUCTOR(SoGL2PSAction); 62 SO_ACTION_CONSTRUCTOR(SoGL2PSAction); 73 } 63 } 74 << 75 ////////////////////////////////////////////// << 76 SoGL2PSAction::~SoGL2PSAction() << 77 ////////////////////////////////////////////// << 78 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! << 79 { << 80 closeFile(); << 81 } << 82 << 83 ////////////////////////////////////////////// 64 ////////////////////////////////////////////////////////////////////////////// 84 void SoGL2PSAction::setFileName(const std::str << 65 void SoGL2PSAction::setFileName( >> 66 const char* aFileName >> 67 ) 85 ////////////////////////////////////////////// 68 ////////////////////////////////////////////////////////////////////////////// 86 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 69 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 87 { 70 { 88 fFileName = aFileName; 71 fFileName = aFileName; 89 } 72 } 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 ////////////////////////////////////////////// 73 ////////////////////////////////////////////////////////////////////////////// 108 bool SoGL2PSAction::enableFileWriting() << 74 void SoGL2PSAction::enableFileWriting( >> 75 ) 109 ////////////////////////////////////////////// 76 ////////////////////////////////////////////////////////////////////////////// 110 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 77 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 111 { 78 { 112 if(!openFile()) { << 79 fFile = ::fopen(fFileName.getString(),"w"); >> 80 if(!fFile) { 113 SoDebugError::post("SoGL2PSAction::enableF 81 SoDebugError::post("SoGL2PSAction::enableFileWriting", 114 "openFile() failed for << 82 "Cannot open file %s",fFileName.getString()); 115 fFileName.c_str()); << 83 return; 116 return false; << 117 } 84 } 118 #ifdef __COIN__ 85 #ifdef __COIN__ 119 #else //SGI 86 #else //SGI 120 const SbViewportRegion& vpr = getViewportReg 87 const SbViewportRegion& vpr = getViewportRegion(); 121 SoViewportRegionElement::set(getState(),vpr) 88 SoViewportRegionElement::set(getState(),vpr); 122 SbVec2s origin = vpr.getViewportOriginPixels << 89 gl2psBegin(); 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 90 #endif 129 return true; << 130 } 91 } 131 ////////////////////////////////////////////// 92 ////////////////////////////////////////////////////////////////////////////// 132 void SoGL2PSAction::disableFileWriting( 93 void SoGL2PSAction::disableFileWriting( 133 ) 94 ) 134 ////////////////////////////////////////////// 95 ////////////////////////////////////////////////////////////////////////////// 135 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 96 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 136 { 97 { 137 #ifdef __COIN__ 98 #ifdef __COIN__ 138 #else //SGI 99 #else //SGI 139 endPage(); << 100 gl2psEndPage(); 140 #endif 101 #endif 141 closeFile(); << 102 ::fclose(fFile); 142 } << 103 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 } 104 } 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 ////////////////////////////////////////////// 105 ////////////////////////////////////////////////////////////////////////////// 269 bool SoGL2PSAction::endPage() << 106 SbBool SoGL2PSAction::fileWritingEnabled( >> 107 ) const 270 ////////////////////////////////////////////// 108 ////////////////////////////////////////////////////////////////////////////// 271 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 109 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 272 { 110 { 273 int _status = 0; << 111 return (fFile?TRUE:FALSE); 274 if(fContext) { << 275 _status = ::tools_gl2psEndPage(fContext); << 276 } << 277 if (_status == TOOLS_GL2PS_OVERFLOW) return << 278 return true; << 279 } 112 } 280 << 281 ////////////////////////////////////////////// 113 ////////////////////////////////////////////////////////////////////////////// 282 bool SoGL2PSAction::addBitmap( << 114 SbBool SoGL2PSAction::addBitmap( 283 int aWidth 115 int aWidth 284 ,int aHeight 116 ,int aHeight 285 ,float aXorig 117 ,float aXorig 286 ,float aYorig 118 ,float aYorig 287 ,float aXmove 119 ,float aXmove 288 ,float aYmove 120 ,float aYmove 289 ) 121 ) 290 ////////////////////////////////////////////// 122 ///////////////////////////////////////////////////////////////////////////// 291 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 123 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// 292 { 124 { 293 if(!fContext) return false; << 125 if(!fFile) return FALSE; 294 GLboolean valid; 126 GLboolean valid; 295 ::glGetBooleanv(GL_CURRENT_RASTER_POSITION_V << 127 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID,&valid); 296 if(!valid) return false; << 128 if(valid==GL_FALSE) return FALSE; 297 float pos[4]; 129 float pos[4]; 298 ::glGetFloatv(GL_CURRENT_RASTER_POSITION,pos << 130 glGetFloatv(GL_CURRENT_RASTER_POSITION,pos); 299 int xoff = -(int)(aXmove + aXorig); 131 int xoff = -(int)(aXmove + aXorig); 300 int yoff = -(int)(aYmove + aYorig); 132 int yoff = -(int)(aYmove + aYorig); 301 int x = (int)(pos[0] + xoff); 133 int x = (int)(pos[0] + xoff); 302 int y = (int)(pos[1] + yoff); 134 int y = (int)(pos[1] + yoff); 303 // Should clip against viewport area : 135 // Should clip against viewport area : 304 GLint vp[4]; 136 GLint vp[4]; 305 ::glGetIntegerv(GL_VIEWPORT,vp); << 137 glGetIntegerv(GL_VIEWPORT,vp); 306 GLsizei w = aWidth; 138 GLsizei w = aWidth; 307 GLsizei h = aHeight; 139 GLsizei h = aHeight; 308 if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x; 140 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; 141 if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y; 310 int s = 3 * w * h; 142 int s = 3 * w * h; 311 if(s<=0) return false; << 143 if(s<=0) return FALSE; 312 float* image = (float*)::malloc(s * sizeof(f 144 float* image = (float*)::malloc(s * sizeof(float)); 313 if(!image) return false; << 145 if(!image) return FALSE; 314 ::glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image << 146 glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image); 315 GLint status = ::tools_gl2psDrawPixels(fCont << 147 GLint status = gl2psDrawPixels(w,h,xoff,yoff,GL_RGB,GL_FLOAT,image); 316 ::free(image); 148 ::free(image); 317 return (status!=TOOLS_GL2PS_SUCCESS ? false << 149 return (status!=GL2PS_SUCCESS ? FALSE : TRUE); >> 150 } >> 151 ////////////////////////////////////////////////////////////////////////////// >> 152 void SoGL2PSAction::beginViewport( >> 153 ) >> 154 ///////////////////////////////////////////////////////////////////////////// >> 155 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// >> 156 { >> 157 if(!fFile) return; >> 158 GLint vp[4]; >> 159 glGetIntegerv(GL_VIEWPORT,vp); >> 160 gl2psBeginViewport(vp); >> 161 } >> 162 ////////////////////////////////////////////////////////////////////////////// >> 163 void SoGL2PSAction::endViewport( >> 164 ) >> 165 ///////////////////////////////////////////////////////////////////////////// >> 166 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// >> 167 { >> 168 if(!fFile) return; >> 169 gl2psEndViewport(); >> 170 } >> 171 ////////////////////////////////////////////////////////////////////////////// >> 172 void SoGL2PSAction::beginTraversal( >> 173 SoNode* aNode >> 174 ) >> 175 ////////////////////////////////////////////////////////////////////////////// >> 176 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// >> 177 { >> 178 if(fFile) { >> 179 #ifdef __COIN__ >> 180 const SbViewportRegion& vpr = getViewportRegion(); >> 181 SoViewportRegionElement::set(getState(),vpr); >> 182 gl2psBegin(); >> 183 traverse(aNode); >> 184 gl2psEndPage(); >> 185 #else //SGI >> 186 SoGLRenderAction::beginTraversal(aNode); >> 187 #endif >> 188 } else { >> 189 SoGLRenderAction::beginTraversal(aNode); >> 190 } >> 191 } >> 192 ////////////////////////////////////////////////////////////////////////////// >> 193 void SoGL2PSAction::gl2psBegin( >> 194 ) >> 195 ////////////////////////////////////////////////////////////////////////////// >> 196 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// >> 197 { >> 198 if(!fFile) return; >> 199 int options = GL2PS_OCCLUSION_CULL | >> 200 GL2PS_BEST_ROOT | GL2PS_SILENT | GL2PS_DRAW_BACKGROUND; >> 201 int sort = GL2PS_BSP_SORT; >> 202 //int sort = GL2PS_SIMPLE_SORT; >> 203 >> 204 const SbViewportRegion& vpr = getViewportRegion(); >> 205 SoViewportRegionElement::set(getState(),vpr); >> 206 >> 207 const SbVec2s& win = vpr.getWindowSize(); >> 208 GLint vp[4]; >> 209 vp[0] = 0; >> 210 vp[1] = 0; >> 211 vp[2] = win[0]; >> 212 vp[3] = win[1]; >> 213 >> 214 int bufsize = 0; >> 215 gl2psBeginPage("title","HEPVis::SoGL2PSAction", >> 216 vp, >> 217 GL2PS_EPS, >> 218 sort, >> 219 options, >> 220 GL_RGBA,0, NULL,0,0,0, >> 221 bufsize, >> 222 fFile,fFileName.getString()); 318 } 223 } 319 224 >> 225 #endif 320 226