Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TGLCameraOverlay.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Alja Mrak-Tadel 2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2007, 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 "TGLIncludes.h"
13#include "TGLCameraOverlay.h"
14#include "TGLViewer.h"
15#include "TGLCamera.h"
16#include "TGLSelectRecord.h"
17#include "TGLUtil.h"
18#include "TGLRnrCtx.h"
19#include "TGLAxisPainter.h"
20
21#include "TMath.h"
22#include "TAxis.h"
23#include "THLimitsFinder.h"
24
25/** \class TGLCameraOverlay
26\ingroup opengl
27A GL overlay element which displays camera furstum.
28*/
29
30
31////////////////////////////////////////////////////////////////////////////////
32
35
36 fShowOrthographic(showOrtho),
37 fShowPerspective(showPersp),
38
41
42 fAxisPainter(nullptr),
43 fAxis(nullptr),
44 fAxisExtend(0.9),
46
49{
50 // Constructor.
51
52 fFrustum[0] = fFrustum[1] = fFrustum[2] = fFrustum[3] = 0;
53
54 fAxis = new TAxis();
55 fAxis->SetNdivisions(710);
56 fAxis->SetLabelSize(0.018);
57 fAxis->SetLabelOffset(0.01);
58 fAxis->SetAxisColor(kGray+1);
59 fAxis->SetLabelColor(kGray+1);
60
62 fAxisPainter->SetFontMode(TGLFont::kBitmap);
63 fAxisPainter->SetUseAxisColors(kFALSE);
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Destructor.
68
74
75////////////////////////////////////////////////////////////////////////////////
76/// Get axis attributes.
77
79{
80 return dynamic_cast<TAttAxis*>(fAxis);
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Set frustum values from given camera.
85
87{
88 TGLVector3 absRef(1., 1., 1.); // needed in case if orthographic camera is negative
89 Float_t l = -cam.FrustumPlane(TGLCamera::kLeft).D() * Dot(cam.GetCamBase().GetBaseVec(2), absRef);
90 Float_t r = cam.FrustumPlane(TGLCamera::kRight).D() * Dot(cam.GetCamBase().GetBaseVec(2), absRef);
93
94 fFrustum[0] = l;
95 fFrustum[1] = b;
96 fFrustum[2] = r;
97 fFrustum[3] = t;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Draw cross section coordinates in top right corner of screen.
102
104{
105 TGLCamera &cam = rnrCtx.RefCamera();
106 // get eye line
107 const TGLMatrix& mx = cam.GetCamBase() * cam.GetCamTrans();
109 TGLVertex3 p = d + mx.GetBaseVec(1);
110 TGLLine3 line(d, p);
111 // get ref plane
114 // get intersection
115 std::pair<Bool_t, TGLVertex3> intersection;
116 intersection = Intersection(rp, line, kTRUE);
117
118 if (intersection.first)
119 {
120 TGLVertex3 v = intersection.second;
121
122 glMatrixMode(GL_PROJECTION);
123 glPushMatrix();
124 glLoadIdentity();
125
126 glMatrixMode(GL_MODELVIEW);
127 glPushMatrix();
128 glLoadIdentity();
129
130 TGLRect &vp = rnrCtx.GetCamera()->RefViewport();
131 TGLFont font;
132 Int_t fs = TMath::Nint(TMath::Sqrt(vp.Width()*vp.Width() + vp.Height()*vp.Height())*0.02);
133 rnrCtx.RegisterFontNoScale(fs, "arial", TGLFont::kPixmap, font);
134 const char* txt = Form("(%f, %f, %f)", v[0], v[1], v[2]);
135 TGLUtil::Color(rnrCtx.ColorSet().Markup());
136 font.Render(txt, 0.98, 0.98, 0, TGLFont::kRight, TGLFont::kBottom);
137
138 // render cross
140 Float_t w = 0.02; // cross size
141 Float_t ce = 0.15; // empty space
142 glBegin(GL_LINES);
143 glVertex2f(0 +w*ce, 0);
144 glVertex2f(0 +w, 0);
145
146 glVertex2f(0 -w*ce, 0);
147 glVertex2f(0 -w, 0);
148
149 Float_t h = w*vp.Width()/vp.Height();
150 glVertex2f(0, 0 +h*ce);
151 glVertex2f(0, 0 +h);
152
153 glVertex2f(0, 0 -h*ce);
154 glVertex2f(0, 0 -h);
155 glEnd();
156
157 glPopMatrix();
158 glMatrixMode(GL_PROJECTION);
159 glPopMatrix();
160 glMatrixMode(GL_MODELVIEW);
161 }
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Draw axis on four edges and a transparent grid.
166
168{
169 fAxisPainter->SetAttAxis(fAxis);
170 fAxisPainter->SetUseAxisColors(fUseAxisColors);
171
172 Color_t lineColor = fUseAxisColors ? fAxis->GetAxisColor() : rnrCtx.ColorSet().Markup().GetColorIndex();
173
174 // font size calculated relative to viewport diagonal
175 GLint vp[4]; glGetIntegerv(GL_VIEWPORT, vp);
176 Float_t rl = 0.5 *((vp[2]-vp[0]) + (vp[3]-vp[1]));
177 Int_t fsizePx = (Int_t)(fAxis->GetLabelSize()*rl);
178 // tick length
179 Float_t tlY = 0.015*rl/(vp[2]-vp[0]);
180 Float_t tlX = 0.015*rl/(vp[3]-vp[1]);
181 // corner vectors
182 Float_t minX, maxX;
183 TGLVector3 xdir = rnrCtx.RefCamera().GetCamBase().GetBaseVec(2); xdir.Normalise(); // left
184 if (fFrustum[2] > fFrustum[0] )
185 {
186 minX = fFrustum[0];
187 maxX = fFrustum[2];
188 }
189 else {
190 xdir = -xdir;
191 minX = fFrustum[2];
192 maxX = fFrustum[0];
193 }
194
195 TGLVector3 ydir = rnrCtx.RefCamera().GetCamBase().GetBaseVec(3); ydir.Normalise(); // up
196 TGLVector3 vy1 = ydir * fFrustum[1];
197 TGLVector3 vy2 = ydir * fFrustum[3];
198
199 TGLVector3 vx1 = xdir * minX;
200 TGLVector3 vx2 = xdir * maxX;
201 // range
202 Double_t rngY = fFrustum[3] - fFrustum[1];
203 Double_t rngX = maxX - minX;
204 Double_t off = TMath::Sqrt((rngX*rngX)+(rngY*rngY)) * 0.03;
205 Double_t minY = fFrustum[1] + off;
206 Double_t maxY = fFrustum[3] - off;
207 minX += off;
208 maxX -= off;
209
210 // grid lines
211 Char_t alpha = 80; //primary
212 Char_t alpha2 = 90; //secondary
213 Int_t secSteps = fAxis->GetNdivisions() % 100;
214 GLushort stipple = 0x5555; // 33333 more rare
215
216 // horizontal X
217 //
218 fAxisPainter->SetLabelPixelFontSize(fsizePx);
219 fAxis->SetTickLength(tlX);
220 fAxisPainter->RefDir() = xdir;
221 fAxis->SetLimits(minX, maxX);
222 fAxisPainter->RefTMOff(0) = ydir*rngY;
223
224 // bottom
225 glPushMatrix();
226 glTranslated(vy1.X(), vy1.Y(), vy1.Z());
228 fAxisPainter->PaintAxis(rnrCtx, fAxis);
229 glPopMatrix();
230
231 // top
232 glPushMatrix();
233 glTranslated(vy2.X(), vy2.Y(), vy2.Z());
235 fAxisPainter->RefTMOff(0).Negate();
236 fAxisPainter->RnrLabels();
237 fAxisPainter->RnrLines();
238 glPopMatrix();
239
241 if (grid)
242 {
243 TGLAxisPainter::LabVec_t& labs = fAxisPainter->RefLabVec();
244 TGLVector3 tmp;
245 // draw label vertical lines
246 TGLUtil::ColorTransparency(lineColor, alpha);
247 glBegin(GL_LINES);
248 for (TGLAxisPainter::LabVec_t::iterator i = labs.begin(); i != labs.end(); ++i) {
249 tmp = vy1 + xdir * (i->first);
250 glVertex3dv(tmp.Arr());
251 tmp = vy2 + xdir * (i->first);
252 glVertex3dv(tmp.Arr());
253 }
254 glEnd();
255
256 // secondary tick mark lines
257 if (labs.size() > 1)
258 {
259 TGLUtil::ColorTransparency(lineColor, alpha2);
260 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
261 glEnable(GL_LINE_STIPPLE);
262 glLineStipple(1, stipple);
263
264 glBegin(GL_LINES);
265 Int_t ondiv = 0;
266 Double_t omin = 0, omax = 0, bw1 = 0;
267 THLimitsFinder::Optimize(labs[0].second, labs[1].second, secSteps, omin, omax, ondiv, bw1);
268 Double_t val = labs[0].second;
269 while (val < fFrustum[2])
270 {
271 for (Int_t k=0; k<ondiv; k++)
272 {
273 val += bw1;
274 tmp = vy1 + xdir * val;
275 glVertex3dv(tmp.Arr());
276 tmp = vy2 + xdir * val;
277 glVertex3dv(tmp.Arr());
278 }
279 }
280 val = labs[0].second - bw1;
281 while(val > fFrustum[0])
282 {
283 tmp = vy1 + xdir * val;
284 glVertex3dv(tmp.Arr());
285 tmp = vy2 + xdir * val;
286 glVertex3dv(tmp.Arr());
287 val -= bw1;
288 }
289 glEnd();
290 glPopAttrib();
291 }
292 } // draw grid
293
294 //
295 // vertical Y axis
296 //
297
298 fAxis->SetTickLength(tlY);
299 fAxisPainter->RefDir() = ydir;
300 fAxis->SetLimits(minY, maxY);
301 fAxisPainter->RefTMOff(0) = xdir*rngX;
302 // left
303 glPushMatrix();
304 glTranslated(vx1.X(), vx1.Y(), vx1.Z());
306 fAxisPainter->PaintAxis(rnrCtx, fAxis);
307 glPopMatrix();
308 // right
309 glPushMatrix();
310 glTranslated(vx2.X(), vx2.Y(), vx2.Z());
312 fAxisPainter->RefTMOff(0).Negate();
313 fAxisPainter->RnrLabels();
314 fAxisPainter->RnrLines();
315 glPopMatrix();
316
317 if (grid)
318 {
319 TGLAxisPainter::LabVec_t& labs = fAxisPainter->RefLabVec();
320 TGLVector3 tmp;
321 // draw label horizontal lines
322 TGLUtil::ColorTransparency(lineColor, alpha);
323 glBegin(GL_LINES);
324 for (TGLAxisPainter::LabVec_t::iterator i = labs.begin(); i != labs.end(); ++i) {
325 tmp = vx1 + ydir *(i->first);
326 glVertex3dv(tmp.Arr());
327 tmp = vx2 + ydir *(i->first);
328 glVertex3dv(tmp.Arr());
329 }
330 glEnd();
331
332 // secondary tick mark lines
333 if (labs.size() > 1)
334 {
335 TGLUtil::ColorTransparency(lineColor, alpha2);
336 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
337 glEnable(GL_LINE_STIPPLE);
338 glLineStipple(1, stipple);
339
340 glBegin(GL_LINES);
341 Int_t ondiv;
342 Double_t omin = 0, omax = 0, bw1 = 0;
343 Double_t val = 0;
344 THLimitsFinder::Optimize(labs[0].second, labs[1].second, secSteps, omin, omax, ondiv, bw1);
345 val = labs[0].second;
346 while(val < fFrustum[3])
347 {
348 for(Int_t k=0; k<ondiv; k++)
349 {
350 val += bw1;
351 tmp = vx1 + ydir *val;
352 glVertex3dv(tmp.Arr());
353 tmp = vx2 + ydir * val;
354 glVertex3dv(tmp.Arr());
355 }
356 }
357
358 val = labs[0].second - bw1;
359 while(val > fFrustum[1])
360 {
361 tmp = vx1 + ydir *val;
362 glVertex3dv(tmp.Arr());
363 tmp = vx2 + ydir * val;
364 glVertex3dv(tmp.Arr());
365 val -= bw1;
366 }
367 glEnd();
368 glPopAttrib();
369 }
370 } // draw grid
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Show frustum size with fixed screen line length and printed value.
375
377{
378 // factors 10, 5 and 2 are allowed
379 Double_t wfrust = TMath::Abs(fFrustum[2]-fFrustum[0]);
380 Float_t barsize= 0.14* wfrust;
381 Int_t exp = (Int_t) TMath::Floor(TMath::Log10(barsize));
382 Double_t fact = barsize/TMath::Power(10, exp);
383 Double_t red;
384 if (fact > 5)
385 {
386 red = 5*TMath::Power(10, exp);
387 }
388 else if (fact > 2)
389 {
390 red = 2*TMath::Power(10, exp);
391 } else
392 {
393 red = TMath::Power(10, exp);
394 }
395
397 TGLVector3 xdir = rnrCtx.RefCamera().GetCamBase().GetBaseVec(2); // left
398 TGLVector3 ydir = rnrCtx.RefCamera().GetCamBase().GetBaseVec(3); // up
399 xdir.Normalise();
400 ydir.Normalise();
401
403
404 const char* txt = Form("%.*f", (exp < 0) ? -exp : 0, red);
405 Float_t bb[6];
406 TGLFont font;
407 rnrCtx.RegisterFont(12, "arial", TGLFont::kPixmap, font);
408 font.BBox(txt, bb[0], bb[1], bb[2], bb[3], bb[4], bb[5]);
409 TGLRect &vp = rnrCtx.GetCamera()->RefViewport();
410 Double_t mH = (fFrustum[3]-fFrustum[1])*bb[4]/vp.Height();
411 glPushMatrix();
412 v = xdir*(fFrustum[2]-barsize) + ydir*(fFrustum[3] - mH*1.5);
413 glTranslated(v.X(), v.Y(), v.Z());
414 glRasterPos2i(0,0);
415 font.Render(txt);
416 glPopMatrix();
417
418 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
420 glPushMatrix();
421 Float_t xt = fFrustum[2] - 1.1*barsize;
422 Float_t yt = fFrustum[3] - 2.1*mH;
423 v = xdir*xt + ydir*yt;
424 glTranslated(v.X(), v.Y(), v.Z());
425
426 glBegin(GL_LINES);
427 // horizontal static
428 v = red*xdir;
429 glVertex3dv(v.Arr());
430 v = barsize*xdir;
431 glVertex3dv(v.Arr());
432 // corner bars end
433 v = xdir*barsize + ydir*mH;
434 glVertex3dv(v.Arr());
435 v = xdir*barsize - ydir*mH;
436 glVertex3dv(v.Arr());
437 // corner bar start
439 v = ydir*mH;
440 glVertex3dv(v.Arr());
441 v.Negate();
442 glVertex3dv(v.Arr());
443 // marker pointer
444 v = red*ydir;
445 glVertex3dv(v.Arr());
446 v += ydir*mH;
447 glVertex3dv(v.Arr());
448 //marker line
449 glVertex3d(0, 0., 0.);
450 v = red*xdir;
451 glVertex3dv(v.Arr());
452 glEnd();
453 glPopAttrib();
454 glPopMatrix();
455}
456
457////////////////////////////////////////////////////////////////////////////////
458/// Display coordinates info of current frustum.
459
461{
462 TGLCamera &cam = rnrCtx.RefCamera();
463
464 if (rnrCtx.Selection() ||
465 (cam.IsPerspective() && ! fShowPerspective) ||
467 {
468 return;
469 }
470
471 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
472 glEnable(GL_BLEND);
473 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
474
475 TGLUtil::Color(rnrCtx.ColorSet().Markup());
476 TGLCapabilitySwitch lights_off(GL_LIGHTING, kFALSE);
477 Float_t old_depth_range[2];
478 glGetFloatv(GL_DEPTH_RANGE, old_depth_range);
479
480 SetFrustum(cam);
481
482 if (cam.IsOrthographic())
483 {
484 switch (fOrthographicMode)
485 {
486 case kBar:
487 glDepthRange(0, 0.1);
488 RenderBar(rnrCtx);
489 break;
490 case kAxis:
491 glDepthRange(0, 0.1);
492 RenderAxis(rnrCtx, kFALSE);
493 break;
494 case kGridFront:
495 glDepthRange(0, 0.1);
496 RenderAxis(rnrCtx, kTRUE);
497 break;
498 case kGridBack:
499 glDepthRange(1, 0.9);
500 RenderAxis(rnrCtx, kTRUE);
501 break;
502 default:
503 break;
504 };
505 }
506 else
507 {
508 RenderPlaneIntersect(rnrCtx);
509 }
510
511 glDepthRange(old_depth_range[0], old_depth_range[1]);
512 glPopAttrib();
513}
ROOT::R::TRInterface & r
Definition Object.C:4
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
char Char_t
Character 1 byte (char).
Definition RtypesCore.h:51
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
short Color_t
Color number (short).
Definition RtypesCore.h:99
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
@ kGray
Definition Rtypes.h:66
@ kRed
Definition Rtypes.h:67
Double_t Dot(const TGLVector3 &v1, const TGLVector3 &v2)
Definition TGLUtil.h:317
std::pair< Bool_t, TGLLine3 > Intersection(const TGLPlane &p1, const TGLPlane &p2)
Find 3D line interestion of this plane with 'other'.
Definition TGLUtil.cxx:511
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
Manages histogram axis attributes.
Definition TAttAxis.h:19
Class to manage histogram axis.
Definition TAxis.h:32
Utility class to paint axis in GL.
std::vector< Lab_t > LabVec_t
void RenderAxis(TGLRnrCtx &rnrCtx, Bool_t drawGrid)
Draw axis on four edges and a transparent grid.
TGLAxisPainter * fAxisPainter
void SetFrustum(TGLCamera &cam)
Set frustum values from given camera.
~TGLCameraOverlay() override
Destructor.
void RenderPlaneIntersect(TGLRnrCtx &rnrCtx)
Draw cross section coordinates in top right corner of screen.
TAttAxis * GetAttAxis()
Get axis attributes.
void Render(TGLRnrCtx &rnrCtx) override
Display coordinates info of current frustum.
TGLCameraOverlay(const TGLCameraOverlay &)=delete
void RenderBar(TGLRnrCtx &rnrCtx)
Show frustum size with fixed screen line length and printed value.
Abstract base camera class - concrete classes for orthographic and perspective cameras derive from it...
Definition TGLCamera.h:44
virtual Bool_t IsPerspective() const
Definition TGLCamera.h:119
const TGLMatrix & GetCamBase() const
Definition TGLCamera.h:166
const TGLMatrix & GetCamTrans() const
Definition TGLCamera.h:167
virtual Bool_t IsOrthographic() const
Definition TGLCamera.h:118
const TGLPlane & FrustumPlane(EFrustumPlane plane) const
Definition TGLCamera.h:219
TGLRect & RefViewport()
Definition TGLCamera.h:128
TGLColor & Foreground()
Definition TGLUtil.h:852
TGLColor & Markup()
Definition TGLUtil.h:854
Color_t GetColorIndex() const
Returns color-index representing the color.
Definition TGLUtil.cxx:1210
A wrapper class for FTFont.
void BBox(const char *txt, Float_t &llx, Float_t &lly, Float_t &llz, Float_t &urx, Float_t &ury, Float_t &urz) const
Get bounding box.
void Render(const char *txt, Double_t x, Double_t y, Double_t angle, Double_t mgn) const
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
TGLVector3 GetBaseVec(Int_t b) const
Definition TGLUtil.h:754
TGLVector3 GetTranslation() const
Return the translation component of matrix.
Definition TGLUtil.cxx:788
TGLOverlayElement(const TGLOverlayElement &)=delete
3D plane class - of format Ax + By + Cz + D = 0
Definition TGLUtil.h:525
Double_t D() const
Definition TGLUtil.h:556
Viewport (pixel base) 2D rectangle class.
Definition TGLUtil.h:422
Int_t Height() const
Definition TGLUtil.h:452
Int_t Width() const
Definition TGLUtil.h:450
The TGLRnrCtx class aggregates data for a given redering context as needed by various parts of the RO...
Definition TGLRnrCtx.h:41
void RegisterFontNoScale(Int_t size, Int_t file, Int_t mode, TGLFont &out)
Get font in the GL rendering context.
TGLColorSet & ColorSet()
Return reference to current color-set (top of the stack).
TGLCamera & RefCamera()
Definition TGLRnrCtx.h:157
void RegisterFont(Int_t size, Int_t file, Int_t mode, TGLFont &out)
Get font in the GL rendering context.
TGLCamera * GetCamera()
Definition TGLRnrCtx.h:156
Bool_t Selection() const
Definition TGLRnrCtx.h:222
static void ColorTransparency(Color_t color_index, Char_t transparency=0)
Set color from color_index and ROOT-style transparency (default 0).
Definition TGLUtil.cxx:1732
static void Color(const TGLColor &color)
Set color from TGLColor.
Definition TGLUtil.cxx:1688
static Float_t LineWidth()
Get the line-width, taking the global scaling into account.
Definition TGLUtil.cxx:1934
3 component (x/y/z) vector class.
Definition TGLUtil.h:248
void Normalise()
Definition TGLUtil.h:304
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 * Arr()
Definition TGLUtil.h:127
Double_t Y() const
Definition TGLUtil.h:121
static void Optimize(Double_t A1, Double_t A2, Int_t nold, Double_t &BinLow, Double_t &BinHigh, Int_t &nbins, Double_t &BWID, Option_t *option="")
Static function to compute reasonable axis limits.
TLine * line
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition TMath.h:704
Double_t Floor(Double_t x)
Rounds x downward, returning the largest integral value that is not greater than x.
Definition TMath.h:691
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:732
Double_t Log10(Double_t x)
Returns the common (base-10) logarithm of x.
Definition TMath.h:773
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122
TLine l
Definition textangle.C:4