File: TGLPadUtils.cxx

package info (click to toggle)
root-system 5.34.19%2Bdfsg-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 210,548 kB
  • ctags: 209,442
  • sloc: cpp: 1,488,197; ansic: 345,435; sh: 19,468; fortran: 12,570; makefile: 7,873; python: 7,683; xml: 555; ruby: 553; csh: 356; perl: 329; objc: 38; sql: 14; tcl: 4
file content (741 lines) | stat: -rw-r--r-- 22,163 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
// @(#)root/gl:$Id$
// Author:  Timur Pocheptsov  06/05/2009

/*************************************************************************
 * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include <stdexcept>
#include <cassert>

#include "TVirtualPad.h"
#include "TVirtualX.h"
#include "RStipples.h"
#include "TColor.h"
#include "TROOT.h"
#include "TMath.h"

#include "TGLPadUtils.h"
#include "TGLIncludes.h"

namespace Rgl {
namespace Pad {

const UInt_t PolygonStippleSet::fgBitSwap[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};


/*
Temporary fix.
*/
#ifndef GL_VERSION_1_2
const GLenum lineWidthPNAME = GLenum(0xB22);
const GLenum pointSizePNAME = GLenum(0xB12);
#else
const GLenum lineWidthPNAME = GLenum(GL_SMOOTH_LINE_WIDTH_RANGE);//Cast for real enums and macros.
const GLenum pointSizePNAME = GLenum(GL_SMOOTH_POINT_SIZE_RANGE);
#endif

/*
Auxiliary class to converts ROOT's polygon stipples from
RStipples.h into GL's stipples and hold them in a fStipples array.
*/
//______________________________________________________________________________
PolygonStippleSet::PolygonStippleSet()
{
    /*
    I have to assume, that gStipple has two chars in a line.
    There in no way to calculate line length and there are no corresponding constants in RStipple.h.
    So, these numbers are hardcode here.
    Ordering in RStipples completely different from OpenGL.
    In OpenGL, if I have, say, 16x2 pattern, GLbytes will be:

    [3][4]
    [1][2]

    and bits inside them

    [7 6 5 4 3 2 1 0][7 6 5 4 3 2 1 0]
    [7 6 5 4 3 2 1 0][7 6 5 4 3 2 1 0].

    But for X11 this will be:

    [2][1]
    [4][3]

    [0 1 2 3 4 5 6 7][0 1 2 3 4 5 6 7]
    [0 1 2 3 4 5 6 7][0 1 2 3 4 5 6 7]

    So, line 0x7, 0xE from X11 must be
    converted into 0x70, 0xE0 for OpenGL.

    As OpenGL expects 32x32 pattern, I have to twice each line.
   */

   /*If somebody will seriously change gStipples declaration,
   so, that sizeof gStipples becomes "wrong", change this!*/
   const UInt_t numOfStipples = sizeof gStipples / sizeof gStipples[0];
   fStipples.resize(kStippleSize * numOfStipples);

   for (UInt_t i = 0; i < numOfStipples; ++i) {
      const UInt_t baseInd = i * kStippleSize;

      for (Int_t j = 15, j1 = 0; j >= 0; --j, ++j1) {//ROOT uses 16x16 stipples.
         const UInt_t rowShift = j1 * kRowSize;

         for (Int_t k = 1, k1 = 0; k >= 0; --k, ++k1) {//Two chars form a line.
            const UChar_t pixel = SwapBits(gStipples[i][j * 2 + k]);
            const UInt_t ind = baseInd + rowShift + k1;

            fStipples[ind]      = pixel;
            fStipples[ind + 2]  = pixel;
            fStipples[ind + 64] = pixel;
            fStipples[ind + 66] = pixel;
         }
      }
   }
}

//______________________________________________________________________________
UInt_t PolygonStippleSet::SwapBits(UInt_t b)
{
#ifdef WIN32
   b = ~b & 0xff;
#endif
   b &= k16Bits;

   const UInt_t low = fgBitSwap[b & kLow4] << 4;
   const UInt_t up  = fgBitSwap[(b & kUp4) >> 4];

   return low | up;
}

/*
Class to manipulate fill parameters.
*/
//______________________________________________________________________________
FillAttribSet::FillAttribSet(const PolygonStippleSet &set, Bool_t ignoreStipple)
                  : fStipple(0), fAlpha(1.)
{
   //Polygon stipple, if required.
   const UInt_t style = gVirtualX->GetFillStyle() / 1000;

   if (!ignoreStipple) {
      if (style == 3) {
         const UInt_t fasi  = gVirtualX->GetFillStyle() % 1000;
         fStipple = (fasi >= 1 && fasi <=25) ? fasi : 2;
         glPolygonStipple(&set.fStipples[fStipple * PolygonStippleSet::kStippleSize]);
         glEnable(GL_POLYGON_STIPPLE);
      }
   }

   // Color and transparency
   Float_t rgba[] = {0.f, 0.f, 0.f, 1.f};
   ExtractRGBA(gVirtualX->GetFillColor(), rgba);
   fAlpha = rgba[3];
   if (fAlpha<1.) {
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   }
   glColor4fv(rgba);
}

//______________________________________________________________________________
FillAttribSet::~FillAttribSet()
{
   if (fStipple)
      glDisable(GL_POLYGON_STIPPLE);

   if (fAlpha<1.)
      glDisable(GL_BLEND);
}

/*
"ROOT like" line stipples.
*/

const UShort_t gLineStipples[] = {0xffff, 0xffff, 0x3333, 0x5555,
                                  0xf040, 0xf4f4, 0xf111, 0xf0f0,
                                  0xff11, 0x3fff, 0x08ff};

const UInt_t gMaxStipple = sizeof gLineStipples / sizeof gLineStipples[0];

/*
Set/unset line attributes.
*/
//______________________________________________________________________________
LineAttribSet::LineAttribSet(Bool_t smooth, UInt_t stipple, Double_t maxWidth, Bool_t setWidth)
                  : fSmooth(smooth), fStipple(stipple), fSetWidth(setWidth), fAlpha(0.8)
{
   //Set up line parameters.
   //Smooth.
   if (fSmooth) {
      glEnable(GL_BLEND);
      glEnable(GL_LINE_SMOOTH);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
   }

   //Stipple.
   if (fStipple > 1) {
      if (fStipple >= gMaxStipple)
         fStipple = 1;
      else {
         glEnable(GL_LINE_STIPPLE);
         glLineStipple(fStipple == 10 ? 2 : 1, gLineStipples[fStipple]);
      }
   }

   //Color and transparency
   Float_t rgba[] = {0.f, 0.f, 0.f, 0.8f};
   ExtractRGBA(gVirtualX->GetLineColor(), rgba);
   fAlpha = rgba[3];
   if (fAlpha<0.8) {
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   }
   glColor4fv(rgba);

   //Width.
   if (fSetWidth) {
      const Width_t w = gVirtualX->GetLineWidth();
      glLineWidth(w > maxWidth ? maxWidth : !w ? 1.f : w);
   }
}

//______________________________________________________________________________
LineAttribSet::~LineAttribSet()
{
   if (fSmooth || fAlpha<0.8) {
      glDisable(GL_LINE_SMOOTH);
      glDisable(GL_BLEND);
   }

   if (fStipple > 1)
      glDisable(GL_LINE_STIPPLE);

   if (fSetWidth)
      glLineWidth(1.f);
}

/*
Auxiliary class to draw markers in a gl-pad.
*/
//______________________________________________________________________________
void MarkerPainter::DrawDot(UInt_t n, const TPoint *xy)const
{
   //Simple 1-pixel dots.
   glBegin(GL_POINTS);

   for (UInt_t i = 0; i < n; ++i)
      glVertex2d(xy[i].fX, xy[i].fY);

   glEnd();
}

//______________________________________________________________________________
void MarkerPainter::DrawPlus(UInt_t n, const TPoint *xy)const
{
   //+ sign. 1 pixel width lines.
   const Double_t im = 4 * gVirtualX->GetMarkerSize() + 0.5;
   glBegin(GL_LINES);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;
      glVertex2d(-im + x, y);
      glVertex2d(im + x, y);
      glVertex2d(x, -im + y);
      glVertex2d(x, im + y);
   }

   glEnd();
}

//______________________________________________________________________________
void MarkerPainter::DrawStar(UInt_t n, const TPoint *xy)const
{
   //* - marker.
   SCoord_t im = SCoord_t(4 * gVirtualX->GetMarkerSize() + 0.5);
   fStar[0].fX = -im;  fStar[0].fY = 0;
   fStar[1].fX =  im;  fStar[1].fY = 0;
   fStar[2].fX = 0  ;  fStar[2].fY = -im;
   fStar[3].fX = 0  ;  fStar[3].fY = im;
   im = SCoord_t(0.707*Float_t(im) + 0.5);
   fStar[4].fX = -im;  fStar[4].fY = -im;
   fStar[5].fX =  im;  fStar[5].fY = im;
   fStar[6].fX = -im;  fStar[6].fY = im;
   fStar[7].fX =  im;  fStar[7].fY = -im;

   glBegin(GL_LINES);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glVertex2d(fStar[0].fX + x, fStar[0].fY + y);
      glVertex2d(fStar[1].fX + x, fStar[1].fY + y);
      glVertex2d(fStar[2].fX + x, fStar[2].fY + y);
      glVertex2d(fStar[3].fX + x, fStar[3].fY + y);
      glVertex2d(fStar[4].fX + x, fStar[4].fY + y);
      glVertex2d(fStar[5].fX + x, fStar[5].fY + y);
      glVertex2d(fStar[6].fX + x, fStar[6].fY + y);
      glVertex2d(fStar[7].fX + x, fStar[7].fY + y);
   }

   glEnd();
}

//______________________________________________________________________________
void MarkerPainter::DrawX(UInt_t n, const TPoint *xy)const
{
   const Double_t im = 0.707 * (4 * gVirtualX->GetMarkerSize() + 0.5) + 0.5;

   glBegin(GL_LINES);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glVertex2d(-im + x, -im + y);
      glVertex2d(im + x, im + y);
      glVertex2d(-im + x, im + y);
      glVertex2d(im + x, -im + y);
   }

   glEnd();
}

//______________________________________________________________________________
void MarkerPainter::DrawFullDotSmall(UInt_t n, const TPoint *xy)const
{
   glBegin(GL_LINES);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glVertex2d(-1. + x, y);
      glVertex2d(x + 1., y);
      glVertex2d(x, -1. + y);
      glVertex2d(x, 1. + y);
   }

   glEnd();
}

//______________________________________________________________________________
void MarkerPainter::DrawFullDotMedium(UInt_t n, const TPoint *xy)const
{
   for (UInt_t i = 0; i < n; ++i)
      glRectd(xy[i].fX - 1, xy[i].fY - 1, xy[i].fX + 1, xy[i].fY + 1);
}

namespace {
//Auxilary function for MarkerPainter. Define near the end of this source file.
void CalculateCircle(std::vector<TPoint> &circle, Double_t r, UInt_t pts);
}

//______________________________________________________________________________
void MarkerPainter::DrawCircle(UInt_t n, const TPoint *xy)const
{
   Double_t r = 4 * gVirtualX->GetMarkerSize() + 0.5;
   if (r > 100.)
      r = 100.;//as in TGX11.

   fCircle.clear();
   CalculateCircle(fCircle, r, r < 100. ? kSmallCirclePts : kLargeCirclePts);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_LINE_LOOP);
      for (UInt_t j = 0, e = fCircle.size(); j < e; ++j)
         glVertex2d(fCircle[j].fX + x, fCircle[j].fY + y);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawFullDotLarge(UInt_t n, const TPoint *xy)const
{
   fCircle.clear();
   fCircle.push_back(TPoint(0, 0));

   Double_t r = 4 * gVirtualX->GetMarkerSize() + 0.5;
   if (r > 100.)
      r = 100;//as in TGX11.

   CalculateCircle(fCircle, r, r < 100 ? kSmallCirclePts : kLargeCirclePts);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_TRIANGLE_FAN);
      for (UInt_t j = 0, e = fCircle.size(); j < e; ++j)
         glVertex2d(fCircle[j].fX + x, fCircle[j].fY + y);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawFullSquare(UInt_t n, const TPoint *xy)const
{
   const Double_t im = 4 * gVirtualX->GetMarkerSize() + 0.5;
   for (UInt_t i = 0; i < n; ++i)
      glRectd(xy[i].fX - im, xy[i].fY - im, xy[i].fX + im, xy[i].fY + im);
}

//______________________________________________________________________________
void MarkerPainter::DrawFullTrianlgeUp(UInt_t n, const TPoint *xy)const
{
   const Double_t im = 4 * gVirtualX->GetMarkerSize() + 0.5;
   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;
      glBegin(GL_POLYGON);
      glVertex2d(x - im, y - im);
      glVertex2d(x + im, y - im);
      glVertex2d(x, im + y);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawFullTrianlgeDown(UInt_t n, const TPoint *xy)const
{
   const Int_t im = Int_t(4 * gVirtualX->GetMarkerSize() + 0.5);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;
      glBegin(GL_POLYGON);
      glVertex2d(x - im, y + im);
      glVertex2d(x, y - im);
      glVertex2d(im + x, y + im);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawDiamond(UInt_t n, const TPoint *xy)const
{
   const Int_t im  = Int_t(4 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t imx = Int_t(2.66 * gVirtualX->GetMarkerSize() + 0.5);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_LINE_LOOP);
      glVertex2d(x - imx,  y);
      glVertex2d(x, y - im);
      glVertex2d(x + imx, y);
      glVertex2d(x, y + im);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawCross(UInt_t n, const TPoint *xy)const
{
   const Int_t im  = Int_t(4 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t imx = Int_t(1.33 * gVirtualX->GetMarkerSize() + 0.5);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_LINE_LOOP);
      glVertex2d(x - im, y - imx);
      glVertex2d(x - imx, y - imx);
      glVertex2d(x - imx, y - im);
      glVertex2d(x + imx, y - im);
      glVertex2d(x + imx, y - imx);
      glVertex2d(x + im, y - imx);
      glVertex2d(x + im, y + imx);
      glVertex2d(x + imx, y + imx);
      glVertex2d(x + imx, y + im);
      glVertex2d(x - imx, y + im);
      glVertex2d(x - imx, y + imx);
      glVertex2d(x - im, y + imx);
      glEnd();
   }
}

//______________________________________________________________________________
void MarkerPainter::DrawFullStar(UInt_t n, const TPoint *xy)const
{
   // HIGZ full star pentagone
   const Int_t im  = Int_t(4 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im1 = Int_t(0.66 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im2 = Int_t(2.00 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im3 = Int_t(2.66 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im4 = Int_t(1.33 * gVirtualX->GetMarkerSize() + 0.5);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_TRIANGLES);
      glVertex2d(x - im, y - im4);//0
      glVertex2d(x - im2, y + im1);//1
      glVertex2d(x - im4, y - im4);//9

      glVertex2d(x - im2, y + im1);//1
      glVertex2d(x - im3, y + im);//2
      glVertex2d(x, y + im2);//3

      glVertex2d(x, y + im2);//3
      glVertex2d(x + im3, y + im);//4
      glVertex2d(x + im2, y + im1);//5

      glVertex2d(x + im2, y + im1);//5
      glVertex2d(x + im, y - im4);//6
      glVertex2d(x + im4, y - im4);//7

      glVertex2d(x + im4, y - im4);//7
      glVertex2d(x, y - im);//8
      glVertex2d(x - im4, y - im4);//9

      glVertex2d(x - im4, y - im4);//9
      glVertex2d(x - im2, y + im1);//1
      glVertex2d(x, y + im2);//3

      glVertex2d(x - im4, y - im4);//9
      glVertex2d(x, y + im2);//3
      glVertex2d(x + im2, y + im1);//5

      glVertex2d(x - im4, y - im4);//9
      glVertex2d(x + im2, y + im1);//5
      glVertex2d(x + im4, y - im4);//7

      glEnd();

   }
}

//______________________________________________________________________________
void MarkerPainter::DrawOpenStar(UInt_t n, const TPoint *xy)const
{
   // HIGZ full star pentagone
   const Int_t im  = Int_t(4 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im1 = Int_t(0.66 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im2 = Int_t(2.00 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im3 = Int_t(2.66 * gVirtualX->GetMarkerSize() + 0.5);
   const Int_t im4 = Int_t(1.33 * gVirtualX->GetMarkerSize() + 0.5);

   for (UInt_t i = 0; i < n; ++i) {
      const Double_t x = xy[i].fX;
      const Double_t y = xy[i].fY;

      glBegin(GL_LINE_LOOP);
      glVertex2d(x - im, y - im4);
      glVertex2d(x - im2, y + im1);
      glVertex2d(x - im3, y + im);
      glVertex2d(x, y + im2);
      glVertex2d(x + im3, y + im);
      glVertex2d(x + im2, y + im1);
      glVertex2d(x + im, y - im4);
      glVertex2d(x + im4, y - im4);
      glVertex2d(x, y - im);
      glVertex2d(x - im4, y - im4);
      glEnd();
   }
}

/*
Small RAII class for GLU tesselator.
*/
#ifndef CALLBACK
#define CALLBACK
#endif

extern "C" {
#if defined(__APPLE_CC__) && __APPLE_CC__ > 4000 && __APPLE_CC__ < 5450 && !defined(__INTEL_COMPILER)
    typedef GLvoid (*tess_t)(...);
#elif defined( __mips ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __sun ) || defined (__CYGWIN__) || defined (__APPLE__) || defined(__FreeBSD_kernel__) || defined(__GNU__)
    typedef GLvoid (*tess_t)();
#elif defined ( WIN32)
    typedef GLvoid (CALLBACK *tess_t)( );
#else
    #error "Error - need to define type tess_t for this platform/compiler"
#endif
}

//______________________________________________________________________________
void Begin(Int_t type)
{
   Tesselation_t *dump = Tesselator::GetDump();
   if (!dump)
      return;

   dump->push_back(MeshPatch_t(type));
}

//______________________________________________________________________________
void Vertex(const Double_t *v)
{
   Tesselation_t *dump = Tesselator::GetDump();
   if (!dump)
      return;

   std::vector<Double_t> & vs = dump->back().fPatch;
   vs.push_back(v[0]);
   vs.push_back(v[1]);
   vs.push_back(v[2]);
}

//______________________________________________________________________________
void End()
{
}

Tesselation_t *Tesselator::fVs = 0;

//______________________________________________________________________________
Tesselator::Tesselator(Bool_t dump)
               : fTess(0)
{
   GLUtesselator *tess = gluNewTess();
   if (!tess)
      throw std::runtime_error("tesselator creation failed");

   if (!dump) {
      gluTessCallback(tess, (GLenum)GLU_BEGIN,  (tess_t) glBegin);
      gluTessCallback(tess, (GLenum)GLU_END,    (tess_t) glEnd);
      gluTessCallback(tess, (GLenum)GLU_VERTEX, (tess_t) glVertex3dv);
   } else {
      gluTessCallback(tess, (GLenum)GLU_BEGIN,  (tess_t) Begin);
      gluTessCallback(tess, (GLenum)GLU_END,    (tess_t) End);
      gluTessCallback(tess, (GLenum)GLU_VERTEX, (tess_t) Vertex);
   }

   gluTessProperty(tess, GLU_TESS_TOLERANCE, 1e-10);
   fTess = tess;
}

//______________________________________________________________________________
Tesselator::~Tesselator()
{
   gluDeleteTess((GLUtesselator *)fTess);
}

/*
In future, this should be an interface to per-pad FBO.
Currently, in only save sizes and coordinates (?)
*/
//______________________________________________________________________________
OffScreenDevice::OffScreenDevice(UInt_t w, UInt_t h, UInt_t x, UInt_t y, Bool_t top)
                   : fW(w), fH(h), fX(x), fY(y), fTop(top)
{
}

//______________________________________________________________________________
GLLimits::GLLimits()
            : fMaxLineWidth(0.),
              fMaxPointSize(0.)
{
}

//______________________________________________________________________________
Double_t GLLimits::GetMaxLineWidth()const
{
   if (!fMaxLineWidth) {
      Double_t lp[2] = {};
      glGetDoublev(lineWidthPNAME, lp);//lineWidthPNAME is defined at the top of this file.
      fMaxLineWidth = lp[1];
   }

   return fMaxLineWidth;
}

//______________________________________________________________________________
Double_t GLLimits::GetMaxPointSize()const
{
   if (!fMaxPointSize) {
      Double_t lp[2] = {};
      glGetDoublev(pointSizePNAME, lp);//pointSizePNAME is defined at the top of this file.
      fMaxPointSize = lp[1];
   }

   return fMaxLineWidth;
}


//______________________________________________________________________________
void ExtractRGBA(Color_t colorIndex, Float_t *rgba)
{
   const TColor *color = gROOT->GetColor(colorIndex);
   if (color) {
      color->GetRGB(rgba[0], rgba[1], rgba[2]);
      rgba[3] = color->GetAlpha();
   }
}

//______________________________________________________________________________
template<class ValueType>
BoundingRect<ValueType> FindBoundingRect(Int_t nPoints, const ValueType *xs, const ValueType *ys)
{
   assert(nPoints > 0 && "FindBoundingRect, invalind number of points");
   assert(xs != nullptr && "FindBoundingRect, parameter 'xs' is null");
   assert(ys != nullptr && "FindBoundingRect, parameter 'ys' is null");
   
   ValueType xMin = xs[0], xMax = xMin;
   ValueType yMin = ys[0], yMax = yMin;
   
   for (Int_t i = 1; i < nPoints; ++i) {
      xMin = TMath::Min(xMin, xs[i]);
      xMax = TMath::Max(xMax, xs[i]);

      yMin = TMath::Min(yMin, ys[i]);
      yMax = TMath::Max(yMax, ys[i]);
   }
   
   BoundingRect<ValueType> box;
   box.fXMin = xMin;
   box.fXMax = xMax;
   box.fWidth = xMax - xMin;
   
   box.fYMin = yMin;
   box.fYMax = yMax;
   box.fHeight = yMax - yMin;
   
   return box;
}

template BoundingRect<Double_t> FindBoundingRect(Int_t nPoints, const Double_t *xs, const Double_t *ys);
template BoundingRect<Float_t> FindBoundingRect(Int_t nPoints, const Float_t *xs, const Float_t *ys);
template BoundingRect<Long_t> FindBoundingRect(Int_t nPoints, const Long_t *xs, const Long_t *ys);
template BoundingRect<Int_t> FindBoundingRect(Int_t nPoints, const Int_t *xs, const Int_t *ys);
template BoundingRect<SCoord_t> FindBoundingRect(Int_t nPoints, const SCoord_t *xs, const SCoord_t *ys);

namespace {

//______________________________________________________________________________
void CalculateCircle(std::vector<TPoint> &circle, Double_t r, UInt_t pts)
{
   const Double_t delta = TMath::TwoPi() / pts;
   const UInt_t first = circle.size();
   Double_t angle = 0.;
   circle.resize(circle.size() + pts + 1);

   for (UInt_t i = 0; i < pts; ++i, angle += delta) {
      circle[first + i].fX = SCoord_t(r * TMath::Cos(angle));
      circle[first + i].fY = SCoord_t(r * TMath::Sin(angle));
   }

   circle.back().fX = circle[first].fX;
   circle.back().fY = circle[first].fY;
}

}//anonymous namespace

}//namespace Pad
}//namespace Rgl