SoGL2PSAction.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 #ifdef G4VIS_BUILD_OI_DRIVER
00027 
00028 /*----------------------------HEPVis----------------------------------------*/
00029 /*                                                                          */
00030 /* Node:             SoGL2PSAction                                          */
00031 /* Author:           Guy Barrand                                            */
00032 /*                                                                          */
00033 /*--------------------------------------------------------------------------*/
00034 
00035 // this :
00036 #include <HEPVis/actions/SoGL2PSAction.h>
00037 
00038 // Inventor :
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   // Should clip against viewport area :
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     // Should have already do G4gl2psBegin() before
00155     SoGLRenderAction::beginTraversal(aNode);
00156     // Should do gl2psEndPage() after
00157 #endif
00158   } else {
00159     SoGLRenderAction::beginTraversal(aNode);
00160   }
00161 }
00162 
00163 #endif

Generated on Mon May 27 17:50:34 2013 for Geant4 by  doxygen 1.4.7