00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
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 { 
00066       kOnRequest,     
00067       kOnKeyMod1      
00068    };
00069 
00070 private:
00071    TGLViewer(const TGLViewer &);             
00072    TGLViewer & operator=(const TGLViewer &); 
00073 
00074    void InitSecondaryObjects();
00075 
00076 protected:
00077    
00078    TVirtualPad   *fPad;         
00079 
00080    
00081    TContextMenu  *fContextMenu; 
00082 
00083    
00084    
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    
00098    Bool_t               fStereo;               
00099    Float_t              fStereoZeroParallax;   
00100    Float_t              fStereoEyeOffsetFac;   
00101    Float_t              fStereoFrustumAsymFac; 
00102 
00103    
00104    TGLLightSet         *fLightSet;             
00105    
00106    TGLClipSet          *fClipSet;              
00107    
00108    TGLSelectRecord      fCurrentSelRec;        
00109    TGLSelectRecord      fSelRec;               
00110    TGLSelectRecord      fSecSelRec;            
00111    TGLManipSet         *fSelectedPShapeRef;    
00112    
00113    TGLOverlayElement   *fCurrentOvlElm;        
00114    TGLOvlSelectRecord   fOvlSelRec;            
00115 
00116    TGEventHandler      *fEventHandler;         
00117    TGedEditor          *fGedEditor;            
00118    TGLPShapeObj        *fPShapeWrap;
00119 
00120    
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    
00132    TGLRedrawTimer      *fRedrawTimer;        
00133    Float_t              fMaxSceneDrawTimeHQ; 
00134    Float_t              fMaxSceneDrawTimeLQ; 
00135 
00136    TGLRect        fViewport;       
00137    TGLColorSet    fDarkColorSet;   
00138    TGLColorSet    fLightColorSet;  
00139    Float_t        fPointScale;     
00140    Float_t        fLineScale;      
00141    Bool_t         fSmoothPoints;   
00142    Bool_t         fSmoothLines;    
00143    Int_t          fAxesType;       
00144    Bool_t         fAxesDepthTest;  
00145    Bool_t         fReferenceOn;    
00146    TGLVertex3     fReferencePos;   
00147    Bool_t         fDrawCameraCenter; 
00148    TGLCameraOverlay  *fCameraOverlay; 
00149 
00150    Bool_t         fSmartRefresh;   
00151 
00152    
00153    Bool_t         fDebugMode;            
00154    Bool_t         fIsPrinting;           
00155    TString        fPictureFileName;      
00156    Float_t        fFader;                
00157 
00158    static TGLColorSet fgDefaultColorSet;                 
00159    static Bool_t      fgUseDefaultColorSetForNewViewers; 
00160 
00161 
00162 
00163    
00164 
00165 
00166    virtual void SetupClipObject();
00167 
00168    
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    
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; 
00184    TGLContextIdentity *fGLCtxId;  
00185 
00186    
00187    Bool_t           fIgnoreSizesOnUpdate;      
00188    Bool_t           fResetCamerasOnUpdate;     
00189    Bool_t           fResetCamerasOnNextUpdate; 
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    
00197 
00198    
00199    virtual Bool_t CanLoopOnPrimitives() const { return kTRUE; }
00200    virtual void   PadPaint(TVirtualPad* pad);
00201    
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    
00205    
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    
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    
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    
00294    EPushAction GetPushAction() const { return fPushAction; }
00295    EDragAction GetDragAction() const { return fDragAction; }
00296 
00297    const TGLPhysicalShape * GetSelected() const;
00298 
00299 
00300    
00301 
00302    
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    
00309    void RequestDraw(Short_t LOD = TGLRnrCtx::kLODMed); 
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);          
00320    Bool_t DoSelect(Int_t x, Int_t y);               
00321    Bool_t RequestSecondarySelect(Int_t x, Int_t y); 
00322    Bool_t DoSecondarySelect(Int_t x, Int_t y);      
00323    void   ApplySelection();
00324 
00325    Bool_t RequestOverlaySelect(Int_t x, Int_t y); 
00326    Bool_t DoOverlaySelect(Int_t x, Int_t y);      
00327 
00328    
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    
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()"); } 
00354 
00355    virtual void MouseIdle(TGLPhysicalShape*,UInt_t,UInt_t); 
00356    virtual void MouseOver(TGLPhysicalShape*); 
00357    virtual void MouseOver(TGLPhysicalShape*, UInt_t state); 
00358    virtual void MouseOver(TObject *obj, UInt_t state); 
00359    virtual void ReMouseOver(TObject *obj, UInt_t state); 
00360    virtual void UnMouseOver(TObject *obj, UInt_t state); 
00361 
00362    virtual void Clicked(TObject *obj); 
00363    virtual void Clicked(TObject *obj, UInt_t button, UInt_t state); 
00364    virtual void ReClicked(TObject *obj, UInt_t button, UInt_t state); 
00365    virtual void UnClicked(TObject *obj, UInt_t button, UInt_t state); 
00366    virtual void DoubleClicked() { Emit("DoubleClicked()"); } 
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) 
00386 };
00387 
00388 
00389 
00390 
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&); 
00425    TGLFaderHelper& operator=(const TGLFaderHelper&); 
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