Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGLPerspectiveCamera.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Richard Maunder 25/05/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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
13#include "TGLUtil.h"
14#include "TGLIncludes.h"
15
16#include "TMath.h"
17#include "TError.h"
18
19/** \class TGLPerspectiveCamera
20\ingroup opengl
21Perspective projection camera - with characteristic foreshortening.
22*/
23
24
29
30////////////////////////////////////////////////////////////////////////////////
31/// Construct default XOZ perspective camera
32
34 TGLCamera(TGLVector3(-1.0, 0.0, 0.0), TGLVector3(0.0, 1.0, 0.0)),
35 fFOV(fgFOVDefault)
36{
37 Setup(TGLBoundingBox(TGLVertex3(-100,-100,-100), TGLVertex3(100,100,100)));
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Construct perspective camera
43
46 fFOV(fgFOVDefault)
47{
48 Setup(TGLBoundingBox(TGLVertex3(-100,-100,-100), TGLVertex3(100,100,100)));
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Destroy perspective camera
54
58
59////////////////////////////////////////////////////////////////////////////////
60/// Setup camera limits suitable to view the world volume defined by 'box'
61/// and call Reset() to initialise camera.
62
64{
66 {
67 if (fFixDefCenter)
68 {
70 }
71 else
72 {
73 TGLVertex3 center = box.Center();
74 SetCenterVec(center.X(), center.Y(), center.Z());
75 }
76 }
77
78 // At default FOV, the dolly should be set so as to encapsulate the scene.
79 TGLVector3 extents = box.Extents();
80 Int_t sortInd[3];
81 TMath::Sort(3, extents.CArr(), sortInd);
84
85 fDollyDefault = size / (2.0*TMath::Tan(fov*TMath::Pi()/360));
87
88 if (reset)
89 {
90 Reset();
91 }
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Reset the camera to defaults - reframe the world volume established in Setup()
96/// in default state. Note: limits defined in Setup() are not adjusted.
97
107
108////////////////////////////////////////////////////////////////////////////////
109/// Zoom the camera - 'adjust lens focal length, retaining camera position'.
110/// Arguments are:
111///
112/// - 'delta' - mouse viewport delta (pixels) - +ive zoom in, -ive zoom out
113/// - 'mod1' / 'mod2' - sensitivity modifiers - see TGLCamera::AdjustAndClampVal()
114///
115/// Returns kTRUE is redraw required (camera change), kFALSE otherwise.
116
118{
119 // TODO: Bring all mouse handling into camera classes - would simplify interface and
120 // remove these non-generic cases.
122 IncTimeStamp();
123 return kTRUE;
124 } else {
125 return kFALSE;
126 }
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Truck the camera - 'move camera parallel to film plane'.
131/// Returns kTRUE is redraw required (camera change), kFALSE otherwise.
132
145
146////////////////////////////////////////////////////////////////////////////////
147/// Apply the camera to the current GL context, setting the viewport, projection
148/// and modelview matrices. After this vertices etc can be directly entered
149/// in the world frame. This also updates the cached frustum values, enabling
150/// all the projection, overlap tests etc defined in TGLCamera to be used.
151///
152/// Arguments are:
153/// - 'box' - view volume box - used to adjust near/far clipping
154/// - 'pickRect' - optional picking rect. If non-null, restrict drawing to this
155/// viewport rect.
156
158 const TGLRect * pickRect) const
159{
160 // TODO: If we retained the box from Setup first argument could be dropped?
161
162 // MT This whole thing is convoluted. We can calculate camera position
163 // and look-at direction without calling unproject and seeking clipping
164 // plane intersection.
165 // Besides, this would give as a proper control over camera transformation
166 // matrix.
167 //
168 // Much better since Oct 2007, the clipping planes stuff still
169 // needs to be cleaned.
170
172
173 if(fViewport.Width() == 0 || fViewport.Height() == 0)
174 {
179 return;
180 }
181
184
185 // To find decent near/far clip plane distances we construct the
186 // frustum thus:
187 // i) first setup perspective with arbitrary near/far planes
188 gluPerspective(fFOV, fViewport.Aspect(), 1.0, 1000.0);
189 // printf("FIRST STEP FOV %f, aspect %f, nearClip %f, farClip %f \n", fFOV, fViewport.Aspect(), 1., 1000.);
190
191 // ii) setup modelview
195 TGLVector3 pos = mx.GetTranslation();
196 TGLVector3 fwd = mx.GetBaseVec(1);
197 TGLVector3 center = pos - fwd;
198 TGLVector3 up = mx.GetBaseVec(3);
199
200 gluLookAt(pos[0], pos[1], pos[2],
201 center[0], center[1], center[2],
202 up[0], up[1], up[2]);
203
204 // iii) update the cached frustum planes so we can get eye point/direction
206 if (fCacheDirty) {
207 UpdateCache();
209 }
210
211 // iv) Create a clip plane, using the eye direction as normal, passing through eye point
214
215 // v) find the near/far distance which just encapsulate the passed bounding box vertexes
216 // not ideal - should really find the nearest/further points on box surface
217 // which intersect frustum - however this much more complicated
219 for (UInt_t i=0; i<8; i++) {
220 currentDist = clipPlane.DistanceTo(sceneBox[i]);
221 if (i==0)
222 {
225 }
228 if (currentDist > fFarClip)
230 }
231 // Add 1% each way to avoid any rounding conflicts with drawn objects
232 fNearClip *= 0.49; // 0.99; TODO Look at - avoid removing clipping + manip objs
233 fFarClip *= 2.01; // 1.01;
234 if (fFarClip < 2.0)
235 fFarClip = 2.0;
236 if (fNearClip < fFarClip/1000.0)
237 fNearClip = fFarClip/1000.0;
238
241
242 // vi) Load up any picking rect and reset the perspective using the
243 // correct near/far clips distances
244 if (pickRect)
245 {
248 gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
249 (Int_t*) fViewport.CArr());
251 }
252 else
253 {
256 }
257
259
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// Configure the camera state.
265/// - fov - set directly field-of-view in degrees (default = 30);
266/// - dolly - additional move along the camera forward direction;
267/// - center - new camera center (can be 0 for no change);
268/// - hRotate - additional "up/down" rotation in radians;
269/// - vRotate - additional "left/right" rotation in radians.
270
273{
274 fFOV = fov;
275
276 // Don't generally constrain external configuration
277 // However exceeding the vRotate limits or silly FOV values will
278 // cause very weird behaviour or projections so fix these
279
280 if (fFOV > 170.0) {
281 fFOV = 170.0;
282 } else if (fFOV < 0.1) {
283 fFOV = 0.1;
284 }
285
286 if (center)
287 SetCenterVec(center[0], center[1], center[2]);
288
291
292 IncTimeStamp();
293}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char mx
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
TGLMatrix fLastNoPickProjM
timestamp
Definition TGLCamera.h:97
TGLVector3 EyeDirection() const
Extract the camera eye direction (vector), running from EyePoint() Camera must have valid frustum cac...
virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
Bool_t fCacheDirty
Definition TGLCamera.h:95
TGLMatrix fCamBase
Definition TGLCamera.h:76
void IncTimeStamp()
Definition TGLCamera.h:124
Double_t fNearClip
current camera center
Definition TGLCamera.h:86
TGLMatrix fCamTrans
Definition TGLCamera.h:77
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.
TGLRect fViewport
frustum planes (cached)
Definition TGLCamera.h:103
Double_t fDollyDefault
last applied far-clip
Definition TGLCamera.h:90
void WindowToViewport(Int_t &, Int_t &y) const
Definition TGLCamera.h:198
Double_t fDollyDistance
Definition TGLCamera.h:91
void UpdateCache() const
largest box diagonal seen in OfInterest() - used when bootstrapping interest box
Bool_t fExternalCenter
Definition TGLCamera.h:78
Bool_t fFixDefCenter
Definition TGLCamera.h:79
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
TGLVertex3 EyePoint() const
Return the camera eye point (vertex) in world space Camera must have valid frustum cache - call Apply...
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
16 component (4x4) transform matrix - column MAJOR as per GL.
Definition TGLUtil.h:598
void MoveLF(Int_t ai, Double_t amount)
Translate in local frame.
Definition TGLUtil.cxx:807
void SetIdentity()
Set matrix to identity.
Definition TGLUtil.cxx:759
Double_t * Arr()
Definition TGLUtil.h:665
void Setup(const TGLBoundingBox &box, Bool_t reset=kTRUE) override
Setup camera limits suitable to view the world volume defined by 'box' and call Reset() to initialise...
void Configure(Double_t fov, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate) override
Configure the camera state.
Bool_t Zoom(Int_t delta, Bool_t mod1, Bool_t mod2) override
Zoom the camera - 'adjust lens focal length, retaining camera position'.
void Apply(const TGLBoundingBox &box, const TGLRect *pickRect=nullptr) const override
Apply the camera to the current GL context, setting the viewport, projection and modelview matrices.
~TGLPerspectiveCamera() override
Destroy perspective camera.
TGLPerspectiveCamera()
Construct default XOZ perspective camera.
Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2) override
Truck the camera - 'move camera parallel to film plane'.
void Reset() override
Reset the camera to defaults - reframe the world volume established in Setup() in default state.
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 Y() const
Definition TGLUtil.h:448
const Int_t * CArr() const
Definition TGLUtil.h:443
Int_t Height() const
Definition TGLUtil.h:452
Int_t Width() const
Definition TGLUtil.h:450
Int_t X() const
Definition TGLUtil.h:446
Double_t Aspect() const
Definition TGLUtil.h:500
3 component (x/y/z) vector class.
Definition TGLUtil.h:248
3 component (x/y/z) vertex class.
Definition TGLUtil.h:84
Double_t X() const
Definition TGLUtil.h:119
Double_t Z() const
Definition TGLUtil.h:123
Double_t Y() const
Definition TGLUtil.h:121
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
constexpr Double_t DegToRad()
Conversion from degree to radian: .
Definition TMath.h:82
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:199
Double_t Hypot(Double_t x, Double_t y)
Returns sqrt(x*x + y*y)
Definition TMath.cxx:59
constexpr Double_t Pi()
Definition TMath.h:40
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:432
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:611