TGLCamera.h

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLCamera.h 34006 2010-06-21 10:36:05Z 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_TGLCamera
00013 #define ROOT_TGLCamera
00014 
00015 #include "TGLUtil.h"
00016 #include "TGLBoundingBox.h"
00017 #include "TPoint.h"
00018 
00019 #include <cassert>
00020 #include <cmath>
00021 
00022 //////////////////////////////////////////////////////////////////////////
00023 //                                                                      //
00024 // TGLCamera                                                            //
00025 //                                                                      //
00026 // Abstract base camera class - concrete classes for orthographic and   //
00027 // persepctive cameras derive from it. This class maintains values for  //
00028 // the current:                                                         //
00029 // i)   Viewport                                                        //
00030 // ii)  Projection, modelview and clip matricies - extracted from GL    //
00031 // iii) The 6 frustum planes                                            //
00032 // iv)  Expanded frustum interest box                                   //
00033 //                                                                      //
00034 // It provides methods for various projection, overlap and intersection //
00035 // tests for viewport and world locations, against the true frustum and //
00036 // expanded interest box, and for extracting eye position and direction.//
00037 //                                                                      //
00038 // It also defines the pure virtual manipulation interface methods the  //
00039 // concrete ortho and prespective classes must implement.               //
00040 //////////////////////////////////////////////////////////////////////////
00041 
00042 class TGLCamera
00043 {
00044 public:
00045    enum EFrustumPlane
00046    {
00047       kNear             = 0,
00048       kLeft             = 1,
00049       kRight            = 2,
00050       kTop              = 3,
00051       kBottom           = 4,
00052       kFar              = 5,
00053       kPlanesPerFrustum = 6
00054    };
00055 
00056 private:
00057    // Fields
00058 
00059    // Debuging visual aids
00060    TGLBoundingBox   fPreviousInterestBox;  //! previous interest box (DEBUG)
00061    TGLBoundingBox   fInterestFrustum;      //! frustum basis of current interest box - NOT a true BB! (DEBUG)
00062    TGLBoundingBox   fInterestFrustumAsBox; //! frustum basis (as box) of current interest box (DEBUG)
00063 
00064    static const Double_t fgInterestBoxExpansion; //! expansion c.f. aligned current frustum box
00065 
00066    // Methods
00067    TGLBoundingBox Frustum(Bool_t asBox = kTRUE) const; // current frustum
00068 
00069    // Non-copyable class
00070    TGLCamera(const TGLCamera &);
00071    TGLCamera & operator=(const TGLCamera &);
00072 
00073 protected:
00074    // Fields
00075    TGLMatrix   fCamBase;         //! tranformation to center and rotation from up to x vector
00076    TGLMatrix   fCamTrans;        //! transformation relative to fCamTrans
00077    Bool_t      fExternalCenter;  //! use external center insead of scene center
00078    Bool_t      fFixDefCenter;    //! use fixed default center
00079    TGLVector3  fExtCenter;       //! external camera center
00080    TGLVector3  fDefCenter;       //! deafult camera center
00081    TGLVector3  fFDCenter;        //! fixed deafult camera center
00082    TGLVector3 *fCenter;          //! current camera center
00083 
00084    mutable Double_t fNearClip;   //! last applied near-clip
00085    mutable Double_t fFarClip;    //! last applied far-clip
00086 
00087    // Set in Setup()
00088    Double_t    fDollyDefault;    //! default distnce from viewing centre
00089    Double_t    fDollyDistance;   //! unit distance for camera movement in fwd/bck direction
00090    Float_t     fVAxisMinAngle;   //! minimal allowed angle between up and fCamTrans Z vector
00091 
00092    // Internal cached matrices and frustum planes
00093    mutable Bool_t    fCacheDirty;                      //! cached items dirty?
00094    mutable UInt_t    fTimeStamp;                       //! timestamp
00095    mutable TGLMatrix fLastNoPickProjM;                 //! no-pick projection matrix (cached)
00096    mutable TGLMatrix fProjM;                           //! projection matrix        (cached)
00097    mutable TGLMatrix fModVM;                           //! modelView matrix         (cached)
00098    mutable TGLMatrix fClipM;                           //! object space clip matrix (cached)
00099    mutable TGLPlane fFrustumPlanes[kPlanesPerFrustum]; //! frustum planes           (cached)
00100 
00101    TGLRect   fViewport;    //! viewport (GL coords - origin bottom left)
00102 
00103    TGLBoundingBox   fInterestBox;          //! the interest box - created in UpdateInterest()
00104    mutable Double_t fLargestSeen;          //! largest box diagonal seen in OfInterest() - used when
00105                                            //! bootstrapping interest box
00106 
00107    // Internal cache update - const as the actual camera configuration is unaffected
00108    void       UpdateCache() const;
00109 
00110    static     UInt_t   fgDollyDeltaSens;
00111 public:
00112    TGLCamera();
00113    TGLCamera(const TGLVector3 & hAxis, const TGLVector3 & vAxis);
00114    virtual ~TGLCamera();
00115 
00116    virtual Bool_t IsOrthographic() const {  return kFALSE; }
00117    virtual Bool_t IsPerspective() const { return kFALSE; }
00118 
00119    const TGLMatrix& RefModelViewMatrix() const { return fModVM; }
00120 
00121    Bool_t IsCacheDirty() const { return fCacheDirty; }
00122    void   IncTimeStamp()       { fCacheDirty = kTRUE; ++fTimeStamp; }
00123    UInt_t TimeStamp()    const { return fTimeStamp; }
00124 
00125    void           SetViewport(const TGLRect & viewport);
00126    TGLRect&       RefViewport()       { return fViewport; }
00127    const TGLRect& RefViewport() const { return fViewport; }
00128 
00129    // Camera manipulation interface (GL coord - origin bottom left)
00130    virtual void   Setup(const TGLBoundingBox & box, Bool_t reset=kTRUE) = 0;
00131    virtual void   Reset() = 0;
00132 
00133    virtual Bool_t Dolly(Int_t delta, Bool_t mod1, Bool_t mod2);
00134    virtual Bool_t Zoom (Int_t delta, Bool_t mod1, Bool_t mod2) = 0;
00135    virtual Bool_t Truck(Double_t xDelta, Double_t yDelta);
00136    virtual Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2) = 0;
00137    virtual Bool_t Rotate(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2);
00138    virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate);
00139 
00140    virtual void   Apply(const TGLBoundingBox & sceneBox, const TGLRect * pickRect = 0) const = 0;
00141 
00142    Bool_t     AdjustAndClampVal(Double_t & val, Double_t min, Double_t max,
00143                                 Int_t screenShift, Int_t screenShiftRange,
00144                                 Bool_t mod1, Bool_t mod2) const;
00145    Double_t   AdjustDelta(Double_t screenShift, Double_t deltaFactor,
00146                           Bool_t mod1, Bool_t mod2) const;
00147 
00148    void    SetExternalCenter(Bool_t x);
00149    Bool_t  GetExternalCenter(){ return fExternalCenter; }
00150 
00151    void    SetCenterVec(Double_t x, Double_t y, Double_t z);
00152    void    SetCenterVecWarp(Double_t x, Double_t y, Double_t z);
00153    Double_t* GetCenterVec() { return fCenter->Arr(); }
00154 
00155    void    SetFixDefCenter(Bool_t x) { fFixDefCenter = x; } 
00156    void    SetFixDefCenterVec(Double_t x, Double_t y, Double_t z) { fFDCenter.Set(x, y, z); }
00157    Double_t* GetFixDefCenterVec() { return fFDCenter.Arr(); }
00158 
00159    Double_t GetNearClip() const { return fNearClip; }
00160    Double_t GetFarClip()  const { return fFarClip;  }
00161 
00162    const TGLMatrix& GetCamBase()  const { return fCamBase;  }
00163    const TGLMatrix& GetCamTrans() const { return fCamTrans; }
00164    // If you manipulate camera ... also call IncTimeStamp() before redraw.
00165    TGLMatrix& RefCamBase()  { return fCamBase;  }
00166    TGLMatrix& RefCamTrans() { return fCamTrans; }
00167 
00168    Double_t GetTheta() const;
00169 
00170    TGLMatrix& RefLastNoPickProjM() const { return fLastNoPickProjM; }
00171 
00172    // Current orientation and frustum
00173    TGLVertex3 EyePoint() const;
00174    TGLVector3 EyeDirection() const;
00175    TGLVertex3 FrustumCenter() const;
00176    const TGLPlane & FrustumPlane(EFrustumPlane plane) const;
00177 
00178    // Overlap / projection / intersection tests
00179    // Viewport is GL coorinate system - origin bottom/left
00180    EOverlap   FrustumOverlap (const TGLBoundingBox & box) const; // box/frustum overlap test
00181    EOverlap   ViewportOverlap(const TGLBoundingBox & box) const; // box/viewport overlap test
00182    TGLRect    ViewportRect   (const TGLBoundingBox & box, TGLBoundingBox::EFace face) const;
00183    TGLRect    ViewportRect   (const TGLBoundingBox & box, const TGLBoundingBox::EFace * face = 0) const;
00184    TGLVertex3 WorldToViewport(const TGLVertex3 & worldVertex, TGLMatrix* modviewMat=0) const;
00185    TGLVector3 WorldDeltaToViewport(const TGLVertex3 & worldRef, const TGLVector3 & worldDelta) const;
00186    TGLVertex3 ViewportToWorld(const TGLVertex3 & viewportVertex, TGLMatrix* modviewMat=0) const;
00187    TGLLine3   ViewportToWorld(Double_t viewportX, Double_t viewportY) const;
00188    TGLLine3   ViewportToWorld(const TPoint & viewport) const;
00189    TGLVector3 ViewportDeltaToWorld(const TGLVertex3 & worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix* modviewMat=0) const;
00190    std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(Double_t viewportX, Double_t viewportY, const TGLPlane & worldPlane) const;
00191    std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(const TPoint & viewport, const TGLPlane & worldPlane) const;
00192 
00193    // Window to GL viewport conversion - invert Y
00194    void WindowToViewport(Int_t & /* x */, Int_t & y) const { y = fViewport.Height() - y; }
00195    void WindowToViewport(TPoint & point)             const { point.SetY(fViewport.Height() - point.GetY()); }
00196    void WindowToViewport(TGLRect & rect)             const { rect.Y() = fViewport.Height() - rect.Y(); }
00197    void WindowToViewport(TGLVertex3 & vertex)        const { vertex.Y() = fViewport.Height() - vertex.Y(); }
00198 
00199    Float_t GetVAxisMinAngle(){return fVAxisMinAngle;}
00200    void    SetVAxisMinAngle(Float_t x){fVAxisMinAngle = x;}
00201 
00202    virtual void Configure(Double_t zoom, Double_t dolly, Double_t center[3],
00203                           Double_t hRotate, Double_t vRotate) = 0;
00204    // Cameras expanded-frustum interest box
00205    Bool_t OfInterest(const TGLBoundingBox & box, Bool_t ignoreSize) const;
00206    Bool_t UpdateInterest(Bool_t force);
00207    void   ResetInterest();
00208 
00209    // Debuging - draw frustum and interest boxes
00210    void  DrawDebugAids() const;
00211 
00212    ClassDef(TGLCamera,0); // Camera abstract base class.
00213 };
00214 
00215 inline const TGLPlane & TGLCamera::FrustumPlane(EFrustumPlane plane) const
00216 {
00217    // Return one of the planes forming the camera frustum
00218    if (fCacheDirty) {
00219       Error("TGLCamera::FrustumBox()", "cache dirty");
00220    }
00221    return fFrustumPlanes[plane];
00222 }
00223 
00224 
00225 #endif // ROOT_TGLCamera

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