CMS 3D CMS Logo

FWTGLViewer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Subsystem/Package
4 // Class : FWTGLViewer
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author:
10 // Created: Tue, 03 Feb 2015 21:45:22 GMT
11 //
12 
13 // system include files
14 
15 #include <stdexcept>
16 
17 // user include files
18 
19 #include "TMath.h"
20 
21 #include "TGLIncludes.h"
22 #include "TGLFBO.h"
23 #include "TGLWidget.h"
24 
26 
27 
28 //
29 // constants, enums and typedefs
30 //
31 
32 //
33 // static data member definitions
34 //
35 
36 //
37 // constructors and destructor
38 //
39 FWTGLViewer::FWTGLViewer(const TGWindow *parent) :
40  TGLEmbeddedViewer(parent, nullptr, nullptr, 0),
41  m_fbo(nullptr),
42  m_fbo_w(-1), m_fbo_h(-1)
43 {
44 }
45 
46 // FWTGLViewer::FWTGLViewer(const FWTGLViewer& rhs)
47 // {
48 // // do actual copying here;
49 // }
50 
52 {
53  delete m_fbo;
54 }
55 
56 //
57 // assignment operators
58 //
59 // const FWTGLViewer& FWTGLViewer::operator=(const FWTGLViewer& rhs)
60 // {
61 // //An exception safe implementation is
62 // FWTGLViewer temp(rhs);
63 // swap(rhs);
64 //
65 // return *this;
66 // }
67 
68 //
69 // member functions
70 //
71 
72 //------------------------------------------------------------------------------
73 // Draw functions
74 //------------------------------------------------------------------------------
75 
76 void FWTGLViewer::DrawHiLod(Bool_t swap_buffers)
77 {
78  fRedrawTimer->Stop();
79 
80  // Ignore request if GL window or context not yet availible or shown.
81  if ((!fGLWidget && fGLDevice == -1) || (fGLWidget && !fGLWidget->IsMapped()))
82  {
83  return;
84  }
85 
86  // Take scene draw lock - to be revisited
87  if ( ! TakeLock(kDrawLock))
88  {
89  // If taking drawlock fails the previous draw is still in progress
90  // set timer to do this one later
91  Error("FWTGLViewer::DrawHiLodNoSwap", "viewer locked - skipping this draw.");
92  fRedrawTimer->RequestDraw(100, TGLRnrCtx::kLODHigh);
93  return;
94  }
95 
96  fLOD = TGLRnrCtx::kLODHigh;
97 
98  DoDraw(swap_buffers);
99 }
100 
102 {
103  fGLWidget->SwapBuffers();
104 }
105 
106 //------------------------------------------------------------------------------
107 // FBO functions
108 //------------------------------------------------------------------------------
109 
110 //______________________________________________________________________________
112 {
113  // Generate FBO with same dimensions as the viewport.
114 
115  return GenerateFbo(fViewport.Width(), fViewport.Height(), kFALSE);
116 }
117 
118 //______________________________________________________________________________
119 TGLFBO* FWTGLViewer::MakeFboWidth(Int_t width, Bool_t pixel_object_scale)
120 {
121  // Generate FBO with given width (height scaled proportinally).
122  // If pixel_object_scale is true (default), the corresponding
123  // scaling gets calculated from the current window size.
124 
125  Float_t scale = Float_t(width) / fViewport.Width();
126  Int_t height = TMath::Nint(scale*fViewport.Height());
127 
128  return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
129 }
130 
131 //______________________________________________________________________________
132 TGLFBO* FWTGLViewer::MakeFboHeight(Int_t height, Bool_t pixel_object_scale)
133 {
134  // Generate FBO with given height (width scaled proportinally).
135  // If pixel_object_scale is true (default), the corresponding
136  // scaling gets calculated from the current window size.
137 
138  Float_t scale = Float_t(height) / fViewport.Height();
139  Int_t width = TMath::Nint(scale*fViewport.Width());
140 
141  return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
142 }
143 
144 //______________________________________________________________________________
145 TGLFBO* FWTGLViewer::MakeFboScale (Float_t scale, Bool_t pixel_object_scale)
146 {
147  // Generate FBO with given scale to current window size.
148  // If pixel_object_scale is true (default), the same scaling is
149  // used.
150 
151  Int_t w = TMath::Nint(scale*fViewport.Width());
152  Int_t h = TMath::Nint(scale*fViewport.Height());
153 
154  return GenerateFbo(w, h, pixel_object_scale ? scale : 0);
155 }
156 
157 //______________________________________________________________________________
158 TGLFBO* FWTGLViewer::GenerateFbo(Int_t w, Int_t h, Float_t pixel_object_scale)
159 {
160  // Generate FBO -- function that does the actual work.
161 
162  static const TString eh("FWTGLViewer::SavePictureUsingFBO");
163 
164  if ( ! GLEW_EXT_framebuffer_object)
165  {
166  ::Warning(eh, "Missing FBO support.");
167  }
168 
169  if ( ! TakeLock(kDrawLock)) {
170  ::Error(eh, "viewer locked - try later.");
171  return nullptr;
172  }
173 
174  TUnlocker ulck(this);
175 
176  MakeCurrent();
177 
178  if (m_fbo == nullptr)
179  {
180  m_fbo = new TGLFBO();
181  }
182  if (m_fbo_w != w || m_fbo_h != h)
183  {
184  try
185  {
186  m_fbo->Init(w, h, fGLWidget->GetPixelFormat()->GetSamples());
187  }
188  catch (std::runtime_error& exc)
189  {
190  m_fbo_w = m_fbo_h = -1;
191 
192  ::Error(eh, "%s",exc.what());
193  return nullptr;
194  }
195 
196  m_fbo_w = w; m_fbo_h = h;
197  }
198 
199  TGLRect old_vp(fViewport);
200  SetViewport(0, 0, w, h);
201 
202  Float_t old_scale = 1;
203  if (pixel_object_scale != 0)
204  {
205  old_scale = fRnrCtx->GetRenderScale();
206  fRnrCtx->SetRenderScale(old_scale * pixel_object_scale);
207  }
208 
209  m_fbo->Bind();
210 
211  fLOD = TGLRnrCtx::kLODHigh;
212  fRnrCtx->SetGrabImage(kTRUE);
213 
214  DoDraw(kFALSE);
215 
216  fRnrCtx->SetGrabImage(kFALSE);
217 
218  m_fbo->Unbind();
219 
220  if (pixel_object_scale != 0)
221  {
222  fRnrCtx->SetRenderScale(old_scale);
223  }
224 
225  SetViewport(old_vp);
226 
227  return m_fbo;
228 }
229 
230 //
231 // const member functions
232 //
233 
234 //
235 // static member functions
236 //
edm::ErrorSummaryEntry Error
TGLFBO * MakeFboScale(Float_t scale, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:145
TGLFBO * MakeFboWidth(Int_t width, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:119
TGLFBO * MakeFboHeight(Int_t height, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:132
TGLFBO * GenerateFbo(Int_t w, Int_t h, Float_t pixel_object_scale)
Definition: FWTGLViewer.cc:158
const double w
Definition: UKUtility.cc:23
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
#define nullptr
TGLFBO * m_fbo
Definition: FWTGLViewer.h:62
~FWTGLViewer() override
Definition: FWTGLViewer.cc:51
FWTGLViewer(const TGWindow *parent)
Definition: FWTGLViewer.cc:39
void JustSwap()
Definition: FWTGLViewer.cc:101
void DrawHiLod(Bool_t swap_buffers)
Definition: FWTGLViewer.cc:76
TGLFBO * MakeFbo()
Definition: FWTGLViewer.cc:111