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