Logo ROOT   6.08/07
Reference Guide
TGLOrthoCamera.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 
12 #include "TMath.h"
13 
14 #include "TGLOrthoCamera.h"
15 #include "TGLIncludes.h"
16 #include "TGLUtil.h"
17 
18 
19 /** \class TGLOrthoCamera
20 \ingroup opengl
21 Orthographic projection camera. Currently limited to three types
22 defined at construction time - kXOY, kXOZ, kZOY - where this refers
23 to the viewport plane axis - e.g. kXOY has X axis horizontal, Y
24 vertical - i.e. looking down Z axis with Y vertical.
25 
26 The plane types restriction could easily be removed to supported
27 arbitrary ortho projections along any axis/orientation with free
28 rotations about them.
29 */
30 
32 
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Construct kXOY orthographic camera.
37 
39  TGLCamera(TGLVector3( 0.0, 0.0, 1.0), TGLVector3(0.0, 1.0, 0.0)),
40  fType(kXOY),
41  fEnableRotate(kFALSE), fDollyToZoom(kTRUE),
42  fZoomMin(0.001), fZoomDefault(0.78), fZoomMax(1000.0),
43  fVolume(TGLVertex3(-100.0, -100.0, -100.0), TGLVertex3(100.0, 100.0, 100.0)),
44  fZoom(1.0)
45 {
46  Setup(TGLBoundingBox(TGLVertex3(-100,-100,-100), TGLVertex3(100,100,100)));
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Construct orthographic camera.
51 
53  TGLCamera(hAxis, vAxis),
54  fType(type),
56  fZoomMin(0.001), fZoomDefault(0.78), fZoomMax(1000.0),
57  fVolume(TGLVertex3(-100.0, -100.0, -100.0), TGLVertex3(100.0, 100.0, 100.0)),
58  fZoom(1.0)
59 {
60  Setup(TGLBoundingBox(TGLVertex3(-100,-100,-100), TGLVertex3(100,100,100)));
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Destroy orthographic camera.
65 
67 {
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Setup camera limits suitable to view the world volume defined by 'box'
72 /// and call Reset() to initialise camera.
73 
75 {
76  fVolume = box;
77 
78  if (fExternalCenter == kFALSE)
79  {
80  if (fFixDefCenter)
81  {
83  }
84  else
85  {
86  TGLVertex3 center = box.Center();
87  SetCenterVec(center.X(), center.Y(), center.Z());
88  }
89  }
90  if (reset)
91  Reset();
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Reset the camera to defaults - trucking, zooming to reframe the world volume
96 /// established in Setup(). Note: limits defined in Setup() are not adjusted.
97 
99 {
101  switch (fType) {
102  case kXOY:
103  case kXnOY:
104  {
105  // X -> X, Y -> Y, Z -> Z
106  fDefXSize = e.X(); fDefYSize = e.Y();
107  break;
108  }
109  case kXOZ:
110  case kXnOZ:
111  {
112  // X -> X, Z -> Y, Y -> Z
113  fDefXSize = e.X(); fDefYSize = e.Z();
114  break;
115  }
116 
117  case kZOY:
118  case kZnOY:
119  {
120  // Z -> X, Y -> Y, X -> Z
121  fDefXSize = e.Z(); fDefYSize = e.Y();
122  break;
123  }
124  }
125 
126  fDollyDefault = 1.25*0.5*TMath::Sqrt(3)*fVolume.Extents().Mag();
127  fDollyDistance = 0.002 * fDollyDefault;
131  IncTimeStamp();
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Dolly the camera.
136 /// By default the dolly is reinterpreted to zoom, but it can be
137 /// changed by modifying the fDollyToZoom data-member.
138 
140 {
141  if (fDollyToZoom) {
142  return Zoom(delta, mod1, mod2);
143  } else {
144  return TGLCamera::Dolly(delta, mod1, mod2);
145  }
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Zoom the camera - 'adjust lens focal length, retaining camera position'.
150 /// Arguments are:
151 ///
152 /// - 'delta' - mouse viewport delta (pixels) - +ive zoom in, -ive zoom out
153 /// - 'mod1' / 'mod2' - sensitivity modifiers - see TGLCamera::AdjustAndClampVal()
154 ///
155 /// For an orthographic camera dollying and zooming are identical and both equate
156 /// logically to a rescaling of the viewport limits - without center shift.
157 /// There is no perspective foreshortening or lens 'focal length'.
158 ///
159 /// Returns kTRUE is redraw required (camera change), kFALSE otherwise.
160 
162 {
163  if (AdjustAndClampVal(fZoom, fZoomMin, fZoomMax, -delta*2, fgZoomDeltaSens, mod1, mod2))
164  {
165  IncTimeStamp();
166  return kTRUE;
167  }
168  else
169  {
170  return kFALSE;
171  }
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Set minimum zoom factor. If current zoom is less than z it is
176 /// set to z.
177 
179 {
180  fZoomMin = z;
181  if (fZoom < fZoomMin) {
182  fZoom = fZoomMin;
183  IncTimeStamp();
184  }
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Set maximum zoom factor. If current zoom is greater than z it
189 /// is set to z.
190 
192 {
193  fZoomMax = z;
194  if (fZoom > fZoomMax) {
195  fZoom = fZoomMax;
196  IncTimeStamp();
197  }
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Truck the camera - 'move camera parallel to film plane'.
202 /// Returns kTRUE is redraw required (camera change), kFALSE otherwise.
203 
205 {
206  Double_t xstep = 2.0 * xDelta / fProjM[0] / fViewport.Width();
207  Double_t ystep = 2.0 * yDelta / fProjM[5] / fViewport.Height();
208 
209  xstep = AdjustDelta(xstep, 1.0, mod1, mod2);
210  ystep = AdjustDelta(ystep, 1.0, mod1, mod2);
211 
212  return Truck(-xstep, -ystep);
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Rotate the camera - 'swivel round the view volume center'.
217 /// Returns kTRUE is redraw required (camera change), kFALSE otherwise.
218 
220 {
221  if (fEnableRotate)
222  return TGLCamera::Rotate(xDelta, yDelta, mod1, mod2);
223  else
224  return kFALSE;
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Apply the camera to the current GL context, setting the viewport, projection
229 /// and modelview matrices. After this vertices etc can be directly entered
230 /// in the world frame. This also updates the cached frustum values, enabling
231 /// all the projection, overlap tests etc defined in TGLCamera to be used.
232 ///
233 /// Arguments are:
234 /// - 'box' - view volume box - ignored for ortho camera. Assumed to be same
235 /// as one passed to Setup().
236 /// - 'pickRect' - optional picking rect. If non-null, restrict drawing to this
237 /// viewport rect.
238 
240  const TGLRect * pickRect) const
241 {
242  glViewport(fViewport.X(), fViewport.Y(), fViewport.Width(), fViewport.Height());
243 
244  if(fViewport.Width() == 0 || fViewport.Height() == 0)
245  {
246  glMatrixMode(GL_PROJECTION);
247  glLoadIdentity();
248  glMatrixMode(GL_MODELVIEW);
249  glLoadIdentity();
250  return;
251  }
252 
253  glMatrixMode(GL_PROJECTION);
254  glLoadIdentity();
255 
256  // Load up any picking rect
257  if (pickRect)
258  {
259  TGLRect rect(*pickRect);
260  WindowToViewport(rect);
261  gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
262  (Int_t*) fViewport.CArr());
263  }
264 
265  Double_t halfRangeX, halfRangeY;
267  halfRangeY = 0.5 *fDefYSize;
268  halfRangeX = halfRangeY*fViewport.Width()/fViewport.Height();
269  } else {
270  halfRangeX = 0.5 *fDefXSize;
271  halfRangeY = halfRangeX*fViewport.Height()/fViewport.Width();
272  }
273 
274  halfRangeX /= fZoom;
275  halfRangeY /= fZoom;
276 
277  fNearClip = 0.05*fDollyDefault;
278  fFarClip = 2.0*fDollyDefault;
279  glOrtho(-halfRangeX, halfRangeX,
280  -halfRangeY, halfRangeY,
282 
283  if (!pickRect) glGetDoublev(GL_PROJECTION_MATRIX, fLastNoPickProjM.Arr());
284 
285  // ii) setup modelview
286  glMatrixMode(GL_MODELVIEW);
287  glLoadIdentity();
289  TGLVector3 pos = mx.GetTranslation();
290  TGLVector3 fwd = mx.GetBaseVec(1);
291  TGLVector3 center = pos - fwd;
292  TGLVector3 up = mx.GetBaseVec(3);
293 
294  gluLookAt(pos[0], pos[1], pos[2],
295  center[0], center[1], center[2],
296  up[0], up[1], up[2]);
297 
298  if (fCacheDirty) UpdateCache();
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 /// Configure the camera state.
303 /// - zoom - set directly (default = 0.78);
304 /// - dolly - additional move along the camera forward direction;
305 /// - center - new camera center (can be 0 for no change);
306 /// - hRotate - additional "up/down" rotation in radians;
307 /// - vRotate - additional "left/right" rotation in radians.
308 
310  Double_t hRotate, Double_t vRotate)
311 {
312  fZoom = zoom;
313 
314  if (center)
315  SetCenterVec(center[0], center[1], center[2]);
316 
317  fCamTrans.MoveLF(1, dolly);
318  RotateRad(hRotate, vRotate);
319 
320  IncTimeStamp();
321 }
TGLOrthoCamera()
Construct kXOY orthographic camera.
Double_t fZoom
x, y size of scene from camera view
TGLVertex3 Center() const
Double_t fZoomMin
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
virtual Bool_t Dolly(Int_t delta, Bool_t mod1, Bool_t mod2)
Dolly the camera.
TGLVector3 Extents() const
Int_t X() const
Definition: TGLUtil.h:451
void SetZoomMax(Double_t z)
Set maximum zoom factor.
16 component (4x4) transform matrix - column MAJOR as per GL.
Definition: TGLUtil.h:600
TGLMatrix fCamTrans
Definition: TGLCamera.h:77
Double_t fFarClip
last applied near-clip
Definition: TGLCamera.h:87
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
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t Y() const
Definition: TGLUtil.h:453
void UpdateCache() const
largest box diagonal seen in OfInterest() - used when bootstrapping interest box
Definition: TGLCamera.cxx:108
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
Double_t fDollyDefault
last applied far-clip
Definition: TGLCamera.h:90
TGLVector3 fFDCenter
Definition: TGLCamera.h:83
virtual Bool_t Zoom(Int_t delta, Bool_t mod1, Bool_t mod2)
Zoom the camera - &#39;adjust lens focal length, retaining camera position&#39;.
Bool_t fFixDefCenter
Definition: TGLCamera.h:79
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:86
TGLRect fViewport
frustum planes (cached)
Definition: TGLCamera.h:103
Viewport (pixel base) 2D rectangle class.
Definition: TGLUtil.h:426
Double_t fDefXSize
scene volume
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
Double_t * Arr()
Definition: TGLUtil.h:668
3 component (x/y/z) vector class.
Definition: TGLUtil.h:250
TGLBoundingBox fVolume
void SetIdentity()
Set matrix to identity.
Definition: TGLUtil.cxx:793
virtual void Setup(const TGLBoundingBox &box, Bool_t reset=kTRUE)
Setup camera limits suitable to view the world volume defined by &#39;box&#39; and call Reset() to initialise...
TGLVector3 GetTranslation() const
Return the translation component of matrix.
Definition: TGLUtil.cxx:822
Double_t fDollyDistance
Definition: TGLCamera.h:91
Double_t Mag() const
Definition: TGLUtil.h:303
void WindowToViewport(Int_t &, Int_t &y) const
Definition: TGLCamera.h:198
Bool_t fEnableRotate
unsigned int UInt_t
Definition: RtypesCore.h:42
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 ~TGLOrthoCamera()
Destroy orthographic camera.
virtual Bool_t Truck(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2)
Truck the camera - &#39;move camera parallel to film plane&#39;.
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
Int_t Width() const
Definition: TGLUtil.h:455
PyObject * fType
virtual void Apply(const TGLBoundingBox &sceneBox, const TGLRect *pickRect=0) const
Apply the camera to the current GL context, setting the viewport, projection and modelview matrices...
void SetCenterVec(Double_t x, Double_t y, Double_t z)
Set camera center vector.
Definition: TGLCamera.cxx:847
TGLMatrix fCamBase
Definition: TGLCamera.h:76
Double_t fNearClip
current camera center
Definition: TGLCamera.h:86
Double_t Z() const
Definition: TGLUtil.h:126
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
TGLMatrix fLastNoPickProjM
timestamp
Definition: TGLCamera.h:97
Bool_t fCacheDirty
Definition: TGLCamera.h:95
virtual void Reset()
Reset the camera to defaults - trucking, zooming to reframe the world volume established in Setup()...
int type
Definition: TGX11.cxx:120
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
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
virtual Bool_t Rotate(Int_t xDelta, Int_t yDelta, Bool_t mod1, Bool_t mod2)
Rotate the camera - &#39;swivel round the view volume center&#39;.
virtual void Configure(Double_t zoom, Double_t dolly, Double_t center[3], Double_t hRotate, Double_t vRotate)
Configure the camera state.
Orthographic projection camera.
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
TGLMatrix fProjM
no-pick projection matrix (cached)
Definition: TGLCamera.h:98
Double_t fDefYSize
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
Int_t Height() const
Definition: TGLUtil.h:457
const Bool_t kTRUE
Definition: Rtypes.h:91
Double_t Y() const
Definition: TGLUtil.h:124
void IncTimeStamp()
Definition: TGLCamera.h:124
Double_t fZoomDefault
void MoveLF(Int_t ai, Double_t amount)
Translate in local frame.
Definition: TGLUtil.cxx:841
void SetZoomMin(Double_t z)
Set minimum zoom factor.
TGLVector3 GetBaseVec(Int_t b) const
Definition: TGLUtil.h:757
Double_t fZoomMax
static UInt_t fgZoomDeltaSens
const Int_t * CArr() const
Definition: TGLUtil.h:448
Double_t X() const
Definition: TGLUtil.h:122