TGLViewer.h

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLViewer.h 37327 2010-12-06 12:23:33Z matevz $
00002 // Author:  Richard Maunder  25/05/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TGLViewer
00013 #define ROOT_TGLViewer
00014 
00015 #include "TGLViewerBase.h"
00016 #include "TGLRnrCtx.h"
00017 #include "TGLSelectRecord.h"
00018 
00019 #include "TVirtualViewer3D.h"
00020 #include "TBuffer3D.h"
00021 
00022 #include "TGLPerspectiveCamera.h"
00023 #include "TGLOrthoCamera.h"
00024 #include "TGLClip.h"
00025 
00026 #include "TTimer.h"
00027 #include "TPoint.h"
00028 
00029 #include "TGEventHandler.h"
00030 
00031 #include "GuiTypes.h"
00032 #include "TQObject.h"
00033 
00034 #include <vector>
00035 
00036 class TGLSceneBase;
00037 class TGLRedrawTimer;
00038 class TGLViewerEditor;
00039 class TGLWidget;
00040 class TGLLightSet;
00041 class TGLClipSet;
00042 class TGLManipSet;
00043 class TGLCameraOverlay;
00044 class TGLContextIdentity;
00045 class TGLAutoRotator;
00046 class TTimer;
00047 
00048 class TContextMenu;
00049 class TGedEditor;
00050 class TGLPShapeObj;
00051 
00052 class TGLViewer : public TVirtualViewer3D,
00053                   public TGLViewerBase,
00054                   public TQObject
00055 
00056 {
00057    friend class TGLOutput;
00058    friend class TGLEventHandler;
00059 public:
00060 
00061    enum ECameraType { kCameraPerspXOZ,  kCameraPerspYOZ,  kCameraPerspXOY,
00062                       kCameraOrthoXOY,  kCameraOrthoXOZ,  kCameraOrthoZOY,
00063                       kCameraOrthoXnOY, kCameraOrthoXnOZ, kCameraOrthoZnOY };
00064 
00065    enum ESecSelType { // When to do secondary-selection:
00066       kOnRequest,     // - on request - when Mod1 is pressed or logical-shape requests it;
00067       kOnKeyMod1      // - only when Mod1 is pressed.
00068    };
00069 
00070 private:
00071    TGLViewer(const TGLViewer &);             // Not implemented
00072    TGLViewer & operator=(const TGLViewer &); // Not implemented
00073 
00074    void InitSecondaryObjects();
00075 
00076 protected:
00077    // External handles
00078    TVirtualPad   *fPad;         //! external pad - remove replace with signal
00079 
00080    // GUI Handles
00081    TContextMenu  *fContextMenu; //!
00082 
00083    // Cameras
00084    // TODO: Put in vector and allow external creation
00085    TGLPerspectiveCamera fPerspectiveCameraXOZ; //!
00086    TGLPerspectiveCamera fPerspectiveCameraYOZ; //!
00087    TGLPerspectiveCamera fPerspectiveCameraXOY; //!
00088    TGLOrthoCamera       fOrthoXOYCamera;       //!
00089    TGLOrthoCamera       fOrthoXOZCamera;       //!
00090    TGLOrthoCamera       fOrthoZOYCamera;       //!
00091    TGLOrthoCamera       fOrthoXnOYCamera;      //!
00092    TGLOrthoCamera       fOrthoXnOZCamera;      //!
00093    TGLOrthoCamera       fOrthoZnOYCamera;      //!
00094    TGLCamera           *fCurrentCamera;        //!
00095    TGLAutoRotator      *fAutoRotator;          //!
00096 
00097    // Stereo
00098    Bool_t               fStereo;               //! use stereo rendering
00099    Float_t              fStereoZeroParallax;   //! position of zero-parallax plane: 0 - near clipping plane, 1 - far clipping plane
00100    Float_t              fStereoEyeOffsetFac;   //!
00101    Float_t              fStereoFrustumAsymFac; //!
00102 
00103    // Lights
00104    TGLLightSet         *fLightSet;             //!
00105    // Clipping
00106    TGLClipSet          *fClipSet;              //!
00107    // Selected physical
00108    TGLSelectRecord      fCurrentSelRec;        //! select record in use as selected
00109    TGLSelectRecord      fSelRec;               //! select record from last select (should go to context)
00110    TGLSelectRecord      fSecSelRec;            //! select record from last secondary select (should go to context)
00111    TGLManipSet         *fSelectedPShapeRef;    //!
00112    // Overlay
00113    TGLOverlayElement   *fCurrentOvlElm;        //! current overlay element
00114    TGLOvlSelectRecord   fOvlSelRec;            //! select record from last overlay select
00115 
00116    TGEventHandler      *fEventHandler;         //! event handler
00117    TGedEditor          *fGedEditor;            //! GED editor
00118    TGLPShapeObj        *fPShapeWrap;
00119 
00120    // Mouse ineraction
00121 public:
00122    enum EPushAction   { kPushStd,
00123                         kPushCamCenter, kPushAnnotate };
00124    enum EDragAction   { kDragNone,
00125                         kDragCameraRotate, kDragCameraTruck, kDragCameraDolly,
00126                         kDragOverlay };
00127 protected:
00128    EPushAction          fPushAction;
00129    EDragAction          fDragAction;
00130 
00131    // Redraw timer
00132    TGLRedrawTimer      *fRedrawTimer;        //! timer for triggering redraws
00133    Float_t              fMaxSceneDrawTimeHQ; //! max time for scene rendering at high LOD (in ms)
00134    Float_t              fMaxSceneDrawTimeLQ; //! max time for scene rendering at high LOD (in ms)
00135 
00136    TGLRect        fViewport;       //! viewport - drawn area
00137    TGLColorSet    fDarkColorSet;   //! color-set with dark background
00138    TGLColorSet    fLightColorSet;  //! color-set with light background
00139    Float_t        fPointScale;     //! size scale for points
00140    Float_t        fLineScale;      //! width scale for lines
00141    Bool_t         fSmoothPoints;   //! smooth point edge rendering
00142    Bool_t         fSmoothLines;    //! smooth line edge rendering
00143    Int_t          fAxesType;       //! axes type
00144    Bool_t         fAxesDepthTest;  //! remove guides hidden-lines
00145    Bool_t         fReferenceOn;    //! reference marker on?
00146    TGLVertex3     fReferencePos;   //! reference position
00147    Bool_t         fDrawCameraCenter; //! reference marker on?
00148    TGLCameraOverlay  *fCameraOverlay; //! markup size of viewport in scene units
00149 
00150    Bool_t         fSmartRefresh;   //! cache logicals during scene rebuilds
00151 
00152    // Debug tracing (for scene rebuilds)
00153    Bool_t         fDebugMode;            //! debug mode (forced rebuild + draw scene/frustum/interest boxes)
00154    Bool_t         fIsPrinting;           //!
00155    TString        fPictureFileName;      //! default file-name for SavePicture()
00156    Float_t        fFader;                //! fade the view (0 - no fade/default, 1 - full fade/no rendering done)
00157 
00158    static TGLColorSet fgDefaultColorSet;                 //! a shared, default color-set
00159    static Bool_t      fgUseDefaultColorSetForNewViewers; //! name says it all
00160 
00161 
00162    ///////////////////////////////////////////////////////////////////////
00163    // Methods
00164    ///////////////////////////////////////////////////////////////////////
00165 
00166    virtual void SetupClipObject();
00167 
00168    // Drawing - can tidy up/remove lots when TGLManager added
00169    void InitGL();
00170    void PreDraw();
00171    void PostDraw();
00172    void FadeView(Float_t alpha);
00173    void MakeCurrent() const;
00174    void SwapBuffers() const;
00175 
00176    // Cameras
00177    void        SetViewport(Int_t x, Int_t y, Int_t width, Int_t height);
00178    void        SetViewport(const TGLRect& vp);
00179    void        SetupCameras(Bool_t reset);
00180 
00181 protected:
00182    TGLWidget          *fGLWidget;
00183    Int_t               fGLDevice; //!for embedded gl viewer
00184    TGLContextIdentity *fGLCtxId;  //!for embedded gl viewer
00185 
00186    // Updata/camera-reset behaviour
00187    Bool_t           fIgnoreSizesOnUpdate;      // ignore sizes of bounding-boxes on update
00188    Bool_t           fResetCamerasOnUpdate;     // reposition camera on each update
00189    Bool_t           fResetCamerasOnNextUpdate; // reposition camera on next update
00190 
00191 public:
00192    TGLViewer(TVirtualPad* pad, Int_t x, Int_t y, Int_t width, Int_t height);
00193    TGLViewer(TVirtualPad* pad);
00194    virtual ~TGLViewer();
00195 
00196    // TVirtualViewer3D interface ... mostly a facade
00197 
00198    // Forward to TGLScenePad
00199    virtual Bool_t CanLoopOnPrimitives() const { return kTRUE; }
00200    virtual void   PadPaint(TVirtualPad* pad);
00201    // Actually used by GL-in-pad
00202    virtual Int_t  DistancetoPrimitive(Int_t px, Int_t py);
00203    virtual void   ExecuteEvent(Int_t event, Int_t px, Int_t py);
00204    // Only implemented because they're abstract ... should throw an
00205    // exception or assert they are not called.
00206    virtual Bool_t PreferLocalFrame() const { return kTRUE; }
00207    virtual void   BeginScene() {}
00208    virtual Bool_t BuildingScene() const { return kFALSE; }
00209    virtual void   EndScene() {}
00210    virtual Int_t  AddObject(const TBuffer3D&, Bool_t* = 0) { return TBuffer3D::kNone; }
00211    virtual Int_t  AddObject(UInt_t, const TBuffer3D&, Bool_t* = 0) { return TBuffer3D::kNone; }
00212    virtual Bool_t OpenComposite(const TBuffer3D&, Bool_t* = 0) { return kFALSE; }
00213    virtual void   CloseComposite() {}
00214    virtual void   AddCompositeOp(UInt_t) {}
00215 
00216    virtual void   PrintObjects();
00217    virtual void   ResetCameras()                { SetupCameras(kTRUE); }
00218    virtual void   ResetCamerasAfterNextUpdate() { fResetCamerasOnNextUpdate = kTRUE; }
00219 
00220    TGLWidget* GetGLWidget() { return fGLWidget; }
00221 
00222    virtual void  CreateGLWidget()  {}
00223    virtual void  DestroyGLWidget() {}
00224 
00225    Int_t   GetDev()          const           { return fGLDevice; }
00226    Bool_t  GetSmartRefresh() const           { return fSmartRefresh; }
00227    void    SetSmartRefresh(Bool_t smart_ref) { fSmartRefresh = smart_ref; }
00228 
00229    TGLColorSet& RefDarkColorSet()  { return fDarkColorSet;  }
00230    TGLColorSet& RefLightColorSet() { return fLightColorSet; }
00231    TGLColorSet& ColorSet()         { return * fRnrCtx->GetBaseColorSet(); }
00232    void         UseDarkColorSet();
00233    void         UseLightColorSet();
00234    void         SwitchColorSet();
00235 
00236    void         UseDefaultColorSet(Bool_t x);
00237    Bool_t       IsUsingDefaultColorSet() const;
00238    Bool_t       IsColorSetDark() const;
00239 
00240    void         SetClearColor(Color_t col);
00241 
00242    static TGLColorSet& GetDefaultColorSet();
00243    static void         UseDefaultColorSetForNewViewers(Bool_t x);
00244    static Bool_t       IsUsingDefaultColorSetForNewViewers();
00245 
00246    const TGLRect& RefViewport()      const { return fViewport; }
00247    Int_t          ViewportDiagonal() const { return fViewport.Diagonal(); }
00248 
00249    Float_t GetPointScale()    const { return fPointScale; }
00250    Float_t GetLineScale()     const { return fLineScale; }
00251    void    SetPointScale(Float_t s) { fPointScale = s; }
00252    void    SetLineScale (Float_t s) { fLineScale  = s; }
00253    Bool_t  GetSmoothPoints()  const { return fSmoothPoints; }
00254    Bool_t  GetSmoothLines()   const { return fSmoothLines; }
00255    void    SetSmoothPoints(Bool_t s){ fSmoothPoints = s; }
00256    void    SetSmoothLines(Bool_t s) { fSmoothLines  = s; }
00257 
00258    TGLLightSet* GetLightSet() const { return fLightSet; }
00259    TGLClipSet * GetClipSet()  const { return fClipSet; }
00260    Bool_t GetClipAutoUpdate() const   { return fClipSet->GetAutoUpdate(); }
00261    void   SetClipAutoUpdate(Bool_t x) { fClipSet->SetAutoUpdate(x); }
00262 
00263    // External GUI component interface
00264    TGLCamera & CurrentCamera() const { return *fCurrentCamera; }
00265    TGLCamera & RefCamera(ECameraType camera);
00266    void SetCurrentCamera(ECameraType camera);
00267    void SetOrthoCamera(ECameraType camera, Double_t zoom, Double_t dolly,
00268                              Double_t center[3], Double_t hRotate, Double_t vRotate);
00269    void SetPerspectiveCamera(ECameraType camera, Double_t fov, Double_t dolly,
00270                              Double_t center[3], Double_t hRotate, Double_t vRotate);
00271    void GetGuideState(Int_t & axesType, Bool_t & axesDepthTest, Bool_t & referenceOn, Double_t* referencePos) const;
00272    void SetGuideState(Int_t axesType, Bool_t axesDepthTest, Bool_t referenceOn, const Double_t* referencePos);
00273    void SetDrawCameraCenter(Bool_t x);
00274    Bool_t GetDrawCameraCenter() { return fDrawCameraCenter; }
00275    void   PickCameraCenter()    { fPushAction = kPushCamCenter; RefreshPadEditor(this); }
00276    void   PickAnnotate()        { fPushAction = kPushAnnotate;  RefreshPadEditor(this); }
00277    TGLCameraOverlay* GetCameraOverlay() const { return fCameraOverlay; }
00278    void SetCameraOverlay(TGLCameraOverlay* m) { fCameraOverlay = m; }
00279    TGLAutoRotator* GetAutoRotator();
00280    void SetAutoRotator(TGLAutoRotator* ar);
00281 
00282    // Stereo
00283    Bool_t  GetStereo()               const { return fStereo; }
00284    Float_t GetStereoZeroParallax()   const { return fStereoZeroParallax;   }
00285    Float_t GetStereoEyeOffsetFac()   const { return fStereoEyeOffsetFac;   }
00286    Float_t GetStereoFrustumAsymFac() const { return fStereoFrustumAsymFac; }
00287 
00288    void SetStereo(Bool_t s)                { fStereo = s; }
00289    void SetStereoZeroParallax(Float_t f)   { fStereoZeroParallax   = f; }
00290    void SetStereoEyeOffsetFac(Float_t f)   { fStereoEyeOffsetFac   = f; }
00291    void SetStereoFrustumAsymFac(Float_t f) { fStereoFrustumAsymFac = f; }
00292 
00293    // Push / drag action
00294    EPushAction GetPushAction() const { return fPushAction; }
00295    EDragAction GetDragAction() const { return fDragAction; }
00296 
00297    const TGLPhysicalShape * GetSelected() const;
00298 
00299 
00300    // Draw and selection
00301 
00302    // Scene rendering timeouts
00303    Float_t GetMaxSceneDrawTimeHQ() const    { return fMaxSceneDrawTimeHQ; }
00304    Float_t GetMaxSceneDrawTimeLQ() const    { return fMaxSceneDrawTimeLQ; }
00305    void    SetMaxSceneDrawTimeHQ(Float_t t) { fMaxSceneDrawTimeHQ = t; }
00306    void    SetMaxSceneDrawTimeLQ(Float_t t) { fMaxSceneDrawTimeLQ = t; }
00307 
00308    // Request methods post cross thread request via TROOT::ProcessLineFast().
00309    void RequestDraw(Short_t LOD = TGLRnrCtx::kLODMed); // Cross thread draw request
00310    virtual void PreRender();
00311    virtual void PostRender();
00312    void DoDraw(Bool_t swap_buffers=kTRUE);
00313    void DoDrawMono(Bool_t swap_buffers);
00314    void DoDrawStereo(Bool_t swap_buffers);
00315 
00316    void DrawGuides();
00317    void DrawDebugInfo();
00318 
00319    Bool_t RequestSelect(Int_t x, Int_t y);          // Cross thread select request
00320    Bool_t DoSelect(Int_t x, Int_t y);               // First level selecton (shapes/objects).
00321    Bool_t RequestSecondarySelect(Int_t x, Int_t y); // Cross thread secondary select request
00322    Bool_t DoSecondarySelect(Int_t x, Int_t y);      // Second level selecton (inner structure).
00323    void   ApplySelection();
00324 
00325    Bool_t RequestOverlaySelect(Int_t x, Int_t y); // Cross thread select request
00326    Bool_t DoOverlaySelect(Int_t x, Int_t y);      // Window coords origin top left
00327 
00328    // Saving of screen image
00329    Bool_t SavePicture();
00330    Bool_t SavePicture(const TString &fileName);
00331    Bool_t SavePictureUsingBB (const TString &fileName);
00332    Bool_t SavePictureUsingFBO(const TString &fileName, Int_t w, Int_t h, Float_t pixel_object_scale=0);
00333    Bool_t SavePictureWidth (const TString &fileName, Int_t width, Bool_t pixel_object_scale=kTRUE);
00334    Bool_t SavePictureHeight(const TString &fileName, Int_t height, Bool_t pixel_object_scale=kTRUE);
00335    Bool_t SavePictureScale (const TString &fileName, Float_t scale, Bool_t pixel_object_scale=kTRUE);
00336 
00337    const char*  GetPictureFileName() const { return fPictureFileName.Data(); }
00338    void         SetPictureFileName(const TString& f) { fPictureFileName = f; }
00339    Float_t      GetFader() const { return fFader; }
00340    void         SetFader(Float_t x) { fFader = x; }
00341    void         AutoFade(Float_t fade, Float_t time=1, Int_t steps=10);
00342 
00343    // Update/camera-reset
00344    void   UpdateScene(Bool_t redraw=kTRUE);
00345    Bool_t GetIgnoreSizesOnUpdate() const        { return fIgnoreSizesOnUpdate; }
00346    void   SetIgnoreSizesOnUpdate(Bool_t v)      { fIgnoreSizesOnUpdate = v; }
00347    void   ResetCurrentCamera();
00348    Bool_t GetResetCamerasOnUpdate() const       { return fResetCamerasOnUpdate; }
00349    void   SetResetCamerasOnUpdate(Bool_t v)     { fResetCamerasOnUpdate = v; }
00350 
00351    virtual void PostSceneBuildSetup(Bool_t resetCameras);
00352 
00353    virtual void Activated() { Emit("Activated()"); } // *SIGNAL*
00354 
00355    virtual void MouseIdle(TGLPhysicalShape*,UInt_t,UInt_t); // *SIGNAL*
00356    virtual void MouseOver(TGLPhysicalShape*); // *SIGNAL*
00357    virtual void MouseOver(TGLPhysicalShape*, UInt_t state); // *SIGNAL*
00358    virtual void MouseOver(TObject *obj, UInt_t state); // *SIGNAL*
00359    virtual void ReMouseOver(TObject *obj, UInt_t state); // *SIGNAL*
00360    virtual void UnMouseOver(TObject *obj, UInt_t state); // *SIGNAL*
00361 
00362    virtual void Clicked(TObject *obj); //*SIGNAL*
00363    virtual void Clicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
00364    virtual void ReClicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
00365    virtual void UnClicked(TObject *obj, UInt_t button, UInt_t state); //*SIGNAL*
00366    virtual void DoubleClicked() { Emit("DoubleClicked()"); } // *SIGNAL*
00367 
00368    TGEventHandler *GetEventHandler() const { return fEventHandler; }
00369    virtual void    SetEventHandler(TGEventHandler *handler);
00370 
00371    TGedEditor*  GetGedEditor() const { return fGedEditor; }
00372    virtual void SetGedEditor(TGedEditor* ed) { fGedEditor = ed; }
00373 
00374    virtual void SelectionChanged();
00375    virtual void OverlayDragFinished();
00376    virtual void RefreshPadEditor(TObject* obj=0);
00377 
00378    virtual void RemoveOverlayElement(TGLOverlayElement* el);
00379 
00380    TGLSelectRecord&    GetSelRec()    { return fSelRec; }
00381    TGLOvlSelectRecord& GetOvlSelRec() { return fOvlSelRec; }
00382    TGLOverlayElement*  GetCurrentOvlElm() const { return fCurrentOvlElm; }
00383    void                ClearCurrentOvlElm();
00384 
00385    ClassDef(TGLViewer,0) // Standard ROOT GL viewer.
00386 };
00387 
00388 
00389 
00390 // TODO: Find a better place/way to do this
00391 class TGLRedrawTimer : public TTimer
00392 {
00393 private:
00394    TGLViewer & fViewer;
00395    Short_t     fRedrawLOD;
00396    Bool_t      fPending;
00397 public:
00398    TGLRedrawTimer(TGLViewer & viewer) :
00399       fViewer(viewer), fRedrawLOD(TGLRnrCtx::kLODHigh), fPending(kFALSE) {}
00400    ~TGLRedrawTimer() {}
00401    void RequestDraw(Int_t milliSec, Short_t redrawLOD)
00402    {
00403       if (fPending) TurnOff(); else fPending = kTRUE;
00404       if (redrawLOD < fRedrawLOD) fRedrawLOD = redrawLOD;
00405       TTimer::Start(milliSec, kTRUE);
00406    }
00407    Bool_t IsPending() const { return fPending; }
00408    virtual void Stop()
00409    {
00410       if (fPending) { TurnOff(); fPending = kFALSE; }
00411    }
00412    Bool_t Notify()
00413    {
00414       TurnOff();
00415       fPending = kFALSE;
00416       fViewer.RequestDraw(fRedrawLOD);
00417       fRedrawLOD = TGLRnrCtx::kLODHigh;
00418       return kTRUE;
00419    }
00420 };
00421 
00422 class TGLFaderHelper {
00423 private:
00424    TGLFaderHelper(const TGLFaderHelper&); // Not implemented
00425    TGLFaderHelper& operator=(const TGLFaderHelper&); // Not implemented
00426 
00427 public:
00428    TGLViewer *fViewer;
00429    Float_t    fFadeTarget;
00430    Float_t    fTime;
00431    Int_t      fNSteps;
00432 
00433    TGLFaderHelper() :
00434       fViewer(0), fFadeTarget(0), fTime(0), fNSteps(0) {}
00435    TGLFaderHelper(TGLViewer* v, Float_t fade, Float_t time, Int_t steps) :
00436       fViewer(v),fFadeTarget(fade), fTime(time), fNSteps(steps) {}
00437    virtual ~TGLFaderHelper() {}
00438 
00439    void MakeFadeStep();
00440 
00441    ClassDef(TGLFaderHelper, 0);
00442 };
00443 
00444 #endif // ROOT_TGLViewer

Generated on Tue Jul 5 14:16:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1