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 : |
---|
36 | #include <HEPVis/actions/SoGL2PSAction.h> |
---|
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) |
---|
47 | ////////////////////////////////////////////////////////////////////////////// |
---|
48 | void SoGL2PSAction::initClass( |
---|
49 | ) |
---|
50 | ////////////////////////////////////////////////////////////////////////////// |
---|
51 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
52 | { |
---|
53 | SO_ACTION_INIT_CLASS(SoGL2PSAction,SoGLRenderAction); |
---|
54 | } |
---|
55 | ////////////////////////////////////////////////////////////////////////////// |
---|
56 | SoGL2PSAction::SoGL2PSAction( |
---|
57 | const SbViewportRegion& aViewPortRegion |
---|
58 | ) |
---|
59 | :SoGLRenderAction(aViewPortRegion) |
---|
60 | ,fFileName("out.ps") |
---|
61 | ,fFile(0) |
---|
62 | ////////////////////////////////////////////////////////////////////////////// |
---|
63 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
64 | { |
---|
65 | SO_ACTION_CONSTRUCTOR(SoGL2PSAction); |
---|
66 | } |
---|
67 | ////////////////////////////////////////////////////////////////////////////// |
---|
68 | void SoGL2PSAction::setFileName( |
---|
69 | const char* aFileName |
---|
70 | ) |
---|
71 | ////////////////////////////////////////////////////////////////////////////// |
---|
72 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
73 | { |
---|
74 | fFileName = aFileName; |
---|
75 | } |
---|
76 | ////////////////////////////////////////////////////////////////////////////// |
---|
77 | void SoGL2PSAction::enableFileWriting( |
---|
78 | ) |
---|
79 | ////////////////////////////////////////////////////////////////////////////// |
---|
80 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
81 | { |
---|
82 | fFile = ::fopen(fFileName.getString(),"w"); |
---|
83 | if(!fFile) { |
---|
84 | SoDebugError::post("SoGL2PSAction::enableFileWriting", |
---|
85 | "Cannot open file %s",fFileName.getString()); |
---|
86 | return; |
---|
87 | } |
---|
88 | #ifdef __COIN__ |
---|
89 | #else //SGI |
---|
90 | const SbViewportRegion& vpr = getViewportRegion(); |
---|
91 | SoViewportRegionElement::set(getState(),vpr); |
---|
92 | gl2psBegin(); |
---|
93 | #endif |
---|
94 | } |
---|
95 | ////////////////////////////////////////////////////////////////////////////// |
---|
96 | void SoGL2PSAction::disableFileWriting( |
---|
97 | ) |
---|
98 | ////////////////////////////////////////////////////////////////////////////// |
---|
99 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
100 | { |
---|
101 | #ifdef __COIN__ |
---|
102 | #else //SGI |
---|
103 | gl2psEndPage(); |
---|
104 | #endif |
---|
105 | ::fclose(fFile); |
---|
106 | fFile = 0; |
---|
107 | } |
---|
108 | ////////////////////////////////////////////////////////////////////////////// |
---|
109 | SbBool SoGL2PSAction::fileWritingEnabled( |
---|
110 | ) const |
---|
111 | ////////////////////////////////////////////////////////////////////////////// |
---|
112 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
113 | { |
---|
114 | return (fFile?TRUE:FALSE); |
---|
115 | } |
---|
116 | ////////////////////////////////////////////////////////////////////////////// |
---|
117 | SbBool SoGL2PSAction::addBitmap( |
---|
118 | int aWidth |
---|
119 | ,int aHeight |
---|
120 | ,float aXorig |
---|
121 | ,float aYorig |
---|
122 | ,float aXmove |
---|
123 | ,float aYmove |
---|
124 | ) |
---|
125 | ///////////////////////////////////////////////////////////////////////////// |
---|
126 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
127 | { |
---|
128 | if(!fFile) return FALSE; |
---|
129 | GLboolean valid; |
---|
130 | glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID,&valid); |
---|
131 | if(valid==GL_FALSE) return FALSE; |
---|
132 | float pos[4]; |
---|
133 | glGetFloatv(GL_CURRENT_RASTER_POSITION,pos); |
---|
134 | int xoff = -(int)(aXmove + aXorig); |
---|
135 | int yoff = -(int)(aYmove + aYorig); |
---|
136 | int x = (int)(pos[0] + xoff); |
---|
137 | int y = (int)(pos[1] + yoff); |
---|
138 | // Should clip against viewport area : |
---|
139 | GLint vp[4]; |
---|
140 | glGetIntegerv(GL_VIEWPORT,vp); |
---|
141 | GLsizei w = aWidth; |
---|
142 | GLsizei h = aHeight; |
---|
143 | if(x+w>(vp[0]+vp[2])) w = vp[0]+vp[2]-x; |
---|
144 | if(y+h>(vp[1]+vp[3])) h = vp[1]+vp[3]-y; |
---|
145 | int s = 3 * w * h; |
---|
146 | if(s<=0) return FALSE; |
---|
147 | float* image = (float*)::malloc(s * sizeof(float)); |
---|
148 | if(!image) return FALSE; |
---|
149 | glReadPixels(x,y,w,h,GL_RGB,GL_FLOAT,image); |
---|
150 | GLint status = gl2psDrawPixels(w,h,xoff,yoff,GL_RGB,GL_FLOAT,image); |
---|
151 | ::free(image); |
---|
152 | return (status!=GL2PS_SUCCESS ? FALSE : TRUE); |
---|
153 | } |
---|
154 | ////////////////////////////////////////////////////////////////////////////// |
---|
155 | void SoGL2PSAction::beginViewport( |
---|
156 | ) |
---|
157 | ///////////////////////////////////////////////////////////////////////////// |
---|
158 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
159 | { |
---|
160 | if(!fFile) return; |
---|
161 | GLint vp[4]; |
---|
162 | glGetIntegerv(GL_VIEWPORT,vp); |
---|
163 | gl2psBeginViewport(vp); |
---|
164 | } |
---|
165 | ////////////////////////////////////////////////////////////////////////////// |
---|
166 | void SoGL2PSAction::endViewport( |
---|
167 | ) |
---|
168 | ///////////////////////////////////////////////////////////////////////////// |
---|
169 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
170 | { |
---|
171 | if(!fFile) return; |
---|
172 | gl2psEndViewport(); |
---|
173 | } |
---|
174 | ////////////////////////////////////////////////////////////////////////////// |
---|
175 | void SoGL2PSAction::beginTraversal( |
---|
176 | SoNode* aNode |
---|
177 | ) |
---|
178 | ////////////////////////////////////////////////////////////////////////////// |
---|
179 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
180 | { |
---|
181 | if(fFile) { |
---|
182 | #ifdef __COIN__ |
---|
183 | const SbViewportRegion& vpr = getViewportRegion(); |
---|
184 | SoViewportRegionElement::set(getState(),vpr); |
---|
185 | gl2psBegin(); |
---|
186 | traverse(aNode); |
---|
187 | gl2psEndPage(); |
---|
188 | #else //SGI |
---|
189 | SoGLRenderAction::beginTraversal(aNode); |
---|
190 | #endif |
---|
191 | } else { |
---|
192 | SoGLRenderAction::beginTraversal(aNode); |
---|
193 | } |
---|
194 | } |
---|
195 | ////////////////////////////////////////////////////////////////////////////// |
---|
196 | void SoGL2PSAction::gl2psBegin( |
---|
197 | ) |
---|
198 | ////////////////////////////////////////////////////////////////////////////// |
---|
199 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!// |
---|
200 | { |
---|
201 | if(!fFile) return; |
---|
202 | int options = GL2PS_OCCLUSION_CULL | |
---|
203 | GL2PS_BEST_ROOT | GL2PS_SILENT | GL2PS_DRAW_BACKGROUND; |
---|
204 | int sort = GL2PS_BSP_SORT; |
---|
205 | //int sort = GL2PS_SIMPLE_SORT; |
---|
206 | |
---|
207 | const SbViewportRegion& vpr = getViewportRegion(); |
---|
208 | SoViewportRegionElement::set(getState(),vpr); |
---|
209 | |
---|
210 | const SbVec2s& win = vpr.getWindowSize(); |
---|
211 | GLint vp[4]; |
---|
212 | vp[0] = 0; |
---|
213 | vp[1] = 0; |
---|
214 | vp[2] = win[0]; |
---|
215 | vp[3] = win[1]; |
---|
216 | |
---|
217 | int bufsize = 0; |
---|
218 | gl2psBeginPage("title","HEPVis::SoGL2PSAction", |
---|
219 | vp, |
---|
220 | GL2PS_EPS, |
---|
221 | sort, |
---|
222 | options, |
---|
223 | GL_RGBA,0, NULL,0,0,0, |
---|
224 | bufsize, |
---|
225 | fFile,fFileName.getString()); |
---|
226 | } |
---|
227 | |
---|
228 | #endif |
---|