-
Notifications
You must be signed in to change notification settings - Fork 4
/
atlas.cc
233 lines (188 loc) · 7.41 KB
/
atlas.cc
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "glutil.h"
#include "atlas.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sys/time.h>
using namespace std;
static FT_Library ft;
static FT_Face face;
static GLuint tex;
static void init_ft(const string& font)
{
if (FT_Init_FreeType(&ft)) {
std::cerr << "init freetype failed" << std::endl;
exit(-1);
}
if (FT_New_Face(ft, font.c_str(), 0, &face)) {
std::cerr << "load " << font << " failed" << std::endl;
if (FT_New_Face(ft, "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf", 0, &face)) {
std::cerr << "load face failed" << std::endl;
exit(-1);
}
}
}
void TextMode::setFontPath(const std::string& path)
{
/* TODO: check validity */
_fontPath = path;
}
bool TextMode::load_char_helper(FT_ULong char_code)
{
FT_GlyphSlot slot = face->glyph;
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER)) {
std::cerr << "load " << char_code << " failed\n";
return false;
}
_atlas.infos[char_code] = {
(float)slot->bitmap_left, (float)slot->bitmap_top,
(float)slot->bitmap.width, (float)slot->bitmap.rows,
float(slot->advance.x >> 6), float(slot->advance.y >> 6),
_atlas.width
};
_atlas.height = std::max(_atlas.height, _atlas.infos[char_code].height);
_atlas.width += _atlas.infos[char_code].width + 10;
return true;
}
//@arg preloads is a wstring which contains all chars that need to load into atlas
void TextMode::create_atlas(FT_Face face, int pointSize, std::string preloads)
{
FT_Set_Pixel_Sizes(face, 0, pointSize);
_atlas.point_size = pointSize;
FT_GlyphSlot slot = face->glyph;
FT_ULong num = 128;
//ASCII is loaded by default
for (auto i = 32; i < num; i++) {
load_char_helper(i);
}
std::wstring ws(preloads.size(), L'\0');
auto ret = std::mbstowcs(&ws[0], preloads.data(), ws.size());
ws.resize(ret);
for (auto i: ws) {
load_char_helper(i);
}
//NOTE: transparent and `+10` above are anti rendering artifacts.
//since two chars in texture are close enough to blur each other's border.
GLubyte transparent[(int)_atlas.width * (int)_atlas.height];
memset(transparent, 0, sizeof transparent);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, _atlas.width, _atlas.height,
0, GL_ALPHA, GL_UNSIGNED_BYTE, transparent);
for (auto p: _atlas.infos) {
auto i = p.first;
if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
std::cerr << "load " << i << " failed\n";
continue;
}
glTexSubImage2D(GL_TEXTURE_2D, 0, _atlas.infos[i].offset, 0, _atlas.infos[i].width,
_atlas.infos[i].height, GL_ALPHA, GL_UNSIGNED_BYTE, slot->bitmap.buffer);
}
}
void TextMode::render_str(std::string s, float x, float y, float sx, float sy)
{
std::wstring ws(s.size(), L'\0');
auto ret = std::mbstowcs(&ws[0], s.data(), ws.size());
ws.resize(ret+1);
int len = ws.length();
struct point_t {
GLfloat x, y, s, t;
} points[6 * len];
for (int i = 0; i < len; i++) {
auto c = ws[i];
GLfloat x0 = x + _atlas.infos[c].left * sx;
GLfloat y0 = y + _atlas.infos[c].top * sy;
GLfloat w = _atlas.infos[c].width * sx, h = _atlas.infos[c].height * sy;
float tw = _atlas.infos[c].width / _atlas.width;
float tx = _atlas.infos[c].offset / _atlas.width;
float ty = _atlas.infos[c].height / _atlas.height;
int p = i * 6;
points[p++] = {x0, y0, tx, 0,};
points[p++] = {x0 + w, y0, tx + tw, 0,};
points[p++] = {x0, y0 - h, tx, ty,};
points[p++] = {x0, y0 - h, tx, ty,};
points[p++] = {x0 + w, y0, tx + tw, 0,};
points[p++] = {x0 + w, y0 - h, tx + tw, ty,};
x += _atlas.infos[c].ax * sx;
y += _atlas.infos[c].ay * sy;
}
glBindBuffer(GL_ARRAY_BUFFER, _proc.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof points, points, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, len * 6);
}
static timeval tv_start = {0, 0};
extern const char _binary_atlas_frag_glsl_end[];
extern const char _binary_atlas_frag_glsl_start[];
extern const char _binary_atlas_vertex_glsl_end[];
extern const char _binary_atlas_vertex_glsl_start[];
bool TextMode::init(int width, int height)
{
_screenWidth = width, _screenHeight = height;
init_ft(_fontPath);
GLuint vlen = _binary_atlas_vertex_glsl_end - _binary_atlas_vertex_glsl_start;
GLuint flen = _binary_atlas_frag_glsl_end - _binary_atlas_frag_glsl_start;
cerr << "binary glsl vlen " << vlen << ", flen " << flen << endl;
string ver_src = strndup(_binary_atlas_vertex_glsl_start, vlen);
string frag_src = strndup(_binary_atlas_frag_glsl_start, flen);
//GLProcess* proc = glprocess_create("atlas_vertex.glsl", "atlas_frag.glsl");
GLProcess* proc = glprocess_create(ver_src.c_str(), frag_src.c_str(), true);
if (!proc) return false;
_proc = *proc;
GLuint program = _proc.program;
glUseProgram(program);
auto resolution = glm::vec3(_screenWidth, _screenHeight, 1.0);
glUniform3fv(glGetUniformLocation(_proc.program, "resolution"),
1, glm::value_ptr(resolution));
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGenBuffers(1, &_proc.vbo);
glBindBuffer(GL_ARRAY_BUFFER, _proc.vbo);
GLint pos_attrib = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(pos_attrib);
glVertexAttribPointer(pos_attrib, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(program, "tex"), 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
create_atlas(face, 28, "正在加载操作系统.....");
glClearColor(0.1, 0.1, 0.4, 1.0);
return true;
}
void TextMode::deinit()
{
glprocess_release(&_proc);
_proc.program = 0;
//...
}
void TextMode::render()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
struct timeval tv;
gettimeofday(&tv, NULL);
if (!tv_start.tv_sec) tv_start = tv;
float t = (tv.tv_sec - tv_start.tv_sec) + (tv.tv_usec - tv_start.tv_usec) / 1000000.0;
GLint time = glGetUniformLocation(_proc.program, "time");
glUniform1f(time, t);
GLfloat bgcolor[] = {
float((glm::cos(t) + 1.0)/2.0), float((glm::sin(t)+1.0)/2.0), 0, 1
};
glUniform4fv(glGetUniformLocation(_proc.program, "bgcolor"), 1, bgcolor);
float ps1 = 24.0;
float sx = 2.0 / _screenWidth, sy = 2.0 / _screenHeight;
float x = -1.0, y = 1.0 - ps1 * sy;
render_str("Welcome to Linux", x, y, sx, sy);
GLfloat bgcolor2[] = {
0, float((glm::cos(t) + 1.0)/2.0), float((glm::sin(t)+1.0)/2.0), 0.5
};
glUniform4fv(glGetUniformLocation(_proc.program, "bgcolor"), 1, bgcolor2);
x = -1.0 + (_screenWidth/2.0) * sx - 0.3, y = 1.0 - (_screenHeight/2.0) * sy;
render_str("真正加载操作系统......", x, y, sx, sy);
y -= ps1 * 2 * sy;
render_str("System Loading...", x, y, sx, sy);
}