Logo ROOT  
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
35The TGLRnrCtx class aggregates data for a given redering context as
36needed by various parts of the ROOT's OpenGL infrastructure. It
37serves as a connecting point between the steering part of the
38infrastructure (viewer, scene) and concrete rendering classes
39(logical, physical shape). It is just a data-holder, there is no
40functionality in it.
41
42Development notes:
43
44One RnrCtx is created by each viewer and it is just an extension of
45the viewer context that changes along the render
46descend. Separating this also has some abstract benefit of hiding
47viewer implementation from those that do not need to know it.
48
49Current scene / scene-info part is always initialized by viewer,
50scenes 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
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Stop the stopwatch.
178
180{
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{
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{
223 fPickRadius = 0;
224 delete fPickRectangle; fPickRectangle = 0;
225
226 if (glResult < 0)
227 {
228 if (fSelectBuffer->CanGrow())
229 {
230 Warning("TGLRnrCtx::EndSelection",
231 "Select buffer size (%d) insufficient, doubling it.",
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{
340 assert(fDLCaptureOpen == kFALSE);
342}
343
344////////////////////////////////////////////////////////////////////////////////
345/// End display list capture.
346
348{
349 assert(fDLCaptureOpen == kTRUE);
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{
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Get font in the GL rendering context.
374
375void TGLRnrCtx::RegisterFontNoScale(Int_t size, const char* name, Int_t mode, TGLFont& out)
376{
378}
379
380////////////////////////////////////////////////////////////////////////////////
381/// Get font in the GL rendering context.
382/// The font is scaled relative to current render scale.
383
385{
387}
388
389////////////////////////////////////////////////////////////////////////////////
390/// Get font in the GL rendering context.
391/// The font is scaled relative to current render scale.
392
393void TGLRnrCtx::RegisterFont(Int_t size, const char* name, Int_t mode, TGLFont& out)
394{
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());
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}
#define GLU_OUTSIDE
Definition: GL_glu.h:203
#define GLU_SMOOTH
Definition: GL_glu.h:198
unsigned int GLenum
Definition: GL_glu.h:266
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
short Short_t
Definition: RtypesCore.h:35
short Color_t
Definition: RtypesCore.h:79
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
void Error(const char *location, const char *msgfmt,...)
void Warning(const char *location, const char *msgfmt,...)
char name[80]
Definition: TGX11.cxx:109
void WindowToViewport(Int_t &, Int_t &y) const
Definition: TGLCamera.h:198
Class encapsulating a set of colors used throughout standard rendering.
Definition: TGLUtil.h:835
TGLFontManager * GetFontManager()
Get the free-type font-manager associated with this context-identity.
Definition: TGLContext.cxx:568
void RegisterFont(Int_t size, Int_t file, TGLFont::EMode mode, TGLFont &out)
Provide font with given size, file and FTGL class.
void ReleaseFont(TGLFont &font)
Release font with given attributes.
A wrapper class for FTFont.
Viewport (pixel base) 2D rectangle class.
Definition: TGLUtil.h:421
Int_t Y() const
Definition: TGLUtil.h:447
Int_t Height() const
Definition: TGLUtil.h:451
Int_t Width() const
Definition: TGLUtil.h:449
void Set(Int_t x, Int_t y, Int_t width, Int_t height)
Definition: TGLUtil.h:470
Int_t X() const
Definition: TGLUtil.h:445
The TGLRnrCtx class aggregates data for a given redering context as needed by various parts of the RO...
Definition: TGLRnrCtx.h:41
TGLColorSet * ChangeBaseColorSet(TGLColorSet *set)
Change the default/bottom color-set.
Definition: TGLRnrCtx.cxx:304
Bool_t fSecSelection
Definition: TGLRnrCtx.h:127
TGLViewerBase * fViewer
Definition: TGLRnrCtx.h:94
std::list< TGLColorSet * > lpTGLColorSet_t
Definition: TGLRnrCtx.h:91
@ kLODHigh
Definition: TGLRnrCtx.h:67
void BeginSelection(Int_t x, Int_t y, Int_t r=3)
Setup context for running selection.
Definition: TGLRnrCtx.cxx:205
TGLSceneInfo * fSceneInfo
Definition: TGLRnrCtx.h:96
void PopColorSet()
Pops the top-most color-set.
Definition: TGLRnrCtx.cxx:287
Int_t fPickRadius
Definition: TGLRnrCtx.h:129
TGLRect * fPickRectangle
Definition: TGLRnrCtx.h:130
Int_t GetPickRadius()
Return pick radius. If selection is not active it returns 0.
Definition: TGLRnrCtx.cxx:257
Short_t fShapeLOD
Definition: TGLRnrCtx.h:101
TGLColorSet * GetBaseColorSet()
Returns the current base color-set.
Definition: TGLRnrCtx.cxx:314
GLUquadric * fQuadric
Current GL context identity.
Definition: TGLRnrCtx.h:142
void ReleaseFont(TGLFont &font)
Release font in the GL rendering context.
Definition: TGLRnrCtx.cxx:359
TGLSceneBase * GetScene()
Return current scene (based on scene-info data).
Definition: TGLRnrCtx.cxx:135
void PushColorSet()
Create copy of current color-set on the top of the stack.
Definition: TGLRnrCtx.cxx:270
TGLStopwatch fStopwatch
Definition: TGLRnrCtx.h:118
TGLRect * GetPickRectangle()
Return current pick rectangle.
Definition: TGLRnrCtx.cxx:249
GLUquadric * GetGluQuadric()
Initialize fQuadric.
Definition: TGLRnrCtx.cxx:405
void RegisterFontNoScale(Int_t size, Int_t file, Int_t mode, TGLFont &out)
Get font in the GL rendering context.
Definition: TGLRnrCtx.cxx:367
void CloseDLCapture()
End display list capture.
Definition: TGLRnrCtx.cxx:347
lpTGLColorSet_t * fColorSetStack
Definition: TGLRnrCtx.h:133
Bool_t IsDrawPassFilled() const
Returns true if current render-pass uses filled polygon style.
Definition: TGLRnrCtx.cxx:153
Short_t fSceneLOD
Definition: TGLRnrCtx.h:99
TGLSceneBase & RefScene()
Return current scene (based on scene-info data).
Definition: TGLRnrCtx.cxx:143
TGLColorSet & ColorSet()
Return reference to current color-set (top of the stack).
Definition: TGLRnrCtx.cxx:278
TGLRnrCtx(const TGLRnrCtx &)
Bool_t fHasTimedOut
Definition: TGLRnrCtx.h:121
virtual ~TGLRnrCtx()
Destructor.
Definition: TGLRnrCtx.cxx:124
void StartStopwatch()
Start the stopwatch.
Definition: TGLRnrCtx.cxx:166
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 StopStopwatch()
Stop the stopwatch.
Definition: TGLRnrCtx.cxx:179
Double_t fRenderTimeOut
Definition: TGLRnrCtx.h:119
void EndSelection(Int_t glResult)
End selection.
Definition: TGLRnrCtx.cxx:219
Bool_t fSelection
Definition: TGLRnrCtx.h:126
Bool_t fDLCaptureOpen
Definition: TGLRnrCtx.h:139
void ProjectionMatrixPushIdentity()
Definition: TGLRnrCtx.cxx:423
Short_t fDrawPass
Definition: TGLRnrCtx.h:116
Bool_t HasStopwatchTimedOut()
Check if the stopwatch went beyond the render time limit.
Definition: TGLRnrCtx.cxx:188
void ColorOrForeground(Color_t col)
Set col if it is different from background, otherwise use current foreground color.
Definition: TGLRnrCtx.cxx:323
TGLSelectBuffer * fSelectBuffer
Definition: TGLRnrCtx.h:131
void ProjectionMatrixPop()
Definition: TGLRnrCtx.cxx:438
TGLContextIdentity * fGLCtxIdentity
DL-capture currently open.
Definition: TGLRnrCtx.h:140
Short_t fCombiLOD
Definition: TGLRnrCtx.h:100
TGLCamera * GetCamera()
Definition: TGLRnrCtx.h:156
static const char * StyleName(Short_t style)
Return string describing the style.
Definition: TGLRnrCtx.cxx:453
void OpenDLCapture()
Start display-list capture.
Definition: TGLRnrCtx.cxx:338
Bool_t fIsRunning
Definition: TGLRnrCtx.h:120
Short_t fSceneStyle
Definition: TGLRnrCtx.h:105
@ kPassFill
Definition: TGLRnrCtx.h:55
@ kPassOutlineFill
Definition: TGLRnrCtx.h:56
Float_t fRenderScale
Definition: TGLRnrCtx.h:134
Bool_t Selection() const
Definition: TGLRnrCtx.h:222
Short_t fViewerStyle
Definition: TGLRnrCtx.h:104
Short_t fViewerLOD
Definition: TGLRnrCtx.h:98
@ kWireFrame
Definition: TGLRnrCtx.h:48
@ kOutline
Definition: TGLRnrCtx.h:47
Scene base-class – provides basic interface expected by the TGLViewer or its sub-classes:
Definition: TGLSceneBase.h:33
TGLSceneBase * GetScene() const
Definition: TGLSceneInfo.h:83
Encapsulates OpenGL select buffer.
void ProcessResult(Int_t glResult)
Process result of GL-selection: sort the hits by their minimum z-coordinate.
void Grow()
Increase size of the select buffer.
Bool_t CanGrow()
static: return true if current buffer is smaller than the max buffer size
Int_t GetBufSize() const
UInt_t * GetBuf() const
void Start()
Start timing.
Double_t Lap() const
Return lap time since Start(), in milliseconds.
Double_t End()
End timing, return total time since Start(), in milliseconds.
static void Color(const TGLColor &color)
Set color from TGLColor.
Definition: TGLUtil.cxx:1692
Base class for GL viewers.
Definition: TGLViewerBase.h:37
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition: TMath.h:703
Definition: file.py:1
TCanvas * style()
Definition: style.C:1