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