CMS 3D CMS Logo

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