Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include <utility>
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
43class TGLCamera : public TObject
44{
45public:
47 {
48 kNear = 0,
49 kLeft = 1,
50 kRight = 2,
51 kTop = 3,
53 kFar = 5,
55 };
56
57private:
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 &) = delete;
72 TGLCamera & operator=(const TGLCamera &) = delete;
73
74protected:
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
113public:
114 TGLCamera();
115 TGLCamera(const TGLVector3 & hAxis, const TGLVector3 & vAxis);
116 ~TGLCamera() override;
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 = nullptr) 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
154
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;
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 = nullptr) const;
188 TGLVertex3 WorldToViewport(const TGLVertex3 & worldVertex, TGLMatrix* modviewMat = nullptr) const;
189 TGLVector3 WorldDeltaToViewport(const TGLVertex3 & worldRef, const TGLVector3 & worldDelta) const;
190 TGLVertex3 ViewportToWorld(const TGLVertex3 & viewportVertex, TGLMatrix* modviewMat = nullptr) 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 = nullptr) 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 ClassDefOverride(TGLCamera,1); // Camera abstract base class.
217};
218
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
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t rect
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:44
Rgl::EOverlap FrustumOverlap(const TGLBoundingBox &box) const
Calculate overlap (kInside, kOutside, kPartial) of box with camera frustum Camera must have valid fru...
void WindowToViewport(TGLVertex3 &vertex) const
Definition TGLCamera.h:201
TGLMatrix fLastNoPickProjM
timestamp
Definition TGLCamera.h:97
static UInt_t fgDollyDeltaSens
Definition TGLCamera.h:112
TGLMatrix & RefLastNoPickProjM() const
Definition TGLCamera.h:174
void WindowToViewport(TGLRect &rect) const
Definition TGLCamera.h:200
TGLVector3 EyeDirection() const
Extract the camera eye direction (vector), running from EyePoint() Camera must have valid frustum cac...
Double_t * GetFixDefCenterVec()
Definition TGLCamera.h:161
virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
virtual Bool_t IsPerspective() const
Definition TGLCamera.h:119
Double_t GetNearClip() const
Definition TGLCamera.h:163
Bool_t fCacheDirty
Definition TGLCamera.h:95
TGLVertex3 FrustumCenter() const
Find the center of the camera frustum from intersection of planes This method will work even with par...
TGLCamera()
Default base camera constructor.
Definition TGLCamera.cxx:44
TGLVector3 fDefCenter
Definition TGLCamera.h:82
void ResetInterest()
Clear out the existing interest box.
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'.
TGLBoundingBox Frustum(Bool_t asBox=kTRUE) const
expansion c.f. aligned current frustum box
TGLMatrix fCamBase
Definition TGLCamera.h:76
const TGLRect & RefViewport() const
Definition TGLCamera.h:129
UInt_t TimeStamp() const
Definition TGLCamera.h:125
Double_t GetFarClip() const
Definition TGLCamera.h:164
virtual Bool_t RotateArcBallRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
TGLVector3 * fCenter
Definition TGLCamera.h:84
void IncTimeStamp()
Definition TGLCamera.h:124
Double_t fNearClip
current camera center
Definition TGLCamera.h:86
TGLMatrix fCamTrans
Definition TGLCamera.h:77
const TGLMatrix & GetCamBase() const
Definition TGLCamera.h:166
TGLMatrix fProjM
no-pick projection matrix (cached)
Definition TGLCamera.h:98
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.
TGLVector3 ViewportDeltaToWorld(const TGLVertex3 &worldRef, Double_t viewportXDelta, Double_t viewportYDelta, TGLMatrix *modviewMat=nullptr) const
Apply a 2D viewport delta (shift) to the projection of worldRef onto viewport, returning the resultan...
virtual Bool_t Truck(Double_t xDelta, Double_t yDelta)
Truck the camera - 'move camera parallel to film plane'.
Rgl::EOverlap ViewportOverlap(const TGLBoundingBox &box) const
Calculate overlap (kInside, kOutside, kPartial) of box projection onto viewport (as rect) against the...
TGLBoundingBox fInterestBox
viewport (GL coords - origin bottom left)
Definition TGLCamera.h:105
Float_t GetVAxisMinAngle()
Definition TGLCamera.h:203
TGLBoundingBox fInterestFrustumAsBox
frustum basis of current interest box - NOT a true BB! (DEBUG)
Definition TGLCamera.h:63
TGLRect ViewportRect(const TGLBoundingBox &box, TGLBoundingBox::EFace face) const
Calculate viewport rectangle which just contains projection of single 'face' of world frame bounding ...
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().
virtual Bool_t Zoom(Int_t delta, Bool_t mod1, Bool_t mod2)=0
void WindowToViewport(TPoint &point) const
Definition TGLCamera.h:199
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.
void SetFixDefCenterVec(Double_t x, Double_t y, Double_t z)
Definition TGLCamera.h:160
TGLVector3 fExtCenter
Definition TGLCamera.h:81
TGLRect fViewport
frustum planes (cached)
Definition TGLCamera.h:103
TGLCamera & operator=(const TGLCamera &)=delete
Double_t * GetCenterVec()
Definition TGLCamera.h:157
TGLVector3 WorldDeltaToViewport(const TGLVertex3 &worldRef, const TGLVector3 &worldDelta) const
Convert a 3D vector worldDelta (shift) about vertex worldRef to a viewport (screen) '3D' vector.
TGLMatrix fModVM
projection matrix (cached)
Definition TGLCamera.h:99
Double_t fDollyDefault
last applied far-clip
Definition TGLCamera.h:90
const TGLMatrix & GetCamTrans() const
Definition TGLCamera.h:167
void WindowToViewport(Int_t &, Int_t &y) const
Definition TGLCamera.h:198
~TGLCamera() override
Base camera destructor.
Definition TGLCamera.cxx:92
void SetVAxisMinAngle(Float_t x)
Definition TGLCamera.h:204
TGLVertex3 WorldToViewport(const TGLVertex3 &worldVertex, TGLMatrix *modviewMat=nullptr) const
Convert a 3D world vertex to '3D' viewport (screen) one.
TGLPlane fFrustumPlanes[kPlanesPerFrustum]
object space clip matrix (cached)
Definition TGLCamera.h:101
Bool_t IsCacheDirty() const
Definition TGLCamera.h:123
Double_t GetTheta() const
Get angle between camera up axis.
Bool_t GetExternalCenter()
Definition TGLCamera.h:153
Double_t fDollyDistance
Definition TGLCamera.h:91
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...
Bool_t UpdateInterest(Bool_t force)
Update the internal interest box (fInterestBox) of the camera.
void DrawDebugAids() const
Draw out some debugging aids for the camera:
virtual void Setup(const TGLBoundingBox &box, Bool_t reset=kTRUE)=0
TGLBoundingBox fInterestFrustum
previous interest box (DEBUG)
Definition TGLCamera.h:62
void UpdateCache() const
largest box diagonal seen in OfInterest() - used when bootstrapping interest box
TGLMatrix & RefCamTrans()
Definition TGLCamera.h:170
Bool_t fExternalCenter
Definition TGLCamera.h:78
Bool_t fFixDefCenter
Definition TGLCamera.h:79
TGLMatrix fClipM
modelView matrix (cached)
Definition TGLCamera.h:100
@ kPlanesPerFrustum
Definition TGLCamera.h:54
UInt_t fTimeStamp
cached items dirty?
Definition TGLCamera.h:96
virtual void Apply(const TGLBoundingBox &sceneBox, const TGLRect *pickRect=nullptr) const =0
virtual Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2)=0
const TGLMatrix & RefModelViewMatrix() const
Definition TGLCamera.h:121
Float_t fVAxisMinAngle
Definition TGLCamera.h:92
void SetExternalCenter(Bool_t x)
Set camera center diffrent than scene center, if enable is kTRUE.
void SetCenterVec(Double_t x, Double_t y, Double_t z)
Set camera center vector.
Double_t fFarClip
last applied near-clip
Definition TGLCamera.h:87
void SetViewport(const TGLRect &viewport)
Set viewport extents from passed 'viewport' rect.
Definition TGLCamera.cxx:99
TGLBoundingBox fPreviousInterestBox
Definition TGLCamera.h:61
Bool_t fWasArcBalled
Definition TGLCamera.h:80
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().
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:106
virtual Bool_t IsOrthographic() const
Definition TGLCamera.h:118
const TGLPlane & FrustumPlane(EFrustumPlane plane) const
Definition TGLCamera.h:219
TGLCamera(const TGLCamera &)=delete
static const Double_t fgInterestBoxExpansion
frustum basis (as box) of current interest box (DEBUG)
Definition TGLCamera.h:65
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.
TGLVertex3 EyePoint() const
Return the camera eye point (vertex) in world space Camera must have valid frustum cache - call Apply...
TGLRect & RefViewport()
Definition TGLCamera.h:128
void SetFixDefCenter(Bool_t x)
Definition TGLCamera.h:159
Double_t AdjustDelta(Double_t screenShift, Double_t deltaFactor, Bool_t mod1, Bool_t mod2) const
Adjust a passed screen value and apply modifiers.
TGLVector3 fFDCenter
Definition TGLCamera.h:83
TGLVertex3 ViewportToWorld(const TGLVertex3 &viewportVertex, TGLMatrix *modviewMat=nullptr) const
Convert a '3D' viewport vertex to 3D world one.
TGLMatrix & RefCamBase()
Definition TGLCamera.h:169
3D space, fixed length, line class, with direction / length 'vector', passing through point 'vertex'.
Definition TGLUtil.h:387
16 component (4x4) transform matrix - column MAJOR as per GL.
Definition TGLUtil.h:598
3D plane class - of format Ax + By + Cz + D = 0
Definition TGLUtil.h:525
Viewport (pixel base) 2D rectangle class.
Definition TGLUtil.h:422
Int_t Height() const
Definition TGLUtil.h:452
3 component (x/y/z) vector class.
Definition TGLUtil.h:248
3 component (x/y/z) vertex class.
Definition TGLUtil.h:84
Double_t * Arr()
Definition TGLUtil.h:127
void Set(Double_t x, Double_t y, Double_t z)
Definition TGLUtil.h:210
Double_t Y() const
Definition TGLUtil.h:121
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
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:35