33 #ifdef G4VIS_BUILD_OPENGL_DRIVER 
   53 #ifdef G4OPENGL_VERSION_2 
   59 #include "Geant4_gl2ps.h" 
   66 G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
 
   68 #ifdef G4OPENGL_VERSION_2
 
   73 fOpenGLSceneHandler(scene),
 
   75 transparency_enabled (true),
 
   76 antialiasing_enabled (false),
 
   77 haloing_enabled (false),
 
   81 fDisplayHeadTime(false),
 
   82 fDisplayHeadTimeX(-0.9),
 
   83 fDisplayHeadTimeY(-0.9),
 
   84 fDisplayHeadTimeSize(24.),
 
   85 fDisplayHeadTimeRed(0.),
 
   86 fDisplayHeadTimeGreen(1.),
 
   87 fDisplayHeadTimeBlue(1.),
 
   88 fDisplayLightFront(false),
 
   89 fDisplayLightFrontX(0.),
 
   90 fDisplayLightFrontY(0.),
 
   91 fDisplayLightFrontZ(0.),
 
   92 fDisplayLightFrontT(0.),
 
   93 fDisplayLightFrontRed(0.),
 
   94 fDisplayLightFrontGreen(1.),
 
   95 fDisplayLightFrontBlue(0.),
 
  100 fDefaultExportImageFormat(
"pdf"),
 
  101 fExportImageFormat(
"pdf"),
 
  102 fExportFilenameIndex(0),
 
  106 fDefaultExportFilename(
"G4OpenGL"),
 
  108 fGl2psDefaultLineWith(1),
 
  109 fGl2psDefaultPointSize(2),
 
  110 fGlViewInitialized(false),
 
  111 fIsGettingPickInfos(false)
 
  112 #ifdef G4OPENGL_VERSION_2
 
  114 ,fVertexPositionAttribute(0)
 
  115 ,fVertexNormalAttribute(0)
 
  122 #ifdef G4DEBUG_VIS_OGL 
  123   printf(
"G4OpenGLViewer:: Creation\n");
 
  126   fVP.SetAutoRefresh(
true);
 
  127   fDefaultVP.SetAutoRefresh(
true);
 
  132   addExportImageFormat(
"eps");
 
  133   addExportImageFormat(
"ps");
 
  134   addExportImageFormat(
"pdf");
 
  135   addExportImageFormat(
"svg");
 
  138   fExportFilename += fDefaultExportFilename + 
"_" + GetShortName().data();
 
  148 G4OpenGLViewer::~G4OpenGLViewer ()
 
  153 void G4OpenGLViewer::InitializeGLView () 
 
  155 #ifdef G4OPENGL_VERSION_2 
  159     fShaderProgram = glCreateProgram();
 
  160     Shader vertexShader = glCreateShader(GL_VERTEX_SHADER);
 
  161     const char * vSrc = fVboDrawer->getVertexShaderSrc();
 
  162     glShaderSource(vertexShader, 1, &vSrc, NULL);
 
  163     glCompileShader(vertexShader);
 
  164     glAttachShader(fShaderProgram, vertexShader);
 
  166     Shader fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 
  167     const char * fSrc = fVboDrawer->getFragmentShaderSrc();
 
  168     glShaderSource(fragmentShader, 1, &fSrc, NULL);
 
  169     glCompileShader(fragmentShader);
 
  171     glAttachShader(fShaderProgram, fragmentShader);
 
  172     glLinkProgram(fShaderProgram);
 
  173     glUseProgram(fShaderProgram);
 
  180     fVertexPositionAttribute =
 
  181     glGetAttribLocation(fShaderProgram, 
"aVertexPosition");
 
  184     glEnableVertexAttribArray(fVertexPositionAttribute);
 
  187     fpMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uPMatrix");
 
  188     fcMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uCMatrix");
 
  189     fmvMatrixUniform = glGetUniformLocation(fShaderProgram, 
"uMVMatrix");
 
  190     fnMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uNMatrix");
 
  191     ftMatrixUniform  = glGetUniformLocation(fShaderProgram, 
"uTMatrix");
 
  199     fGlViewInitialized = 
true;
 
  203 #ifdef G4DEBUG_VIS_OGL 
  204   printf(
"G4OpenGLViewer::InitializeGLView\n");
 
  207   fWinSize_x = fVP.GetWindowSizeHintX();
 
  208   fWinSize_y = fVP.GetWindowSizeHintY();
 
  210   glClearColor (0.0, 0.0, 0.0, 0.0);
 
  212 #ifndef G4OPENGL_VERSION_2 
  213   glDisable (GL_LINE_SMOOTH);
 
  214   glDisable (GL_POLYGON_SMOOTH);
 
  221   glDepthFunc (GL_LEQUAL);
 
  222   glDepthMask (GL_TRUE);
 
  225   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
  227 #ifdef G4DEBUG_VIS_OGL 
  228   printf(
"G4OpenGLViewer::InitializeGLView END\n");
 
  232 void G4OpenGLViewer::ClearView () {
 
  233   ClearViewWithoutFlush();
 
  238 void G4OpenGLViewer::ClearViewWithoutFlush () {
 
  241 #if GL_EXT_framebuffer_object 
  247   glClearColor (background.GetRed(),
 
  248                 background.GetGreen(),
 
  249                 background.GetBlue(),
 
  254   glClear (GL_COLOR_BUFFER_BIT);
 
  255   glClear (GL_DEPTH_BUFFER_BIT);
 
  256   glClear (GL_STENCIL_BUFFER_BIT);
 
  260 void G4OpenGLViewer::ResizeWindow(
unsigned int aWidth, 
unsigned int aHeight) {
 
  261   if ((fWinSize_x != aWidth) || (fWinSize_y != aHeight)) {
 
  263     fWinSize_y = aHeight;
 
  264     fSizeHasChanged = 
true;
 
  266     fSizeHasChanged = 
false;
 
  276 void G4OpenGLViewer::ResizeGLView()
 
  278 #ifdef G4DEBUG_VIS_OGL 
  279   printf(
"G4OpenGLViewer::ResizeGLView %d %d %#lx\n",fWinSize_x,fWinSize_y,(
unsigned long)
this);
 
  286   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
 
  288   if ((dims[0] !=0 ) && (dims[1] !=0)) {
 
  290     if (fWinSize_x > (
unsigned)dims[0]) {
 
  291       G4cerr << 
"Try to resize view greater than max X viewport dimension. Desired size "<<fWinSize_x <<
" is resize to "<<  dims[0] << 
G4endl;
 
  292       fWinSize_x = dims[0];
 
  294     if (fWinSize_y > (
unsigned)dims[1]) {
 
  295       G4cerr << 
"Try to resize view greater than max Y viewport dimension. Desired size "<<fWinSize_y <<
" is resize to "<<  dims[1] << 
G4endl;
 
  296       fWinSize_y = dims[1];
 
  300   glViewport(0, 0, fWinSize_x,fWinSize_y);   
 
  306 void G4OpenGLViewer::SetView () {
 
  308   if (fIsGettingPickInfos) 
return;
 
  310   if (!fSceneHandler.GetScene()) {
 
  318   GLfloat lightPosition [4];
 
  319   lightPosition [0] = fVP.GetActualLightpointDirection().x();
 
  320   lightPosition [1] = fVP.GetActualLightpointDirection().y();
 
  321   lightPosition [2] = fVP.GetActualLightpointDirection().z();
 
  322   lightPosition [3] = 0.;
 
  324   GLfloat ambient [] = { 0.2, 0.2, 0.2, 1.};
 
  325   GLfloat diffuse [] = { 0.8, 0.8, 0.8, 1.};
 
  326   glEnable (GL_LIGHT0);
 
  327   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
 
  328   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
 
  332   if (fWinSize_y > fWinSize_x) {
 
  335   if (fWinSize_x > fWinSize_y) {
 
  342     = fSceneHandler.GetScene()->GetStandardTargetPoint()
 
  343     + fVP.GetCurrentTargetPoint ();
 
  344   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
 
  345   if(radius<=0.) radius = 1.;
 
  346   const G4double cameraDistance = fVP.GetCameraDistance (radius);
 
  348     targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
 
  349   const GLdouble pnear  = fVP.GetNearDistance (cameraDistance, radius);
 
  350   const GLdouble pfar   = fVP.GetFarDistance  (cameraDistance, pnear, radius);
 
  351   const GLdouble 
right  = fVP.GetFrontHalfHeight (pnear, radius) * ratioY;
 
  353   const GLdouble top    = fVP.GetFrontHalfHeight (pnear, radius) * ratioX;
 
  354   const GLdouble bottom = -top;
 
  360   glMatrixMode (GL_PROJECTION); 
 
  363   const G4Vector3D scaleFactor = fVP.GetScaleFactor();
 
  364   glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z());
 
  366   if (fVP.GetFieldHalfAngle() == 0.) {
 
  367     glOrtho (left, right, bottom, top, pnear, pfar);
 
  370     glFrustum (left, right, bottom, top, pnear, pfar);
 
  373   glMatrixMode (GL_MODELVIEW); 
 
  376   const G4Normal3D& upVector = fVP.GetUpVector ();  
 
  378   if (cameraDistance > 1.e-6 * radius) {
 
  379     gltarget = targetPoint;
 
  382     gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit();
 
  385   const G4Point3D& pCamera = cameraPosition;  
 
  387   g4GluLookAt (pCamera.x(),  pCamera.y(),  pCamera.z(),       
 
  388              gltarget.x(), gltarget.y(), gltarget.z(),      
 
  389              upVector.x(), upVector.y(), upVector.z());     
 
  391   glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
 
  405     sArray[3] = sp.d() + radius * 1.e-05;
 
  406     glClipPlane (GL_CLIP_PLANE0, sArray);
 
  407     glEnable (GL_CLIP_PLANE0);
 
  411     sArray[3] = -sp.d() + radius * 1.e-05;
 
  412     glClipPlane (GL_CLIP_PLANE1, sArray);
 
  413     glEnable (GL_CLIP_PLANE1);
 
  415     glDisable (GL_CLIP_PLANE0);
 
  416     glDisable (GL_CLIP_PLANE1);
 
  423   const G4Planes& cutaways = fVP.GetCutawayPlanes();
 
  424   size_t nPlanes = cutaways.size();
 
  425   if (fVP.IsCutaway() &&
 
  429     a[0] = cutaways[0].a();
 
  430     a[1] = cutaways[0].b();
 
  431     a[2] = cutaways[0].c();
 
  432     a[3] = cutaways[0].d();
 
  433     glClipPlane (GL_CLIP_PLANE2, a);
 
  434     glEnable (GL_CLIP_PLANE2);
 
  436       a[0] = cutaways[1].a();
 
  437       a[1] = cutaways[1].b();
 
  438       a[2] = cutaways[1].c();
 
  439       a[3] = cutaways[1].d();
 
  440       glClipPlane (GL_CLIP_PLANE3, a);
 
  441       glEnable (GL_CLIP_PLANE3);
 
  444       a[0] = cutaways[2].a();
 
  445       a[1] = cutaways[2].b();
 
  446       a[2] = cutaways[2].c();
 
  447       a[3] = cutaways[2].d();
 
  448       glClipPlane (GL_CLIP_PLANE4, a);
 
  449       glEnable (GL_CLIP_PLANE4);
 
  452     glDisable (GL_CLIP_PLANE2);
 
  453     glDisable (GL_CLIP_PLANE3);
 
  454     glDisable (GL_CLIP_PLANE4);
 
  458   background = fVP.GetBackgroundColour ();
 
  464 void G4OpenGLViewer::ResetView () {
 
  471 void G4OpenGLViewer::HaloingFirstPass () {
 
  481   glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
  484   glDepthMask (GL_TRUE);
 
  485   glDepthFunc (GL_LESS);
 
  489   ChangeLineWidth(3.0);
 
  493 void G4OpenGLViewer::HaloingSecondPass () {
 
  496   glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 
  497   glDepthFunc (GL_LEQUAL);
 
  498   ChangeLineWidth(1.0);
 
  502 G4String G4OpenGLViewer::Pick(GLdouble x, GLdouble y)
 
  504   std::vector < G4OpenGLViewerPickMap* >  pickMap = GetPickDetails(x,y);
 
  506   if (pickMap.size() == 0) {
 
  507     txt += 
"Too many hits.  Zoom in to reduce overlaps.";;
 
  509     for (
unsigned int a=0; a< pickMap.size(); a++) {
 
  510       txt += pickMap[
a]->print();
 
  516 std::vector < G4OpenGLViewerPickMap* > G4OpenGLViewer::GetPickDetails(GLdouble x, GLdouble y)
 
  518   std::vector < G4OpenGLViewerPickMap* > pickMapVector;
 
  520   std::ostringstream oss;
 
  521   const G4int BUFSIZE = 512;
 
  522   GLuint selectBuffer[BUFSIZE];
 
  523   glSelectBuffer(BUFSIZE, selectBuffer);
 
  524   glRenderMode(GL_SELECT);
 
  527   glMatrixMode(GL_PROJECTION);
 
  528   G4double currentProjectionMatrix[16];
 
  529   glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix);
 
  533   glGetIntegerv(GL_VIEWPORT, viewport);
 
  534   fIsGettingPickInfos = 
true;
 
  536   g4GluPickMatrix(x, viewport[3] - y, 5., 5., viewport);
 
  537   glMultMatrixd(currentProjectionMatrix);
 
  538   glMatrixMode(GL_MODELVIEW);
 
  540   GLint hits = glRenderMode(GL_RENDER);
 
  541   fIsGettingPickInfos = 
false;
 
  544     GLuint* p = selectBuffer;
 
  545     for (GLint i = 0; i < hits; ++i) {
 
  546       G4OpenGLViewerPickMap* pickMap = 
new G4OpenGLViewerPickMap();
 
  547       GLuint nnames = *p++;
 
  556       for (GLuint j = 0; j < nnames; ++j) {
 
  558         pickMap->setHitNumber(i);
 
  559         pickMap->setSubHitNumber(j);
 
  560         pickMap->setPickName(name);
 
  561         std::map<GLuint, G4AttHolder*>::iterator iter =
 
  562           fOpenGLSceneHandler.fPickMap.find(name);
 
  563         if (iter != fOpenGLSceneHandler.fPickMap.end()) {
 
  565           if(attHolder && attHolder->
GetAttDefs().size()) {
 
  566             for (
size_t iAtt = 0;
 
  567                  iAtt < attHolder->
GetAttDefs().size(); ++iAtt) {
 
  570               pickMap->addAttributes(oss.str());
 
  575       pickMapVector.push_back(pickMap);
 
  578   glMatrixMode(GL_PROJECTION);
 
  580   glMatrixMode(GL_MODELVIEW);
 
  581   return pickMapVector;
 
  584 GLubyte* G4OpenGLViewer::grabPixels
 
  585 (
int inColor, 
unsigned int width, 
unsigned int height) {
 
  588   GLint swapbytes, lsbfirst, rowlength;
 
  589   GLint skiprows, skippixels, alignment;
 
  595     size = width*height*3;
 
  597     format = GL_LUMINANCE;
 
  598     size = width*height*1;
 
  601   buffer = 
new GLubyte[size];
 
  605   glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
 
  606   glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
 
  607   glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
 
  609   glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
 
  610   glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
 
  611   glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
 
  613   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
 
  614   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
 
  615   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
 
  617   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
 
  618   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
 
  619   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 
  621   glReadBuffer(GL_FRONT);
 
  622   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
 
  624   glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
 
  625   glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
 
  626   glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
 
  628   glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
 
  629   glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
 
  630   glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
 
  635 bool G4OpenGLViewer::printEPS() {
 
  637 #ifdef G4DEBUG_VIS_OGL 
  638   printf(
"G4OpenGLViewer::printEPS file:%s Vec:%d Name:%s\n",getRealPrintFilename().c_str(),fVectoredPs,GetName().c_str());
 
  643   size_t len = strlen(setlocale(LC_NUMERIC,NULL));
 
  644   char* oldLocale = (
char*)(malloc(len+1));
 
  645   if(oldLocale!=NULL) strncpy(oldLocale,setlocale(LC_NUMERIC,NULL),len);
 
  646   setlocale(LC_NUMERIC,
"C");
 
  648   if (((fExportImageFormat == 
"eps") || (fExportImageFormat == 
"ps")) && (!fVectoredPs)) {
 
  649     res = printNonVectoredEPS();
 
  651     res = printVectoredEPS();
 
  656     setlocale(LC_NUMERIC,oldLocale);
 
  661     G4cerr << 
"Error saving file... " << getRealPrintFilename().c_str() << 
G4endl;
 
  663     G4cout << 
"File " << getRealPrintFilename().c_str() << 
" size: " << getRealExportWidth() << 
"x" << getRealExportHeight() << 
" has been saved " << 
G4endl;
 
  666     if ( fExportFilenameIndex != -1) {
 
  667       fExportFilenameIndex++;
 
  672 #ifdef G4DEBUG_VIS_OGL 
  673   printf(
"G4OpenGLViewer::printEPS END\n");
 
  678 bool G4OpenGLViewer::printVectoredEPS() {
 
  682 bool G4OpenGLViewer::printNonVectoredEPS () {
 
  684   int width = getRealExportWidth();
 
  685   int height = getRealExportHeight();
 
  687 #ifdef G4DEBUG_VIS_OGL 
  688   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);
 
  693   int components, 
pos, i;
 
  695   pixels = grabPixels (fPrintColour, width, height);
 
  697   if (pixels == NULL) {
 
  698       G4cerr << 
"Failed to get pixels from OpenGl viewport" << 
G4endl;
 
  706   std::string name = getRealPrintFilename();
 
  707   fp = fopen (name.c_str(), 
"w");
 
  709     G4cerr << 
"Can't open filename " << name.c_str() << 
G4endl;
 
  713   fprintf (fp, 
"%%!PS-Adobe-2.0 EPSF-1.2\n");
 
  714   fprintf (fp, 
"%%%%Title: %s\n", name.c_str());
 
  715   fprintf (fp, 
"%%%%Creator: OpenGL pixmap render output\n");
 
  716   fprintf (fp, 
"%%%%BoundingBox: 0 0 %d %d\n", width, height);
 
  717   fprintf (fp, 
"%%%%EndComments\n");
 
  718   fprintf (fp, 
"gsave\n");
 
  719   fprintf (fp, 
"/bwproc {\n");
 
  720   fprintf (fp, 
"    rgbproc\n");
 
  721   fprintf (fp, 
"    dup length 3 idiv string 0 3 0 \n");
 
  722   fprintf (fp, 
"    5 -1 roll {\n");
 
  723   fprintf (fp, 
"    add 2 1 roll 1 sub dup 0 eq\n");
 
  724   fprintf (fp, 
"    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
 
  725   fprintf (fp, 
"       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
 
  726   fprintf (fp, 
"    { 2 1 roll } ifelse\n");
 
  727   fprintf (fp, 
"    }forall\n");
 
  728   fprintf (fp, 
"    pop pop pop\n");
 
  729   fprintf (fp, 
"} def\n");
 
  730   fprintf (fp, 
"systemdict /colorimage known not {\n");
 
  731   fprintf (fp, 
"   /colorimage {\n");
 
  732   fprintf (fp, 
"       pop\n");
 
  733   fprintf (fp, 
"       pop\n");
 
  734   fprintf (fp, 
"       /rgbproc exch def\n");
 
  735   fprintf (fp, 
"       { bwproc } image\n");
 
  736   fprintf (fp, 
"   }  def\n");
 
  737   fprintf (fp, 
"} if\n");
 
  738   fprintf (fp, 
"/picstr %d string def\n", width * components);
 
  739   fprintf (fp, 
"%d %d scale\n", width, height);
 
  740   fprintf (fp, 
"%d %d %d\n", width, height, 8);
 
  741   fprintf (fp, 
"[%d 0 0 %d 0 0]\n", width, height);
 
  742   fprintf (fp, 
"{currentfile picstr readhexstring pop}\n");
 
  743   fprintf (fp, 
"false %d\n", components);
 
  744   fprintf (fp, 
"colorimage\n");
 
  746   curpix = (GLubyte*) pixels;
 
  748   for (i = width*height*components; i>0; i--) {
 
  749     fprintf (fp, 
"%02hx ", (
unsigned short)(*(curpix++)));
 
  758   fprintf (fp, 
"grestore\n");
 
  759   fprintf (fp, 
"showpage\n");
 
  772 bool G4OpenGLViewer::isGl2psWriting() {
 
  774   if (!fGL2PSAction) 
return false;
 
  775   if (fGL2PSAction->fileWritingEnabled()) {
 
  784 void G4OpenGLViewer::DrawText(
const G4Text& g4text)
 
  787   if (isGl2psWriting()) {
 
  790     G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
 
  795     glRasterPos3d(position.x(),position.y(),position.z());
 
  796     GLint align = GL2PS_TEXT_B;
 
  804     gl2psTextOpt(textString.c_str(),
"Times-Roman",GLshort(size),align,0);
 
  808     static G4int callCount = 0;
 
  811     if (callCount <= 1) {
 
  813         "G4OpenGLViewer::DrawText: Not implemented for \"" 
  824 void G4OpenGLViewer::ChangePointSize(
G4double size) {
 
  826   if (isGl2psWriting()) {
 
  827     fGL2PSAction->setPointSize(
int(size));
 
  836 void G4OpenGLViewer::ChangeLineWidth(
G4double width) {
 
  838   if (isGl2psWriting()) {
 
  839     fGL2PSAction->setLineWidth(
int(width));
 
  853 bool G4OpenGLViewer::exportImage(std::string name, 
int width, 
int height) {
 
  855   if (! setExportFilename(name)) {
 
  859   if ((width =! -1) && (height != -1)) {
 
  860     setExportSize(width, height);
 
  863   if (fExportImageFormat == 
"eps") {
 
  864     fGL2PSAction->setExportImageFormat(GL2PS_EPS);
 
  865   } 
else if (fExportImageFormat == 
"ps") {
 
  866     fGL2PSAction->setExportImageFormat(GL2PS_PS);
 
  867   } 
else if (fExportImageFormat == 
"svg") {
 
  868     fGL2PSAction->setExportImageFormat(GL2PS_SVG);
 
  869   } 
else if (fExportImageFormat == 
"pdf") {
 
  870     fGL2PSAction->setExportImageFormat(GL2PS_PDF);
 
  872     setExportImageFormat(fExportImageFormat,
true); 
 
  879 bool G4OpenGLViewer::printGl2PS() {
 
  881   int width = getRealExportWidth();
 
  882   int height = getRealExportHeight();
 
  886   G4OpenGLSceneHandler& oglSceneHandler = 
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
 
  887   G4int drawInterval = oglSceneHandler.GetEventsDrawInterval();
 
  888   oglSceneHandler.SetEventsDrawInterval(1000); 
 
  890   if (!fGL2PSAction) 
return false;
 
  892   fGL2PSAction->setFileName(getRealPrintFilename().c_str());
 
  909    bool extendBuffer = 
true;
 
  910    bool endWriteAction = 
false;
 
  911    bool beginWriteAction = 
true;
 
  912    bool filePointerOk = 
true;
 
  913    while ((extendBuffer) && (! endWriteAction) && (filePointerOk)) {
 
  915      beginWriteAction = fGL2PSAction->enableFileWriting();
 
  921      filePointerOk = fGL2PSAction->fileWritingEnabled();
 
  923      if (beginWriteAction) {
 
  927        fGL2PSAction->setLineWidth(fGl2psDefaultLineWith);
 
  929        fGL2PSAction->setPointSize(fGl2psDefaultPointSize);
 
  932        endWriteAction = fGL2PSAction->disableFileWriting();
 
  935        if ((! endWriteAction) || (! beginWriteAction)) {
 
  936          extendBuffer = fGL2PSAction->extendBufferSize();
 
  940    fGL2PSAction->resetBufferSizeParameters();
 
  942    if (!extendBuffer ) {
 
  943      G4cerr << 
"ERROR: gl2ps buffer size is not big enough to print this geometry. Try to extend it. No output produced"<< 
G4endl;
 
  946    if (!beginWriteAction ) {
 
  947      G4cerr << 
"ERROR: saving file "<<getRealPrintFilename().c_str()<<
". Check read/write access. No output produced" << 
G4endl;
 
  950    if (!endWriteAction ) {
 
  957   oglSceneHandler.SetEventsDrawInterval(drawInterval);
 
  966 unsigned int G4OpenGLViewer::getWinWidth()
 const{
 
  970 unsigned int G4OpenGLViewer::getWinHeight()
 const{
 
  974 G4bool G4OpenGLViewer::sizeHasChanged() {
 
  975   return fSizeHasChanged;
 
  978 G4int G4OpenGLViewer::getRealExportWidth() {
 
  979   if (fPrintSizeX == -1) {
 
  983   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
 
  986   if ((dims[0] !=0 ) && (dims[1] !=0)) {
 
  987     if (fPrintSizeX > dims[0]){
 
  991   if (fPrintSizeX < -1){
 
  997 G4int G4OpenGLViewer::getRealExportHeight() {
 
  998   if (fPrintSizeY == -1) {
 
 1002   glGetIntegerv(GL_MAX_VIEWPORT_DIMS, dims);
 
 1005   if ((dims[0] !=0 ) && (dims[1] !=0)) {
 
 1006     if (fPrintSizeY > dims[1]){
 
 1010   if (fPrintSizeY < -1){
 
 1016 void G4OpenGLViewer::setExportSize(
G4int X, 
G4int Y) {
 
 1027 bool G4OpenGLViewer::setExportFilename(
G4String name,
G4bool inc) {
 
 1033     if ((name != 
"") && (fExportFilename != name)) {
 
 1034       fExportFilenameIndex=0;
 
 1037     fExportFilenameIndex=-1;
 
 1040   if (name.size() == 0) {
 
 1041     name = getRealPrintFilename().c_str();
 
 1044     std::string extension = name.substr(name.find_last_of(
".") + 1);
 
 1046     if (name.size() != extension.size()) {
 
 1047       if (! setExportImageFormat(extension, 
false)) {
 
 1052     fExportFilename = name.substr(0,name.find_last_of(
"."));
 
 1057 std::string G4OpenGLViewer::getRealPrintFilename() {
 
 1058   std::string temp = fExportFilename;
 
 1059   if (fExportFilenameIndex != -1) {
 
 1060     temp += std::string(
"_");
 
 1061     std::ostringstream os;
 
 1062     os << fExportFilenameIndex;
 
 1063     std::string nb_str = os.str();
 
 1066   temp += 
"."+fExportImageFormat;
 
 1070 GLdouble G4OpenGLViewer::getSceneNearWidth()
 
 1072   if (!fSceneHandler.GetScene()) {
 
 1076     = fSceneHandler.GetScene()->GetStandardTargetPoint()
 
 1077     + fVP.GetCurrentTargetPoint ();
 
 1078   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
 
 1079   if(radius<=0.) radius = 1.;
 
 1080   const G4double cameraDistance = fVP.GetCameraDistance (radius);
 
 1081   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
 
 1082   return 2 * fVP.GetFrontHalfHeight (pnear, radius);
 
 1085 GLdouble G4OpenGLViewer::getSceneFarWidth()
 
 1087   if (!fSceneHandler.GetScene()) {
 
 1091     = fSceneHandler.GetScene()->GetStandardTargetPoint()
 
 1092     + fVP.GetCurrentTargetPoint ();
 
 1093   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
 
 1094   if(radius<=0.) radius = 1.;
 
 1095   const G4double cameraDistance = fVP.GetCameraDistance (radius);
 
 1096   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
 
 1097   const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
 
 1098   return 2 * fVP.GetFrontHalfHeight (pfar, radius);
 
 1102 GLdouble G4OpenGLViewer::getSceneDepth()
 
 1104   if (!fSceneHandler.GetScene()) {
 
 1108     = fSceneHandler.GetScene()->GetStandardTargetPoint()
 
 1109     + fVP.GetCurrentTargetPoint ();
 
 1110   G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
 
 1111   if(radius<=0.) radius = 1.;
 
 1112   const G4double cameraDistance = fVP.GetCameraDistance (radius);
 
 1113   const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
 
 1114   return fVP.GetFarDistance  (cameraDistance, pnear, radius)- pnear;
 
 1122     rotateSceneInViewDirection(dx,dy);
 
 1125       rotateSceneThetaPhi(dx,0);
 
 1128       rotateSceneThetaPhi(0,dy);
 
 1137     rotateSceneInViewDirection(dx,dy);
 
 1140       rotateSceneThetaPhi(dx,0);
 
 1143       rotateSceneThetaPhi(0,dy);
 
 1150   if (!fSceneHandler.GetScene()) {
 
 1178   vp = fVP.GetViewpointDirection ().unit ();
 
 1179   up = fVP.GetUpVector ().unit ();
 
 1181   yprime = (up.cross(vp)).unit();
 
 1182   zprime = (vp.cross(yprime)).unit();
 
 1184   if (fVP.GetLightsMoveWithCamera()) {
 
 1185     delta_alpha = dy * fRot_sens;
 
 1186     delta_theta = -dx * fRot_sens;
 
 1188     delta_alpha = -dy * fRot_sens;
 
 1189     delta_theta = dx * fRot_sens;
 
 1195   new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
 
 1200   if (fVP.GetLightsMoveWithCamera()) {
 
 1201     new_up = (new_vp.cross(yprime)).unit();
 
 1202     if (new_vp.z()*vp.z() <0) {
 
 1203       new_up.set(new_up.x(),-new_up.y(),new_up.z());
 
 1207     if (new_vp.z()*vp.z() <0) {
 
 1208       new_up.set(new_up.x(),-new_up.y(),new_up.z());
 
 1211   fVP.SetUpVector(new_up);
 
 1215   cosalpha = new_up.dot (new_vp.unit());
 
 1216   sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
 
 1217   yprime = (new_up.cross (new_vp.unit())).unit ();
 
 1218   xprime = yprime.cross (new_up);
 
 1220   a1 = sinalpha * xprime;
 
 1222   a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
 
 1226   viewPoint = new_vp.unit() + delta;
 
 1228   fVP.SetViewAndLights (viewPoint);
 
 1234   if (!fSceneHandler.GetScene()) {
 
 1258 #ifdef G4DEBUG_VIS_OGL 
 1259   printf(
"G4OpenGLViewer::rotateScene dx:%f dy:%f delta:%f\n",dx,dy, fRot_sens);
 
 1262   vp = fVP.GetViewpointDirection ().unit();
 
 1263   up = fVP.GetUpVector ().unit();
 
 1266                              up.z()*vp.x()-up.x()*vp.z(),
 
 1267                              up.x()*vp.y()-up.y()*vp.x());
 
 1269   viewPoint = vp/fRot_sens + (zPrimeVector*dx - up*dy) ;
 
 1270   new_up = 
G4Vector3D(viewPoint.y()*zPrimeVector.z()-viewPoint.z()*zPrimeVector.y(),
 
 1271                        viewPoint.z()*zPrimeVector.x()-viewPoint.x()*zPrimeVector.z(),
 
 1272                        viewPoint.x()*zPrimeVector.y()-viewPoint.y()*zPrimeVector.x());
 
 1278    fVP.SetUpVector(new_upUnit);
 
 1279    fVP.SetViewAndLights (viewPoint);
 
 1283 void G4OpenGLViewer::addExportImageFormat(std::string format) {
 
 1284   fExportImageFormatVector.push_back(format);
 
 1287 bool G4OpenGLViewer::setExportImageFormat(std::string format, 
bool quiet) {
 
 1290   for (
unsigned int a=0; a<fExportImageFormatVector.size(); a++) {
 
 1291     list +=fExportImageFormatVector.at(a) + 
" ";
 
 1293     if (fExportImageFormatVector.at(a) == format) {
 
 1295         G4cout << 
" Changing export format to \"" << format << 
"\"" << 
G4endl;
 
 1297       if (format != fExportImageFormat) {
 
 1298         fExportFilenameIndex = 0;
 
 1299         fExportImageFormat = format;
 
 1305     if (format.size() == 0) {
 
 1306       G4cout << 
" Current formats availables are : " << list << 
G4endl;
 
 1308       G4cerr << 
" Format \"" << format << 
"\" is not available for the selected viewer. Current formats availables are : " << list << 
G4endl;
 
 1318 void G4OpenGLViewer::g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
 
 1325     sx = viewport[2] / 
width;
 
 1326     sy = viewport[3] / height;
 
 1327     tx = (viewport[2] + 2.0 * (viewport[0] - x)) / 
width;
 
 1328     ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
 
 1330 #define M(row, col) mat[col*4+row] 
 1360 void G4OpenGLViewer::g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
 
 1361                         GLdouble centerx, GLdouble centery, GLdouble
 
 1363                         GLdouble upx, GLdouble upy, GLdouble upz )
 
 1366         GLfloat x[3], y[3], 
z[3];
 
 1372         z[0] = eyex - centerx;
 
 1373         z[1] = eyey - centery;
 
 1374         z[2] = eyez - centerz;
 
 1375         mag = std::sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
 
 1388         x[0] = y[1] * z[2] - y[2] * z[1];
 
 1389         x[1] = -y[0] * z[2] + y[2] * z[0];
 
 1390         x[2] = y[0] * z[1] - y[1] * z[0];
 
 1393         y[0] = z[1] * x[2] - z[2] * x[1];
 
 1394         y[1] = -z[0] * x[2] + z[2] * x[0];
 
 1395         y[2] = z[0] * x[1] - z[1] * x[0];
 
 1402         mag = std::sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
 
 1409         mag = std::sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
 
 1416 #define M(row,col)  mat[col*4+row] 
 1437         glTranslatef(-eyex, -eyey, -eyez);
 
 1442 #ifdef G4OPENGL_VERSION_2 
 1445 void G4OpenGLViewer::setVboDrawer(G4OpenGLVboDrawer* drawer) {
 
 1446   fVboDrawer = drawer;
 
 1448     G4OpenGLSceneHandler& sh = 
dynamic_cast<G4OpenGLSceneHandler&
>(fSceneHandler);
 
 1449     sh.setVboDrawer(fVboDrawer);
 
 1450   } 
catch(std::bad_cast exp) { }
 
 1457   std::ostringstream txt;
 
 1461   txt << 
"Hit: " << fHitNumber << 
", Sub-hit: " << fSubHitNumber << 
", PickName: " << fPickName << 
"\n";
 
 1463   for (
unsigned int a=0; a<fAttributes.size(); a++) {
 
 1464     txt << fAttributes[
a] << 
"\n";
 
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const 
 
HepGeom::Point3D< G4double > G4Point3D
 
HepGeom::Vector3D< G4double > G4Vector3D
 
G4Point3D GetPosition() const 
 
G4GLOB_DLL std::ostream G4cout
 
std::vector< G4Plane3D > G4Planes
 
HepGeom::Plane3D< G4double > G4Plane3D
 
void print(const std::vector< T > &data)
 
static const G4double pos
 
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const 
 
G4GLOB_DLL std::ostream G4cerr
 
HepGeom::Normal3D< G4double > G4Normal3D