// @(#)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.             *
 *************************************************************************/

#ifndef ROOT_TGLPadPainter
#define ROOT_TGLPadPainter

#include <deque>

#ifndef ROOT_TVirtualPadPainter
#include "TVirtualPadPainter.h"
#endif
#ifndef ROOT_TGLFontmanager
#include "TGLFontManager.h"
#endif
#ifndef ROOT_TGLPadUtils
#include "TGLPadUtils.h"
#endif
#ifndef ROOT_TPoint
#include "TPoint.h"
#endif

class TLinearGradient;
class TRadialGradient;
/*
The _main_ purpose of TGLPadPainter is to enable 2d gl raphics
inside standard TPad/TCanvas.
*/
class TGLPadPainter : public TVirtualPadPainter {
private:
   Rgl::Pad::PolygonStippleSet fSSet;
   Rgl::Pad::Tesselator        fTess;
   Rgl::Pad::MarkerPainter     fMarker;
   Rgl::Pad::GLLimits          fLimits;
   
   std::vector<Double_t>       fVs;//Vertex buffer for tesselator.
   
   TGLFontManager              fFM;
   TGLFont                     fF;
   
   Int_t                       fVp[4];

   std::vector<TPoint>         fPoly;
   Bool_t                      fIsHollowArea;
   
   Bool_t                      fLocked;
   
   template<class Char_t>
   void DrawTextHelper(Double_t x, Double_t y, const Char_t *text, ETextMode mode);
public:
   TGLPadPainter();
   
   //Final overriders for TVirtualPadPainter pure virtual functions.
   //1. Part, which simply delegates to TVirtualX.
   //Line attributes.
   Color_t  GetLineColor() const;
   Style_t  GetLineStyle() const;
   Width_t  GetLineWidth() const;
   
   void     SetLineColor(Color_t lcolor);
   void     SetLineStyle(Style_t lstyle);
   void     SetLineWidth(Width_t lwidth);
   //Fill attributes.
   Color_t  GetFillColor() const;
   Style_t  GetFillStyle() const;
   Bool_t   IsTransparent() const;

   void     SetFillColor(Color_t fcolor);
   void     SetFillStyle(Style_t fstyle);
   void     SetOpacity(Int_t percent);
   //Text attributes.
   Short_t  GetTextAlign() const;
   Float_t  GetTextAngle() const;
   Color_t  GetTextColor() const;
   Font_t   GetTextFont()  const;
   Float_t  GetTextSize()  const;
   Float_t  GetTextMagnitude() const;
   
   void     SetTextAlign(Short_t align);
   void     SetTextAngle(Float_t tangle);
   void     SetTextColor(Color_t tcolor);
   void     SetTextFont(Font_t tfont);
   void     SetTextSize(Float_t tsize);
   void     SetTextSizePixels(Int_t npixels);
   
   //2. "Off-screen management" part.
   Int_t    CreateDrawable(UInt_t w, UInt_t h);
   void     ClearDrawable();
   void     CopyDrawable(Int_t id, Int_t px, Int_t py);
   void     DestroyDrawable();
   void     SelectDrawable(Int_t device);

   void     InitPainter();
   void     InvalidateCS();
   void     LockPainter();
   
   void     DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2);
   void     DrawLineNDC(Double_t u1, Double_t v1, Double_t u2, Double_t v2);
   
   void     DrawBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2, EBoxMode mode);
   //TPad needs double and float versions.
   void     DrawFillArea(Int_t n, const Double_t *x, const Double_t *y);
   void     DrawFillArea(Int_t n, const Float_t *x, const Float_t *y);
   
   //TPad needs both double and float versions of DrawPolyLine.
   void     DrawPolyLine(Int_t n, const Double_t *x, const Double_t *y);
   void     DrawPolyLine(Int_t n, const Float_t *x, const Float_t *y);
   void     DrawPolyLineNDC(Int_t n, const Double_t *u, const Double_t *v);
   
   //TPad needs both versions.
   void     DrawPolyMarker(Int_t n, const Double_t *x, const Double_t *y);
   void     DrawPolyMarker(Int_t n, const Float_t *x, const Float_t *y);
   
   void     DrawText(Double_t x, Double_t y, const char *text, ETextMode mode);
   void     DrawText(Double_t, Double_t, const wchar_t *, ETextMode);
   void     DrawTextNDC(Double_t x, Double_t y, const char *text, ETextMode mode);
   void     DrawTextNDC(Double_t, Double_t, const wchar_t *, ETextMode);
   
   //jpg, png, gif and bmp output.
   void     SaveImage(TVirtualPad *pad, const char *fileName, Int_t type) const;

   //TASImage support.
   void     DrawPixels(const unsigned char *pixelData, UInt_t width, UInt_t height,
                       Int_t dstX, Int_t dstY, Bool_t enableBlending);
   

private:
   
   //Attention! GL_PROJECTION will become 
   //the current matrix after these calls.
   void     SaveProjectionMatrix()const;
   void     RestoreProjectionMatrix()const;
   
   //Attention! GL_MODELVIEW will become the 
   //current matrix after these calls.
   void     SaveModelviewMatrix()const;
   void     RestoreModelviewMatrix()const;
   
   void     SaveViewport();
   void     RestoreViewport();
   
   void     DrawPolyMarker();
   
   //Aux. functions for a gradient and solid fill:
   void DrawPolygonWithGradient(Int_t n, const Double_t *x, const Double_t *y);
   //
   void DrawGradient(const TLinearGradient *gradient, Int_t n, const Double_t *x, const Double_t *y);
   void DrawGradient(const TRadialGradient *gradient, Int_t n, const Double_t *x, const Double_t *y);
   //
   void DrawTesselation(Int_t n, const Double_t *x, const Double_t *y);
   
   TGLPadPainter(const TGLPadPainter &rhs);
   TGLPadPainter & operator = (const TGLPadPainter &rhs);
   
   ClassDef(TGLPadPainter, 0)
};

#endif

 TGLPadPainter.h:1
 TGLPadPainter.h:2
 TGLPadPainter.h:3
 TGLPadPainter.h:4
 TGLPadPainter.h:5
 TGLPadPainter.h:6
 TGLPadPainter.h:7
 TGLPadPainter.h:8
 TGLPadPainter.h:9
 TGLPadPainter.h:10
 TGLPadPainter.h:11
 TGLPadPainter.h:12
 TGLPadPainter.h:13
 TGLPadPainter.h:14
 TGLPadPainter.h:15
 TGLPadPainter.h:16
 TGLPadPainter.h:17
 TGLPadPainter.h:18
 TGLPadPainter.h:19
 TGLPadPainter.h:20
 TGLPadPainter.h:21
 TGLPadPainter.h:22
 TGLPadPainter.h:23
 TGLPadPainter.h:24
 TGLPadPainter.h:25
 TGLPadPainter.h:26
 TGLPadPainter.h:27
 TGLPadPainter.h:28
 TGLPadPainter.h:29
 TGLPadPainter.h:30
 TGLPadPainter.h:31
 TGLPadPainter.h:32
 TGLPadPainter.h:33
 TGLPadPainter.h:34
 TGLPadPainter.h:35
 TGLPadPainter.h:36
 TGLPadPainter.h:37
 TGLPadPainter.h:38
 TGLPadPainter.h:39
 TGLPadPainter.h:40
 TGLPadPainter.h:41
 TGLPadPainter.h:42
 TGLPadPainter.h:43
 TGLPadPainter.h:44
 TGLPadPainter.h:45
 TGLPadPainter.h:46
 TGLPadPainter.h:47
 TGLPadPainter.h:48
 TGLPadPainter.h:49
 TGLPadPainter.h:50
 TGLPadPainter.h:51
 TGLPadPainter.h:52
 TGLPadPainter.h:53
 TGLPadPainter.h:54
 TGLPadPainter.h:55
 TGLPadPainter.h:56
 TGLPadPainter.h:57
 TGLPadPainter.h:58
 TGLPadPainter.h:59
 TGLPadPainter.h:60
 TGLPadPainter.h:61
 TGLPadPainter.h:62
 TGLPadPainter.h:63
 TGLPadPainter.h:64
 TGLPadPainter.h:65
 TGLPadPainter.h:66
 TGLPadPainter.h:67
 TGLPadPainter.h:68
 TGLPadPainter.h:69
 TGLPadPainter.h:70
 TGLPadPainter.h:71
 TGLPadPainter.h:72
 TGLPadPainter.h:73
 TGLPadPainter.h:74
 TGLPadPainter.h:75
 TGLPadPainter.h:76
 TGLPadPainter.h:77
 TGLPadPainter.h:78
 TGLPadPainter.h:79
 TGLPadPainter.h:80
 TGLPadPainter.h:81
 TGLPadPainter.h:82
 TGLPadPainter.h:83
 TGLPadPainter.h:84
 TGLPadPainter.h:85
 TGLPadPainter.h:86
 TGLPadPainter.h:87
 TGLPadPainter.h:88
 TGLPadPainter.h:89
 TGLPadPainter.h:90
 TGLPadPainter.h:91
 TGLPadPainter.h:92
 TGLPadPainter.h:93
 TGLPadPainter.h:94
 TGLPadPainter.h:95
 TGLPadPainter.h:96
 TGLPadPainter.h:97
 TGLPadPainter.h:98
 TGLPadPainter.h:99
 TGLPadPainter.h:100
 TGLPadPainter.h:101
 TGLPadPainter.h:102
 TGLPadPainter.h:103
 TGLPadPainter.h:104
 TGLPadPainter.h:105
 TGLPadPainter.h:106
 TGLPadPainter.h:107
 TGLPadPainter.h:108
 TGLPadPainter.h:109
 TGLPadPainter.h:110
 TGLPadPainter.h:111
 TGLPadPainter.h:112
 TGLPadPainter.h:113
 TGLPadPainter.h:114
 TGLPadPainter.h:115
 TGLPadPainter.h:116
 TGLPadPainter.h:117
 TGLPadPainter.h:118
 TGLPadPainter.h:119
 TGLPadPainter.h:120
 TGLPadPainter.h:121
 TGLPadPainter.h:122
 TGLPadPainter.h:123
 TGLPadPainter.h:124
 TGLPadPainter.h:125
 TGLPadPainter.h:126
 TGLPadPainter.h:127
 TGLPadPainter.h:128
 TGLPadPainter.h:129
 TGLPadPainter.h:130
 TGLPadPainter.h:131
 TGLPadPainter.h:132
 TGLPadPainter.h:133
 TGLPadPainter.h:134
 TGLPadPainter.h:135
 TGLPadPainter.h:136
 TGLPadPainter.h:137
 TGLPadPainter.h:138
 TGLPadPainter.h:139
 TGLPadPainter.h:140
 TGLPadPainter.h:141
 TGLPadPainter.h:142
 TGLPadPainter.h:143
 TGLPadPainter.h:144
 TGLPadPainter.h:145
 TGLPadPainter.h:146
 TGLPadPainter.h:147
 TGLPadPainter.h:148
 TGLPadPainter.h:149
 TGLPadPainter.h:150
 TGLPadPainter.h:151
 TGLPadPainter.h:152
 TGLPadPainter.h:153
 TGLPadPainter.h:154
 TGLPadPainter.h:155
 TGLPadPainter.h:156
 TGLPadPainter.h:157
 TGLPadPainter.h:158
 TGLPadPainter.h:159
 TGLPadPainter.h:160
 TGLPadPainter.h:161
 TGLPadPainter.h:162
 TGLPadPainter.h:163
 TGLPadPainter.h:164
 TGLPadPainter.h:165
 TGLPadPainter.h:166