ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SoGL2PSAction.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SoGL2PSAction.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 #ifdef G4VIS_BUILD_OI_DRIVER
27 
28 /*----------------------------HEPVis----------------------------------------*/
29 /* */
30 /* Node: SoGL2PSAction */
31 /* Author: Guy Barrand */
32 /* */
33 /*--------------------------------------------------------------------------*/
34 
35 // this :
37 
38 // Inventor :
39 #include <Inventor/elements/SoViewportRegionElement.h>
40 #include <Inventor/errors/SoDebugError.h>
41 
42 #include "Geant4_gl2ps.h"
43 
44 #include <stdio.h>
45 
46 SO_ACTION_SOURCE(SoGL2PSAction)
48 void SoGL2PSAction::initClass(
49 )
52 {
53  SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRenderAction);
54 }
57  const SbViewportRegion& aViewPortRegion
58 )
59 :SoGLRenderAction(aViewPortRegion)
63 {
64  setFileName("out.ps");
65  SO_ACTION_CONSTRUCTOR(SoGL2PSAction);
66 }
67 
69 )
72 {
73  fFile = ::fopen(fFileName,"w");
74  if(!fFile) {
75  SoDebugError::post("SoGL2PSAction::enableFileWriting",
76  "Cannot open file %s",fFileName.c_str());
77  return false;
78  }
79 #ifdef __COIN__
80 #else //SGI
81  const SbViewportRegion& vpr = getViewportRegion();
82  SoViewportRegionElement::set(getState(),vpr);
83  G4gl2psBegin();
84 #endif
85  return true;
86 }
89 )
92 {
93 #ifdef __COIN__
94 #else //SGI
95  gl2psEndPage();
96 #endif
97  ::fclose(fFile);
98  fFile = 0;
99 }
100 
103  int aWidth
104 ,int aHeight
105 ,float aXorig
106 ,float aYorig
107 ,float aXmove
108 ,float aYmove
109 )
112 {
113  if(!fFile) return false;
114  GLboolean valid;
115  glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID,&valid);
116  if(!valid) return false;
117  float pos[4];
118  glGetFloatv(GL_CURRENT_RASTER_POSITION,pos);
119  int xoff = -(int)(aXmove + aXorig);
120  int yoff = -(int)(aYmove + aYorig);
121  int x = (int)(pos[0] + xoff);
122  int y = (int)(pos[1] + yoff);
123  // Should clip against viewport area :
124  GLint vp[4];
125  glGetIntegerv(GL_VIEWPORT,vp);
126  GLsizei w = aWidth;
127  GLsizei h = aHeight;
128  if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x;
129  if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y;
130  int s = 3 * w * h;
131  if(s<=0) return false;
132  float* image = (float*)::malloc(s * sizeof(float));
133  if(!image) return false;
134  glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image);
135  GLint status = gl2psDrawPixels(w,h,xoff,yoff,GL_RGB,GL_FLOAT,image);
136  ::free(image);
137  return (status!=GL2PS_SUCCESS ? false : true);
138 }
141  SoNode* aNode
142 )
145 {
146  if(fFile) {
147 #ifdef __COIN__
148  const SbViewportRegion& vpr = getViewportRegion();
149  SoViewportRegionElement::set(getState(),vpr);
150  G4gl2psBegin();
151  traverse(aNode);
152  gl2psEndPage();
153 #else //SGI
154  // Should have already do G4gl2psBegin() before
155  SoGLRenderAction::beginTraversal(aNode);
156  // Should do gl2psEndPage() after
157 #endif
158  } else {
159  SoGLRenderAction::beginTraversal(aNode);
160  }
161 }
162 
163 #endif