TGLText.cxx

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLText.cxx 33334 2010-05-02 15:51:16Z rdm $
00002 // Author:  Olivier Couet  12/04/2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TError.h"
00015 
00016 #include "TGLText.h"
00017 #include "TGLUtil.h"
00018 #include "TColor.h"
00019 #include "TSystem.h"
00020 #include "TEnv.h"
00021 #include "TGLIncludes.h"
00022 
00023 // Direct inclussion of FTGL headers is deprecated in ftgl-2.1.3 while
00024 // ftgl-2.1.2 shipped with ROOT requires manual inclusion.
00025 #ifndef BUILTIN_FTGL
00026 # include <FTGL/ftgl.h>
00027 #else
00028 # include "FTFont.h"
00029 # include "FTGLExtrdFont.h"
00030 # include "FTGLOutlineFont.h"
00031 # include "FTGLPolygonFont.h"
00032 # include "FTGLTextureFont.h"
00033 # include "FTGLPixmapFont.h"
00034 # include "FTGLBitmapFont.h"
00035 #endif
00036 
00037 #define FTGL_BITMAP  0
00038 #define FTGL_PIXMAP  1
00039 #define FTGL_OUTLINE 2
00040 #define FTGL_POLYGON 3
00041 #define FTGL_EXTRUDE 4
00042 #define FTGL_TEXTURE 5
00043 
00044 //______________________________________________________________________________
00045 /* Begin_Html
00046 <center><h2>GL Text</h2></center>
00047 To draw a 3D text in a GL window. This class uses uses FTGL to render text.
00048 FTGL is a package making the interface between the Free Type fonts and GL.
00049 End_Html */
00050 
00051 ClassImp(TGLText)
00052 
00053 //______________________________________________________________________________
00054 TGLText::TGLText()
00055 {
00056    fX      = 0;
00057    fY      = 0;
00058    fZ      = 0;
00059    fAngle1 = 90;
00060    fAngle2 = 0;
00061    fAngle3 = 0;
00062    fGLTextFont = 0;
00063    SetGLTextFont(13); // Default font.
00064 }
00065 
00066 
00067 //______________________________________________________________________________
00068 TGLText::TGLText(Double_t x, Double_t y, Double_t z, const char * /*text*/)
00069 {
00070    // TGLext normal constructor.
00071 
00072    fX      = x;
00073    fY      = y;
00074    fZ      = z;
00075    fAngle1 = 90;
00076    fAngle2 = 0;
00077    fAngle3 = 0;
00078    fGLTextFont = 0;
00079    SetGLTextFont(13); // Default font.
00080 }
00081 
00082 
00083 //______________________________________________________________________________
00084 TGLText::~TGLText()
00085 {
00086    if (fGLTextFont) delete fGLTextFont;
00087 }
00088 
00089 
00090 //______________________________________________________________________________
00091 void TGLText::PaintGLText(Double_t x, Double_t y, Double_t z, const char *text)
00092 {
00093    // Draw text
00094 
00095    if (!fGLTextFont) return;
00096 
00097    glPushMatrix();
00098    glTranslatef(x, y, z);
00099 
00100    TGLUtil::Color(GetTextColor());
00101    Double_t s = GetTextSize();
00102    glScalef(s,s,s);
00103 
00104    // Text alignment
00105    Float_t llx, lly, llz, urx, ury, urz;
00106    fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
00107    Short_t halign = fTextAlign/10;
00108    Short_t valign = fTextAlign - 10*halign;
00109    Float_t dx = 0, dy = 0;
00110    switch (halign) {
00111       case 1 : dx =  0    ; break;
00112       case 2 : dx = -urx/2; break;
00113       case 3 : dx = -urx  ; break;
00114    }
00115    switch (valign) {
00116       case 1 : dy =  0    ; break;
00117       case 2 : dy = -ury/2; break;
00118       case 3 : dy = -ury  ; break;
00119    }
00120    glTranslatef(dx, dy, 0);
00121 
00122    //In XY plane
00123    glRotatef(fAngle1,1.,0.,0.);
00124 
00125    //In XZ plane
00126    glRotatef(fAngle2,0.,1.,0.);
00127 
00128    //In YZ plane
00129    glRotatef(fAngle3,0.,0.,1.);
00130 
00131    // Render text
00132    fGLTextFont->Render(text);
00133 
00134    glPopMatrix();
00135 }
00136 
00137 
00138 //______________________________________________________________________________
00139 void TGLText::PaintBBox(const char *text)
00140 {
00141    Float_t llx, lly, llz, urx, ury, urz;
00142    fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
00143    glBegin(GL_LINES);
00144    glVertex3f(   0,   0, 0); glVertex3f( urx,   0, 0);
00145    glVertex3f(   0,   0, 0); glVertex3f(   0, ury, 0);
00146    glVertex3f(   0, ury, 0); glVertex3f( urx, ury, 0);
00147    glVertex3f( urx, ury, 0); glVertex3f( urx,   0, 0);
00148    glEnd();
00149 }
00150 
00151 //______________________________________________________________________________
00152 void TGLText::BBox(const char* string, float& llx, float& lly, float& llz,
00153                                        float& urx, float& ury, float& urz)
00154 {
00155    // Calculate bounding-box for given string.
00156 
00157    fGLTextFont->BBox(string, llx, lly, llz, urx, ury, urz);
00158 }
00159 
00160 //______________________________________________________________________________
00161 void TGLText::SetGLTextAngles(Double_t a1, Double_t a2, Double_t a3)
00162 {
00163    // Set the text rotation angles.
00164 
00165    fAngle1 = a1;
00166    fAngle2 = a2;
00167    fAngle3 = a3;
00168 }
00169 
00170 
00171 //______________________________________________________________________________
00172 void TGLText::SetGLTextFont(Font_t fontnumber)
00173 {
00174    int fontid = fontnumber / 10;
00175 
00176    const char *fontname=0;
00177    if (fontid == 0)  fontname = "arialbd.ttf";
00178    if (fontid == 1)  fontname = "timesi.ttf";
00179    if (fontid == 2)  fontname = "timesbd.ttf";
00180    if (fontid == 3)  fontname = "timesbi.ttf";
00181    if (fontid == 4)  fontname = "arial.ttf";
00182    if (fontid == 5)  fontname = "ariali.ttf";
00183    if (fontid == 6)  fontname = "arialbd.ttf";
00184    if (fontid == 7)  fontname = "arialbi.ttf";
00185    if (fontid == 8)  fontname = "cour.ttf";
00186    if (fontid == 9)  fontname = "couri.ttf";
00187    if (fontid == 10) fontname = "courbd.ttf";
00188    if (fontid == 11) fontname = "courbi.ttf";
00189    if (fontid == 12) fontname = "symbol.ttf";
00190    if (fontid == 13) fontname = "times.ttf";
00191    if (fontid == 14) fontname = "wingding.ttf";
00192 
00193    // try to load font (font must be in Root.TTFontPath resource)
00194    const char *ttpath = gEnv->GetValue("Root.TTFontPath",
00195 # ifdef TTFFONTDIR
00196                                         TTFFONTDIR
00197 # else
00198                                         "$(ROOTSYS)/fonts"
00199 # endif
00200                                         );
00201 
00202    char *ttfont = gSystem->Which(ttpath, fontname, kReadPermission);
00203 
00204    if (fGLTextFont) delete fGLTextFont;
00205 
00206 // fGLTextFont = new FTGLOutlineFont(ttfont);
00207 
00208    fGLTextFont = new FTGLPolygonFont(ttfont);
00209 
00210    if (!fGLTextFont->FaceSize(1))
00211       Error("SetGLTextFont","Cannot set FTGL::FaceSize"),
00212    delete [] ttfont;
00213 }

Generated on Tue Jul 5 14:18:09 2011 for ROOT_528-00b_version by  doxygen 1.5.1