00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef G4VIS_BUILD_OI_DRIVER
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <HEPVis/actions/SoGL2PSAction.h>
00037
00038
00039 #include <Inventor/elements/SoViewportRegionElement.h>
00040 #include <Inventor/errors/SoDebugError.h>
00041
00042 #include "Geant4_gl2ps.h"
00043
00044 #include <stdio.h>
00045
00046 SO_ACTION_SOURCE(SoGL2PSAction)
00048 void SoGL2PSAction::initClass(
00049 )
00052 {
00053 SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRenderAction);
00054 }
00056 SoGL2PSAction::SoGL2PSAction(
00057 const SbViewportRegion& aViewPortRegion
00058 )
00059 :SoGLRenderAction(aViewPortRegion)
00060 ,G4OpenGL2PSAction()
00063 {
00064 setFileName("out.ps");
00065 SO_ACTION_CONSTRUCTOR(SoGL2PSAction);
00066 }
00067
00068 bool SoGL2PSAction::enableFileWriting(
00069 )
00072 {
00073 fFile = ::fopen(fFileName,"w");
00074 if(!fFile) {
00075 SoDebugError::post("SoGL2PSAction::enableFileWriting",
00076 "Cannot open file %s",fFileName);
00077 return false;
00078 }
00079 #ifdef __COIN__
00080 #else //SGI
00081 const SbViewportRegion& vpr = getViewportRegion();
00082 SoViewportRegionElement::set(getState(),vpr);
00083 G4gl2psBegin();
00084 #endif
00085 return true;
00086 }
00088 void SoGL2PSAction::disableFileWriting(
00089 )
00092 {
00093 #ifdef __COIN__
00094 #else //SGI
00095 gl2psEndPage();
00096 #endif
00097 ::fclose(fFile);
00098 fFile = 0;
00099 }
00100
00102 bool SoGL2PSAction::addBitmap(
00103 int aWidth
00104 ,int aHeight
00105 ,float aXorig
00106 ,float aYorig
00107 ,float aXmove
00108 ,float aYmove
00109 )
00112 {
00113 if(!fFile) return false;
00114 GLboolean valid;
00115 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID,&valid);
00116 if(!valid) return false;
00117 float pos[4];
00118 glGetFloatv(GL_CURRENT_RASTER_POSITION,pos);
00119 int xoff = -(int)(aXmove + aXorig);
00120 int yoff = -(int)(aYmove + aYorig);
00121 int x = (int)(pos[0] + xoff);
00122 int y = (int)(pos[1] + yoff);
00123
00124 GLint vp[4];
00125 glGetIntegerv(GL_VIEWPORT,vp);
00126 GLsizei w = aWidth;
00127 GLsizei h = aHeight;
00128 if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x;
00129 if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y;
00130 int s = 3 * w * h;
00131 if(s<=0) return false;
00132 float* image = (float*)::malloc(s * sizeof(float));
00133 if(!image) return false;
00134 glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image);
00135 GLint status = gl2psDrawPixels(w,h,xoff,yoff,GL_RGB,GL_FLOAT,image);
00136 ::free(image);
00137 return (status!=GL2PS_SUCCESS ? false : true);
00138 }
00140 void SoGL2PSAction::beginTraversal(
00141 SoNode* aNode
00142 )
00145 {
00146 if(fFile) {
00147 #ifdef __COIN__
00148 const SbViewportRegion& vpr = getViewportRegion();
00149 SoViewportRegionElement::set(getState(),vpr);
00150 G4gl2psBegin();
00151 traverse(aNode);
00152 gl2psEndPage();
00153 #else //SGI
00154
00155 SoGLRenderAction::beginTraversal(aNode);
00156
00157 #endif
00158 } else {
00159 SoGLRenderAction::beginTraversal(aNode);
00160 }
00161 }
00162
00163 #endif