Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGLViewer.h
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Richard Maunder 25/05/2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2004, 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 #ifndef ROOT_TGLViewer
13 #define ROOT_TGLViewer
14 
15 #include "TGLViewerBase.h"
16 #include "TGLRnrCtx.h"
17 #include "TGLSelectRecord.h"
18 
19 #include "TVirtualViewer3D.h"
20 #include "TBuffer3D.h"
21 
22 #include "TGLPerspectiveCamera.h"
23 #include "TGLOrthoCamera.h"
24 #include "TGLClip.h"
25 
26 #include "TTimer.h"
27 #include "TPoint.h"
28 
29 #include "TGEventHandler.h"
30 
31 #include "GuiTypes.h"
32 #include "TQObject.h"
33 
34 #include <vector>
35 
36 class TGLSceneBase;
37 class TGLRedrawTimer;
38 class TGLViewerEditor;
39 class TGLWidget;
40 class TGLLightSet;
41 class TGLClipSet;
42 class TGLManipSet;
43 class TGLCameraOverlay;
44 class TGLContextIdentity;
45 class TGLAutoRotator;
46 class TTimer;
47 class TImage;
48 
49 class TContextMenu;
50 class TGedEditor;
51 class TGLPShapeObj;
52 
53 class TGLViewer : public TVirtualViewer3D,
54  public TGLViewerBase,
55  public TQObject
56 
57 {
58  friend class TGLOutput;
59  friend class TGLEventHandler;
60  friend class TGLAutoRotator;
61 public:
62 
63  enum ECameraType { kCameraPerspXOZ, kCameraPerspYOZ, kCameraPerspXOY,
64  kCameraOrthoXOY, kCameraOrthoXOZ, kCameraOrthoZOY, kCameraOrthoZOX,
65  kCameraOrthoXnOY, kCameraOrthoXnOZ, kCameraOrthoZnOY, kCameraOrthoZnOX };
66 
67  enum ESecSelType { // When to do secondary-selection:
68  kOnRequest, // - on request - when Mod1 is pressed or logical-shape requests it;
69  kOnKeyMod1 // - only when Mod1 is pressed.
70  };
71 
72 private:
73  TGLViewer(const TGLViewer &); // Not implemented
74  TGLViewer & operator=(const TGLViewer &); // Not implemented
75 
76  void InitSecondaryObjects();
77 
78 protected:
79  // External handles
80  TVirtualPad *fPad; //! external pad - remove replace with signal
81 
82  // GUI Handles
83  TContextMenu *fContextMenu; //!
84 
85  // Cameras
86  // TODO: Put in vector and allow external creation
87  TGLPerspectiveCamera fPerspectiveCameraXOZ; //!
88  TGLPerspectiveCamera fPerspectiveCameraYOZ; //!
89  TGLPerspectiveCamera fPerspectiveCameraXOY; //!
90  TGLOrthoCamera fOrthoXOYCamera; //!
91  TGLOrthoCamera fOrthoXOZCamera; //!
92  TGLOrthoCamera fOrthoZOYCamera; //!
93  TGLOrthoCamera fOrthoZOXCamera; //!
94  TGLOrthoCamera fOrthoXnOYCamera; //!
95  TGLOrthoCamera fOrthoXnOZCamera; //!
96  TGLOrthoCamera fOrthoZnOYCamera; //!
97  TGLOrthoCamera fOrthoZnOXCamera; //!
98  TGLCamera *fCurrentCamera; //!
99  TGLAutoRotator *fAutoRotator; //!
100 
101  // Stereo
102  Bool_t fStereo; //! use stereo rendering
103  Bool_t fStereoQuadBuf; //! draw quad buffer or left/right stereo in left/right half of window
104  Float_t fStereoZeroParallax; //! position of zero-parallax plane: 0 - near clipping plane, 1 - far clipping plane
105  Float_t fStereoEyeOffsetFac; //!
106  Float_t fStereoFrustumAsymFac; //!
107 
108  // Lights
109  TGLLightSet *fLightSet; //!
110  // Clipping
111  TGLClipSet *fClipSet; //!
112  // Selected physical
113  TGLSelectRecord fCurrentSelRec; //! select record in use as selected
114  TGLSelectRecord fSelRec; //! select record from last select (should go to context)
115  TGLSelectRecord fSecSelRec; //! select record from last secondary select (should go to context)
116  TGLManipSet *fSelectedPShapeRef; //!
117  // Overlay
118  TGLOverlayElement *fCurrentOvlElm; //! current overlay element
119  TGLOvlSelectRecord fOvlSelRec; //! select record from last overlay select
120 
121  TGEventHandler *fEventHandler; //! event handler
122  TGedEditor *fGedEditor; //! GED editor
123  TGLPShapeObj *fPShapeWrap;
124 
125  // Mouse ineraction
126 public:
127  enum EPushAction { kPushStd,
128  kPushCamCenter, kPushAnnotate };
129  enum EDragAction { kDragNone,
130  kDragCameraRotate, kDragCameraTruck, kDragCameraDolly,
131  kDragOverlay };
132 protected:
133  EPushAction fPushAction;
134  EDragAction fDragAction;
135 
136  // Redraw timer
137  TGLRedrawTimer *fRedrawTimer; //! timer for triggering redraws
138  Float_t fMaxSceneDrawTimeHQ; //! max time for scene rendering at high LOD (in ms)
139  Float_t fMaxSceneDrawTimeLQ; //! max time for scene rendering at high LOD (in ms)
140 
141  TGLRect fViewport; //! viewport - drawn area
142  TGLColorSet fDarkColorSet; //! color-set with dark background
143  TGLColorSet fLightColorSet; //! color-set with light background
144  Float_t fPointScale; //! size scale for points
145  Float_t fLineScale; //! width scale for lines
146  Bool_t fSmoothPoints; //! smooth point edge rendering
147  Bool_t fSmoothLines; //! smooth line edge rendering
148  Int_t fAxesType; //! axes type
149  Bool_t fAxesDepthTest; //! remove guides hidden-lines
150  Bool_t fReferenceOn; //! reference marker on?
151  TGLVertex3 fReferencePos; //! reference position
152  Bool_t fDrawCameraCenter; //! reference marker on?
153  TGLCameraOverlay *fCameraOverlay; //! markup size of viewport in scene units
154 
155  Bool_t fSmartRefresh; //! cache logicals during scene rebuilds
156 
157  // Debug tracing (for scene rebuilds)
158  Bool_t fDebugMode; //! debug mode (forced rebuild + draw scene/frustum/interest boxes)
159  Bool_t fIsPrinting; //!
160  TString fPictureFileName; //! default file-name for SavePicture()
161  Float_t fFader; //! fade the view (0 - no fade/default, 1 - full fade/no rendering done)
162 
163  static TGLColorSet fgDefaultColorSet; //! a shared, default color-set
164  static Bool_t fgUseDefaultColorSetForNewViewers; //! name says it all
165 
166 
167  ///////////////////////////////////////////////////////////////////////
168  // Methods
169  ///////////////////////////////////////////////////////////////////////
170 
171  virtual void SetupClipObject();
172 
173  // Drawing - can tidy up/remove lots when TGLManager added
174  void InitGL();
175  void PreDraw();
176  void PostDraw();
177  void FadeView(Float_t alpha);
178  void MakeCurrent() const;
179  void SwapBuffers() const;
180 
181  // Cameras
182  void SetViewport(Int_t x, Int_t y, Int_t width, Int_t height);
183  void SetViewport(const TGLRect& vp);
184  void SetupCameras(Bool_t reset);
185 
186 protected:
187  TGLWidget *fGLWidget;
188  Int_t fGLDevice; //!for embedded gl viewer
189  TGLContextIdentity *fGLCtxId; //!for embedded gl viewer
190 
191  // Updata/camera-reset behaviour
192  Bool_t fIgnoreSizesOnUpdate; // ignore sizes of bounding-boxes on update
193  Bool_t fResetCamerasOnUpdate; // reposition camera on each update
194  Bool_t fResetCamerasOnNextUpdate; // reposition camera on next update
195 
196 public:
197  TGLViewer(TVirtualPad* pad, Int_t x, Int_t y, Int_t width, Int_t height);
198  TGLViewer(TVirtualPad* pad);
199  virtual ~TGLViewer();
200 
201  // TVirtualViewer3D interface ... mostly a facade
202 
203  // Forward to TGLScenePad
204  virtual Bool_t CanLoopOnPrimitives() const { return kTRUE; }
205  virtual void PadPaint(TVirtualPad* pad);
206  // Actually used by GL-in-pad
207  virtual Int_t DistancetoPrimitive(Int_t px, Int_t py);
208  virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py);
209  // Only implemented because they're abstract ... should throw an
210  // exception or assert they are not called.
211  virtual Bool_t PreferLocalFrame() const { return kTRUE; }
212  virtual void BeginScene() {}
213  virtual Bool_t BuildingScene() const { return kFALSE; }
214  virtual void EndScene() {}
215  virtual Int_t AddObject(const TBuffer3D&, Bool_t* = 0) { return TBuffer3D::kNone; }
216  virtual Int_t AddObject(UInt_t, const TBuffer3D&, Bool_t* = 0) { return TBuffer3D::kNone; }
217  virtual Bool_t OpenComposite(const TBuffer3D&, Bool_t* = 0) { return kFALSE; }
218  virtual void CloseComposite() {}
219  virtual void AddCompositeOp(UInt_t) {}
220 
221  virtual void PrintObjects();
222  virtual void ResetCameras() { SetupCameras(kTRUE); }
223  virtual void ResetCamerasAfterNextUpdate() { fResetCamerasOnNextUpdate = kTRUE; }
224 
225  TGLWidget* GetGLWidget() { return fGLWidget; }
226 
227  virtual void CreateGLWidget() {}
228  virtual void DestroyGLWidget() {}
229 
230  Int_t GetDev() const { return fGLDevice; }
231  Bool_t GetSmartRefresh() const { return fSmartRefresh; }
232  void SetSmartRefresh(Bool_t smart_ref) { fSmartRefresh = smart_ref; }
233 
234  TGLColorSet& RefDarkColorSet() { return fDarkColorSet; }
235  TGLColorSet& RefLightColorSet() { return fLightColorSet; }
236  TGLColorSet& ColorSet() { return * fRnrCtx->GetBaseColorSet(); }
237  void UseDarkColorSet();
238  void UseLightColorSet();
239  void SwitchColorSet();
240 
241  void UseDefaultColorSet(Bool_t x);
242  Bool_t IsUsingDefaultColorSet() const;
243  Bool_t IsColorSetDark() const;
244 
245  void SetClearColor(Color_t col);
246 
247  static TGLColorSet& GetDefaultColorSet();
248  static void UseDefaultColorSetForNewViewers(Bool_t x);
249  static Bool_t IsUsingDefaultColorSetForNewViewers();
250 
251  const TGLRect& RefViewport() const { return fViewport; }
252  Int_t ViewportDiagonal() const { return fViewport.Diagonal(); }
253 
254  Float_t GetPointScale() const { return fPointScale; }
255  Float_t GetLineScale() const { return fLineScale; }
256  void SetPointScale(Float_t s) { fPointScale = s; }
257  void SetLineScale (Float_t s) { fLineScale = s; }
258  Bool_t GetSmoothPoints() const { return fSmoothPoints; }
259  Bool_t GetSmoothLines() const { return fSmoothLines; }
260  void SetSmoothPoints(Bool_t s){ fSmoothPoints = s; }
261  void SetSmoothLines(Bool_t s) { fSmoothLines = s; }
262 
263  TGLLightSet* GetLightSet() const { return fLightSet; }
264  TGLClipSet * GetClipSet() const { return fClipSet; }
265  Bool_t GetClipAutoUpdate() const { return fClipSet->GetAutoUpdate(); }
266  void SetClipAutoUpdate(Bool_t x) { fClipSet->SetAutoUpdate(x); }
267 
268  // External GUI component interface
269  TGLCamera & CurrentCamera() const { return *fCurrentCamera; }
270  TGLCamera & RefCamera(ECameraType camera);
271  void SetCurrentCamera(ECameraType camera);
272  void SetOrthoCamera(ECameraType camera, Double_t zoom, Double_t dolly,
273  Double_t center[3], Double_t hRotate, Double_t vRotate);
274  void SetPerspectiveCamera(ECameraType camera, Double_t fov, Double_t dolly,
275  Double_t center[3], Double_t hRotate, Double_t vRotate);
276  void ReinitializeCurrentCamera(const TGLVector3& hAxis, const TGLVector3& vAxis, Bool_t redraw=kTRUE);
277  void GetGuideState(Int_t & axesType, Bool_t & axesDepthTest, Bool_t & referenceOn, Double_t* referencePos) const;
278  void SetGuideState(Int_t axesType, Bool_t axesDepthTest, Bool_t referenceOn, const Double_t* referencePos);
279  void SetDrawCameraCenter(Bool_t x);
280  Bool_t GetDrawCameraCenter() { return fDrawCameraCenter; }
281  void PickCameraCenter() { fPushAction = kPushCamCenter; RefreshPadEditor(this); }
282  void PickAnnotate() { fPushAction = kPushAnnotate; RefreshPadEditor(this); }
283  TGLCameraOverlay* GetCameraOverlay() const { return fCameraOverlay; }
284  void SetCameraOverlay(TGLCameraOverlay* m) { fCameraOverlay = m; }
285  TGLAutoRotator* GetAutoRotator();
286  void SetAutoRotator(TGLAutoRotator* ar);
287 
288  // Stereo
289  Bool_t GetStereo() const { return fStereo; }
290  Float_t GetStereoZeroParallax() const { return fStereoZeroParallax; }
291  Float_t GetStereoEyeOffsetFac() const { return fStereoEyeOffsetFac; }
292  Float_t GetStereoFrustumAsymFac() const { return fStereoFrustumAsymFac; }
293 
294  void SetStereo(Bool_t stereo, Bool_t quad_buf=kTRUE);
295  void SetStereoZeroParallax(Float_t f) { fStereoZeroParallax = f; }
296  void SetStereoEyeOffsetFac(Float_t f) { fStereoEyeOffsetFac = f; }
297  void SetStereoFrustumAsymFac(Float_t f) { fStereoFrustumAsymFac = f; }
298 
299  // Push / drag action
300  EPushAction GetPushAction() const { return fPushAction; }
301  EDragAction GetDragAction() const { return fDragAction; }
302 
303  const TGLPhysicalShape * GetSelected() const;
304 
305 
306  // Draw and selection
307 
308  // Scene rendering timeouts
309  Float_t GetMaxSceneDrawTimeHQ() const { return fMaxSceneDrawTimeHQ; }
310  Float_t GetMaxSceneDrawTimeLQ() const { return fMaxSceneDrawTimeLQ; }
311  void SetMaxSceneDrawTimeHQ(Float_t t) { fMaxSceneDrawTimeHQ = t; }
312  void SetMaxSceneDrawTimeLQ(Float_t t) { fMaxSceneDrawTimeLQ = t; }
313 
314  // Request methods post cross thread request via TROOT::ProcessLineFast().
315  void RequestDraw(Short_t LOD = TGLRnrCtx::kLODMed); // Cross thread draw request
316  virtual void PreRender();
317  virtual void Render();
318  virtual void PostRender();
319  void DoDraw(Bool_t swap_buffers=kTRUE);
320  void DoDrawMono(Bool_t swap_buffers);
321  void DoDrawStereo(Bool_t swap_buffers);
322 
323  void DrawGuides();
324  void DrawDebugInfo();
325 
326  Bool_t RequestSelect(Int_t x, Int_t y); // Cross thread select request
327  Bool_t DoSelect(Int_t x, Int_t y); // First level selecton (shapes/objects).
328  Bool_t RequestSecondarySelect(Int_t x, Int_t y); // Cross thread secondary select request
329  Bool_t DoSecondarySelect(Int_t x, Int_t y); // Second level selecton (inner structure).
330  void ApplySelection();
331 
332  Bool_t RequestOverlaySelect(Int_t x, Int_t y); // Cross thread select request
333  Bool_t DoOverlaySelect(Int_t x, Int_t y); // Window coords origin top left
334 
335  // Saving of screen image
336  Bool_t SavePicture();
337  Bool_t SavePicture(const TString &fileName);
338  Bool_t SavePictureUsingBB (const TString &fileName);
339  Bool_t SavePictureUsingFBO(const TString &fileName, Int_t w, Int_t h, Float_t pixel_object_scale=0);
340  Bool_t SavePictureWidth (const TString &fileName, Int_t width, Bool_t pixel_object_scale=kTRUE);
341  Bool_t SavePictureHeight(const TString &fileName, Int_t height, Bool_t pixel_object_scale=kTRUE);
342  Bool_t SavePictureScale (const TString &fileName, Float_t scale, Bool_t pixel_object_scale=kTRUE);
343 
344  // Methods returning screen image
345  TImage* GetPictureUsingBB();
346  TImage* GetPictureUsingFBO(Int_t w, Int_t h,Float_t pixel_object_scale=0);
347 
348  const char* GetPictureFileName() const { return fPictureFileName.Data(); }
349  void SetPictureFileName(const TString& f) { fPictureFileName = f; }
350  Float_t GetFader() const { return fFader; }
351  void SetFader(Float_t x) { fFader = x; }
352  void AutoFade(Float_t fade, Float_t time=1, Int_t steps=10);
353 
354  // Update/camera-reset
355  void UpdateScene(Bool_t redraw=kTRUE);
356  Bool_t GetIgnoreSizesOnUpdate() const { return fIgnoreSizesOnUpdate; }
357  void SetIgnoreSizesOnUpdate(Bool_t v) { fIgnoreSizesOnUpdate = v; }
358  void ResetCurrentCamera();
359  Bool_t GetResetCamerasOnUpdate() const { return fResetCamerasOnUpdate; }
360  void SetResetCamerasOnUpdate(Bool_t v) { fResetCamerasOnUpdate = v; }
361 
362  virtual void PostSceneBuildSetup(Bool_t resetCameras);
363 
364  virtual void Activated() { Emit("Activated()"); } // *SIGNAL*
365 
366  virtual void MouseIdle(TGLPhysicalShape*,UInt_t,UInt_t); // *SIGNAL*
367  virtual void MouseOver(TGLPhysicalShape*); // *SIGNAL*
368  virtual void MouseOver(TGLPhysicalShape*, UInt_t state); // *SIGNAL*
369  virtual void MouseOver(TObject *obj, UInt_t state); // *SIGNAL*
370  virtual void ReMouseOver(TObject *obj, UInt_t state); // *SIGNAL*
371  virtual void UnMouseOver(TObject *obj, UInt_t state); // *SIGNAL*
372 
373  virtual void Clicked(TObject *obj); //*SIGNAL*
374  virtual void Clicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
375  virtual void ReClicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
376  virtual void UnClicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
377  virtual void DoubleClicked() { Emit("DoubleClicked()"); } // *SIGNAL*
378 
379  TGEventHandler *GetEventHandler() const { return fEventHandler; }
380  virtual void SetEventHandler(TGEventHandler *handler);
381 
382  TGedEditor* GetGedEditor() const { return fGedEditor; }
383  virtual void SetGedEditor(TGedEditor* ed) { fGedEditor = ed; }
384 
385  virtual void SelectionChanged();
386  virtual void OverlayDragFinished();
387  virtual void RefreshPadEditor(TObject* obj=0);
388 
389  virtual void RemoveOverlayElement(TGLOverlayElement* el);
390 
391  TGLSelectRecord& GetSelRec() { return fSelRec; }
392  TGLOvlSelectRecord& GetOvlSelRec() { return fOvlSelRec; }
393  TGLOverlayElement* GetCurrentOvlElm() const { return fCurrentOvlElm; }
394  void ClearCurrentOvlElm();
395 
396  ClassDef(TGLViewer,0) // Standard ROOT GL viewer.
397 };
398 
399 
400 
401 // TODO: Find a better place/way to do this
402 class TGLRedrawTimer : public TTimer
403 {
404 private:
405  TGLViewer & fViewer;
406  Short_t fRedrawLOD;
407  Bool_t fPending;
408 public:
409  TGLRedrawTimer(TGLViewer & viewer) :
410  fViewer(viewer), fRedrawLOD(TGLRnrCtx::kLODHigh), fPending(kFALSE) {}
411  ~TGLRedrawTimer() {}
412  void RequestDraw(Int_t milliSec, Short_t redrawLOD)
413  {
414  if (fPending) TurnOff(); else fPending = kTRUE;
415  if (redrawLOD < fRedrawLOD) fRedrawLOD = redrawLOD;
416  TTimer::Start(milliSec, kTRUE);
417  }
418  Bool_t IsPending() const { return fPending; }
419  virtual void Stop()
420  {
421  if (fPending) { TurnOff(); fPending = kFALSE; }
422  }
423  Bool_t Notify()
424  {
425  TurnOff();
426  fPending = kFALSE;
427  fViewer.RequestDraw(fRedrawLOD);
428  fRedrawLOD = TGLRnrCtx::kLODHigh;
429  return kTRUE;
430  }
431 };
432 
433 class TGLFaderHelper {
434 private:
435  TGLFaderHelper(const TGLFaderHelper&); // Not implemented
436  TGLFaderHelper& operator=(const TGLFaderHelper&); // Not implemented
437 
438 public:
439  TGLViewer *fViewer;
440  Float_t fFadeTarget;
441  Float_t fTime;
442  Int_t fNSteps;
443 
444  TGLFaderHelper() :
445  fViewer(0), fFadeTarget(0), fTime(0), fNSteps(0) {}
446  TGLFaderHelper(TGLViewer* v, Float_t fade, Float_t time, Int_t steps) :
447  fViewer(v),fFadeTarget(fade), fTime(time), fNSteps(steps) {}
448  virtual ~TGLFaderHelper() {}
449 
450  void MakeFadeStep();
451 
452  ClassDef(TGLFaderHelper, 0);
453 };
454 
455 #endif // ROOT_TGLViewer