Logo ROOT   6.12/07
Reference Guide
TGLOutput.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Richard Maunder, Olivier Couet 02/07/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGLOutput.h"
13 
14 #include "Riostream.h"
15 #include "TVirtualPad.h"
16 #include "TVirtualPS.h"
17 #include "TGLViewer.h"
18 #include "TSystem.h" // For gSystem
19 #include "gl2ps.h"
20 #include "TError.h"
21 #include <assert.h>
22 
23 /** \class TGLOutput
24 \ingroup opengl
25 Wrapper class for GL capture & output routines
26 */
27 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Capture viewer to file. Arguments are:
32 /// - 'viewer' - viewer object to capture from
33 /// - 'format' - output format - only postscript types presently.
34 /// One of kEPS_SIMPLE, kEPS_BSP, kPDF_SIMPLE or kPDF_BSP
35 /// See TGLOutput::CapturePostscript() for meanings
36 /// - 'filePath' - file output name. If null defaults to './viewer.eps' or './viewer.pdf'
37 /// depending on format requested
38 ///
39 /// Note : Output files can be large and take considerable time (up to mins)
40 /// to generate
41 
42 Bool_t TGLOutput::Capture(TGLViewer & viewer, EFormat format, const char * filePath)
43 {
44  switch(format) {
45  case(kEPS_SIMPLE):
46  case(kEPS_BSP):
47  case(kPDF_SIMPLE):
48  case(kPDF_BSP): {
49  return CapturePostscript(viewer, format, filePath);
50  }
51  }
52 
53  assert(kFALSE);
54  return kFALSE;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Capture viewer to postscript file. Arguments are:
59 /// - 'viewer' - viewer object to capture from
60 /// - 'format' - output format
61 /// - kEPS_SIMPLE - lower quality EPS
62 /// - kEPS_BSP - higher quality EPS
63 /// - kPDF_SIMPLE - lower quality PDF
64 /// - kPDF_BSP - higher quality PDF
65 /// - 'filePath' - file output name. If null defaults to './viewer.eps' or './viewer.pdf'
66 /// depending on format requested
67 
68 Bool_t TGLOutput::CapturePostscript(TGLViewer & viewer, EFormat format, const char * filePath)
69 {
70  if (!filePath || !filePath[0]) {
71  if (format == kEPS_SIMPLE || format == kEPS_BSP) {
72  filePath = "viewer.eps";
73  } else if (format == kPDF_SIMPLE || format == kPDF_BSP) {
74  filePath = "viewer.pdf";
75  }
76  }
77  Info("TGLOutput::Postscript()", "Start creating %s.", filePath);
78  std::cout << "Please wait.";
79 
80  if (FILE *output = fopen (filePath, "w+b"))
81  {
82  Int_t gl2psFormat;
83  Int_t gl2psSort;
84 
85  switch(format) {
86  case kEPS_SIMPLE:
87  gl2psFormat = GL2PS_EPS;
88  gl2psSort = GL2PS_SIMPLE_SORT;
89  break;
90  case kEPS_BSP:
91  gl2psFormat = GL2PS_EPS;
92  gl2psSort = GL2PS_BSP_SORT;
93  break;
94  case kPDF_SIMPLE:
95  gl2psFormat = GL2PS_PDF;
96  gl2psSort = GL2PS_SIMPLE_SORT;
97  break;
98  case kPDF_BSP:
99  gl2psFormat = GL2PS_PDF;
100  gl2psSort = GL2PS_BSP_SORT;
101  break;
102  default:
103  assert(kFALSE);
104  return kFALSE;
105  }
106  Int_t buffsize = 0, state = GL2PS_OVERFLOW;
107  viewer.DoDraw();
108  viewer.fIsPrinting = kTRUE;
109  while (state == GL2PS_OVERFLOW) {
110  buffsize += 1024*1024;
111  gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
112  gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
113  | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
114  GL_RGBA, 0, NULL,0, 0, 0,
115  buffsize, output, NULL);
116  viewer.DoDraw();
117  state = gl2psEndPage();
118  std::cout << ".";
119  }
120  std::cout << std::endl;
121  fclose (output);
122  viewer.fIsPrinting = kFALSE;
123  if (!gSystem->AccessPathName(filePath)) {
124  Info("TGLOutput::Postscript", "Finished creating %s.", filePath);
125  return kTRUE;
126  }
127  } else {
128  Error("TGLOutput::Postscript", "Failed to create %s. ", filePath);
129  }
130 
131  return kFALSE;
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// this function used by gl-in-pad
136 
138 {
139  Info("TGLOutput::StartEmbeddedPS", "PS output started ...");
140 
141  gVirtualPS->PrintStr("@");
142  gVirtualPS->PrintStr("% Start gl2ps EPS@");
143  gVirtualPS->PrintStr("newpath gsave save@");
144  Double_t xx[2] = {0.}, yy[2] = {0.};
145  xx[0] = gPad->GetUxmin();
146  yy[0] = gPad->GetUymin();
147  xx[1] = gPad->GetUxmax();
148  yy[1] = gPad->GetUymax();
149  gVirtualPS->PrintStr("@");
150 
151  GLint vp[4];
152  glGetIntegerv(GL_VIEWPORT,vp);
153  gVirtualPS->DrawPS(0, xx, yy);
154  gVirtualPS->PrintStr(" exch");
155  xx[0] = xx[1];
156  yy[0] = yy[1];
157  gVirtualPS->DrawPS(0, xx, yy);
158  gVirtualPS->PrintStr(" 4 1 roll exch sub 3 1 roll sub");
159  gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
160  gVirtualPS->PrintStr(" sub exch");
161  gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
162  gVirtualPS->PrintStr(" sub exch");
163  gVirtualPS->WriteInteger((Int_t)(vp[3]));
164  gVirtualPS->WriteInteger((Int_t)(vp[2]));
165  gVirtualPS->PrintStr(" 4 1 roll div 3 1 roll exch div exch scale@");
166  gVirtualPS->PrintStr("@");
167  gVirtualPS->PrintStr("countdictstack@");
168  gVirtualPS->PrintStr("mark@");
169  gVirtualPS->PrintStr("/showpage {} def@");
170 
171  // Close the gVirtualPS output stream
172  std::ofstream *fs = (std::ofstream*)gVirtualPS->GetStream();
173  fs->close();
174 
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 ///this function used by gl-in-pad
179 /// Restore the gVirtualPS output stream
180 
182 {
183  std::ofstream *fs = new std::ofstream(gVirtualPS->GetName(),std::ios::app);
184  gVirtualPS->SetStream(fs);
185  gVirtualPS->PrintStr("@");
186  gVirtualPS->PrintStr("cleartomark@");
187  gVirtualPS->PrintStr("countdictstack exch sub { end } repeat@");
188  gVirtualPS->PrintStr("restore grestore@");
189  gVirtualPS->PrintStr("% End gl2ps EPS@");
190 
191  Info("TGLOutput::CloseEmbeddedPS", "PS output finished");
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 ///this function used by gl-viewer, embedded into pad
196 
198 {
199  StartEmbeddedPS();
200 
201  FILE *output = fopen (gVirtualPS->GetName(), "a");
202  if (!output) {
203  //Quite stupid fix, since if fopen fails, CloseEmbeddedPS will also
204  //fail but still I have to do it.
205  Error("TGLOutput::Capture", "can not open file for embedding ps");
206  CloseEmbeddedPS();
207  return;
208  }
209 
210  Int_t gl2psFormat = GL2PS_EPS;
211  Int_t gl2psSort = GL2PS_BSP_SORT;
212  Int_t buffsize = 0, state = GL2PS_OVERFLOW;
213  viewer.DoDraw();
214  viewer.fIsPrinting = kTRUE;
215 
216  while (state == GL2PS_OVERFLOW) {
217  buffsize += 1024*1024;
218  gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
219  gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
220  | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
221  GL_RGBA, 0, NULL,0, 0, 0,
222  buffsize, output, NULL);
223  viewer.DoDraw();
224  state = gl2psEndPage();
225  std::cout << ".";
226  }
227 
228  std::cout << std::endl;
229  fclose (output);
230  viewer.fIsPrinting = kFALSE;
231 
232  CloseEmbeddedPS();
233 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
virtual void SetStream(std::ofstream *os)
Definition: TVirtualPS.h:72
void DoDraw(Bool_t swap_buffers=kTRUE)
Draw out the viewer.
Definition: TGLViewer.cxx:550
virtual void WriteInteger(Int_t i, Bool_t space=kTRUE)
Write one Integer to the file.
Definition: TVirtualPS.cxx:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static Bool_t CapturePostscript(TGLViewer &viewer, EFormat format, const char *filePath)
Capture viewer to postscript file.
Definition: TGLOutput.cxx:68
Wrapper class for GL capture & output routines.
Definition: TGLOutput.h:26
virtual void PrintStr(const char *string="")
Output the string str in the output buffer.
Definition: TVirtualPS.cxx:72
void Info(const char *location, const char *msgfmt,...)
virtual void * GetStream() const
Definition: TVirtualPS.h:71
void Error(const char *location, const char *msgfmt,...)
static void StartEmbeddedPS()
this function used by gl-in-pad
Definition: TGLOutput.cxx:137
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
Bool_t fIsPrinting
debug mode (forced rebuild + draw scene/frustum/interest boxes)
Definition: TGLViewer.h:159
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition: TGLViewer.h:53
const Bool_t kFALSE
Definition: RtypesCore.h:88
static Bool_t Capture(TGLViewer &viewer, EFormat format, const char *filePath=0)
Capture viewer to file.
Definition: TGLOutput.cxx:42
#define ClassImp(name)
Definition: Rtypes.h:359
double Double_t
Definition: RtypesCore.h:55
static void CloseEmbeddedPS()
this function used by gl-in-pad Restore the gVirtualPS output stream
Definition: TGLOutput.cxx:181
virtual void DrawPS(Int_t n, Float_t *xw, Float_t *yw)=0
R__EXTERN TVirtualPS * gVirtualPS
Definition: TVirtualPS.h:81
#define gPad
Definition: TVirtualPad.h:285
const Bool_t kTRUE
Definition: RtypesCore.h:87