Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DrawTextGL.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include "DrawTextGL.hh"
27 #include <Gui/biasgl.h>
28 
29 #if !defined(WIN32)
30 # if defined(BIAS_HAVE_GLEW) || defined(MIP_HAVE_GLEW)
31 # include <GL/glxew.h>
32 # else
33 # include <GL/glx.h>
34 # endif // glew
35 #endif // Linux
36 
37 #ifdef WIN32
38 # include <windows.h> // Header File For Windows
39 #else //WIN32
40 # include <X11/extensions/xf86vmode.h>
41 # include <X11/keysym.h>
42 # include <X11/Intrinsic.h> /* Display, Window */
43 //# include <GL/glx.h> /* GLXContext */
44 #endif // WIN32
45 
46 #include <iostream>
47 #include <Base/Debug/Error.hh>
48 #include <Base/Debug/DebugSimple.hh>
49 
50 
51 // ------------------------------
52 using namespace std;
53 using namespace BIAS;
54 // ------------------------------
55 // alphabet starts at char 32 and ends at (32+96)
56 #define CHARS_START 32
57 #define CHARS_N 96
58 // ------------------------------
59 
60 
61 DrawTextGL::~DrawTextGL()
62 {
63  DeleteFont();
64 }
65 
66 
67 DrawTextGL::DrawTextGL()
68 : initialized(false)
69 {
70  base=0;
71  fontname="";
72 }
73 
74 
75 
76 // Build a bitmap font for OpenGL usage
77 // Stores individual charcters in display lists
78 // Inspired by Nehe lesson 13
79 // Jan Woetzel 10/2005
80 int DrawTextGL::InitFont(void* handleDC,
81  const std::string theFontname,
82  const int fontHeight
83  )
84 {
85 
86  this->fontname=theFontname;
87 
88 #ifdef WIN32
89  BIASASSERT(handleDC!=NULL);
90  //
91  // WINDOWS (JW)
92  //
93 
94  // Windows Font ID
95  HFONT font =NULL;
96  HFONT oldfont=NULL;
97  // Storage For Characters
98  base = glGenLists(CHARS_N);
99 
100  // CFont:
101  font = CreateFont(
102  fontHeight, // height
103  0, // Width
104  0,
105  0,
106  FW_NORMAL, // font weight, e.g. FW_NORMAL FW_BOLD
107  FALSE, // Italic
108  FALSE, // Underline
109  FALSE, // Strikeout
110  ANSI_CHARSET,
111  OUT_TT_PRECIS, // Output Precision OUT_DEFAULT_PRECIS OUT_TT_PRECIS
112  CLIP_DEFAULT_PRECIS, // Clipping Precision
113  ANTIALIASED_QUALITY, // Output Quality
114  FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
115  fontname.c_str()
116  );
117 
118  oldfont = (HFONT)SelectObject(HDC(handleDC), font);
119  if (oldfont==NULL){
120  BIASERR("SelectObject failed. NULL");
121  }
122 
123  // Build maps
124  wglUseFontBitmaps(HDC(handleDC), CHARS_START, CHARS_N, base);
125 
126  // Selects The Font We Want
127  SelectObject(HDC(handleDC), oldfont);
128 
129  // Delete The Font since w ehave our display lists
130  DeleteObject(font);
131 
132 #else // WIN32
133 
134  //
135  // LINUX (JW)
136  //
137  Display* display=NULL;
138  //display = (Display*)(handleDC);
139 
140  display = XOpenDisplay(0);
141  if (display == NULL){
142  BIASERR("XOpenDisplay failed for GLX font initialization. aborting");
143  return -1;
144  };
145  {
146 
147  XFontStruct *fontX = NULL;
148  // use N display lists, one for each character
149  base = glGenLists(CHARS_N);
150  // load a font with a specific name in "Host Portable Character Encoding"
151  fontX = XLoadQueryFont(display
152  //display.dpy
153  ,fontname.c_str()
154  // "-*-helvetica-bold-r-normal--24-*-*-*-p-*-iso8859-1"
155  );
156  if (fontX == NULL)
157  {
158  //BIASERR("Could not load font "<<fontname<<" switching to fixed");
159  // try a fallback font
160  fontname="fixed";
161  fontX = XLoadQueryFont( display
162  //display.dpy
163  ,fontname.c_str() );
164 
165  if (fontX == NULL)
166  {
167  BIASERR("Problem loading font - fallback failed, too: "<<fontname);;
168  this->DeleteFont();
169  initialized=false;
170  return -1;
171  }
172  }
173  // Build maps
174  glXUseXFont(fontX->fid, CHARS_START, CHARS_N, base);
175 
176  // free XFontStruct since we have our display lists
177  XFreeFont(display
178  //display.dpy
179  ,fontX);
180  }
181  // clean up
182  XCloseDisplay(display);
183  display = NULL;
184 
185 #endif // WIN32
186 
187 
188  initialized=true;
189  return 0; // OK
190 }
191 
192 
193 // JW
195 {
196  // clean up the display lists we used.
197  glDeleteLists(base, CHARS_N);
198 }
199 
200 
201 // JW: tested, works
202 void DrawTextGL::PrintNormCoords(const std::string & text,
203  const float & xpos, const float & ypos) const
204 {
205 #ifdef BIAS_DEBUG
206  if ((0>xpos)|(xpos>1) | (0>ypos)|(ypos>1) )
207  BIASERR("("<<xpos<<","<<ypos<<" is out of normalized 0..1 coordinate bounds. Is this intentional?");
208 #endif //BIAS_DEBUG
209 
210  // works with OpenGL 1.1
211  glMatrixMode(GL_PROJECTION);
212  glPushMatrix();
213  {
214  glLoadIdentity();
215  glOrtho(0.0, 1.0,
216  0.0, 1.0,
217  0.1, 1.0);
218  glMatrixMode(GL_MODELVIEW);
219  glPushMatrix();
220  {
221  glLoadIdentity();
222  // move into screen
223  glTranslatef(0.0, 0.0, -0.5);
224  glRasterPos2f(xpos, ypos);
225  Print(text); // output
226  }
227  glMatrixMode(GL_MODELVIEW);
228  glPopMatrix();
229  }
230  glMatrixMode(GL_PROJECTION);
231  glPopMatrix();
232 }
233 
234 
235 #include <Base/Common/BIASpragmaStart.hh>
236 // JW: tested, works
237 void DrawTextGL::PrintWinCoords(const std::string & text,
238  const int & xpos, const int & ypos,
239  const int & winWidthFallback, const int &winHeightFallback) const
240 {
241 #ifdef BIAS_HAVE_GLEW
242  if (GL_ARB_window_pos) {
243  // Supported at least by OpenGL 1.4
244  glWindowPos2f(xpos, ypos);
245  Print(text);
246  } else
247 #endif // BIAS_HAVE_GLEW
248  {
249  // fallback for OpenGL 1.1
250  glMatrixMode(GL_PROJECTION);
251  glPushMatrix();
252  {
253  glLoadIdentity();
254  gluOrtho2D(
255  0.0, float(winWidthFallback),
256  0.0, float(winHeightFallback)
257  );
258  glMatrixMode(GL_MODELVIEW);
259  glPushMatrix();
260  {
261  glLoadIdentity();
262  Print(text); // output
263  }
264  glMatrixMode(GL_MODELVIEW);
265  glPopMatrix();
266  }
267  glMatrixMode(GL_PROJECTION);
268  glPopMatrix();
269  }
270 }
271 #include <Base/Common/BIASpragmaEnd.hh>
272 
273 
274 // JW
275 void DrawTextGL::Print3D(const std::string & text,
276  const float & xpos, const float & ypos, const float & zpos) const
277 {
278  glRasterPos3f(xpos, ypos, zpos);
279  Print(text);
280 }
281 
282 
283 // JW
284 void DrawTextGL::Print3DAbsolute(const std::string & text,
285  const float & xpos, const float & ypos, const float & zpos) const
286 {
287  glMatrixMode(GL_MODELVIEW);
288  glPushMatrix();
289  {
290  glLoadIdentity();
291  Print3D(text, xpos, ypos, zpos);
292  }
293  glMatrixMode(GL_MODELVIEW);
294  glPopMatrix();
295 }
296 
297 
298 // JW
299 void DrawTextGL::Print(const std::string & text) const
300 {
301  if (!initialized) {
302  cout<<"You must call InitFont before using DrawTextGL::Print (JW), aborting."<<endl;
303  return;
304  };
305 
306  glMatrixMode(GL_MODELVIEW);
307  glPushMatrix();
308  {
309  //glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
310 
311 
312  glPushAttrib(GL_LIST_BIT);
313  // set display list offset base
314  glListBase(base - CHARS_START);
315  // Draws all characters as individual display list calls
316  glCallLists( text.size(), GL_UNSIGNED_BYTE, text.c_str() );
317  glPopAttrib(); // restire prevois displa ylist basis
318  }
319  glPopMatrix();
320 }
321 
322 
unsigned int base
inde xof our first display list:
Definition: DrawTextGL.hh:142
void Print3D(const std::string &text, const float &xpos, const float &ypos, const float &zpos) const
Definition: DrawTextGL.cpp:275
void Print3DAbsolute(const std::string &text, const float &xpos, const float &ypos, const float &zpos) const
draw text in 3d at absolute position, thus ModelView matrix is set to identity (using push and pop)...
Definition: DrawTextGL.cpp:284
bool initialized
true if we created the font as display lists
Definition: DrawTextGL.hh:139
int InitFont(void *handleDC, const std::string theFontname=std::string(DrawTextGL_DEFAULT_FONTNAME), const int fontHeight=DrawTextGL_DEFAULT_FONTSIZE)
set the font up
Definition: DrawTextGL.cpp:80
void Print(const std::string &text) const
print text on OpenGL screen using current RasterPosition , settings etc.
Definition: DrawTextGL.cpp:299
void DeleteFont()
frees the display lists JW
Definition: DrawTextGL.cpp:194
void PrintNormCoords(const std::string &text, const float &xpos, const float &ypos) const
print text on OpenGL screen in 0..1 normalized coordinates with origin at lower left corner with 0...
Definition: DrawTextGL.cpp:202
std::string fontname
the name of our font, e.g. &quot;Arial&quot; or &quot;-*-helvetica-bold-r-normal--24-*-*-*-p-*-iso8859-1&quot;);" ...
Definition: DrawTextGL.hh:130
void PrintWinCoords(const std::string &text, const int &xpos, const int &ypos, const int &winWidthFallback=640, const int &winHeightFallback=480) const
print text on OpenGL screen at 2D pos (xpos,ypos) in windows (pixel) coordiantes. ...
Definition: DrawTextGL.cpp:237