-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfont_rendering.cpp
More file actions
173 lines (149 loc) · 5.12 KB
/
font_rendering.cpp
File metadata and controls
173 lines (149 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "font_rendering.h"
#include <cstdlib>
#include <cassert>
#include <OpenGL/glu.h>
#include "misc_fixed_6x12.h"
void FontRendering::DrawStringFixed6x12(
int x, int y,
const char *str,
uint32 color,
bool vertical)
{
// Keep a record of all text rendered during a frame
m_text += str;
m_text += "\n";
// Draw text as textured quads
uint xoffs = 0, yoffs = 0;
const size_t length = std::strlen(str);
for (uint i=0; i<length; i++)
{
if (str[i] == '\n')
{
xoffs = 0;
yoffs -= 11;
continue;
}
const int idx = str[i];
// Position of character on the font grid
const uint tx = (idx % font_grid_wdh);
const uint ty =
font_grid_hgt - ((idx - (idx % font_grid_wdh)) / font_grid_wdh + 1);
// Texture coordinate origin of character
const float ftx = float(tx * font_char_wdh) / float(font_tex_wdh);
const float fty = float(ty * font_char_hgt) / float(font_tex_wdh);
const uint idx_base = m_pos.size();
// Position & texture coordinates
//
// TODO: Re-use last two vertices from the second character on
m_tex_coords.push_back(Vec2f(
ftx,
fty));
m_pos.push_back(Vec2f(x + xoffs, y + yoffs));
m_tex_coords.push_back(Vec2f(
ftx + float(font_char_wdh) / float(font_tex_wdh),
fty));
m_pos.push_back(Vec2f(x + xoffs + font_char_wdh, y + yoffs));
m_tex_coords.push_back(Vec2f(
ftx + float(font_char_wdh) / float(font_tex_wdh),
fty + float(font_char_hgt) / float(font_tex_wdh)));
m_pos.push_back(Vec2f(x + xoffs + font_char_wdh, y + font_char_hgt + yoffs));
m_tex_coords.push_back(Vec2f(
ftx,
fty + float(font_char_hgt) / float(font_tex_wdh)));
m_pos.push_back(Vec2f(x + xoffs, y + font_char_hgt + yoffs));
// Colors
for (uint rep=0; rep<4; rep++)
m_colors.push_back(color);
// Indices
m_indices.push_back(idx_base + 0);
m_indices.push_back(idx_base + 1);
m_indices.push_back(idx_base + 3);
m_indices.push_back(idx_base + 1);
m_indices.push_back(idx_base + 2);
m_indices.push_back(idx_base + 3);
// For drawing vertical text
if (vertical)
{
xoffs = 0;
yoffs -= 9;
}
else
xoffs += font_char_wdh;
}
}
void FontRendering::Render(bool filter_font_texture)
{
assert(m_pos.size() == m_tex_coords.size());
assert(m_indices.size() % 6 == 0);
assert(m_indices.size() / 6 * 4 == m_pos.size());
if (m_indices.empty())
return;
// Preserve text
m_last_frame_text = m_text;
m_text.clear();
// Initialize OpenGL font texture
if (m_font_tex == (GLuint) -1)
{
// Create font texture
glGenTextures(1, &m_font_tex);
glBindTexture(GL_TEXTURE_2D, m_font_tex);
// Convert bit packed font image to luminance texture
uint tex_image[font_tex_wdh * font_tex_wdh];
for (uint y=0; y<font_img_hgt; y++)
for (uint x=0; x<font_img_wdh; x++)
{
uint *dst = &tex_image[x + y * font_tex_wdh];
const uint src_idx = x + y * font_img_wdh;
// Extract bit (reversed in byte), store white / back pixel
(* dst) = (reinterpret_cast<const uchar *>(g_misc_fixed_6x12_data)[src_idx / 8]
& (1 << (7 - (src_idx % 8)))) ? 0xFFFFFFFF : 0x00FFFFFF;
}
// Upload texture image
gluBuild2DMipmapLevels(
GL_TEXTURE_2D,
GL_RGBA8,
font_tex_wdh,
font_tex_wdh,
GL_BGRA,
GL_UNSIGNED_BYTE,
0,
0,
8,
tex_image);
}
// Bind font texture
glBindTexture(GL_TEXTURE_2D, m_font_tex);
glEnable(GL_TEXTURE_2D);
// Texture filtering?
if (filter_font_texture)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
// Alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Draw
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &m_pos[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &m_tex_coords[0]);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, &m_colors[0]);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices[0]);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// Empty for the next frame
m_pos.clear();
m_tex_coords.clear();
m_colors.clear();
m_indices.clear();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}