Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TGLCamera.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/gl:$Id$
0002 // Author:  Richard Maunder  25/05/2005
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TGLCamera
0013 #define ROOT_TGLCamera
0014 
0015 #include "TGLUtil.h"
0016 #include "TGLBoundingBox.h"
0017 #include "TPoint.h"
0018 #include "TObject.h"
0019 
0020 #include <cmath>
0021 #include <utility>
0022 
0023 //////////////////////////////////////////////////////////////////////////
0024 //                                                                      //
0025 // TGLCamera                                                            //
0026 //                                                                      //
0027 // Abstract base camera class - concrete classes for orthographic and   //
0028 // persepctive cameras derive from it. This class maintains values for  //
0029 // the current:                                                         //
0030 // i)   Viewport                                                        //
0031 // ii)  Projection, modelview and clip matricies - extracted from GL    //
0032 // iii) The 6 frustum planes                                            //
0033 // iv)  Expanded frustum interest box                                   //
0034 //                                                                      //
0035 // It provides methods for various projection, overlap and intersection //
0036 // tests for viewport and world locations, against the true frustum and //
0037 // expanded interest box, and for extracting eye position and direction.//
0038 //                                                                      //
0039 // It also defines the pure virtual manipulation interface methods the  //
0040 // concrete ortho and prespective classes must implement.               //
0041 //////////////////////////////////////////////////////////////////////////
0042 
0043 class TGLCamera : public TObject
0044 {
0045 public:
0046    enum EFrustumPlane
0047    {
0048       kNear             = 0,
0049       kLeft             = 1,
0050       kRight            = 2,
0051       kTop              = 3,
0052       kBottom           = 4,
0053       kFar              = 5,
0054       kPlanesPerFrustum = 6
0055    };
0056 
0057 private:
0058    // Fields
0059 
0060    // Debuging visual aids
0061    TGLBoundingBox   fPreviousInterestBox;  //! previous interest box (DEBUG)
0062    TGLBoundingBox   fInterestFrustum;      //! frustum basis of current interest box - NOT a true BB! (DEBUG)
0063    TGLBoundingBox   fInterestFrustumAsBox; //! frustum basis (as box) of current interest box (DEBUG)
0064 
0065    static const Double_t fgInterestBoxExpansion; //! expansion c.f. aligned current frustum box
0066 
0067    // Methods
0068    TGLBoundingBox Frustum(Bool_t asBox = kTRUE) const; // current frustum
0069 
0070    // Non-copyable class
0071    TGLCamera(const TGLCamera &) = delete;
0072    TGLCamera & operator=(const TGLCamera &) = delete;
0073 
0074 protected:
0075     // Fields
0076     TGLMatrix   fCamBase;         //  tranformation to center and rotation from up to x vector
0077     TGLMatrix   fCamTrans;        //  transformation relative to fCamTrans
0078     Bool_t      fExternalCenter;  //  use external center insead of scene center
0079     Bool_t      fFixDefCenter;    //  use fixed default center
0080     Bool_t      fWasArcBalled;    //  set when arc-ball rotation is used
0081     TGLVector3  fExtCenter;       //  external camera center
0082     TGLVector3  fDefCenter;       //  default camera center
0083     TGLVector3  fFDCenter;        //  fixed default camera center
0084     TGLVector3 *fCenter;          //! current camera center
0085 
0086     mutable Double_t fNearClip;   //! last applied near-clip
0087     mutable Double_t fFarClip;    //! last applied far-clip
0088 
0089     // Set in Setup()
0090     Double_t    fDollyDefault;    // default distnce from viewing centre
0091     Double_t    fDollyDistance;   // unit distance for camera movement in fwd/bck direction
0092     Float_t     fVAxisMinAngle;   // minimal allowed angle between up and fCamTrans Z vector
0093 
0094    // Internal cached matrices and frustum planes
0095    mutable Bool_t    fCacheDirty;                      //! cached items dirty?
0096    mutable UInt_t    fTimeStamp;                       //! timestamp
0097    mutable TGLMatrix fLastNoPickProjM;                 //! no-pick projection matrix (cached)
0098    mutable TGLMatrix fProjM;                           //! projection matrix        (cached)
0099    mutable TGLMatrix fModVM;                           //! modelView matrix         (cached)
0100    mutable TGLMatrix fClipM;                           //! object space clip matrix (cached)
0101    mutable TGLPlane fFrustumPlanes[kPlanesPerFrustum]; //! frustum planes           (cached)
0102 
0103    TGLRect   fViewport;    //! viewport (GL coords - origin bottom left)
0104 
0105    TGLBoundingBox   fInterestBox;          //! the interest box - created in UpdateInterest()
0106    mutable Double_t fLargestSeen;          //! largest box diagonal seen in OfInterest() - used when
0107                                            //! bootstrapping interest box
0108 
0109    // Internal cache update - const as the actual camera configuration is unaffected
0110    void       UpdateCache() const;
0111 
0112    static     UInt_t   fgDollyDeltaSens;
0113 public:
0114    TGLCamera();
0115    TGLCamera(const TGLVector3 & hAxis, const TGLVector3 & vAxis);
0116    ~TGLCamera() override;
0117 
0118    virtual Bool_t IsOrthographic() const {  return kFALSE; }
0119    virtual Bool_t IsPerspective() const { return kFALSE; }
0120 
0121    const TGLMatrix& RefModelViewMatrix() const { return fModVM; }
0122 
0123    Bool_t IsCacheDirty() const { return fCacheDirty; }
0124    void   IncTimeStamp()       { fCacheDirty = kTRUE; ++fTimeStamp; }
0125    UInt_t TimeStamp()    const { return fTimeStamp; }
0126 
0127    void           SetViewport(const TGLRect & viewport);
0128    TGLRect&       RefViewport()       { return fViewport; }
0129    const TGLRect& RefViewport() const { return fViewport; }
0130 
0131    // Camera manipulation interface (GL coord - origin bottom left)
0132    virtual void   Setup(const TGLBoundingBox & box, Bool_t reset=kTRUE) = 0;
0133    virtual void   Reset() = 0;
0134 
0135    virtual Bool_t Dolly(Int_t delta, Bool_t mod1, Bool_t mod2);
0136    virtual Bool_t Zoom (Int_t delta, Bool_t mod1, Bool_t mod2) = 0;
0137    virtual Bool_t Truck(Double_t xDelta, Double_t yDelta);
0138    virtual Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2) = 0;
0139    virtual Bool_t Rotate(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2);
0140    virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate);
0141    virtual Bool_t RotateArcBall(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2);
0142    virtual Bool_t RotateArcBallRad(Double_t hRotate, Double_t vRotate);
0143 
0144    virtual void   Apply(const TGLBoundingBox & sceneBox, const TGLRect * pickRect = nullptr) const = 0;
0145 
0146    Bool_t     AdjustAndClampVal(Double_t & val, Double_t min, Double_t max,
0147                                 Int_t screenShift, Int_t screenShiftRange,
0148                                 Bool_t mod1, Bool_t mod2) const;
0149    Double_t   AdjustDelta(Double_t screenShift, Double_t deltaFactor,
0150                           Bool_t mod1, Bool_t mod2) const;
0151 
0152    void    SetExternalCenter(Bool_t x);
0153    Bool_t  GetExternalCenter(){ return fExternalCenter; }
0154 
0155    void    SetCenterVec(Double_t x, Double_t y, Double_t z);
0156    void    SetCenterVecWarp(Double_t x, Double_t y, Double_t z);
0157    Double_t* GetCenterVec() { return fCenter->Arr(); }
0158 
0159    void    SetFixDefCenter(Bool_t x) { fFixDefCenter = x; }
0160    void    SetFixDefCenterVec(Double_t x, Double_t y, Double_t z) { fFDCenter.Set(x, y, z); }
0161    Double_t* GetFixDefCenterVec() { return fFDCenter.Arr(); }
0162 
0163    Double_t GetNearClip() const { return fNearClip; }
0164    Double_t GetFarClip()  const { return fFarClip;  }
0165 
0166    const TGLMatrix& GetCamBase()  const { return fCamBase;  }
0167    const TGLMatrix& GetCamTrans() const { return fCamTrans; }
0168    // If you manipulate camera ... also call IncTimeStamp() before redraw.
0169    TGLMatrix& RefCamBase()  { return fCamBase;  }
0170    TGLMatrix& RefCamTrans() { return fCamTrans; }
0171 
0172    Double_t GetTheta() const;
0173 
0174    TGLMatrix& RefLastNoPickProjM() const { return fLastNoPickProjM; }
0175 
0176    // Current orientation and frustum
0177    TGLVertex3 EyePoint() const;
0178    TGLVector3 EyeDirection() const;
0179    TGLVertex3 FrustumCenter() const;
0180    const TGLPlane & FrustumPlane(EFrustumPlane plane) const;
0181 
0182    // Overlap / projection / intersection tests
0183    // Viewport is GL coorinate system - origin bottom/left
0184    Rgl::EOverlap FrustumOverlap (const TGLBoundingBox & box) const; // box/frustum overlap test
0185    Rgl::EOverlap ViewportOverlap(const TGLBoundingBox & box) const; // box/viewport overlap test
0186    TGLRect    ViewportRect   (const TGLBoundingBox & box, TGLBoundingBox::EFace face) const;
0187    TGLRect    ViewportRect   (const TGLBoundingBox & box, const TGLBoundingBox::EFace * face = nullptr) const;
0188    TGLVertex3 WorldToViewport(const TGLVertex3 & worldVertex, TGLMatrix* modviewMat = nullptr) const;
0189    TGLVector3 WorldDeltaToViewport(const TGLVertex3 & worldRef, const TGLVector3 & worldDelta) const;
0190    TGLVertex3 ViewportToWorld(const TGLVertex3 & viewportVertex, TGLMatrix* modviewMat = nullptr) const;
0191    TGLLine3   ViewportToWorld(Double_t viewportX, Double_t viewportY) const;
0192    TGLLine3   ViewportToWorld(const TPoint & viewport) const;
0193    TGLVector3 ViewportDeltaToWorld(const TGLVertex3 & worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix* modviewMat = nullptr) const;
0194    std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(Double_t viewportX, Double_t viewportY, const TGLPlane & worldPlane) const;
0195    std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(const TPoint & viewport, const TGLPlane & worldPlane) const;
0196 
0197    // Window to GL viewport conversion - invert Y
0198    void WindowToViewport(Int_t & /* x */, Int_t & y) const { y = fViewport.Height() - y; }
0199    void WindowToViewport(TPoint & point)             const { point.SetY(fViewport.Height() - point.GetY()); }
0200    void WindowToViewport(TGLRect & rect)             const { rect.Y() = fViewport.Height() - rect.Y(); }
0201    void WindowToViewport(TGLVertex3 & vertex)        const { vertex.Y() = fViewport.Height() - vertex.Y(); }
0202 
0203    Float_t GetVAxisMinAngle(){return fVAxisMinAngle;}
0204    void    SetVAxisMinAngle(Float_t x){fVAxisMinAngle = x;}
0205 
0206    virtual void Configure(Double_t zoom, Double_t dolly, Double_t center[3],
0207                           Double_t hRotate, Double_t vRotate) = 0;
0208    // Cameras expanded-frustum interest box
0209    Bool_t OfInterest(const TGLBoundingBox & box, Bool_t ignoreSize) const;
0210    Bool_t UpdateInterest(Bool_t force);
0211    void   ResetInterest();
0212 
0213    // Debuging - draw frustum and interest boxes
0214    void  DrawDebugAids() const;
0215 
0216    ClassDefOverride(TGLCamera,1); // Camera abstract base class.
0217 };
0218 
0219 inline const TGLPlane & TGLCamera::FrustumPlane(EFrustumPlane plane) const
0220 {
0221    // Return one of the planes forming the camera frustum
0222    if (fCacheDirty) {
0223       Error("TGLCamera::FrustumBox()", "cache dirty");
0224    }
0225    return fFrustumPlanes[plane];
0226 }
0227 
0228 
0229 #endif // ROOT_TGLCamera