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 // constants, enums and typedefs
29 //
30 
31 //
32 // static data member definitions
33 //
34 
35 //
36 // constructors and destructor
37 //
39  : TGLEmbeddedViewer(parent, nullptr, nullptr, 0), m_fbo(nullptr), m_fbo_w(-1), m_fbo_h(-1) {}
40 
41 // FWTGLViewer::FWTGLViewer(const FWTGLViewer& rhs)
42 // {
43 // // do actual copying here;
44 // }
45 
47 
48 //
49 // assignment operators
50 //
51 // const FWTGLViewer& FWTGLViewer::operator=(const FWTGLViewer& rhs)
52 // {
53 // //An exception safe implementation is
54 // FWTGLViewer temp(rhs);
55 // swap(rhs);
56 //
57 // return *this;
58 // }
59 
60 //
61 // member functions
62 //
63 
64 //------------------------------------------------------------------------------
65 // Draw functions
66 //------------------------------------------------------------------------------
67 
68 void FWTGLViewer::DrawHiLod(Bool_t swap_buffers) {
69  fRedrawTimer->Stop();
70 
71  // Ignore request if GL window or context not yet availible or shown.
72  if ((!fGLWidget && fGLDevice == -1) || (fGLWidget && !fGLWidget->IsMapped())) {
73  return;
74  }
75 
76  // Take scene draw lock - to be revisited
77  if (!TakeLock(kDrawLock)) {
78  // If taking drawlock fails the previous draw is still in progress
79  // set timer to do this one later
80  Error("FWTGLViewer::DrawHiLodNoSwap", "viewer locked - skipping this draw.");
81  fRedrawTimer->RequestDraw(100, TGLRnrCtx::kLODHigh);
82  return;
83  }
84 
85  fLOD = TGLRnrCtx::kLODHigh;
86 
87  DoDraw(swap_buffers);
88 }
89 
90 void FWTGLViewer::JustSwap() { fGLWidget->SwapBuffers(); }
91 
92 //------------------------------------------------------------------------------
93 // FBO functions
94 //------------------------------------------------------------------------------
95 
96 //______________________________________________________________________________
98  // Generate FBO with same dimensions as the viewport.
99 
100  return GenerateFbo(fViewport.Width(), fViewport.Height(), kFALSE);
101 }
102 
103 //______________________________________________________________________________
104 TGLFBO* FWTGLViewer::MakeFboWidth(Int_t width, Bool_t pixel_object_scale) {
105  // Generate FBO with given width (height scaled proportinally).
106  // If pixel_object_scale is true (default), the corresponding
107  // scaling gets calculated from the current window size.
108 
109  Float_t scale = Float_t(width) / fViewport.Width();
110  Int_t height = TMath::Nint(scale * fViewport.Height());
111 
112  return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
113 }
114 
115 //______________________________________________________________________________
116 TGLFBO* FWTGLViewer::MakeFboHeight(Int_t height, Bool_t pixel_object_scale) {
117  // Generate FBO with given height (width scaled proportinally).
118  // If pixel_object_scale is true (default), the corresponding
119  // scaling gets calculated from the current window size.
120 
121  Float_t scale = Float_t(height) / fViewport.Height();
122  Int_t width = TMath::Nint(scale * fViewport.Width());
123 
124  return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
125 }
126 
127 //______________________________________________________________________________
128 TGLFBO* FWTGLViewer::MakeFboScale(Float_t scale, Bool_t pixel_object_scale) {
129  // Generate FBO with given scale to current window size.
130  // If pixel_object_scale is true (default), the same scaling is
131  // used.
132 
133  Int_t w = TMath::Nint(scale * fViewport.Width());
134  Int_t h = TMath::Nint(scale * fViewport.Height());
135 
136  return GenerateFbo(w, h, pixel_object_scale ? scale : 0);
137 }
138 
139 //______________________________________________________________________________
140 TGLFBO* FWTGLViewer::GenerateFbo(Int_t w, Int_t h, Float_t pixel_object_scale) {
141  // Generate FBO -- function that does the actual work.
142 
143  static const TString eh("FWTGLViewer::SavePictureUsingFBO");
144 
145  if (!GLEW_EXT_framebuffer_object) {
146  ::Warning(eh, "Missing FBO support.");
147  }
148 
149  if (!TakeLock(kDrawLock)) {
150  ::Error(eh, "viewer locked - try later.");
151  return nullptr;
152  }
153 
154  TUnlocker ulck(this);
155 
156  MakeCurrent();
157 
158  if (m_fbo == nullptr) {
159  m_fbo = new TGLFBO();
160  }
161  if (m_fbo_w != w || m_fbo_h != h) {
162  try {
163  m_fbo->Init(w, h, fGLWidget->GetPixelFormat()->GetSamples());
164  } catch (std::runtime_error& exc) {
165  m_fbo_w = m_fbo_h = -1;
166 
167  ::Error(eh, "%s", exc.what());
168  return nullptr;
169  }
170 
171  m_fbo_w = w;
172  m_fbo_h = h;
173  }
174 
175  TGLRect old_vp(fViewport);
176  SetViewport(0, 0, w, h);
177 
178  Float_t old_scale = 1;
179  if (pixel_object_scale != 0) {
180  old_scale = fRnrCtx->GetRenderScale();
181  fRnrCtx->SetRenderScale(old_scale * pixel_object_scale);
182  }
183 
184  m_fbo->Bind();
185 
186  fLOD = TGLRnrCtx::kLODHigh;
187  fRnrCtx->SetGrabImage(kTRUE);
188 
189  DoDraw(kFALSE);
190 
191  fRnrCtx->SetGrabImage(kFALSE);
192 
193  m_fbo->Unbind();
194 
195  if (pixel_object_scale != 0) {
196  fRnrCtx->SetRenderScale(old_scale);
197  }
198 
199  SetViewport(old_vp);
200 
201  return m_fbo;
202 }
203 
204 //
205 // const member functions
206 //
207 
208 //
209 // static member functions
210 //
edm::ErrorSummaryEntry Error
TGLFBO * MakeFboScale(Float_t scale, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:128
TGLFBO * MakeFboWidth(Int_t width, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:104
TGLFBO * MakeFboHeight(Int_t height, Bool_t pixel_object_scale=kTRUE)
Definition: FWTGLViewer.cc:116
TGLFBO * GenerateFbo(Int_t w, Int_t h, Float_t pixel_object_scale)
Definition: FWTGLViewer.cc:140
T w() const
TGLFBO * m_fbo
Definition: FWTGLViewer.h:60
~FWTGLViewer() override
Definition: FWTGLViewer.cc:46
FWTGLViewer(const TGWindow *parent)
Definition: FWTGLViewer.cc:38
void JustSwap()
Definition: FWTGLViewer.cc:90
void DrawHiLod(Bool_t swap_buffers)
Definition: FWTGLViewer.cc:68
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
TGLFBO * MakeFbo()
Definition: FWTGLViewer.cc:97