ROOT  6.06/09
Reference Guide
TGLRnrCtx.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Matevz Tadel, Feb 2007
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 #include "TVirtualX.h"
13 #include "TString.h"
14 #include "TROOT.h"
15 
16 #include "TGLRnrCtx.h"
17 #include "TGLSceneInfo.h"
18 #include "TGLSelectBuffer.h"
19 #include "TGLIncludes.h"
20 #include "TGLUtil.h"
21 #include "TGLCamera.h"
22 #include "TGLFontManager.h"
23 #include "TGLContext.h"
24 
25 #include "TError.h"
26 #include "TMathBase.h"
27 #include "TMath.h"
28 
29 #include <list>
30 #include <algorithm>
31 #include <cassert>
32 
33 /** \class TGLRnrCtx
34 \ingroup opengl
35 The TGLRnrCtx class aggregates data for a given redering context as
36 needed by various parts of the ROOT's OpenGL infrastructure. It
37 serves as a connecting point between the steering part of the
38 infrastructure (viewer, scene) and concrete rendering classes
39 (logical, physical shape). It is just a data-holder, there is no
40 functionality in it.
41 
42 Development notes:
43 
44 One RnrCtx is created by each viewer and it is just an extension of
45 the viewer context that changes along the render
46 descend. Separating this also has some abstract benefit of hiding
47 viewer implementation from those that do not need to know it.
48 
49 Current scene / scene-info part is always initialized by viewer,
50 scenes can assume they're ok.
51 */
52 
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 
58  fViewer (viewer),
59  fCamera (0),
60  fSceneInfo (0),
61 
62  fViewerLOD (kLODUndef),
63  fSceneLOD (kLODUndef),
64  fCombiLOD (kLODUndef),
65  fShapeLOD (kLODUndef),
66  fShapePixSize (0),
67 
68  fViewerStyle (kStyleUndef),
69  fSceneStyle (kStyleUndef),
70 
71  fViewerWFLineW (0),
72  fSceneWFLineW (0),
73  fViewerOLLineW (0),
74  fSceneOLLineW (0),
75 
76  fViewerClip (0),
77  fSceneClip (0),
78  fClip (0),
79  fDrawPass (kPassUndef),
80 
81  fStopwatch (),
82  fRenderTimeOut(0.0),
83  fIsRunning (kFALSE),
84  fHasTimedOut (kFALSE),
85 
86  fHighlight (kFALSE), fHighlightOutline (kFALSE),
87  fSelection (kFALSE), fSecSelection (kFALSE),
88  fSelectTransparents (kIfNoOpaques),
89  fPickRadius (0),
90  fPickRectangle(0),
91  fSelectBuffer (0),
92 
93  fColorSetStack(0),
94  fRenderScale (1),
95 
96  fEventKeySym (0),
97 
98  fDLCaptureOpen (kFALSE),
99  fGLCtxIdentity (0),
100  fQuadric (0),
101 
102  fGrabImage (kFALSE),
103  fGrabBuffer (-1),
104  fGrabbedImage (0)
105 {
106  // Constructor.
107 
109  fColorSetStack->push_back(0);
110 
112  if (fViewer == 0)
113  {
114  // Assume external usage, initialize for highest quality.
118  }
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Destructor.
123 
125 {
126  gluDeleteQuadric(fQuadric);
127  delete fPickRectangle;
128  delete fSelectBuffer;
129  delete fColorSetStack;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Return current scene (based on scene-info data).
134 
136 {
137  return fSceneInfo->GetScene();
138 }
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// Return current scene (based on scene-info data).
142 
144 {
145  return *fSceneInfo->GetScene();
146 }
147 
148 /**************************************************************************/
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Returns true if current render-pass uses filled polygon style.
152 
154 {
156 }
157 
158 
159 /******************************************************************************/
160 // Stopwatch
161 /******************************************************************************/
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Start the stopwatch.
165 
167 {
168  if (fIsRunning)
169  return;
170 
171  fStopwatch.Start();
172  fIsRunning = kTRUE;
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Stop the stopwatch.
178 
180 {
182  fIsRunning = kFALSE;
183 }
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Check if the stopwatch went beyond the render time limit.
187 
189 {
190  if (fHasTimedOut) return kTRUE;
193  return fHasTimedOut;
194 }
195 
196 
197 /******************************************************************************/
198 // Selection & picking
199 /******************************************************************************/
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// Setup context for running selection.
203 /// x and y are in window coordinates.
204 
206 {
207  fSelection = kTRUE;
209  fPickRadius = r;
211  fPickRectangle->Set(x, y, r, r);
212 
213  glSelectBuffer(fSelectBuffer->GetBufSize(), fSelectBuffer->GetBuf());
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// End selection.
218 
220 {
221  fSelection = kFALSE;
223  fPickRadius = 0;
224  delete fPickRectangle; fPickRectangle = 0;
225 
226  if (glResult < 0)
227  {
228  if (fSelectBuffer->CanGrow() && fSelectBuffer->GetBufSize() > 0x10000)
229  {
230  Warning("TGLRnrCtx::EndSelection",
231  "Select buffer size (%d) insufficient, doubling it.",
233  fSelectBuffer->Grow();
234  }
235  else
236  {
237  Warning("TGLRnrCtx::EndSelection",
238  "Select buffer size (%d) insufficient. This is maximum.",
240  }
241  }
242  fSelectBuffer->ProcessResult(glResult);
243 }
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Return current pick rectangle. This is *zero* when
247 /// selection is not set.
248 
250 {
251  return fPickRectangle;
252 }
253 
254 ////////////////////////////////////////////////////////////////////////////////
255 /// Return pick radius. If selection is not active it returns 0.
256 
258 {
259  return fPickRadius;
260 }
261 
262 
263 /**************************************************************************/
264 // ColorSet access & management
265 /******************************************************************************/
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Create copy of current color-set on the top of the stack.
269 
271 {
272  fColorSetStack->push_back(new TGLColorSet(*fColorSetStack->back()));
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Return reference to current color-set (top of the stack).
277 
279 {
280  return * fColorSetStack->back();
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// Pops the top-most color-set.
285 /// If only one entry is available, error is printed and the entry remains.
286 
288 {
289  if (fColorSetStack->size() >= 2)
290  {
291  delete fColorSetStack->back();
292  fColorSetStack->pop_back();
293  }
294  else
295  {
296  Error("PopColorSet()", "Attempting to remove the last entry.");
297  }
298 }
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 /// Change the default/bottom color-set.
302 /// Returns the previous color-set.
303 
305 {
306  TGLColorSet* old = fColorSetStack->front();
307  fColorSetStack->front() = set;
308  return old;
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// Returns the current base color-set.
313 
315 {
316  return fColorSetStack->front();
317 }
318 
319 ////////////////////////////////////////////////////////////////////////////////
320 /// Set col if it is different from background, otherwise use
321 /// current foreground color.
322 
324 {
325  if (fColorSetStack->back()->Background().GetColorIndex() == col)
326  TGLUtil::Color(fColorSetStack->back()->Foreground());
327  else
328  TGLUtil::Color(col);
329 }
330 
331 /**************************************************************************/
332 // Display-list state
333 /******************************************************************************/
334 
335 ////////////////////////////////////////////////////////////////////////////////
336 /// Start display-list capture.
337 
339 {
342 }
343 
344 ////////////////////////////////////////////////////////////////////////////////
345 /// End display list capture.
346 
348 {
351 }
352 
353 /******************************************************************************/
354 // TGLFont interface
355 /******************************************************************************/
356 ////////////////////////////////////////////////////////////////////////////////
357 /// Release font in the GL rendering context.
358 
360 {
362 }
363 
364 ////////////////////////////////////////////////////////////////////////////////
365 /// Get font in the GL rendering context.
366 
368 {
369  fGLCtxIdentity->GetFontManager()->RegisterFont( size, file, (TGLFont::EMode)mode, out);
370 }
371 
372 ////////////////////////////////////////////////////////////////////////////////
373 /// Get font in the GL rendering context.
374 
375 void TGLRnrCtx::RegisterFontNoScale(Int_t size, const char* name, Int_t mode, TGLFont& out)
376 {
377  fGLCtxIdentity->GetFontManager()->RegisterFont(size, name, (TGLFont::EMode)mode, out);
378 }
379 
380 ////////////////////////////////////////////////////////////////////////////////
381 /// Get font in the GL rendering context.
382 /// The font is scaled relative to current render scale.
383 
385 {
386  RegisterFontNoScale(TMath::Nint(size*fRenderScale), file, mode, out);
387 }
388 
389 ////////////////////////////////////////////////////////////////////////////////
390 /// Get font in the GL rendering context.
391 /// The font is scaled relative to current render scale.
392 
393 void TGLRnrCtx::RegisterFont(Int_t size, const char* name, Int_t mode, TGLFont& out)
394 {
395  RegisterFontNoScale(TMath::Nint(size*fRenderScale), name, mode, out);
396 }
397 
398 /******************************************************************************/
399 // fQuadric's initialization.
400 /******************************************************************************/
401 
402 ////////////////////////////////////////////////////////////////////////////////
403 /// Initialize fQuadric.
404 
406 {
407  if (!fQuadric) {
408  if ((fQuadric = gluNewQuadric())) {
409  gluQuadricOrientation(fQuadric, (GLenum)GLU_OUTSIDE);
410  gluQuadricNormals(fQuadric, (GLenum)GLU_SMOOTH);
411  } else
412  Error("TGLRnrCtx::GetGluQuadric", "gluNewQuadric failed");
413  }
414 
415  return fQuadric;
416 }
417 
418 
419 /******************************************************************************/
420 // Matrix manipulation helpers
421 /******************************************************************************/
422 
424 {
425  glMatrixMode(GL_PROJECTION);
426  glPushMatrix();
427  glLoadIdentity();
428  if (Selection())
429  {
430  TGLRect rect(*GetPickRectangle());
431  GetCamera()->WindowToViewport(rect);
432  gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
433  (Int_t*) GetCamera()->RefViewport().CArr());
434  }
435  glMatrixMode(GL_MODELVIEW);
436 }
437 
439 {
440  glMatrixMode(GL_PROJECTION);
441  glPopMatrix();
442  glMatrixMode(GL_MODELVIEW);
443 }
444 
445 
446 /**************************************************************************/
447 // Static helpers
448 /**************************************************************************/
449 
450 ////////////////////////////////////////////////////////////////////////////////
451 /// Return string describing the style.
452 
454 {
455  switch (style)
456  {
457  case TGLRnrCtx::kFill: return "Filled Polys";
458  case TGLRnrCtx::kWireFrame: return "Wireframe";
459  case TGLRnrCtx::kOutline: return "Outline";
460  default: return "Oogaa-dooga style";
461  }
462 }
TGLSelectBuffer * fSelectBuffer
Definition: TGLRnrCtx.h:131
TGLRnrCtx(const TGLRnrCtx &)
The TGLRnrCtx class aggregates data for a given redering context as needed by various parts of the RO...
Definition: TGLRnrCtx.h:40
Class encapsulating a set of colors used throughout standard rendering.
Definition: TGLUtil.h:838
Int_t GetPickRadius()
Return pick radius. If selection is not active it returns 0.
Definition: TGLRnrCtx.cxx:257
static void Color(const TGLColor &color)
Set color from TGLColor.
Definition: TGLUtil.cxx:1658
Int_t Width() const
Definition: TGLUtil.h:455
void Start()
Start timing.
void ProcessResult(Int_t glResult)
Process result of GL-selection: sort the hits by their minimum z-coordinate.
void ProjectionMatrixPushIdentity()
Definition: TGLRnrCtx.cxx:423
Int_t GetBufSize() const
Double_t Lap() const
Return lap time since Start(), in milliseconds.
Short_t fShapeLOD
Definition: TGLRnrCtx.h:101
#define assert(cond)
Definition: unittest.h:542
Bool_t Selection() const
Definition: TGLRnrCtx.h:222
Scene base-class – provides basic interface expected by the TGLViewer or its sub-classes: ...
Definition: TGLSceneBase.h:32
GLUquadric * GetGluQuadric()
Initialize fQuadric.
Definition: TGLRnrCtx.cxx:405
void ReleaseFont(TGLFont &font)
Release font with given attributes.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
ClassImp(TGLRnrCtx)
void RegisterFontNoScale(Int_t size, Int_t file, Int_t mode, TGLFont &out)
Get font in the GL rendering context.
Definition: TGLRnrCtx.cxx:367
TGLRect & RefViewport()
Definition: TGLCamera.h:128
Short_t fCombiLOD
Definition: TGLRnrCtx.h:100
Bool_t fIsRunning
Definition: TGLRnrCtx.h:120
void StartStopwatch()
Start the stopwatch.
Definition: TGLRnrCtx.cxx:166
TGLRect * GetPickRectangle()
Return current pick rectangle.
Definition: TGLRnrCtx.cxx:249
Base class for GL viewers.
Definition: TGLViewerBase.h:36
GLUquadric * fQuadric
Current GL context identity.
Definition: TGLRnrCtx.h:142
void RegisterFont(Int_t size, Int_t file, TGLFont::EMode mode, TGLFont &out)
Provide font with given size, file and FTGL class.
TGLStopwatch fStopwatch
Definition: TGLRnrCtx.h:118
Encapsulates OpenGL select buffer.
Double_t x[n]
Definition: legend1.C:17
void PushColorSet()
Create copy of current color-set on the top of the stack.
Definition: TGLRnrCtx.cxx:270
void ColorOrForeground(Color_t col)
Set col if it is different from background, otherwise use current foreground color.
Definition: TGLRnrCtx.cxx:323
Bool_t fDLCaptureOpen
Definition: TGLRnrCtx.h:139
void StopStopwatch()
Stop the stopwatch.
Definition: TGLRnrCtx.cxx:179
TGLCamera * GetCamera()
Definition: TGLRnrCtx.h:156
Bool_t IsDrawPassFilled() const
Returns true if current render-pass uses filled polygon style.
Definition: TGLRnrCtx.cxx:153
Double_t fRenderTimeOut
Definition: TGLRnrCtx.h:119
Viewport (pixel base) 2D rectangle class.
Definition: TGLUtil.h:426
static const char * StyleName(Short_t style)
Return string describing the style.
Definition: TGLRnrCtx.cxx:453
Short_t fViewerStyle
Definition: TGLRnrCtx.h:104
void RegisterFont(Int_t size, Int_t file, Int_t mode, TGLFont &out)
Get font in the GL rendering context.
Definition: TGLRnrCtx.cxx:384
void ProjectionMatrixPop()
Definition: TGLRnrCtx.cxx:438
Bool_t CanGrow()
static: return true if current buffer is smaller than the max buffer size
void Error(const char *location, const char *msgfmt,...)
char * out
Definition: TBase64.cxx:29
short Color_t
Definition: RtypesCore.h:79
Float_t fRenderScale
Definition: TGLRnrCtx.h:134
void WindowToViewport(Int_t &, Int_t &y) const
Definition: TGLCamera.h:198
Short_t fViewerLOD
Definition: TGLRnrCtx.h:98
void CloseDLCapture()
End display list capture.
Definition: TGLRnrCtx.cxx:347
TGLContextIdentity * fGLCtxIdentity
DL-capture currently open.
Definition: TGLRnrCtx.h:140
virtual ~TGLRnrCtx()
Destructor.
Definition: TGLRnrCtx.cxx:124
ROOT::R::TRInterface & r
Definition: Object.C:4
Bool_t fHasTimedOut
Definition: TGLRnrCtx.h:121
TGLSceneBase & RefScene()
Return current scene (based on scene-info data).
Definition: TGLRnrCtx.cxx:143
Short_t fSceneLOD
Definition: TGLRnrCtx.h:99
short Short_t
Definition: RtypesCore.h:35
void Warning(const char *location, const char *msgfmt,...)
Int_t X() const
Definition: TGLUtil.h:451
TGLFontManager * GetFontManager()
Get the free-type font-manager associated with this context-identity.
Definition: TGLContext.cxx:576
std::list< TGLColorSet * > lpTGLColorSet_t
Definition: TGLRnrCtx.h:91
TGLColorSet * ChangeBaseColorSet(TGLColorSet *set)
Change the default/bottom color-set.
Definition: TGLRnrCtx.cxx:304
void OpenDLCapture()
Start display-list capture.
Definition: TGLRnrCtx.cxx:338
const Int_t * CArr() const
Definition: TGLUtil.h:448
void EndSelection(Int_t glResult)
End selection.
Definition: TGLRnrCtx.cxx:219
lpTGLColorSet_t * fColorSetStack
Definition: TGLRnrCtx.h:133
TGLSceneBase * GetScene() const
Definition: TGLSceneInfo.h:83
Bool_t fSecSelection
Definition: TGLRnrCtx.h:127
void BeginSelection(Int_t x, Int_t y, Int_t r=3)
Setup context for running selection.
Definition: TGLRnrCtx.cxx:205
void ReleaseFont(TGLFont &font)
Release font in the GL rendering context.
Definition: TGLRnrCtx.cxx:359
TCanvas * style()
Definition: style.C:1
Double_t y[n]
Definition: legend1.C:17
TGLRect * fPickRectangle
Definition: TGLRnrCtx.h:130
Bool_t fSelection
Definition: TGLRnrCtx.h:126
TGLSceneBase * GetScene()
Return current scene (based on scene-info data).
Definition: TGLRnrCtx.cxx:135
#define name(a, b)
Definition: linkTestLib0.cpp:5
Int_t fPickRadius
Definition: TGLRnrCtx.h:129
void PopColorSet()
Pops the top-most color-set.
Definition: TGLRnrCtx.cxx:287
Bool_t HasStopwatchTimedOut()
Check if the stopwatch went beyond the render time limit.
Definition: TGLRnrCtx.cxx:188
Short_t fSceneStyle
Definition: TGLRnrCtx.h:105
Int_t Height() const
Definition: TGLUtil.h:457
TGLColorSet * GetBaseColorSet()
Returns the current base color-set.
Definition: TGLRnrCtx.cxx:314
void Grow()
Increase size of the select buffer.
A wrapper class for FTFont.
TGLViewerBase * fViewer
Definition: TGLRnrCtx.h:94
TGLColorSet & ColorSet()
Return reference to current color-set (top of the stack).
Definition: TGLRnrCtx.cxx:278
TGLSceneInfo * fSceneInfo
Definition: TGLRnrCtx.h:96
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t Nint(T x)
Definition: TMath.h:480
Int_t Y() const
Definition: TGLUtil.h:453
UInt_t * GetBuf() const
Double_t End()
End timing, return total time since Start(), in milliseconds.
void Set(Int_t x, Int_t y, Int_t width, Int_t height)
Definition: TGLUtil.h:476
Short_t fDrawPass
Definition: TGLRnrCtx.h:116