Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OGLFont.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
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  // Create the texture
59  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, _image->getData());
60 
61  return true;
62 }
63 
64 void OGLFont::drawText(const std::string& msg, const OColor& color, double x, double y) const
65 {
66  if (!_image)
67  {
68  return;
69  }
70 
71  // Texts must always be displayed in GL_FILL mode
72  GLdouble polygonMode[2];
73  glGetDoublev(GL_POLYGON_MODE, polygonMode);
74  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
75 
76  glDisable(GL_DEPTH_TEST);
77  glEnable(GL_TEXTURE_2D);
78 
79  // Text color
80  glColor3fv(color);
81 
82  // Draw a quad
83  glBegin(GL_QUADS);
84 
85  // Recover the texture's dimensions
86  size_t width = _image->getSizeX();
87  size_t height = _image->getSizeY();
88 
89  // Process each character of msg
90  for (unsigned int i = 0; i < msg.length(); ++i)
91  {
93 
94  // Compute the relative coordinates of the char on the texture
95  double x0 = static_cast<double>(c.x) / width;
96  double y0 = static_cast<double>(c.y) / height;
97 
98  double x1 = static_cast<double>(c.x + c.w) / width;
99  double y1 = static_cast<double>(c.y + c.h) / height;
100 
101  // Map the piece of texture corresponding to the current char to its position
102  glTexCoord2d(x0, y0);
103  glVertex2d(x, y);
104  glTexCoord2d(x0, y1);
105  glVertex2d(x, y - c.h);
106  glTexCoord2d(x1, y1);
107  glVertex2d(x + c.w, y - c.h);
108  glTexCoord2d(x1, y0);
109  glVertex2d(x + c.w, y);
110 
111  // Apply the advance to the x position
112  x += c.advance;
113 
114  // Apply the kerning between the current character and next one
115  if (i < msg.length() - 1)
116  x += _image->getKerning(msg[i], msg[i + 1]);
117  }
118 
119  // End quad
120  glEnd();
121 
122  glDisable(GL_TEXTURE_2D);
123  glEnable(GL_DEPTH_TEST);
124 
125  glPolygonMode(GL_FRONT_AND_BACK, (GLenum)polygonMode[0]);
126 }
NxReal c
Definition: NxVec3.cpp:317
Definition: color.h:36
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:64
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:147
const OGLFontChar & getChar(unsigned char c) const
Definition: OImageFont.cpp:139
unsigned char * getData() const
Definition: OImage.h:45
unsigned int getSizeX() const
Definition: OImage.h:53
unsigned int getSizeY() const
Definition: OImage.h:61
static LPTYImageManager get()