Logo ROOT   6.21/01
Reference Guide
TGLCamera.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_TGLCamera
13 #define ROOT_TGLCamera
14 
15 #include "TGLUtil.h"
16 #include "TGLBoundingBox.h"
17 #include "TPoint.h"
18 #include "TObject.h"
19 
20 #include <cassert>
21 #include <cmath>
22 
23 //////////////////////////////////////////////////////////////////////////
24 // //
25 // TGLCamera //
26 // //
27 // Abstract base camera class - concrete classes for orthographic and //
28 // persepctive cameras derive from it. This class maintains values for //
29 // the current: //
30 // i) Viewport //
31 // ii) Projection, modelview and clip matricies - extracted from GL //
32 // iii) The 6 frustum planes //
33 // iv) Expanded frustum interest box //
34 // //
35 // It provides methods for various projection, overlap and intersection //
36 // tests for viewport and world locations, against the true frustum and //
37 // expanded interest box, and for extracting eye position and direction.//
38 // //
39 // It also defines the pure virtual manipulation interface methods the //
40 // concrete ortho and prespective classes must implement. //
41 //////////////////////////////////////////////////////////////////////////
42 
43 class TGLCamera : public TObject
44 {
45 public:
47  {
48  kNear = 0,
49  kLeft = 1,
50  kRight = 2,
51  kTop = 3,
52  kBottom = 4,
53  kFar = 5,
55  };
56 
57 private:
58  // Fields
59 
60  // Debuging visual aids
61  TGLBoundingBox fPreviousInterestBox; //! previous interest box (DEBUG)
62  TGLBoundingBox fInterestFrustum; //! frustum basis of current interest box - NOT a true BB! (DEBUG)
63  TGLBoundingBox fInterestFrustumAsBox; //! frustum basis (as box) of current interest box (DEBUG)
64 
65  static const Double_t fgInterestBoxExpansion; //! expansion c.f. aligned current frustum box
66 
67  // Methods
68  TGLBoundingBox Frustum(Bool_t asBox = kTRUE) const; // current frustum
69 
70  // Non-copyable class
71  TGLCamera(const TGLCamera &);
72  TGLCamera & operator=(const TGLCamera &);
73 
74 protected:
75  // Fields
76  TGLMatrix fCamBase; // tranformation to center and rotation from up to x vector
77  TGLMatrix fCamTrans; // transformation relative to fCamTrans
78  Bool_t fExternalCenter; // use external center insead of scene center
79  Bool_t fFixDefCenter; // use fixed default center
80  Bool_t fWasArcBalled; // set when arc-ball rotation is used
81  TGLVector3 fExtCenter; // external camera center
82  TGLVector3 fDefCenter; // default camera center
83  TGLVector3 fFDCenter; // fixed default camera center
84  TGLVector3 *fCenter; //! current camera center
85 
86  mutable Double_t fNearClip; //! last applied near-clip
87  mutable Double_t fFarClip; //! last applied far-clip
88 
89  // Set in Setup()
90  Double_t fDollyDefault; // default distnce from viewing centre
91  Double_t fDollyDistance; // unit distance for camera movement in fwd/bck direction
92  Float_t fVAxisMinAngle; // minimal allowed angle between up and fCamTrans Z vector
93 
94  // Internal cached matrices and frustum planes
95  mutable Bool_t fCacheDirty; //! cached items dirty?
96  mutable UInt_t fTimeStamp; //! timestamp
97  mutable TGLMatrix fLastNoPickProjM; //! no-pick projection matrix (cached)
98  mutable TGLMatrix fProjM; //! projection matrix (cached)
99  mutable TGLMatrix fModVM; //! modelView matrix (cached)
100  mutable TGLMatrix fClipM; //! object space clip matrix (cached)
101  mutable TGLPlane fFrustumPlanes[kPlanesPerFrustum]; //! frustum planes (cached)
102 
103  TGLRect fViewport; //! viewport (GL coords - origin bottom left)
104 
105  TGLBoundingBox fInterestBox; //! the interest box - created in UpdateInterest()
106  mutable Double_t fLargestSeen; //! largest box diagonal seen in OfInterest() - used when
107  //! bootstrapping interest box
108 
109  // Internal cache update - const as the actual camera configuration is unaffected
110  void UpdateCache() const;
111 
113 public:
114  TGLCamera();
115  TGLCamera(const TGLVector3 & hAxis, const TGLVector3 & vAxis);
116  virtual ~TGLCamera();
117 
118  virtual Bool_t IsOrthographic() const { return kFALSE; }
119  virtual Bool_t IsPerspective() const { return kFALSE; }
120 
121  const TGLMatrix& RefModelViewMatrix() const { return fModVM; }
122 
123  Bool_t IsCacheDirty() const { return fCacheDirty; }
125  UInt_t TimeStamp() const { return fTimeStamp; }
126 
127  void SetViewport(const TGLRect & viewport);
129  const TGLRect& RefViewport() const { return fViewport; }
130 
131  // Camera manipulation interface (GL coord - origin bottom left)
132  virtual void Setup(const TGLBoundingBox & box, Bool_t reset=kTRUE) = 0;
133  virtual void Reset() = 0;
134 
135  virtual Bool_t Dolly(Int_t delta, Bool_t mod1, Bool_t mod2);
136  virtual Bool_t Zoom (Int_t delta, Bool_t mod1, Bool_t mod2) = 0;
137  virtual Bool_t Truck(Double_t xDelta, Double_t yDelta);
138  virtual Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2) = 0;
139  virtual Bool_t Rotate(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2);
140  virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate);
141  virtual Bool_t RotateArcBall(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2);
142  virtual Bool_t RotateArcBallRad(Double_t hRotate, Double_t vRotate);
143 
144  virtual void Apply(const TGLBoundingBox & sceneBox, const TGLRect * pickRect = 0) const = 0;
145 
147  Int_t screenShift, Int_t screenShiftRange,
148  Bool_t mod1, Bool_t mod2) const;
149  Double_t AdjustDelta(Double_t screenShift, Double_t deltaFactor,
150  Bool_t mod1, Bool_t mod2) const;
151 
152  void SetExternalCenter(Bool_t x);
154 
157  Double_t* GetCenterVec() { return fCenter->Arr(); }
158 
162 
163  Double_t GetNearClip() const { return fNearClip; }
164  Double_t GetFarClip() const { return fFarClip; }
165 
166  const TGLMatrix& GetCamBase() const { return fCamBase; }
167  const TGLMatrix& GetCamTrans() const { return fCamTrans; }
168  // If you manipulate camera ... also call IncTimeStamp() before redraw.
171 
172  Double_t GetTheta() const;
173 
175 
176  // Current orientation and frustum
177  TGLVertex3 EyePoint() const;
178  TGLVector3 EyeDirection() const;
179  TGLVertex3 FrustumCenter() const;
180  const TGLPlane & FrustumPlane(EFrustumPlane plane) const;
181 
182  // Overlap / projection / intersection tests
183  // Viewport is GL coorinate system - origin bottom/left
184  Rgl::EOverlap FrustumOverlap (const TGLBoundingBox & box) const; // box/frustum overlap test
185  Rgl::EOverlap ViewportOverlap(const TGLBoundingBox & box) const; // box/viewport overlap test
187  TGLRect ViewportRect (const TGLBoundingBox & box, const TGLBoundingBox::EFace * face = 0) const;
188  TGLVertex3 WorldToViewport(const TGLVertex3 & worldVertex, TGLMatrix* modviewMat=0) const;
189  TGLVector3 WorldDeltaToViewport(const TGLVertex3 & worldRef, const TGLVector3 & worldDelta) const;
190  TGLVertex3 ViewportToWorld(const TGLVertex3 & viewportVertex, TGLMatrix* modviewMat=0) const;
191  TGLLine3 ViewportToWorld(Double_t viewportX, Double_t viewportY) const;
192  TGLLine3 ViewportToWorld(const TPoint & viewport) const;
193  TGLVector3 ViewportDeltaToWorld(const TGLVertex3 & worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix* modviewMat=0) const;
194  std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(Double_t viewportX, Double_t viewportY, const TGLPlane & worldPlane) const;
195  std::pair<Bool_t, TGLVertex3> ViewportPlaneIntersection(const TPoint & viewport, const TGLPlane & worldPlane) const;
196 
197  // Window to GL viewport conversion - invert Y
198  void WindowToViewport(Int_t & /* x */, Int_t & y) const { y = fViewport.Height() - y; }
199  void WindowToViewport(TPoint & point) const { point.SetY(fViewport.Height() - point.GetY()); }
200  void WindowToViewport(TGLRect & rect) const { rect.Y() = fViewport.Height() - rect.Y(); }
201  void WindowToViewport(TGLVertex3 & vertex) const { vertex.Y() = fViewport.Height() - vertex.Y(); }
202 
205 
206  virtual void Configure(Double_t zoom, Double_t dolly, Double_t center[3],
207  Double_t hRotate, Double_t vRotate) = 0;
208  // Cameras expanded-frustum interest box
209  Bool_t OfInterest(const TGLBoundingBox & box, Bool_t ignoreSize) const;
211  void ResetInterest();
212 
213  // Debuging - draw frustum and interest boxes
214  void DrawDebugAids() const;
215 
216  ClassDef(TGLCamera,1); // Camera abstract base class.
217 };
218 
219 inline const TGLPlane & TGLCamera::FrustumPlane(EFrustumPlane plane) const
220 {
221  // Return one of the planes forming the camera frustum
222  if (fCacheDirty) {
223  Error("TGLCamera::FrustumBox()", "cache dirty");
224  }
225  return fFrustumPlanes[plane];
226 }
227 
228 
229 #endif // ROOT_TGLCamera
std::pair< Bool_t, TGLVertex3 > ViewportPlaneIntersection(Double_t viewportX, Double_t viewportY, const TGLPlane &worldPlane) const
Find the intersection of projection of supplied viewport point (a 3D world line - see ViewportToWorld...
Definition: TGLCamera.cxx:517
TGLBoundingBox fInterestFrustum
previous interest box (DEBUG)
Definition: TGLCamera.h:62
void SetFixDefCenter(Bool_t x)
Definition: TGLCamera.h:159
TGLMatrix fModVM
projection matrix (cached)
Definition: TGLCamera.h:99
Bool_t fExternalCenter
Definition: TGLCamera.h:78
Abstract base camera class - concrete classes for orthographic and perspective cameras derive from it...
Definition: TGLCamera.h:43
TGLBoundingBox fInterestFrustumAsBox
frustum basis of current interest box - NOT a true BB! (DEBUG)
Definition: TGLCamera.h:63
void SetFixDefCenterVec(Double_t x, Double_t y, Double_t z)
Definition: TGLCamera.h:160
Bool_t IsCacheDirty() const
Definition: TGLCamera.h:123
float Float_t
Definition: RtypesCore.h:53
16 component (4x4) transform matrix - column MAJOR as per GL.
Definition: TGLUtil.h:596
TGLMatrix fCamTrans
Definition: TGLCamera.h:77
Double_t GetFarClip() const
Definition: TGLCamera.h:164
Double_t fFarClip
last applied near-clip
Definition: TGLCamera.h:87
const TGLMatrix & GetCamTrans() const
Definition: TGLCamera.h:167
void WindowToViewport(TGLRect &rect) const
Definition: TGLCamera.h:200
int Int_t
Definition: RtypesCore.h:41
virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
Definition: TGLCamera.cxx:927
bool Bool_t
Definition: RtypesCore.h:59
SCoord_t GetY() const
Definition: TPoint.h:47
Int_t Y() const
Definition: TGLUtil.h:447
UInt_t TimeStamp() const
Definition: TGLCamera.h:125
void UpdateCache() const
largest box diagonal seen in OfInterest() - used when bootstrapping interest box
Definition: TGLCamera.cxx:108
Bool_t OfInterest(const TGLBoundingBox &box, Bool_t ignoreSize) const
Calculate if the an object defined by world frame bounding box is &#39;of interest&#39; to the camera...
Definition: TGLCamera.cxx:578
virtual Bool_t RotateArcBall(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2)
Rotate the camera round view volume center established in Setup().
Definition: TGLCamera.cxx:990
TGLRect & RefViewport()
Definition: TGLCamera.h:128
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
Double_t fLargestSeen
the interest box - created in UpdateInterest()
Definition: TGLCamera.h:106
Double_t fDollyDefault
last applied far-clip
Definition: TGLCamera.h:90
void Set(Double_t x, Double_t y, Double_t z)
Definition: TGLUtil.h:209
TGLCamera()
Default base camera constructor.
Definition: TGLCamera.cxx:44
TGLVector3 fFDCenter
Definition: TGLCamera.h:83
TGLVertex3 ViewportToWorld(const TGLVertex3 &viewportVertex, TGLMatrix *modviewMat=0) const
Convert a &#39;3D&#39; viewport vertex to 3D world one.
Definition: TGLCamera.cxx:442
void SetY(SCoord_t y)
Definition: TPoint.h:49
TGLVector3 fExtCenter
Definition: TGLCamera.h:81
TGLRect ViewportRect(const TGLBoundingBox &box, TGLBoundingBox::EFace face) const
Calculate viewport rectangle which just contains projection of single &#39;face&#39; of world frame bounding ...
Definition: TGLCamera.cxx:329
virtual Bool_t IsPerspective() const
Definition: TGLCamera.h:119
Bool_t fFixDefCenter
Definition: TGLCamera.h:79
virtual void Apply(const TGLBoundingBox &sceneBox, const TGLRect *pickRect=0) const =0
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:326
TGLBoundingBox fInterestBox
viewport (GL coords - origin bottom left)
Definition: TGLCamera.h:105
const TGLMatrix & RefModelViewMatrix() const
Definition: TGLCamera.h:121
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:82
virtual void Setup(const TGLBoundingBox &box, Bool_t reset=kTRUE)=0
TGLRect fViewport
frustum planes (cached)
Definition: TGLCamera.h:103
Viewport (pixel base) 2D rectangle class.
Definition: TGLUtil.h:420
virtual Bool_t Rotate(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2)
Rotate the camera round view volume center established in Setup().
Definition: TGLCamera.cxx:916
3 component (x/y/z) vector class.
Definition: TGLUtil.h:246
const TGLMatrix & GetCamBase() const
Definition: TGLCamera.h:166
TGLBoundingBox Frustum(Bool_t asBox=kTRUE) const
expansion c.f. aligned current frustum box
Definition: TGLCamera.cxx:171
void SetCenterVecWarp(Double_t x, Double_t y, Double_t z)
Set camera center vector and do not keep the same combined camera transformation matrix.
Definition: TGLCamera.cxx:867
Bool_t fWasArcBalled
Definition: TGLCamera.h:80
void SetVAxisMinAngle(Float_t x)
Definition: TGLCamera.h:204
REAL * vertex
Definition: triangle.c:512
TGLVector3 ViewportDeltaToWorld(const TGLVertex3 &worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix *modviewMat=0) const
Apply a 2D viewport delta (shift) to the projection of worldRef onto viewport, returning the resultan...
Definition: TGLCamera.cxx:546
Definition: TPoint.h:31
Double_t * GetCenterVec()
Definition: TGLCamera.h:157
TGLMatrix & RefCamTrans()
Definition: TGLCamera.h:170
TGLPlane fFrustumPlanes[kPlanesPerFrustum]
object space clip matrix (cached)
Definition: TGLCamera.h:101
TGLMatrix fClipM
modelView matrix (cached)
Definition: TGLCamera.h:100
Double_t fDollyDistance
Definition: TGLCamera.h:91
void WindowToViewport(Int_t &, Int_t &y) const
Definition: TGLCamera.h:198
Double_t * Arr()
Definition: TGLUtil.h:126
TGLVector3 * fCenter
Definition: TGLCamera.h:84
TGLVertex3 WorldToViewport(const TGLVertex3 &worldVertex, TGLMatrix *modviewMat=0) const
Convert a 3D world vertex to &#39;3D&#39; viewport (screen) one.
Definition: TGLCamera.cxx:403
TGLMatrix & RefCamBase()
Definition: TGLCamera.h:169
virtual Bool_t Zoom(Int_t delta, Bool_t mod1, Bool_t mod2)=0
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void SetExternalCenter(Bool_t x)
Set camera center diffrent than scene center, if enable is kTRUE.
Definition: TGLCamera.cxx:825
virtual Bool_t Truck(Double_t xDelta, Double_t yDelta)
Truck the camera - &#39;move camera parallel to film plane&#39;.
Definition: TGLCamera.cxx:894
3D space, fixed length, line class, with direction / length &#39;vector&#39;, passing through point &#39;vertex&#39;...
Definition: TGLUtil.h:385
Bool_t AdjustAndClampVal(Double_t &val, Double_t min, Double_t max, Int_t screenShift, Int_t screenShiftRange, Bool_t mod1, Bool_t mod2) const
Adjust a passed REFERENCE value &#39;val&#39;, based on screenShift delta.
Definition: TGLCamera.cxx:722
virtual ~TGLCamera()
Base camera destructor.
Definition: TGLCamera.cxx:92
TGLCamera & operator=(const TGLCamera &)
void WindowToViewport(TGLVertex3 &vertex) const
Definition: TGLCamera.h:201
virtual void Configure(Double_t zoom, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate)=0
Double_t AdjustDelta(Double_t screenShift, Double_t deltaFactor, Bool_t mod1, Bool_t mod2) const
Adjust a passed screen value and apply modifiers.
Definition: TGLCamera.cxx:762
virtual Bool_t IsOrthographic() const
Definition: TGLCamera.h:118
const Bool_t kFALSE
Definition: RtypesCore.h:88
void SetCenterVec(Double_t x, Double_t y, Double_t z)
Set camera center vector.
Definition: TGLCamera.cxx:847
void WindowToViewport(TPoint &point) const
Definition: TGLCamera.h:199
TGLMatrix fCamBase
Definition: TGLCamera.h:76
Double_t fNearClip
current camera center
Definition: TGLCamera.h:86
Float_t GetVAxisMinAngle()
Definition: TGLCamera.h:203
TGLBoundingBox fPreviousInterestBox
Definition: TGLCamera.h:61
double Double_t
Definition: RtypesCore.h:55
TGLMatrix fLastNoPickProjM
timestamp
Definition: TGLCamera.h:97
Bool_t fCacheDirty
Definition: TGLCamera.h:95
void ResetInterest()
Clear out the existing interest box.
Definition: TGLCamera.cxx:699
TGLVector3 WorldDeltaToViewport(const TGLVertex3 &worldRef, const TGLVector3 &worldDelta) const
Convert a 3D vector worldDelta (shift) about vertex worldRef to a viewport (screen) &#39;3D&#39; vector...
Definition: TGLCamera.cxx:426
static UInt_t fgDollyDeltaSens
Definition: TGLCamera.h:112
Double_t y[n]
Definition: legend1.C:17
Float_t fVAxisMinAngle
Definition: TGLCamera.h:92
TGLVertex3 FrustumCenter() const
Find the center of the camera frustum from intersection of planes This method will work even with par...
Definition: TGLCamera.cxx:251
Bool_t UpdateInterest(Bool_t force)
Update the internal interest box (fInterestBox) of the camera.
Definition: TGLCamera.cxx:643
virtual Bool_t Dolly(Int_t delta, Bool_t mod1, Bool_t mod2)
Dolly the camera - &#39;move camera along eye line, retaining lens focal length&#39;.
Definition: TGLCamera.cxx:1046
TGLVertex3 EyePoint() const
Return the camera eye point (vertex) in world space Camera must have valid frustum cache - call Apply...
Definition: TGLCamera.cxx:219
Rgl::EOverlap FrustumOverlap(const TGLBoundingBox &box) const
Calculate overlap (kInside, kOutside, kPartial) of box with camera frustum Camera must have valid fru...
Definition: TGLCamera.cxx:275
Double_t GetNearClip() const
Definition: TGLCamera.h:163
Mother of all ROOT objects.
Definition: TObject.h:37
Concrete class describing an orientated (free) or axis aligned box of 8 vertices. ...
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Double_t * GetFixDefCenterVec()
Definition: TGLCamera.h:161
EOverlap
Definition: TGLUtil.h:33
TGLMatrix fProjM
no-pick projection matrix (cached)
Definition: TGLCamera.h:98
const TGLPlane & FrustumPlane(EFrustumPlane plane) const
Definition: TGLCamera.h:219
Double_t GetTheta() const
Get angle between camera up axis.
Definition: TGLCamera.cxx:882
Bool_t GetExternalCenter()
Definition: TGLCamera.h:153
UInt_t fTimeStamp
cached items dirty?
Definition: TGLCamera.h:96
TGLVector3 fDefCenter
Definition: TGLCamera.h:82
Int_t Height() const
Definition: TGLUtil.h:451
void SetViewport(const TGLRect &viewport)
Set viewport extents from passed &#39;viewport&#39; rect.
Definition: TGLCamera.cxx:99
virtual void Reset()=0
const TGLRect & RefViewport() const
Definition: TGLCamera.h:129
TGLMatrix & RefLastNoPickProjM() const
Definition: TGLCamera.h:174
const Bool_t kTRUE
Definition: RtypesCore.h:87
3D plane class - of format Ax + By + Cz + D = 0
Definition: TGLUtil.h:523
Rgl::EOverlap ViewportOverlap(const TGLBoundingBox &box) const
Calculate overlap (kInside, kOutside, kPartial) of box projection onto viewport (as rect) against the...
Definition: TGLCamera.cxx:319
void IncTimeStamp()
Definition: TGLCamera.h:124
static const Double_t fgInterestBoxExpansion
frustum basis (as box) of current interest box (DEBUG)
Definition: TGLCamera.h:65
virtual Bool_t RotateArcBallRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
Definition: TGLCamera.cxx:1001
TGLVector3 EyeDirection() const
Extract the camera eye direction (vector), running from EyePoint() Camera must have valid frustum cac...
Definition: TGLCamera.cxx:236
void DrawDebugAids() const
Draw out some debugging aids for the camera:
Definition: TGLCamera.cxx:793