Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGLPadUtils.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Timur Pocheptsov 06/05/2009
3
4/*************************************************************************
5 * Copyright (C) 1995-2009, 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 <stdexcept>
13#include <cassert>
14#include <cmath>
15
16#include "TVirtualX.h"
17#include "RStipples.h"
18#include "TColor.h"
19#include "TROOT.h"
20#include "TMath.h"
21#include "TAttMarker.h"
22
23#include "TGLPadUtils.h"
24#include "TGLIncludes.h"
25
26namespace Rgl {
27namespace Pad {
28
29const UInt_t PolygonStippleSet::fgBitSwap[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
30
31
32/*
33Temporary fix.
34*/
35#ifndef GL_VERSION_1_2
38#else
39const GLenum lineWidthPNAME = GLenum(GL_SMOOTH_LINE_WIDTH_RANGE);//Cast for real enums and macros.
41#endif
42
43/*
44Auxiliary class to converts ROOT's polygon stipples from
45RStipples.h into GL's stipples and hold them in a fStipples array.
46*/
47
48////////////////////////////////////////////////////////////////////////////////
49
51{
52 /*
53 I have to assume, that gStipple has two chars in a line.
54 There in no way to calculate line length and there are no corresponding constants in RStipple.h.
55 So, these numbers are hardcode here.
56 Ordering in RStipples completely different from OpenGL.
57 In OpenGL, if I have, say, 16x2 pattern, GLbytes will be:
58
59 [3][4]
60 [1][2]
61
62 and bits inside them
63
64 [7 6 5 4 3 2 1 0][7 6 5 4 3 2 1 0]
65 [7 6 5 4 3 2 1 0][7 6 5 4 3 2 1 0].
66
67 But for X11 this will be:
68
69 [2][1]
70 [4][3]
71
72 [0 1 2 3 4 5 6 7][0 1 2 3 4 5 6 7]
73 [0 1 2 3 4 5 6 7][0 1 2 3 4 5 6 7]
74
75 So, line 0x7, 0xE from X11 must be
76 converted into 0x70, 0xE0 for OpenGL.
77
78 As OpenGL expects 32x32 pattern, I have to twice each line.
79 */
80
81 /*If somebody will seriously change gStipples declaration,
82 so, that sizeof gStipples becomes "wrong", change this!*/
83 const UInt_t numOfStipples = sizeof gStipples / sizeof gStipples[0];
85
86 for (UInt_t i = 0; i < numOfStipples; ++i) {
87 const UInt_t baseInd = i * kStippleSize;
88
89 for (Int_t j = 15, j1 = 0; j >= 0; --j, ++j1) {//ROOT uses 16x16 stipples.
90 const UInt_t rowShift = j1 * kRowSize;
91
92 for (Int_t k = 1, k1 = 0; k >= 0; --k, ++k1) {//Two chars form a line.
93 const UChar_t pixel = SwapBits(gStipples[i][j * 2 + k]);
94 const UInt_t ind = baseInd + rowShift + k1;
95
97 fStipples[ind + 2] = pixel;
98 fStipples[ind + 64] = pixel;
99 fStipples[ind + 66] = pixel;
100 }
101 }
102 }
103}
104
105////////////////////////////////////////////////////////////////////////////////
106
108{
109 b &= k16Bits;
110
111 const UInt_t low = fgBitSwap[b & kLow4] << 4;
112 const UInt_t up = fgBitSwap[(b & kUp4) >> 4];
113
114 return low | up;
115}
116
117/*
118Class to manipulate fill parameters.
119*/
120////////////////////////////////////////////////////////////////////////////////
121///Polygon stipple, if required.
122
124 : fStipple(0), fAlpha(1.)
125{
126 Style_t fillStyle = att ? att->GetFillStyle() : gVirtualX->GetFillStyle();
127 Color_t fillColor = att ? att->GetFillColor() : gVirtualX->GetFillColor();
128
129 const UInt_t style = fillStyle / 1000;
130
131 if (!ignoreStipple) {
132 if (style == 3) {
133 const UInt_t fasi = fillStyle % 1000;
134 fStipple = (fasi >= 1 && fasi <=25) ? fasi : 2;
137 }
138 }
139
140 // Color and transparency
141 Float_t rgba[] = {0.f, 0.f, 0.f, 1.f};
143 fAlpha = rgba[3];
144 if (fAlpha<1.) {
147 }
149}
150
151////////////////////////////////////////////////////////////////////////////////
152
161
162/*
163"ROOT like" line stipples.
164*/
165
166const UShort_t gLineStipples[] = {0xffff, 0xffff, 0x3333, 0x5555,
167 0xf040, 0xf4f4, 0xf111, 0xf0f0,
168 0xff11, 0x3fff, 0x08ff};
169
171
172/*
173Set/unset line attributes.
174*/
175////////////////////////////////////////////////////////////////////////////////
176///Set up line parameters.
177///Smooth.
178
180 : fSmooth(smooth), fStipple(stipple), fSetWidth(setWidth), fAlpha(0.8)
181{
182 if (fSmooth) {
187 }
188
189 Color_t lineColor = att ? att->GetLineColor() : gVirtualX->GetLineColor();
190 Width_t lineWidth = att ? att->GetLineWidth() : gVirtualX->GetLineWidth();
191
192 //Stipple.
193 if (fStipple > 1) {
194 if (fStipple >= gMaxStipple)
195 fStipple = 1;
196 else {
199 }
200 }
201
202 //Color and transparency
203 Float_t rgba[] = {0.f, 0.f, 0.f, 0.8f};
205 fAlpha = rgba[3];
206 if (fAlpha<0.8) {
209 }
211
212 //Width.
213 if (fSetWidth) {
214 glLineWidth(lineWidth > maxWidth ? maxWidth : !lineWidth ? 1.f : lineWidth);
215 }
216}
217
218////////////////////////////////////////////////////////////////////////////////
219
221{
222 if (fSmooth || fAlpha<0.8) {
225 }
226
227 if (fStipple > 1)
229
230 if (fSetWidth)
231 glLineWidth(1.f);
232}
233
234
235////////////////////////////////////////////////////////////////////////////////
236/// Auxiliary class to draw markers in a gl-pad.
237
239{
240 Int_t markerSize = 0;
241 std::vector<TPoint> markerShape;
242 auto markerType = attr.GetMarkerShape(markerSize, markerShape, 1., TAttMarker::kPreferTriangles | TAttMarker::kDotAsLines);
243
244 auto masrkerStyle = TAttMarker::GetMarkerStyleBase(attr.GetMarkerStyle());
246
247 switch(markerType) {
249 glBegin(GL_POINTS);
250 for (UInt_t i = 0; i < n; ++i)
251 glVertex2d(xy[i].fX, xy[i].fY);
252 glEnd();
253 break;
255 // to fill circle, place point in the middle
256 markerShape.emplace_back(0,0);
257 // no break, circle points will be append
259 Double_t r = markerSize * 0.5;
260 const int pts = r < 100 ? kSmallCirclePts : kLargeCirclePts;
261 const Double_t delta = TMath::TwoPi() / pts;
262 markerShape.reserve(markerShape.size() + pts + 1);
263 Double_t angle = 0.;
264 for (int i = 0; i < pts; ++i, angle += delta)
265 markerShape.emplace_back(std::round(r * TMath::Cos(angle)), std::round(r * TMath::Sin(angle)));
266 markerShape.emplace_back(r, 0); // close circle
267 }
268 // no break, markerShape will be used as all other marker shapes
273 for (unsigned i = 0; i < n; ++i) {
274 const Double_t x = xy[i].fX;
275 const Double_t y = xy[i].fY;
276 if ((markerType == TAttMarker::kShapePolyLine) || (markerType == TAttMarker::kShapeCircle))
277 glBegin(GL_LINE_LOOP);
278 else if (markerType == TAttMarker::kShapeFilledCircle)
280 else
282
283 for (auto &pnt : markerShape)
284 glVertex2d(x + pnt.fX, y - pnt.fY);
285 glEnd();
286 }
289 break;
291 glBegin(GL_LINES);
292 for (unsigned i = 0; i < n; ++i) {
293 const Double_t x = xy[i].fX;
294 const Double_t y = xy[i].fY;
295 for (auto &pnt : markerShape)
296 glVertex2d(x + pnt.fX, y - pnt.fY);
297 }
298 glEnd();
299 break;
301 for (unsigned i = 0; i < n; ++i) {
302 const Double_t x = xy[i].fX;
303 const Double_t y = xy[i].fY;
304 glBegin(GL_TRIANGLES);
305 for (auto &pnt : markerShape)
306 glVertex2d(x + pnt.fX, y - pnt.fY);
307 glEnd();
308 }
309 break;
310 }
311
312}
313
314
315
316/*
317Small RAII class for GLU tesselator.
318*/
319#ifndef CALLBACK
320#define CALLBACK
321#endif
322
323extern "C" {
324#if defined(__APPLE_CC__) && __APPLE_CC__ > 4000 && __APPLE_CC__ < 5450 && !defined(__INTEL_COMPILER)
325 typedef GLvoid (*tess_t)(...);
326#elif defined( __mips ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __sun ) || defined (__CYGWIN__) || defined (__APPLE__)
327 typedef GLvoid (*tess_t)();
328#elif defined ( WIN32)
329 typedef GLvoid (CALLBACK *tess_t)( );
330#else
331 #error "Error - need to define type tess_t for this platform/compiler"
332#endif
333}
334
335////////////////////////////////////////////////////////////////////////////////
336
338{
340 if (!dump)
341 return;
342
343 dump->push_back(MeshPatch_t(type));
344}
345
346////////////////////////////////////////////////////////////////////////////////
347
348void Vertex(const Double_t *v)
349{
351 if (!dump)
352 return;
353
354 std::vector<Double_t> & vs = dump->back().fPatch;
355 vs.push_back(v[0]);
356 vs.push_back(v[1]);
357 vs.push_back(v[2]);
358}
359
360////////////////////////////////////////////////////////////////////////////////
361
362void End()
363{
364}
365
367
368////////////////////////////////////////////////////////////////////////////////
369
371 : fTess(nullptr)
372{
374 if (!tess)
375 throw std::runtime_error("tesselator creation failed");
376
377#if defined(__GNUC__) && __GNUC__ >= 8
378#pragma GCC diagnostic push
379#pragma GCC diagnostic ignored "-Wcast-function-type"
380#endif
381
382 if (!dump) {
386 } else {
390 }
391
392#if defined(__GNUC__) && __GNUC__ >= 8
393#pragma GCC diagnostic pop
394#endif
395
397 fTess = tess;
398}
399
400////////////////////////////////////////////////////////////////////////////////
401
406
407/*
408In future, this should be an interface to per-pad FBO.
409Currently, in only save sizes and coordinates (?)
410*/
411////////////////////////////////////////////////////////////////////////////////
412
414 : fW(w), fH(h), fX(x), fY(y), fTop(top)
415{
416}
417
418////////////////////////////////////////////////////////////////////////////////
419
421 : fMaxLineWidth(0.),
422 fMaxPointSize(0.)
423{
424}
425
426////////////////////////////////////////////////////////////////////////////////
427
429{
430 if (!fMaxLineWidth) {
431 Double_t lp[2] = {};
432 glGetDoublev(lineWidthPNAME, lp);//lineWidthPNAME is defined at the top of this file.
433 fMaxLineWidth = lp[1];
434 }
435
436 return fMaxLineWidth;
437}
438
439////////////////////////////////////////////////////////////////////////////////
440
442{
443 if (!fMaxPointSize) {
444 Double_t lp[2] = {};
445 glGetDoublev(pointSizePNAME, lp);//pointSizePNAME is defined at the top of this file.
446 fMaxPointSize = lp[1];
447 }
448
449 return fMaxLineWidth;
450}
451
452
453////////////////////////////////////////////////////////////////////////////////
454
456{
457 const TColor *color = gROOT->GetColor(colorIndex);
458 if (color) {
459 color->GetRGB(rgba[0], rgba[1], rgba[2]);
460 rgba[3] = color->GetAlpha();
461 }
462}
463
464////////////////////////////////////////////////////////////////////////////////
465
466template<class ValueType>
467BoundingRect<ValueType> FindBoundingRect(Int_t nPoints, const ValueType *xs, const ValueType *ys)
468{
469 assert(nPoints > 0 && "FindBoundingRect, invalind number of points");
470 assert(xs != nullptr && "FindBoundingRect, parameter 'xs' is null");
471 assert(ys != nullptr && "FindBoundingRect, parameter 'ys' is null");
472
473 ValueType xMin = xs[0], xMax = xMin;
474 ValueType yMin = ys[0], yMax = yMin;
475
476 for (Int_t i = 1; i < nPoints; ++i) {
477 xMin = TMath::Min(xMin, xs[i]);
478 xMax = TMath::Max(xMax, xs[i]);
479
480 yMin = TMath::Min(yMin, ys[i]);
481 yMax = TMath::Max(yMax, ys[i]);
482 }
483
485 box.fXMin = xMin;
486 box.fXMax = xMax;
487 box.fWidth = xMax - xMin;
488
489 box.fYMin = yMin;
490 box.fYMax = yMax;
491 box.fHeight = yMax - yMin;
492
493 return box;
494}
495
501
502
503}//namespace Pad
504}//namespace Rgl
#define b(i)
Definition RSha256.hxx:100
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
const unsigned char gStipples[26][32]
Definition RStipples.h:24
short Style_t
Style number (short)
Definition RtypesCore.h:97
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
short Color_t
Color number (short)
Definition RtypesCore.h:100
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:69
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
short Width_t
Line width (short)
Definition RtypesCore.h:99
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
short SCoord_t
Screen coordinates (short)
Definition RtypesCore.h:101
@ kOpenSquare
Definition TAttMarker.h:78
@ kOpenTriangleUp
Definition TAttMarker.h:79
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define CALLBACK
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void pixel
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 r
Option_t Option_t TPoint TPoint angle
Option_t Option_t TPoint xy
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
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 Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t style
#define gROOT
Definition TROOT.h:417
#define gVirtualX
Definition TVirtualX.h:377
FillAttribSet(const PolygonStippleSet &set, Bool_t ignoreStipple, const TAttFill *att=nullptr)
Polygon stipple, if required.
Double_t GetMaxLineWidth() const
Double_t GetMaxPointSize() const
LineAttribSet(Bool_t smooth, UInt_t stipple, Double_t maxWidth, Bool_t setWidth, const TAttLine *att=nullptr)
Set up line parameters.
static void DrawMarkers(UInt_t n, const TPoint *xy, const TAttMarker &attr)
Auxiliary class to draw markers in a gl-pad.
OffScreenDevice(UInt_t w, UInt_t h, UInt_t x, UInt_t y, Bool_t top)
std::vector< unsigned char > fStipples
Definition TGLPadUtils.h:44
static UInt_t SwapBits(UInt_t bits)
static const UInt_t fgBitSwap[]
Definition TGLPadUtils.h:46
Tesselator(Bool_t dump=kFALSE)
static Tesselation_t * GetDump()
static Tesselation_t * fVs
Fill Area Attributes class.
Definition TAttFill.h:21
Line Attributes class.
Definition TAttLine.h:21
Marker Attributes class.
Definition TAttMarker.h:22
@ kPreferTriangles
Definition TAttMarker.h:50
static Style_t GetMarkerStyleBase(Style_t style)
Internal helper function that returns the corresponding marker style with line width 1 for the given ...
@ kShapeTriangles
Definition TAttMarker.h:48
@ kShapeFilledArea
Definition TAttMarker.h:48
@ kShapeFilledCircle
Definition TAttMarker.h:48
The color creation and management class.
Definition TColor.h:22
virtual void GetRGB(Float_t &r, Float_t &g, Float_t &b) const
Definition TColor.h:55
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1939
Float_t GetAlpha() const
Definition TColor.h:67
SCoord_t fY
Definition TPoint.h:36
SCoord_t fX
Definition TPoint.h:35
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
const UShort_t gLineStipples[]
std::list< MeshPatch_t > Tesselation_t
BoundingRect< ValueType > FindBoundingRect(Int_t nPoints, const ValueType *xs, const ValueType *ys)
void Vertex(const Double_t *v)
const UInt_t gMaxStipple
void ExtractRGBA(Color_t colorIndex, Float_t *rgba)
const GLenum lineWidthPNAME
void Begin(Int_t type)
const GLenum pointSizePNAME
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:607
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:601
constexpr Double_t TwoPi()
Definition TMath.h:47