Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OGLFont.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include <stdio.h>
21 #include "Tympan/core/color.h"
22 #include "OGLFont.h"
23 #include "OImageFont.h"
24 #include "TYImageManager.h"
25 #include <iostream>
26 OGLFont::OGLFont() : _image(0) {}
27 
29 
30 bool OGLFont::load(const char* filename)
31 {
32  if (id > 0)
33  {
34  free();
35  }
36 
37  genTexture();
38 
39  glBindTexture(GL_TEXTURE_2D, id);
40  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
41  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
42  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
43  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
45 
46  // Recover the image
47  _image = (OImageFont*)TYImageManager::get()->getImage(filename);
48 
49  if (!_image)
50  {
51  return false;
52  }
53 
54  // Recover the texture's dimensions
55  GLsizei w = _image->getSizeX();
56  GLsizei h = _image->getSizeY();
57 
58  unsigned int bpp = _image->getDepth() / 8;
59 
60  GLenum format = GL_ALPHA;
61 
62  // Create the texture
63  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, _image->getData());
64 
65  return true;
66 }
67 
68 void OGLFont::drawText(const std::string& msg, const OColor& color, double x, double y) const
69 {
70  if (!_image)
71  {
72  return;
73  }
74 
75  // Texts must always be displayed in GL_FILL mode
76  GLdouble polygonMode[2];
77  glGetDoublev(GL_POLYGON_MODE, polygonMode);
78  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
79 
80  glDisable(GL_DEPTH_TEST);
81  glEnable(GL_TEXTURE_2D);
82 
83  // Text color
84  glColor3fv(color);
85 
86  // Draw a quad
87  glBegin(GL_QUADS);
88 
89  // Recover the texture's dimensions
90  size_t width = _image->getSizeX();
91  size_t height = _image->getSizeY();
92 
93  // Process each character of msg
94  for (unsigned int i = 0; i < msg.length(); ++i)
95  {
97 
98  // Compute the relative coordinates of the char on the texture
99  double x0 = static_cast<double>(c.x) / width;
100  double y0 = static_cast<double>(c.y) / height;
101 
102  double x1 = static_cast<double>(c.x + c.w) / width;
103  double y1 = static_cast<double>(c.y + c.h) / height;
104 
105  // Map the piece of texture corresponding to the current char to its position
106  glTexCoord2d(x0, y0);
107  glVertex2d(x, y);
108  glTexCoord2d(x0, y1);
109  glVertex2d(x, y - c.h);
110  glTexCoord2d(x1, y1);
111  glVertex2d(x + c.w, y - c.h);
112  glTexCoord2d(x1, y0);
113  glVertex2d(x + c.w, y);
114 
115  // Apply the advance to the x position
116  x += c.advance;
117 
118  // Apply the kerning between the current character and next one
119  if (i < msg.length() - 1)
120  x += _image->getKerning(msg[i], msg[i + 1]);
121  }
122 
123  // End quad
124  glEnd();
125 
126  glDisable(GL_TEXTURE_2D);
127  glEnable(GL_DEPTH_TEST);
128 
129  glPolygonMode(GL_FRONT_AND_BACK, (GLenum)polygonMode[0]);
130 }
NxReal c
Definition: NxVec3.cpp:317
Definition: color.h:31
bool load(const char *filename)
Definition: OGLFont.cpp:30
OImageFont * _image
Definition: OGLFont.h:50
OGLFont()
Definition: OGLFont.cpp:26
void drawText(const std::string &msg, const OColor &color, double x, double y) const
Definition: OGLFont.cpp:68
virtual ~OGLFont()
Definition: OGLFont.cpp:28
void genTexture()
Definition: OGLTexture.cpp:46
virtual void free()
Definition: OGLTexture.cpp:37
const int getKerning(unsigned char first, unsigned char second) const
Definition: OImageFont.cpp:123
const OGLFontChar & getChar(unsigned char c) const
Definition: OImageFont.cpp:115
unsigned char * getData() const
Definition: OImage.h:45
unsigned int getDepth() const
Definition: OImage.h:69
unsigned int getSizeX() const
Definition: OImage.h:53
unsigned int getSizeY() const
Definition: OImage.h:61
static LPTYImageManager get()